[companion] Adjust GVAR not possible in global functions (fix #5425)
[opentx.git] / companion / src / simulation / radiouiaction.h
blobd2904c491b072c0cc319344dab5cc23b38050ac6
1 /*
2 * Copyright (C) OpenTX
4 * Based on code named
5 * th9x - http://code.google.com/p/th9x
6 * er9x - http://code.google.com/p/er9x
7 * gruvin9x - http://code.google.com/p/gruvin9x
9 * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
21 #ifndef RADIOUIACTION_H
22 #define RADIOUIACTION_H
24 #include <QtCore>
25 #include <QWidget>
26 #include <QKeyEvent>
29 * This class is somewhat like a QAction but specific for the radio UI.
30 * Actions can have one or more keyboard shortcuts associated with them (currently single-key only, w/out modifiers).
32 class RadioUiAction : public QObject
34 Q_OBJECT
36 public:
38 * @param index Typically this is the hardware array index corresponding to button on current radio model,
39 * but it could be anything. Use -1 or any other negative value for non-hardware indices.
40 * @param key An optional Qt:Key code for shortcut (zero for none).
41 * @param parent Parent widget, required for handling keyboard events.
42 * @param text Optional title for this action. The text and description are currently used in generated help text.
43 * @param descript Optional longer description text for this action.
45 RadioUiAction(int index = -1, int key = 0, const QString &text = "", const QString &descript = "", QWidget * parent = NULL):
46 m_hwIndex(index),
47 m_active(false),
48 m_keys(QList<int>()),
49 m_text(text),
50 m_description(descript),
51 m_parent(parent)
53 addKey(key);
56 * @param keys QList of Qt:Key codes to use as shortcuts.
57 * [See above for other params.]
59 RadioUiAction(int index, QList<int> keys, const QString &text = "", const QString &descript = "", QWidget * parent = NULL):
60 RadioUiAction(index, 0, text, descript, parent)
62 addKeys(keys);
65 void addKey(const int & key)
67 if (key > 0 && !m_keys.contains(key)) {
68 m_keys.append(key);
69 addShortcut();
73 void addKeys(const QList<int> & keys)
75 foreach (int key, keys)
76 addKey(key);
79 void addShortcut()
81 if (m_parent) {
82 m_parent->removeEventFilter(this);
83 if (m_keys.size())
84 m_parent->installEventFilter(this);
88 // override
89 void setParent(QObject * parent)
91 QObject::setParent(parent);
92 QWidget * w = qobject_cast<QWidget *>(parent);
93 if (w && w != m_parent) {
94 m_parent = w;
95 addShortcut();
99 void setDescription(const QString & description) { m_description = description; }
100 void setText(QPair<QString, QString> name_descript) { setText(name_descript.first, name_descript.second); }
102 void setText(const QString & text, const QString & description = "")
104 m_text = text;
105 if (!description.isEmpty())
106 setDescription(description);
109 int getIndex() const { return m_hwIndex; }
110 bool isActive() const { return m_active; }
111 QString getDescription() const { return m_description; }
112 QString getText() const { return m_text; }
114 bool eventFilter(QObject * obj, QEvent * event)
116 if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
117 QKeyEvent *keyEvent = static_cast<QKeyEvent *>(event);
118 if ((!keyEvent->modifiers() || keyEvent->modifiers() == Qt::KeypadModifier) && m_keys.contains(keyEvent->key())) {
119 trigger(event->type() == QEvent::KeyPress);
120 return true;
123 return QObject::eventFilter(obj, event);
126 public slots:
128 // "toggle" is the most basic way to set the action status and emits only one "toggled" signal
129 bool toggle(bool active = true)
131 if (active != m_active) {
132 m_active = active;
133 emit toggled(m_hwIndex, active);
134 return true;
136 return false;
139 // "trigger" indicates a user-initiated action and emits more signals than "toggle" does
140 void trigger(bool active = true)
142 if (toggle(active)) {
143 emit triggered(m_hwIndex, active);
144 if (active) {
145 emit pushed(m_hwIndex);
146 emit pushed();
151 private:
153 int m_hwIndex;
154 bool m_active;
155 QList<int> m_keys;
156 QString m_text;
157 QString m_description;
158 QWidget * m_parent;
161 signals:
162 void toggled(int index, bool active); // on programmatic or user interaction change
163 void triggered(int index, bool active); // on user interaction change only
164 void pushed(int index); // only emitted on user interaction && when 'active' is true
165 void pushed();
168 #endif // RADIOUIACTION_H