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