Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/widgets/kernel/qactiongroup.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||
---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||
2 | ** | - | ||||||
3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||
4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||
5 | ** | - | ||||||
6 | ** This file is part of the QtWidgets module of the Qt Toolkit. | - | ||||||
7 | ** | - | ||||||
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||
9 | ** Commercial License Usage | - | ||||||
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||
11 | ** accordance with the commercial license agreement provided with the | - | ||||||
12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||
13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||
14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||
15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||
16 | ** | - | ||||||
17 | ** GNU Lesser General Public License Usage | - | ||||||
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||
19 | ** General Public License version 3 as published by the Free Software | - | ||||||
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||
21 | ** packaging of this file. Please review the following information to | - | ||||||
22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||
24 | ** | - | ||||||
25 | ** GNU General Public License Usage | - | ||||||
26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||
27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||
28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||
29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||
31 | ** included in the packaging of this file. Please review the following | - | ||||||
32 | ** information to ensure the GNU General Public License requirements will | - | ||||||
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||
35 | ** | - | ||||||
36 | ** $QT_END_LICENSE$ | - | ||||||
37 | ** | - | ||||||
38 | ****************************************************************************/ | - | ||||||
39 | - | |||||||
40 | #include "qactiongroup.h" | - | ||||||
41 | - | |||||||
42 | #ifndef QT_NO_ACTION | - | ||||||
43 | - | |||||||
44 | #include "qaction_p.h" | - | ||||||
45 | #include "qapplication.h" | - | ||||||
46 | #include "qevent.h" | - | ||||||
47 | #include "qlist.h" | - | ||||||
48 | - | |||||||
49 | QT_BEGIN_NAMESPACE | - | ||||||
50 | - | |||||||
51 | class QActionGroupPrivate : public QObjectPrivate | - | ||||||
52 | { | - | ||||||
53 | Q_DECLARE_PUBLIC(QActionGroup) | - | ||||||
54 | public: | - | ||||||
55 | QActionGroupPrivate() : exclusive(1), enabled(1), visible(1) { } never executed: end of block | 0 | ||||||
56 | QList<QAction *> actions; | - | ||||||
57 | QPointer<QAction> current; | - | ||||||
58 | uint exclusive : 1; | - | ||||||
59 | uint enabled : 1; | - | ||||||
60 | uint visible : 1; | - | ||||||
61 | - | |||||||
62 | private: | - | ||||||
63 | void _q_actionTriggered(); //private slot | - | ||||||
64 | void _q_actionChanged(); //private slot | - | ||||||
65 | void _q_actionHovered(); //private slot | - | ||||||
66 | }; | - | ||||||
67 | - | |||||||
68 | void QActionGroupPrivate::_q_actionChanged() | - | ||||||
69 | { | - | ||||||
70 | Q_Q(QActionGroup); | - | ||||||
71 | QAction *action = qobject_cast<QAction*>(q->sender()); | - | ||||||
72 | Q_ASSERT_X(action != 0, "QWidgetGroup::_q_actionChanged", "internal error"); | - | ||||||
73 | if(exclusive) {
| 0 | ||||||
74 | if (action->isChecked()) {
| 0 | ||||||
75 | if (action != current) {
| 0 | ||||||
76 | if(current)
| 0 | ||||||
77 | current->setChecked(false); never executed: current->setChecked(false); | 0 | ||||||
78 | current = action; | - | ||||||
79 | } never executed: end of block | 0 | ||||||
80 | } else if (action == current) { never executed: end of block
| 0 | ||||||
81 | current = 0; | - | ||||||
82 | } never executed: end of block | 0 | ||||||
83 | } never executed: end of block | 0 | ||||||
84 | } never executed: end of block | 0 | ||||||
85 | - | |||||||
86 | void QActionGroupPrivate::_q_actionTriggered() | - | ||||||
87 | { | - | ||||||
88 | Q_Q(QActionGroup); | - | ||||||
89 | QAction *action = qobject_cast<QAction*>(q->sender()); | - | ||||||
90 | Q_ASSERT_X(action != 0, "QWidgetGroup::_q_actionTriggered", "internal error"); | - | ||||||
91 | emit q->triggered(action); | - | ||||||
92 | } never executed: end of block | 0 | ||||||
93 | - | |||||||
94 | void QActionGroupPrivate::_q_actionHovered() | - | ||||||
95 | { | - | ||||||
96 | Q_Q(QActionGroup); | - | ||||||
97 | QAction *action = qobject_cast<QAction*>(q->sender()); | - | ||||||
98 | Q_ASSERT_X(action != 0, "QWidgetGroup::_q_actionHovered", "internal error"); | - | ||||||
99 | emit q->hovered(action); | - | ||||||
100 | } never executed: end of block | 0 | ||||||
101 | - | |||||||
102 | /*! | - | ||||||
103 | \class QActionGroup | - | ||||||
104 | \brief The QActionGroup class groups actions together. | - | ||||||
105 | - | |||||||
106 | \ingroup mainwindow-classes | - | ||||||
107 | \inmodule QtWidgets | - | ||||||
108 | - | |||||||
109 | In some situations it is useful to group QAction objects together. | - | ||||||
110 | For example, if you have a \uicontrol{Left Align} action, a \uicontrol{Right | - | ||||||
111 | Align} action, a \uicontrol{Justify} action, and a \uicontrol{Center} action, | - | ||||||
112 | only one of these actions should be active at any one time. One | - | ||||||
113 | simple way of achieving this is to group the actions together in | - | ||||||
114 | an action group. | - | ||||||
115 | - | |||||||
116 | Here's a example (from the \l{mainwindows/menus}{Menus} example): | - | ||||||
117 | - | |||||||
118 | \snippet mainwindows/menus/mainwindow.cpp 6 | - | ||||||
119 | - | |||||||
120 | Here we create a new action group. Since the action group is | - | ||||||
121 | exclusive by default, only one of the actions in the group is | - | ||||||
122 | checked at any one time. | - | ||||||
123 | - | |||||||
124 | \image qactiongroup-align.png Alignment options in a QMenu | - | ||||||
125 | - | |||||||
126 | A QActionGroup emits an triggered() signal when one of its | - | ||||||
127 | actions is chosen. Each action in an action group emits its | - | ||||||
128 | triggered() signal as usual. | - | ||||||
129 | - | |||||||
130 | As stated above, an action group is \l exclusive by default; it | - | ||||||
131 | ensures that only one checkable action is active at any one time. | - | ||||||
132 | If you want to group checkable actions without making them | - | ||||||
133 | exclusive, you can turn of exclusiveness by calling | - | ||||||
134 | setExclusive(false). | - | ||||||
135 | - | |||||||
136 | Actions can be added to an action group using addAction(), but it | - | ||||||
137 | is usually more convenient to specify a group when creating | - | ||||||
138 | actions; this ensures that actions are automatically created with | - | ||||||
139 | a parent. Actions can be visually separated from each other by | - | ||||||
140 | adding a separator action to the group; create an action and use | - | ||||||
141 | QAction's \l {QAction::}{setSeparator()} function to make it | - | ||||||
142 | considered a separator. Action groups are added to widgets with | - | ||||||
143 | the QWidget::addActions() function. | - | ||||||
144 | - | |||||||
145 | \sa QAction | - | ||||||
146 | */ | - | ||||||
147 | - | |||||||
148 | /*! | - | ||||||
149 | Constructs an action group for the \a parent object. | - | ||||||
150 | - | |||||||
151 | The action group is exclusive by default. Call setExclusive(false) | - | ||||||
152 | to make the action group non-exclusive. | - | ||||||
153 | */ | - | ||||||
154 | QActionGroup::QActionGroup(QObject* parent) : QObject(*new QActionGroupPrivate, parent) | - | ||||||
155 | { | - | ||||||
156 | } never executed: end of block | 0 | ||||||
157 | - | |||||||
158 | /*! | - | ||||||
159 | Destroys the action group. | - | ||||||
160 | */ | - | ||||||
161 | QActionGroup::~QActionGroup() | - | ||||||
162 | { | - | ||||||
163 | } | - | ||||||
164 | - | |||||||
165 | /*! | - | ||||||
166 | \fn QAction *QActionGroup::addAction(QAction *action) | - | ||||||
167 | - | |||||||
168 | Adds the \a action to this group, and returns it. | - | ||||||
169 | - | |||||||
170 | Normally an action is added to a group by creating it with the | - | ||||||
171 | group as its parent, so this function is not usually used. | - | ||||||
172 | - | |||||||
173 | \sa QAction::setActionGroup() | - | ||||||
174 | */ | - | ||||||
175 | QAction *QActionGroup::addAction(QAction* a) | - | ||||||
176 | { | - | ||||||
177 | Q_D(QActionGroup); | - | ||||||
178 | if(!d->actions.contains(a)) {
| 0 | ||||||
179 | d->actions.append(a); | - | ||||||
180 | QObject::connect(a, SIGNAL(triggered()), this, SLOT(_q_actionTriggered())); | - | ||||||
181 | QObject::connect(a, SIGNAL(changed()), this, SLOT(_q_actionChanged())); | - | ||||||
182 | QObject::connect(a, SIGNAL(hovered()), this, SLOT(_q_actionHovered())); | - | ||||||
183 | } never executed: end of block | 0 | ||||||
184 | if(!a->d_func()->forceDisabled) {
| 0 | ||||||
185 | a->setEnabled(d->enabled); | - | ||||||
186 | a->d_func()->forceDisabled = false; | - | ||||||
187 | } never executed: end of block | 0 | ||||||
188 | if(!a->d_func()->forceInvisible) {
| 0 | ||||||
189 | a->setVisible(d->visible); | - | ||||||
190 | a->d_func()->forceInvisible = false; | - | ||||||
191 | } never executed: end of block | 0 | ||||||
192 | if(a->isChecked())
| 0 | ||||||
193 | d->current = a; never executed: d->current = a; | 0 | ||||||
194 | QActionGroup *oldGroup = a->d_func()->group; | - | ||||||
195 | if(oldGroup != this) {
| 0 | ||||||
196 | if (oldGroup)
| 0 | ||||||
197 | oldGroup->removeAction(a); never executed: oldGroup->removeAction(a); | 0 | ||||||
198 | a->d_func()->group = this; | - | ||||||
199 | a->d_func()->sendDataChanged(); | - | ||||||
200 | } never executed: end of block | 0 | ||||||
201 | return a; never executed: return a; | 0 | ||||||
202 | } | - | ||||||
203 | - | |||||||
204 | /*! | - | ||||||
205 | Creates and returns an action with \a text. The newly created | - | ||||||
206 | action is a child of this action group. | - | ||||||
207 | - | |||||||
208 | Normally an action is added to a group by creating it with the | - | ||||||
209 | group as parent, so this function is not usually used. | - | ||||||
210 | - | |||||||
211 | \sa QAction::setActionGroup() | - | ||||||
212 | */ | - | ||||||
213 | QAction *QActionGroup::addAction(const QString &text) | - | ||||||
214 | { | - | ||||||
215 | return new QAction(text, this); never executed: return new QAction(text, this); | 0 | ||||||
216 | } | - | ||||||
217 | - | |||||||
218 | /*! | - | ||||||
219 | Creates and returns an action with \a text and an \a icon. The | - | ||||||
220 | newly created action is a child of this action group. | - | ||||||
221 | - | |||||||
222 | Normally an action is added to a group by creating it with the | - | ||||||
223 | group as its parent, so this function is not usually used. | - | ||||||
224 | - | |||||||
225 | \sa QAction::setActionGroup() | - | ||||||
226 | */ | - | ||||||
227 | QAction *QActionGroup::addAction(const QIcon &icon, const QString &text) | - | ||||||
228 | { | - | ||||||
229 | return new QAction(icon, text, this); never executed: return new QAction(icon, text, this); | 0 | ||||||
230 | } | - | ||||||
231 | - | |||||||
232 | /*! | - | ||||||
233 | Removes the \a action from this group. The action will have no | - | ||||||
234 | parent as a result. | - | ||||||
235 | - | |||||||
236 | \sa QAction::setActionGroup() | - | ||||||
237 | */ | - | ||||||
238 | void QActionGroup::removeAction(QAction *action) | - | ||||||
239 | { | - | ||||||
240 | Q_D(QActionGroup); | - | ||||||
241 | if (d->actions.removeAll(action)) {
| 0 | ||||||
242 | if (action == d->current)
| 0 | ||||||
243 | d->current = 0; never executed: d->current = 0; | 0 | ||||||
244 | QObject::disconnect(action, SIGNAL(triggered()), this, SLOT(_q_actionTriggered())); | - | ||||||
245 | QObject::disconnect(action, SIGNAL(changed()), this, SLOT(_q_actionChanged())); | - | ||||||
246 | QObject::disconnect(action, SIGNAL(hovered()), this, SLOT(_q_actionHovered())); | - | ||||||
247 | action->d_func()->group = 0; | - | ||||||
248 | } never executed: end of block | 0 | ||||||
249 | } never executed: end of block | 0 | ||||||
250 | - | |||||||
251 | /*! | - | ||||||
252 | Returns the list of this groups's actions. This may be empty. | - | ||||||
253 | */ | - | ||||||
254 | QList<QAction*> QActionGroup::actions() const | - | ||||||
255 | { | - | ||||||
256 | Q_D(const QActionGroup); | - | ||||||
257 | return d->actions; never executed: return d->actions; | 0 | ||||||
258 | } | - | ||||||
259 | - | |||||||
260 | /*! | - | ||||||
261 | \property QActionGroup::exclusive | - | ||||||
262 | \brief whether the action group does exclusive checking | - | ||||||
263 | - | |||||||
264 | If exclusive is true, only one checkable action in the action group | - | ||||||
265 | can ever be active at any time. If the user chooses another | - | ||||||
266 | checkable action in the group, the one they chose becomes active and | - | ||||||
267 | the one that was active becomes inactive. | - | ||||||
268 | - | |||||||
269 | \sa QAction::checkable | - | ||||||
270 | */ | - | ||||||
271 | void QActionGroup::setExclusive(bool b) | - | ||||||
272 | { | - | ||||||
273 | Q_D(QActionGroup); | - | ||||||
274 | d->exclusive = b; | - | ||||||
275 | } never executed: end of block | 0 | ||||||
276 | - | |||||||
277 | bool QActionGroup::isExclusive() const | - | ||||||
278 | { | - | ||||||
279 | Q_D(const QActionGroup); | - | ||||||
280 | return d->exclusive; never executed: return d->exclusive; | 0 | ||||||
281 | } | - | ||||||
282 | - | |||||||
283 | /*! | - | ||||||
284 | \fn void QActionGroup::setDisabled(bool b) | - | ||||||
285 | - | |||||||
286 | This is a convenience function for the \l enabled property, that | - | ||||||
287 | is useful for signals--slots connections. If \a b is true the | - | ||||||
288 | action group is disabled; otherwise it is enabled. | - | ||||||
289 | */ | - | ||||||
290 | - | |||||||
291 | /*! | - | ||||||
292 | \property QActionGroup::enabled | - | ||||||
293 | \brief whether the action group is enabled | - | ||||||
294 | - | |||||||
295 | Each action in the group will be enabled or disabled unless it | - | ||||||
296 | has been explicitly disabled. | - | ||||||
297 | - | |||||||
298 | \sa QAction::setEnabled() | - | ||||||
299 | */ | - | ||||||
300 | void QActionGroup::setEnabled(bool b) | - | ||||||
301 | { | - | ||||||
302 | Q_D(QActionGroup); | - | ||||||
303 | d->enabled = b; | - | ||||||
304 | for(QList<QAction*>::const_iterator it = d->actions.constBegin(); it != d->actions.constEnd(); ++it) {
| 0 | ||||||
305 | if(!(*it)->d_func()->forceDisabled) {
| 0 | ||||||
306 | (*it)->setEnabled(b); | - | ||||||
307 | (*it)->d_func()->forceDisabled = false; | - | ||||||
308 | } never executed: end of block | 0 | ||||||
309 | } never executed: end of block | 0 | ||||||
310 | } never executed: end of block | 0 | ||||||
311 | - | |||||||
312 | bool QActionGroup::isEnabled() const | - | ||||||
313 | { | - | ||||||
314 | Q_D(const QActionGroup); | - | ||||||
315 | return d->enabled; never executed: return d->enabled; | 0 | ||||||
316 | } | - | ||||||
317 | - | |||||||
318 | /*! | - | ||||||
319 | Returns the currently checked action in the group, or 0 if none | - | ||||||
320 | are checked. | - | ||||||
321 | */ | - | ||||||
322 | QAction *QActionGroup::checkedAction() const | - | ||||||
323 | { | - | ||||||
324 | Q_D(const QActionGroup); | - | ||||||
325 | return d->current; never executed: return d->current; | 0 | ||||||
326 | } | - | ||||||
327 | - | |||||||
328 | /*! | - | ||||||
329 | \property QActionGroup::visible | - | ||||||
330 | \brief whether the action group is visible | - | ||||||
331 | - | |||||||
332 | Each action in the action group will match the visible state of | - | ||||||
333 | this group unless it has been explicitly hidden. | - | ||||||
334 | - | |||||||
335 | \sa QAction::setEnabled() | - | ||||||
336 | */ | - | ||||||
337 | void QActionGroup::setVisible(bool b) | - | ||||||
338 | { | - | ||||||
339 | Q_D(QActionGroup); | - | ||||||
340 | d->visible = b; | - | ||||||
341 | for(QList<QAction*>::Iterator it = d->actions.begin(); it != d->actions.end(); ++it) {
| 0 | ||||||
342 | if(!(*it)->d_func()->forceInvisible) {
| 0 | ||||||
343 | (*it)->setVisible(b); | - | ||||||
344 | (*it)->d_func()->forceInvisible = false; | - | ||||||
345 | } never executed: end of block | 0 | ||||||
346 | } never executed: end of block | 0 | ||||||
347 | } never executed: end of block | 0 | ||||||
348 | - | |||||||
349 | bool QActionGroup::isVisible() const | - | ||||||
350 | { | - | ||||||
351 | Q_D(const QActionGroup); | - | ||||||
352 | return d->visible; never executed: return d->visible; | 0 | ||||||
353 | } | - | ||||||
354 | - | |||||||
355 | /*! | - | ||||||
356 | \fn void QActionGroup::triggered(QAction *action) | - | ||||||
357 | - | |||||||
358 | This signal is emitted when the given \a action in the action | - | ||||||
359 | group is activated by the user; for example, when the user clicks | - | ||||||
360 | a menu option, toolbar button, or presses an action's shortcut key | - | ||||||
361 | combination. | - | ||||||
362 | - | |||||||
363 | Connect to this signal for command actions. | - | ||||||
364 | - | |||||||
365 | \sa QAction::activate() | - | ||||||
366 | */ | - | ||||||
367 | - | |||||||
368 | /*! | - | ||||||
369 | \fn void QActionGroup::hovered(QAction *action) | - | ||||||
370 | - | |||||||
371 | This signal is emitted when the given \a action in the action | - | ||||||
372 | group is highlighted by the user; for example, when the user | - | ||||||
373 | pauses with the cursor over a menu option, toolbar button, or | - | ||||||
374 | presses an action's shortcut key combination. | - | ||||||
375 | - | |||||||
376 | \sa QAction::activate() | - | ||||||
377 | */ | - | ||||||
378 | - | |||||||
379 | QT_END_NAMESPACE | - | ||||||
380 | - | |||||||
381 | #include "moc_qactiongroup.cpp" | - | ||||||
382 | - | |||||||
383 | #endif // QT_NO_ACTION | - | ||||||
Source code | Switch to Preprocessed file |