casa  5.7.0-16
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QtActionGroup.qo.h
Go to the documentation of this file.
1 //# QtActionGroup.qo.h: Like QActionGroup but with additional functionality.
2 //# Copyright (C) 2009
3 //# Associated Universities, Inc. Washington DC, USA.
4 //#
5 //# This library is free software; you can redistribute it and/or modify it
6 //# under the terms of the GNU Library General Public License as published by
7 //# the Free Software Foundation; either version 2 of the License, or (at your
8 //# option) any later version.
9 //#
10 //# This library is distributed in the hope that it will be useful, but WITHOUT
11 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 //# FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13 //# License for more details.
14 //#
15 //# You should have received a copy of the GNU Library General Public License
16 //# along with this library; if not, write to the Free Software Foundation,
17 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18 //#
19 //# Correspondence concerning AIPS++ should be addressed as follows:
20 //# Internet email: aips2-request@nrao.edu.
21 //# Postal address: AIPS++ Project Office
22 //# National Radio Astronomy Observatory
23 //# 520 Edgemont Road
24 //# Charlottesville, VA 22903-2475 USA
25 //#
26 //# $Id: $
27 #ifndef QTACTIONGROUP_QO_H_
28 #define QTACTIONGROUP_QO_H_
29 
30 #include <QAbstractButton>
31 #include <QAction>
32 #include <QList>
33 #include <QMultiMap>
34 
35 using namespace std;
36 
37 namespace casa {
38 
39 // This class is used to synchronize the checked state of actions with
40 // checkable buttons or other actions. This makes it so that clicking the
41 // synchronized button/action has the same effect as clicking the action
42 // itself.
43 class QtActionSynchronizer : public QObject {
44  Q_OBJECT
45 
46 public:
47  // Constructor which takes an optional parent.
48  QtActionSynchronizer(QObject* parent = NULL);
49 
50  // Destructor.
51  virtual ~QtActionSynchronizer();
52 
53  // Synchronizes the given action in this group with the given button or
54  // other action.
55  // <group>
56  virtual void synchronize(QAction* action, QAbstractButton* button);
57  virtual void synchronize(QAction* action, QAction* otherAction);
58  // </group>
59 
60  // Unsynchronizes all buttons/actions associated with the given in this
61  // group.
62  // <group>
63  virtual void unsynchronize(QAction* action);
64  virtual void unsynchronize(QAbstractButton* button);
65  // </group>
66 
67  // Unsyncrhonizes the given action in this group with the given
68  // button/action.
69  // <group>
70  virtual void unsynchronize(QAction* action, QAbstractButton* button);
71  virtual void unsynchronize(QAction* action, QAction* otherAction);
72  // </group>
73 
74 protected:
75  // Synchronized actions maps.
76  // <group>
77  QMultiMap<QAction*, QAbstractButton*> itsSynchedButtons_;
78  QMultiMap<QAction*, QAction*> itsSynchedActions_;
79  // </group>
80 
81 
82  // Method which updates the actions/buttons associated with the given
83  // action.
84  virtual void actionTriggered_(QAction* action, bool checked);
85 
86  // Method which updates the action(s) associated with the given action or
87  // button to the given checked state.
88  virtual void synchronizedTriggered_(QAction* action,
89  QAbstractButton* button, bool checked);
90 
91 protected slots:
92  // Slot for when an action is toggled, which calls actionTriggered_() as
93  // needed.
94  virtual void actionToggled(bool checked);
95 
96  // Slot for when an action is triggered, which calls actionTriggered_() as
97  // needed.
98  virtual void actionTriggered(bool checked);
99 
100  // Slot for when a synchronizing button/action is toggled, which calls
101  // synchronizedTriggered_() as needed.
102  virtual void synchronizedToggled(bool checked);
103 
104  // Slot for when a synchronizing button/action is clicked/triggered, which
105  // calls synchronizedTriggered_() as needed.
106  virtual void synchronizedTriggered(bool checked);
107 };
108 
109 
110 // This class meant to be used almost identically to an exclusive QActionGroup.
111 // The difference is that this class allows all QActions to be unchecked; in
112 // other words, either one action is checked or none are. Some methods from
113 // QActionGroup have not been included, but they would be easy to include if
114 // desired. This class also has the added functionality included in
115 // QtActionSynchronnizer.
117  Q_OBJECT
118 
119 public:
120  // Constructor that optionally takes a parent object.
121  QtActionGroup(QObject* parent = NULL);
122 
123  // Destructor.
124  ~QtActionGroup();
125 
126  // See QActionGroup::actions().
127  QList<QAction*> actions() const;
128 
129  // See QActionGroup::checkedAction(). Will return NULL if no action is
130  // currently checked.
131  QAction* checkedAction() const;
132 
133  // See QActionGroup::addAction().
134  QAction* addAction(QAction* action);
135 
136  // See QActionGroup::removeAction().
137  void removeAction(QAction* action);
138 
139  // See QActionGroup::isEnabled().
140  bool isEnabled() const;
141 
142  // Overrides QtActionSynchronizer::synchronize to only allow actions that
143  // are in the group.
144  // <group>
145  void synchronize(QAction* action, QAbstractButton* button);
146  void synchronize(QAction* action, QAction* otherAction);
147  // </group>
148 
149 public slots:
150  // Sets all actions to enabled.
151  void setEnabled(bool enabled);
152 
153  // Sets all actions to disabled.
154  void setDisabled(bool disabled) { setEnabled(!disabled); }
155 
156 signals:
157  // See QActionGroup::hovered().
158  void hovered(QAction* action);
159 
160  // See QActionGroup::triggered().
161  void triggered(QAction* action);
162 
163  // This signals is emitted when all actions in the group have been
164  // unchecked.
165  void unchecked();
166 
167 private:
168  // Enabled flag.
170 
171  // Currently checked action, or NULL if none are checked.
173 
174  // Actions in group.
175  QList<QAction*> itsActions_;
176 
177 
178  // Enforces mutual exclusivity and emits signals as needed.
179  void action_(QAction* action, bool checked);
180 
181 private slots:
182  // Slot for QAction::hover().
183  void actionHovered();
184 
185  // Slot for QAction::triggered(), which calls actionTriggered_ as needed.
186  void actionTriggered(bool checked);
187 
188  // Slot for QAction::toggled(), which calls actionTriggered_ as needed.
189  void actionToggled(bool checked);
190 };
191 
192 }
193 
194 #endif /* QTACTIONGROUP_QO_H_ */
void setDisabled(bool disabled)
Sets all actions to disabled.
QMultiMap< QAction *, QAbstractButton * > itsSynchedButtons_
Synchronized actions maps.
bool isEnabled_
Enabled flag.
QAction * itsCheckedAction_
Currently checked action, or NULL if none are checked.
This class meant to be used almost identically to an exclusive QActionGroup.
QList< QAction * > itsActions_
Actions in group.
QMultiMap< QAction *, QAction * > itsSynchedActions_
This class is used to synchronize the checked state of actions with checkable buttons or other action...