Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qactiongroup.cpp
Go to the documentation of this file.
1// Copyright (C) 2019 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include "qactiongroup.h"
6
7#include "qaction.h"
8#include "qaction_p.h"
10#include "qevent.h"
11#include "qlist.h"
12
14
15QActionGroupPrivate::QActionGroupPrivate() :
16 enabled(1), visible(1)
17{
18}
19
20QActionGroupPrivate::~QActionGroupPrivate() = default;
21
22void QActionGroup::_q_actionChanged()
23{
24 Q_D(QActionGroup);
25 auto action = qobject_cast<QAction*>(sender());
26 Q_ASSERT_X(action != nullptr, "QActionGroup::_q_actionChanged", "internal error");
27 if (d->exclusionPolicy != QActionGroup::ExclusionPolicy::None) {
28 if (action->isChecked()) {
29 if (action != d->current) {
30 if (!d->current.isNull())
31 d->current->setChecked(false);
32 d->current = action;
33 }
34 } else if (action == d->current) {
35 d->current = nullptr;
36 }
37 }
38}
39
40void QActionGroup::_q_actionTriggered()
41{
42 auto action = qobject_cast<QAction*>(sender());
43 Q_ASSERT_X(action != nullptr, "QActionGroup::_q_actionTriggered", "internal error");
44 emit triggered(action);
45}
46
47void QActionGroup::_q_actionHovered()
48{
49 auto action = qobject_cast<QAction*>(sender());
50 Q_ASSERT_X(action != nullptr, "QActionGroup::_q_actionHovered", "internal error");
51 emit hovered(action);
52}
53
54/*!
55 \class QActionGroup
56 \brief The QActionGroup class groups actions together.
57 \since 6.0
58
59 \inmodule QtGui
60
61 QActionGroup is a base class for classes grouping
62 classes inhheriting QAction objects together.
63
64 In some situations it is useful to group QAction objects together.
65 For example, if you have a \uicontrol{Left Align} action, a \uicontrol{Right
66 Align} action, a \uicontrol{Justify} action, and a \uicontrol{Center} action,
67 only one of these actions should be active at any one time. One
68 simple way of achieving this is to group the actions together in
69 an action group, inheriting QActionGroup.
70
71 \sa QAction
72*/
73
74/*!
75 \enum QActionGroup::ExclusionPolicy
76
77 This enum specifies the different policies that can be used to
78 control how the group performs exclusive checking on checkable actions.
79
80 \value None
81 The actions in the group can be checked independently of each other.
82
83 \value Exclusive
84 Exactly one action can be checked at any one time.
85 This is the default policy.
86
87 \value ExclusiveOptional
88 At most one action can be checked at any one time. The actions
89 can also be all unchecked.
90
91 \sa exclusionPolicy
92*/
93
94/*!
95 Constructs an action group for the \a parent object.
96
97 The action group is exclusive by default. Call setExclusive(false)
98 to make the action group non-exclusive. To make the group exclusive
99 but allow unchecking the active action call instead
100 setExclusionPolicy(QActionGroup::ExclusionPolicy::ExclusiveOptional)
101*/
102QActionGroup::QActionGroup(QObject* parent) :
103 QActionGroup(*new QActionGroupPrivate, parent)
104{
105}
106
107QActionGroup::QActionGroup(QActionGroupPrivate &dd, QObject *parent) :
108 QObject(dd, parent)
109{
110}
111
112/*!
113 Destroys the action group.
114*/
115QActionGroup::~QActionGroup() = default;
116
117/*!
118 \fn QAction *QActionGroup::addAction(QAction *action)
119
120 Adds the \a action to this group, and returns it.
121
122 Normally an action is added to a group by creating it with the
123 group as its parent, so this function is not usually used.
124
125 \sa QAction::setActionGroup()
126*/
127QAction *QActionGroup::addAction(QAction* a)
128{
129 Q_D(QActionGroup);
130 if (!d->actions.contains(a)) {
131 d->actions.append(a);
132 QObject::connect(a, &QAction::triggered, this, &QActionGroup::_q_actionTriggered);
133 QObject::connect(a, &QAction::changed, this, &QActionGroup::_q_actionChanged);
134 QObject::connect(a, &QAction::hovered, this, &QActionGroup::_q_actionHovered);
135 }
136 a->d_func()->setEnabled(d->enabled, true);
137 if (!a->d_func()->forceInvisible)
138 a->d_func()->setVisible(d->visible);
139 if (a->isChecked())
140 d->current = a;
141 QActionGroup *oldGroup = a->d_func()->group;
142 if (oldGroup != this) {
143 if (oldGroup)
144 oldGroup->removeAction(a);
145 a->d_func()->group = this;
146 a->d_func()->sendDataChanged();
147 }
148 return a;
149}
150
151/*!
152 Creates and returns an action with \a text. The newly created
153 action is a child of this action group.
154
155 Normally an action is added to a group by creating it with the
156 group as parent, so this function is not usually used.
157
158 \sa QAction::setActionGroup()
159*/
160QAction *QActionGroup::addAction(const QString &text)
161{
162 return new QAction(text, this);
163}
164
165/*!
166 Creates and returns an action with \a text and an \a icon. The
167 newly created action is a child of this action group.
168
169 Normally an action is added to a group by creating it with the
170 group as its parent, so this function is not usually used.
171
172 \sa QAction::setActionGroup()
173*/
174QAction *QActionGroup::addAction(const QIcon &icon, const QString &text)
175{
176 return new QAction(icon, text, this);
177}
178
179/*!
180 Removes the \a action from this group. The action will have no
181 parent as a result.
182
183 \sa QAction::setActionGroup()
184*/
185void QActionGroup::removeAction(QAction *action)
186{
187 Q_D(QActionGroup);
188 if (d->actions.removeAll(action)) {
189 if (action == d->current)
190 d->current = nullptr;
191 QObject::disconnect(action, &QAction::triggered, this, &QActionGroup::_q_actionTriggered);
192 QObject::disconnect(action, &QAction::changed, this, &QActionGroup::_q_actionChanged);
193 QObject::disconnect(action, &QAction::hovered, this, &QActionGroup::_q_actionHovered);
194 action->d_func()->group = nullptr;
195 }
196}
197
198/*!
199 Returns the list of this groups's actions. This may be empty.
200*/
201QList<QAction*> QActionGroup::actions() const
202{
203 Q_D(const QActionGroup);
204 return d->actions;
205}
206
207/*!
208 \brief Enable or disable the group exclusion checking
209
210 This is a convenience method that calls
211 setExclusionPolicy(ExclusionPolicy::Exclusive) when \a b is true,
212 else setExclusionPolicy(QActionGroup::ExclusionPolicy::None).
213
214 \sa QActionGroup::exclusionPolicy
215*/
216void QActionGroup::setExclusive(bool b)
217{
218 setExclusionPolicy(b ? QActionGroup::ExclusionPolicy::Exclusive
219 : QActionGroup::ExclusionPolicy::None);
220}
221
222/*!
223 \brief Returns true if the group is exclusive
224
225 The group is exclusive if the ExclusionPolicy is either Exclusive
226 or ExclusionOptional.
227
228*/
229bool QActionGroup::isExclusive() const
230{
231 return exclusionPolicy() != QActionGroup::ExclusionPolicy::None;
232}
233
234/*!
235 \property QActionGroup::exclusionPolicy
236 \brief This property holds the group exclusive checking policy
237
238 If exclusionPolicy is set to Exclusive, only one checkable
239 action in the action group can ever be active at any time. If the user
240 chooses another checkable action in the group, the one they chose becomes
241 active and the one that was active becomes inactive. If exclusionPolicy is
242 set to ExclusionOptional the group is exclusive but the active checkable
243 action in the group can be unchecked leaving the group with no actions
244 checked.
245
246 \sa QAction::checkable
247*/
248void QActionGroup::setExclusionPolicy(QActionGroup::ExclusionPolicy policy)
249{
250 Q_D(QActionGroup);
251 d->exclusionPolicy = policy;
252}
253
254QActionGroup::ExclusionPolicy QActionGroup::exclusionPolicy() const
255{
256 Q_D(const QActionGroup);
257 return d->exclusionPolicy;
258}
259
260/*!
261 \fn void QActionGroup::setDisabled(bool b)
262
263 This is a convenience function for the \l enabled property, that
264 is useful for signals--slots connections. If \a b is true the
265 action group is disabled; otherwise it is enabled.
266*/
267
268/*!
269 \property QActionGroup::enabled
270 \brief whether the action group is enabled
271
272 Each action in the group will be enabled or disabled unless it
273 has been explicitly disabled.
274
275 \sa QAction::setEnabled()
276*/
277void QActionGroup::setEnabled(bool b)
278{
279 Q_D(QActionGroup);
280 d->enabled = b;
281 for (auto action : std::as_const(d->actions)) {
282 action->d_func()->setEnabled(b, true);
283 }
284}
285
286bool QActionGroup::isEnabled() const
287{
288 Q_D(const QActionGroup);
289 return d->enabled;
290}
291
292/*!
293 Returns the currently checked action in the group, or \nullptr if
294 none are checked.
295*/
296QAction *QActionGroup::checkedAction() const
297{
298 Q_D(const QActionGroup);
299 return d->current.data();
300}
301
302/*!
303 \property QActionGroup::visible
304 \brief whether the action group is visible
305
306 Each action in the action group will match the visible state of
307 this group unless it has been explicitly hidden.
308
309 \sa QAction::setEnabled()
310*/
311void QActionGroup::setVisible(bool b)
312{
313 Q_D(QActionGroup);
314 d->visible = b;
315 for (auto action : std::as_const(d->actions)) {
316 if (!action->d_func()->forceInvisible)
317 action->d_func()->setVisible(b);
318 }
319}
320
321bool QActionGroup::isVisible() const
322{
323 Q_D(const QActionGroup);
324 return d->visible;
325}
326
327/*!
328 \fn void QActionGroup::triggered(QAction *action)
329
330 This signal is emitted when the given \a action in the action
331 group is activated by the user; for example, when the user clicks
332 a menu option or a toolbar button, or presses an action's shortcut
333 key combination.
334
335 Connect to this signal for command actions.
336
337 \sa QAction::activate()
338*/
339
340/*!
341 \fn void QActionGroup::hovered(QAction *action)
342
343 This signal is emitted when the given \a action in the action
344 group is highlighted by the user; for example, when the user
345 pauses with the cursor over a menu option or a toolbar button,
346 or presses an action's shortcut key combination.
347
348 \sa QAction::activate()
349*/
350
351QT_END_NAMESPACE
352
353#include "moc_qactiongroup.cpp"
Combined button and popup list for selecting options.