Qt 4.8
qactiongroup.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 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 
52 
54 {
56 public:
63 
64 private:
65  void _q_actionTriggered(); //private slot
66  void _q_actionChanged(); //private slot
67  void _q_actionHovered(); //private slot
68 };
69 
71 {
73  QAction *action = qobject_cast<QAction*>(q->sender());
74  Q_ASSERT_X(action != 0, "QWidgetGroup::_q_actionChanged", "internal error");
75  if(exclusive) {
76  if (action->isChecked()) {
77  if (action != current) {
78  if(current)
79  current->setChecked(false);
80  current = action;
81  }
82  } else if (action == current) {
83  current = 0;
84  }
85  }
86 }
87 
89 {
91  QAction *action = qobject_cast<QAction*>(q->sender());
92  Q_ASSERT_X(action != 0, "QWidgetGroup::_q_actionTriggered", "internal error");
93  emit q->triggered(action);
94  emit q->selected(action);
95 }
96 
98 {
100  QAction *action = qobject_cast<QAction*>(q->sender());
101  Q_ASSERT_X(action != 0, "QWidgetGroup::_q_actionHovered", "internal error");
102  emit q->hovered(action);
103 }
104 
160 {
161 }
162 
167 {
168 }
169 
181 {
182  Q_D(QActionGroup);
183  if(!d->actions.contains(a)) {
184  d->actions.append(a);
185  QObject::connect(a, SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
186  QObject::connect(a, SIGNAL(changed()), this, SLOT(_q_actionChanged()));
187  QObject::connect(a, SIGNAL(hovered()), this, SLOT(_q_actionHovered()));
188  }
189  if(!a->d_func()->forceDisabled) {
190  a->setEnabled(d->enabled);
191  a->d_func()->forceDisabled = false;
192  }
193  if(!a->d_func()->forceInvisible) {
194  a->setVisible(d->visible);
195  a->d_func()->forceInvisible = false;
196  }
197  if(a->isChecked())
198  d->current = a;
199  QActionGroup *oldGroup = a->d_func()->group;
200  if(oldGroup != this) {
201  if (oldGroup)
202  oldGroup->removeAction(a);
203  a->d_func()->group = this;
204  }
205  return a;
206 }
207 
218 {
219  return new QAction(text, this);
220 }
221 
232 {
233  return new QAction(icon, text, this);
234 }
235 
243 {
244  Q_D(QActionGroup);
245  if (d->actions.removeAll(action)) {
246  if (action == d->current)
247  d->current = 0;
248  QObject::disconnect(action, SIGNAL(triggered()), this, SLOT(_q_actionTriggered()));
249  QObject::disconnect(action, SIGNAL(changed()), this, SLOT(_q_actionChanged()));
250  QObject::disconnect(action, SIGNAL(hovered()), this, SLOT(_q_actionHovered()));
251  action->d_func()->group = 0;
252  }
253 }
254 
259 {
260  Q_D(const QActionGroup);
261  return d->actions;
262 }
263 
279 {
280  Q_D(QActionGroup);
281  d->exclusive = b;
282 }
283 
285 {
286  Q_D(const QActionGroup);
287  return d->exclusive;
288 }
289 
314 {
315  Q_D(QActionGroup);
316  d->enabled = b;
317  for(QList<QAction*>::const_iterator it = d->actions.constBegin(); it != d->actions.constEnd(); ++it) {
318  if(!(*it)->d_func()->forceDisabled) {
319  (*it)->setEnabled(b);
320  (*it)->d_func()->forceDisabled = false;
321  }
322  }
323 }
324 
326 {
327  Q_D(const QActionGroup);
328  return d->enabled;
329 }
330 
336 {
337  Q_D(const QActionGroup);
338  return d->current;
339 }
340 
354 {
355  Q_D(QActionGroup);
356  d->visible = b;
357  for(QList<QAction*>::Iterator it = d->actions.begin(); it != d->actions.end(); ++it) {
358  if(!(*it)->d_func()->forceInvisible) {
359  (*it)->setVisible(b);
360  (*it)->d_func()->forceInvisible = false;
361  }
362  }
363 }
364 
366 {
367  Q_D(const QActionGroup);
368  return d->visible;
369 }
370 
434 
435 #include "moc_qactiongroup.cpp"
436 
437 #endif // QT_NO_ACTION
T qobject_cast(QObject *object)
Definition: qobject.h:375
double d
Definition: qnumeric_p.h:62
QPointer< QAction > current
void hovered(QAction *)
This signal is emitted when the given action in the action group is highlighted by the user; for exam...
QActionGroup(QObject *parent)
Constructs an action group for the parent object.
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
#define it(className, varName)
void removeAction(QAction *a)
Removes the action from this group.
QAction * checkedAction() const
Returns the currently checked action in the group, or 0 if none are checked.
#define SLOT(a)
Definition: qobjectdefs.h:226
The QList::const_iterator class provides an STL-style const iterator for QList and QQueue...
Definition: qlist.h:228
long ASN1_INTEGER_get ASN1_INTEGER * a
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_D(Class)
Definition: qglobal.h:2482
The QActionGroup class groups actions together.
Definition: qactiongroup.h:57
void setEnabled(bool)
Definition: qaction.cpp:1192
#define Q_Q(Class)
Definition: qglobal.h:2483
#define SIGNAL(a)
Definition: qobjectdefs.h:227
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
void triggered(QAction *)
This signal is emitted when the given action in the action group is activated by the user; for exampl...
QAction * addAction(QAction *a)
Adds the action to this group, and returns it.
#define emit
Definition: qobjectdefs.h:76
unsigned int uint
Definition: qglobal.h:996
QList< QAction * > actions
void setExclusive(bool)
The QList::iterator class provides an STL-style non-const iterator for QList and QQueue.
Definition: qlist.h:181
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
void setVisible(bool)
Definition: qaction.cpp:1230
#define Q_DECLARE_PUBLIC(Class)
Definition: qglobal.h:2477
void setChecked(bool)
Definition: qaction.cpp:1138
bool isChecked() const
Definition: qaction.cpp:1151
QObject * parent
Definition: qobject.h:92
void setEnabled(bool)
~QActionGroup()
Destroys the action group.
bool isEnabled() const
QList< QAction * > actions() const
Returns the list of this groups&#39;s actions.
bool isVisible() const
bool isExclusive() const
The QAction class provides an abstract user interface action that can be inserted into widgets...
Definition: qaction.h:64
#define text
Definition: qobjectdefs.h:80
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
void setVisible(bool)
The QIcon class provides scalable icons in different modes and states.
Definition: qicon.h:60