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