casa  $Rev:20696$
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
QtActionGroup.qo.h
Go to the documentation of this file.
00001 //# QtActionGroup.qo.h: Like QActionGroup but with additional functionality.
00002 //# Copyright (C) 2009
00003 //# Associated Universities, Inc. Washington DC, USA.
00004 //#
00005 //# This library is free software; you can redistribute it and/or modify it
00006 //# under the terms of the GNU Library General Public License as published by
00007 //# the Free Software Foundation; either version 2 of the License, or (at your
00008 //# option) any later version.
00009 //#
00010 //# This library is distributed in the hope that it will be useful, but WITHOUT
00011 //# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 //# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
00013 //# License for more details.
00014 //#
00015 //# You should have received a copy of the GNU Library General Public License
00016 //# along with this library; if not, write to the Free Software Foundation,
00017 //# Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
00018 //#
00019 //# Correspondence concerning AIPS++ should be addressed as follows:
00020 //#        Internet email: aips2-request@nrao.edu.
00021 //#        Postal address: AIPS++ Project Office
00022 //#                        National Radio Astronomy Observatory
00023 //#                        520 Edgemont Road
00024 //#                        Charlottesville, VA 22903-2475 USA
00025 //#
00026 //# $Id: $
00027 #ifndef QTACTIONGROUP_QO_H_
00028 #define QTACTIONGROUP_QO_H_
00029 
00030 #include <QAbstractButton>
00031 #include <QAction>
00032 #include <QList>
00033 #include <QMultiMap>
00034 
00035 using namespace std;
00036 
00037 namespace casa {
00038 
00039 // This class is used to synchronize the checked state of actions with
00040 // checkable buttons or other actions.  This makes it so that clicking the
00041 // synchronized button/action has the same effect as clicking the action
00042 // itself.
00043 class QtActionSynchronizer : public QObject {
00044     Q_OBJECT
00045     
00046 public:
00047     // Constructor which takes an optional parent.
00048     QtActionSynchronizer(QObject* parent = NULL);
00049     
00050     // Destructor.
00051     virtual ~QtActionSynchronizer();
00052     
00053     // Synchronizes the given action in this group with the given button or
00054     // other action.
00055     // <group>
00056     virtual void synchronize(QAction* action, QAbstractButton* button);
00057     virtual void synchronize(QAction* action, QAction* otherAction);
00058     // </group>
00059     
00060     // Unsynchronizes all buttons/actions associated with the given in this
00061     // group.
00062     // <group>
00063     virtual void unsynchronize(QAction* action);
00064     virtual void unsynchronize(QAbstractButton* button);
00065     // </group>
00066     
00067     // Unsyncrhonizes the given action in this group with the given
00068     // button/action.
00069     // <group>
00070     virtual void unsynchronize(QAction* action, QAbstractButton* button);
00071     virtual void unsynchronize(QAction* action, QAction* otherAction);
00072     // </group>
00073     
00074 protected:
00075     // Synchronized actions maps.
00076     // <group>
00077     QMultiMap<QAction*, QAbstractButton*> itsSynchedButtons_;
00078     QMultiMap<QAction*, QAction*> itsSynchedActions_;
00079     // </group>
00080     
00081     
00082     // Method which updates the actions/buttons associated with the given
00083     // action.
00084     virtual void actionTriggered_(QAction* action, bool checked);
00085     
00086     // Method which updates the action(s) associated with the given action or
00087     // button to the given checked state.
00088     virtual void synchronizedTriggered_(QAction* action,
00089             QAbstractButton* button, bool checked);
00090     
00091 protected slots:
00092     // Slot for when an action is toggled, which calls actionTriggered_() as
00093     // needed.
00094     virtual void actionToggled(bool checked);
00095     
00096     // Slot for when an action is triggered, which calls actionTriggered_() as
00097     // needed.
00098     virtual void actionTriggered(bool checked);
00099 
00100     // Slot for when a synchronizing button/action is toggled, which calls
00101     // synchronizedTriggered_() as needed.
00102     virtual void synchronizedToggled(bool checked);
00103     
00104     // Slot for when a synchronizing button/action is clicked/triggered, which
00105     // calls synchronizedTriggered_() as needed.
00106     virtual void synchronizedTriggered(bool checked);
00107 };
00108 
00109 
00110 // This class meant to be used almost identically to an exclusive QActionGroup.
00111 // The difference is that this class allows all QActions to be unchecked; in
00112 // other words, either one action is checked or none are.  Some methods from
00113 // QActionGroup have not been included, but they would be easy to include if
00114 // desired.  This class also has the added functionality included in
00115 // QtActionSynchronnizer.
00116 class QtActionGroup : public QtActionSynchronizer {
00117     Q_OBJECT
00118     
00119 public:
00120     // Constructor that optionally takes a parent object.
00121     QtActionGroup(QObject* parent = NULL);
00122     
00123     // Destructor.
00124     ~QtActionGroup();
00125 
00126     // See QActionGroup::actions().
00127     QList<QAction*> actions() const;
00128     
00129     // See QActionGroup::checkedAction().  Will return NULL if no action is
00130     // currently checked.
00131     QAction* checkedAction() const;
00132 
00133     // See QActionGroup::addAction().
00134     QAction* addAction(QAction* action);
00135     
00136     // See QActionGroup::removeAction().
00137     void removeAction(QAction* action);
00138     
00139     // See QActionGroup::isEnabled().
00140     bool isEnabled() const;
00141     
00142     // Overrides QtActionSynchronizer::synchronize to only allow actions that
00143     // are in the group.
00144     // <group>
00145     void synchronize(QAction* action, QAbstractButton* button);
00146     void synchronize(QAction* action, QAction* otherAction);
00147     // </group>
00148     
00149 public slots:    
00150     // Sets all actions to enabled.
00151     void setEnabled(bool enabled);
00152     
00153     // Sets all actions to disabled.
00154     void setDisabled(bool disabled) { setEnabled(!disabled); }
00155     
00156 signals:
00157     // See QActionGroup::hovered().
00158     void hovered(QAction* action);
00159     
00160     // See QActionGroup::triggered().
00161     void triggered(QAction* action);
00162     
00163     // This signals is emitted when all actions in the group have been
00164     // unchecked.
00165     void unchecked();
00166     
00167 private:
00168     // Enabled flag.
00169     bool isEnabled_;
00170     
00171     // Currently checked action, or NULL if none are checked.
00172     QAction* itsCheckedAction_;
00173     
00174     // Actions in group.
00175     QList<QAction*> itsActions_;
00176     
00177     
00178     // Enforces mutual exclusivity and emits signals as needed.
00179     void action_(QAction* action, bool checked);
00180     
00181 private slots:
00182     // Slot for QAction::hover().
00183     void actionHovered();
00184     
00185     // Slot for QAction::triggered(), which calls actionTriggered_ as needed.
00186     void actionTriggered(bool checked);
00187     
00188     // Slot for QAction::toggled(), which calls actionTriggered_ as needed.
00189     void actionToggled(bool checked);
00190 };
00191 
00192 }
00193 
00194 #endif /* QTACTIONGROUP_QO_H_ */