From: vsr Date: Wed, 24 Sep 2008 13:18:06 +0000 (+0000) Subject: 0013946: Implement QActionGroup functionality for SALOME series 5x X-Git-Tag: V5_1_0a2~3 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=f777fdf6c9550e0d1ebb42a0c6d3c3f0051cb74a;p=modules%2Fgui.git 0013946: Implement QActionGroup functionality for SALOME series 5x --- diff --git a/src/Qtx/Makefile.am b/src/Qtx/Makefile.am index 84a178835..99d868f2a 100755 --- a/src/Qtx/Makefile.am +++ b/src/Qtx/Makefile.am @@ -29,6 +29,7 @@ include $(top_srcdir)/adm_local/unix/make_common_starter.am salomeinclude_HEADERS = \ Qtx.h \ QtxAction.h \ + QtxActionGroup.h \ QtxActionMenuMgr.h \ QtxActionMgr.h \ QtxActionSet.h \ @@ -85,6 +86,7 @@ lib_LTLIBRARIES = libqtx.la dist_libqtx_la_SOURCES = \ Qtx.cxx \ QtxAction.cxx \ + QtxActionGroup.cxx \ QtxActionMenuMgr.cxx \ QtxActionMgr.cxx \ QtxActionSet.cxx \ @@ -135,6 +137,7 @@ dist_libqtx_la_SOURCES = \ #VSR: already migrated to Qt4 files MOC_FILES = \ QtxAction_moc.cxx \ + QtxActionGroup_moc.cxx \ QtxActionMenuMgr_moc.cxx \ QtxActionMgr_moc.cxx \ QtxActionSet_moc.cxx \ diff --git a/src/Qtx/QtxActionGroup.cxx b/src/Qtx/QtxActionGroup.cxx index c3db645bf..88a80dbbe 100644 --- a/src/Qtx/QtxActionGroup.cxx +++ b/src/Qtx/QtxActionGroup.cxx @@ -23,24 +23,63 @@ #include "QtxComboBox.h" -#include +#include +#include +#include /*! \class QtxActionGroup - \brief An action class which is represented in the menu bar (or toolbar) as - a group of actions or combo box and support exclusive triggerd action behaviour. + \brief The QtxActionGroup class groups actions together. + + QtxActionGroup class operates with a list of actions in the similar way as it does QActionGroup class. + But in contrast to the Qt 4's class, QtxActrionGroup behaves rather like it was in Qt series 3x. + For example, it automatically shows exclusive combo box widget when action group is added to the toolbar + and if \a usesDropDown and \a exclusive flags are both set to \c true. + + The setExclusive() function is used to ensure that only one action is active at any moment: + it should be used with actions which have their \a checkable state set to \c true. + + Action group actions appear as individual menu options and toolbar buttons. For exclusive action + groups use setUsesDropDown() to display the actions in a subwidget of the toolbar or menu the action group + is added on. + + Actions can be added to the action group using add() function. Add the action group to the menu or + toolbar in the same way as for single action - using addAction() method of QMenu or QToolbar class. */ /*! - \brief Constructor. - \param parent parent object + \brief Constructor + + The created action group is exclusive by default. + + \param parent owner object + \sa setExclusive() */ QtxActionGroup::QtxActionGroup( QObject* parent ) : QtxActionSet( parent ), - myExclusive( false ), myDropDown( false ) { - connect( this, SIGNAL( triggered( QAction* ) ), this, SLOT( onTriggered( QAction* ) ) ); + setMenu( new QMenu( 0 ) ); + myActionGroup = new QActionGroup( this ); + + connect( myActionGroup, SIGNAL( triggered( QAction* ) ), this, SIGNAL( selected( QAction* ) ) ); +} + +/*! + \brief Constructor + \param parent owner object + \param exclusive if \c true only one action in the group will ever be active + \sa setExclusive() +*/ +QtxActionGroup::QtxActionGroup( QObject* parent, const bool exclusive ) +: QtxActionSet( parent ), + myDropDown( false ) +{ + setMenu( new QMenu( 0 ) ); + myActionGroup = new QActionGroup( this ); + myActionGroup->setExclusive( exclusive ); + + connect( myActionGroup, SIGNAL( triggered( QAction* ) ), this, SIGNAL( selected( QAction* ) ) ); } /*! @@ -51,33 +90,41 @@ QtxActionGroup::~QtxActionGroup() } /*! - \brief Returns true if the action group is exclusive. + \brief Check if the action group is exclusive + \return \c true if the action group is exclusive and \c false otherwise + \sa setExclusive(), setUsesDropDown() */ bool QtxActionGroup::isExclusive() const { - return myExclusive; + return myActionGroup->isExclusive(); } /*! - \brief Set the action group is exclusive. - \param on if true group should be exclusive otherwise not. + \brief Set/clear the action group exclusiveness + \param on if \c true the action group will be exclusive + \sa isExclusive(), setUsesDropDown() */ void QtxActionGroup::setExclusive( const bool on ) { - if ( myExclusive == on ) + if ( myActionGroup->isExclusive() == on ) return; bool e = isEmptyAction(); - myExclusive = on; + myActionGroup->setExclusive( on ); if ( e != isEmptyAction() ) updateType(); } /*! - \brief Returns . - \param on if true group should be exclusive otherwise not. + \brief Check if action group should appear in a subwidget of parent widget + + Note: for this option to take into effect, the \a exclusive flag should + be also set to \c true + + \return \c true if the action group is shown in subwidget + \sa setUsesDropDown(), setExclusive() */ bool QtxActionGroup::usesDropDown() const { @@ -85,7 +132,11 @@ bool QtxActionGroup::usesDropDown() const } /*! - \brief Sets whether the group's actions are displayed in a subwidget of the widgets the action group is added to. + \brief Defines a way how the group's actions should be displayed in parent widget + action group is added to - as a group of actions or in a subwidget (e.g. in the + combo box). + \param on if \c true, action group will be shown in the subwidget + \sa usesDropDown(), setExclusive() */ void QtxActionGroup::setUsesDropDown( const bool on ) { @@ -102,6 +153,7 @@ void QtxActionGroup::setUsesDropDown( const bool on ) /*! \brief Append the specified action into group. + \a action action to be added to the action group */ void QtxActionGroup::add( QAction* a ) { @@ -109,28 +161,9 @@ void QtxActionGroup::add( QAction* a ) } /*! - \brief Called when some action is activated by the user. - \param a toggled action + \brief Called when some subwidget item is activated by the user. + \param id item identifier */ -void QtxActionGroup::onTriggered( QAction* a ) -{ - if ( !isExclusive() ) - return; - - if ( !a->isCheckable() || !a->isChecked() ) - return; - - QList aList = actions(); - for ( QList::const_iterator it = aList.begin(); it != aList.end(); ++it ) - { - QAction* cur = *it; - if ( cur->isCheckable() ) - cur->setChecked( cur == a ); - } - - emit selected( a ); -} - void QtxActionGroup::onActivated( int id ) { const QObject* s = sender(); @@ -143,6 +176,7 @@ void QtxActionGroup::onActivated( int id ) return; a->setChecked( true ); + a->trigger(); QList lst = createdWidgets(); for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) @@ -154,24 +188,41 @@ void QtxActionGroup::onActivated( int id ) } /*! - \brief Update action set for the specified widget. - \param w a widget this action is added to + \brief Update action group for the specified widget. + \param w a widget this action group is added to */ void QtxActionGroup::updateAction( QWidget* w ) { - QtxComboBox* cb = createdWidget( w ); - if ( !cb ) - QtxActionSet::updateAction( w ); + if ( !::qobject_cast( w ) && !::qobject_cast( w ) ) { + QtxComboBox* cb = createdWidget( w ); + if ( !cb ) + QtxActionSet::updateAction( w ); + else + { + updateAction( cb ); + + QList lst = actions(); + for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) + w->removeAction( *it ); + } + } else { - updateAction( cb ); - - QList lst = actions(); - for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) - w->removeAction( *it ); + if ( !usesDropDown() ) { + QtxActionSet::updateAction( w ); + } + else { + QList lst = actions(); + for ( QList::iterator it = lst.begin(); it != lst.end(); ++it ) + w->removeAction( *it ); + } } } +/*! + \brief Update action group for the specified combo box. + \param cb a combo box this action group is added to +*/ void QtxActionGroup::updateAction( QtxComboBox* cb ) { if ( !cb ) @@ -197,8 +248,17 @@ void QtxActionGroup::updateAction( QtxComboBox* cb ) cb->setCleared( true ); } +/*! + \brief Create widget representing action group in the widget + this action group is added to. + \param p widget this action group is being added to + \return new widget representing this action group +*/ QWidget* QtxActionGroup::createWidget( QWidget* p ) { + if ( ::qobject_cast( p ) || ::qobject_cast( p ) ) + return 0; + QtxComboBox* cb = !isEmptyAction() ? new QtxComboBox( p ) : 0; if ( cb ) connect( cb, SIGNAL( activatedId( int ) ), this, SLOT( onActivated( int ) ) ); @@ -215,7 +275,31 @@ bool QtxActionGroup::isEmptyAction() const return !isExclusive() || !usesDropDown(); } +/*! + \brief Called when action is added to the action group + \param a action being added to the action group +*/ +void QtxActionGroup::actionAdded( QAction* a ) +{ + myActionGroup->addAction( a ); + if ( menu() ) + menu()->addAction( a ); +} + +/*! + \brief Called when action is removed from the action group + \param a action being removed from the action group +*/ +void QtxActionGroup::actionRemoved( QAction* a ) +{ + myActionGroup->removeAction( a ); + if ( menu() ) + menu()->removeAction( a ); +} +/*! + \brief Internal update +*/ void QtxActionGroup::updateType() { QList lst = associatedWidgets(); @@ -230,8 +314,14 @@ void QtxActionGroup::updateType() lst = w->actions(); w->insertAction( i < lst.count() ? lst.at( i ) : 0, this ); } + setVisible( !isEmptyAction() ); } +/*! + \brief Get combo box created by this action group for the specified widget. + \param p widget this action group is added to + \return combo box if it was created for the specified widget or 0 otherwise +*/ QtxComboBox* QtxActionGroup::createdWidget( QWidget* p ) { QtxComboBox* cb = 0; @@ -244,12 +334,8 @@ QtxComboBox* QtxActionGroup::createdWidget( QWidget* p ) return cb; } - - - - /*! \fn void QtxActionGroup::selected( QAction* a ); - \brief Emitted when some child action is checked by the user. - \param a action being checked + \brief Emitted when some child action is toggled by the user. + \param a action being toggled */ diff --git a/src/Qtx/QtxActionGroup.h b/src/Qtx/QtxActionGroup.h index aae9b119d..bb39763e4 100644 --- a/src/Qtx/QtxActionGroup.h +++ b/src/Qtx/QtxActionGroup.h @@ -29,6 +29,7 @@ #endif class QtxComboBox; +class QActionGroup; class QTX_EXPORT QtxActionGroup : public QtxActionSet { @@ -40,19 +41,19 @@ public: virtual ~QtxActionGroup(); bool isExclusive() const; - void setExclusive( const bool ); - bool usesDropDown() const; - void setUsesDropDown( const bool ); void add( QAction* ); +public slots: + void setExclusive( const bool ); + void setUsesDropDown( const bool ); + signals: void selected( QAction* ); private slots: void onActivated( int ); - void onTriggered( QAction* ); protected: virtual void updateAction( QWidget* ); @@ -61,6 +62,8 @@ protected: virtual QWidget* createWidget( QWidget* ); virtual bool isEmptyAction() const; + virtual void actionAdded( QAction* ); + virtual void actionRemoved( QAction* ); private: void updateType(); @@ -68,7 +71,7 @@ private: private: bool myDropDown; - bool myExclusive; + QActionGroup* myActionGroup; }; #ifdef WIN32