src/platform/qt/input/InputItem.cpp (view raw)
1/* Copyright (c) 2013-2017 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include "InputItem.h"
7
8#include <QMenu>
9
10using namespace QGBA;
11
12InputItem::InputItem()
13{
14}
15
16InputItem::InputItem(QAction* action, const QString& name, QMenu* parent)
17 : QObject(parent)
18 , m_action(action)
19 , m_shortcut(action->shortcut().isEmpty() ? -2 : action->shortcut()[0])
20 , m_name(name)
21 , m_menu(parent)
22{
23 m_visibleName = action->text()
24 .remove(QRegExp("&(?!&)"))
25 .remove("...");
26}
27
28InputItem::InputItem(InputItem::Functions functions, const QString& visibleName, const QString& name, QMenu* parent)
29 : QObject(parent)
30 , m_functions(functions)
31 , m_name(name)
32 , m_visibleName(visibleName)
33 , m_menu(parent)
34{
35}
36
37InputItem::InputItem(const QString& visibleName, const QString& name, QMenu* parent)
38 : QObject(parent)
39 , m_name(name)
40 , m_visibleName(visibleName)
41 , m_menu(parent)
42{
43}
44
45InputItem::InputItem(const InputItem& other)
46 : QObject(other.m_menu)
47 , m_name(other.m_name)
48 , m_visibleName(other.m_visibleName)
49 , m_shortcut(other.m_shortcut)
50 , m_button(other.m_button)
51 , m_axis(other.m_axis)
52 , m_direction(other.m_direction)
53 , m_menu(other.m_menu)
54{
55}
56
57InputItem::InputItem(InputItem& other)
58 : QObject(other.m_menu)
59 , m_action(other.m_action)
60 , m_functions(other.m_functions)
61 , m_name(other.m_name)
62 , m_visibleName(other.m_visibleName)
63 , m_shortcut(other.m_shortcut)
64 , m_button(other.m_button)
65 , m_axis(other.m_axis)
66 , m_direction(other.m_direction)
67 , m_menu(other.m_menu)
68{
69}
70
71void InputItem::setShortcut(int shortcut) {
72 m_shortcut = shortcut;
73 if (m_action) {
74 m_action->setShortcut(QKeySequence(shortcut));
75 }
76 emit shortcutBound(this, shortcut);
77}
78
79void InputItem::clearShortcut() {
80 setShortcut(0);
81}
82
83void InputItem::setButton(int button) {
84 m_button = button;
85 emit buttonBound(this, button);
86}
87
88void InputItem::clearButton() {
89 setButton(-1);
90}
91
92void InputItem::setAxis(int axis, GamepadAxisEvent::Direction direction) {
93 m_axis = axis;
94 m_direction = direction;
95 emit axisBound(this, axis, direction);
96}
97
98void InputItem::trigger(bool active) {
99 if (active) {
100 if (m_functions.first) {
101 m_functions.first();
102 }
103 } else {
104 if (m_functions.second) {
105 m_functions.second();
106 }
107 }
108}