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
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
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
):
50 m_description(descript
),
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
)
65 void addKey(const int & key
)
67 if (key
> 0 && !m_keys
.contains(key
)) {
73 void addKeys(const QList
<int> & keys
)
75 foreach (int key
, keys
)
82 m_parent
->removeEventFilter(this);
84 m_parent
->installEventFilter(this);
89 void setParent(QObject
* parent
)
91 QObject::setParent(parent
);
92 QWidget
* w
= qobject_cast
<QWidget
*>(parent
);
93 if (w
&& w
!= m_parent
) {
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
= "")
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
);
123 return QObject::eventFilter(obj
, event
);
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
) {
133 emit
toggled(m_hwIndex
, active
);
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
);
145 emit
pushed(m_hwIndex
);
157 QString m_description
;
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
168 #endif // RADIOUIACTION_H