1 // Copyright (C) 2007-2008 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
22 // File: QtxActionGroup.cxx
23 // Author: Sergey TELKOV
25 #include "QtxActionGroup.h"
27 #include "QtxComboBox.h"
31 #include <QActionGroup>
35 \brief The QtxActionGroup class groups actions together.
37 QtxActionGroup class operates with a list of actions in the similar way as it does QActionGroup class.
38 But in contrast to the Qt 4's class, QtxActrionGroup behaves rather like it was in Qt series 3x.
39 For example, it automatically shows exclusive combo box widget when action group is added to the toolbar
40 and if \a usesDropDown and \a exclusive flags are both set to \c true.
42 The setExclusive() function is used to ensure that only one action is active at any moment:
43 it should be used with actions which have their \a checkable state set to \c true.
45 Action group actions appear as individual menu options and toolbar buttons. For exclusive action
46 groups use setUsesDropDown() to display the actions in a subwidget of the toolbar or menu the action group
49 Actions can be added to the action group using add() function. Add the action group to the menu or
50 toolbar in the same way as for single action - using addAction() method of QMenu or QToolbar class.
56 The created action group is exclusive by default.
58 \param parent owner object
61 QtxActionGroup::QtxActionGroup( QObject* parent )
62 : QtxActionSet( parent ),
65 setMenu( new QMenu( 0 ) );
66 myActionGroup = new QActionGroup( this );
68 connect( myActionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( onTriggered( QAction* ) ) );
73 \param parent owner object
74 \param exclusive if \c true only one action in the group will ever be active
77 QtxActionGroup::QtxActionGroup( QObject* parent, const bool exclusive )
78 : QtxActionSet( parent ),
81 setMenu( new QMenu( 0 ) );
82 myActionGroup = new QActionGroup( this );
83 myActionGroup->setExclusive( exclusive );
85 connect( myActionGroup, SIGNAL( triggered( QAction* ) ), this, SIGNAL( selected( QAction* ) ) );
91 QtxActionGroup::~QtxActionGroup()
96 \brief Check if the action group is exclusive
97 \return \c true if the action group is exclusive and \c false otherwise
98 \sa setExclusive(), setUsesDropDown()
100 bool QtxActionGroup::isExclusive() const
102 return myActionGroup->isExclusive();
106 \brief Set/clear the action group exclusiveness
107 \param on if \c true the action group will be exclusive
108 \sa isExclusive(), setUsesDropDown()
110 void QtxActionGroup::setExclusive( const bool on )
112 if ( myActionGroup->isExclusive() == on )
115 bool e = isEmptyAction();
117 myActionGroup->setExclusive( on );
119 if ( e != isEmptyAction() )
124 \brief Check if action group should appear in a subwidget of parent widget
126 Note: for this option to take into effect, the \a exclusive flag should
127 be also set to \c true
129 \return \c true if the action group is shown in subwidget
130 \sa setUsesDropDown(), setExclusive()
132 bool QtxActionGroup::usesDropDown() const
138 \brief Defines a way how the group's actions should be displayed in parent widget
139 action group is added to - as a group of actions or in a subwidget (e.g. in the
141 \param on if \c true, action group will be shown in the subwidget
142 \sa usesDropDown(), setExclusive()
144 void QtxActionGroup::setUsesDropDown( const bool on )
146 if ( myDropDown == on )
149 bool e = isEmptyAction();
153 if ( e != isEmptyAction() )
158 \brief Append the specified action into group.
159 \a action action to be added to the action group
161 void QtxActionGroup::add( QAction* a )
167 \brief Called when some subwidget item is activated by the user.
168 \param id item identifier
170 void QtxActionGroup::onActivated( int id )
172 const QObject* s = sender();
174 QAction* a = action( id );
178 if ( a->isChecked() )
181 a->setChecked( true );
184 QList<QWidget*> lst = createdWidgets();
185 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
187 QtxComboBox* cb = ::qobject_cast<QtxComboBox*>( *it );
189 cb->setCurrentId( id );
194 \brief Called when some action owned by this action group is activated by the user
195 \param a action being activated
197 void QtxActionGroup::onTriggered( QAction* a )
199 int id = actionId( a );
201 QList<QWidget*> lst = createdWidgets();
202 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
204 QtxComboBox* cb = ::qobject_cast<QtxComboBox*>( *it );
206 cb->setCurrentId( id );
214 \brief Update action group for the specified widget.
215 \param w a widget this action group is added to
217 void QtxActionGroup::updateAction( QWidget* w )
219 if ( !::qobject_cast<QMenu*>( w ) && !::qobject_cast<QMenuBar*>( w ) ) {
220 QtxComboBox* cb = createdWidget( w );
222 QtxActionSet::updateAction( w );
227 QList<QAction*> lst = actions();
228 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
229 w->removeAction( *it );
234 if ( !usesDropDown() ) {
235 QtxActionSet::updateAction( w );
238 QList<QAction*> lst = actions();
239 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
240 w->removeAction( *it );
246 \brief Update action group for the specified combo box.
247 \param cb a combo box this action group is added to
249 void QtxActionGroup::updateAction( QtxComboBox* cb )
255 cb->setCleared( false );
258 QList<QAction*> lst = actions();
259 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
262 cb->addItem( a->icon(), a->text() );
263 cb->setId( cb->count() - 1, actionId( a ) );
264 if ( a->isChecked() )
269 cb->setCurrentId( actionId( cur ) );
271 cb->setCleared( true );
275 \brief Create widget representing action group in the widget
276 this action group is added to.
277 \param p widget this action group is being added to
278 \return new widget representing this action group
280 QWidget* QtxActionGroup::createWidget( QWidget* p )
282 if ( ::qobject_cast<QMenu*>( p ) || ::qobject_cast<QMenuBar*>( p ) )
285 QtxComboBox* cb = !isEmptyAction() ? new QtxComboBox( p ) : 0;
287 connect( cb, SIGNAL( activatedId( int ) ), this, SLOT( onActivated( int ) ) );
292 \brief Check if the action itself should be invisible
293 (only child action are shown)
294 \return \c true if the action itself should be visible
296 bool QtxActionGroup::isEmptyAction() const
298 return !isExclusive() || !usesDropDown();
302 \brief Called when action is added to the action group
303 \param a action being added to the action group
305 void QtxActionGroup::actionAdded( QAction* a )
307 myActionGroup->addAction( a );
309 menu()->addAction( a );
313 \brief Called when action is removed from the action group
314 \param a action being removed from the action group
316 void QtxActionGroup::actionRemoved( QAction* a )
318 myActionGroup->removeAction( a );
320 menu()->removeAction( a );
324 \brief Internal update
326 void QtxActionGroup::updateType()
328 QList<QWidget*> lst = associatedWidgets();
329 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
332 QList<QAction*> lst = w->actions();
334 int i = lst.indexOf( this );
335 w->removeAction( this );
338 w->insertAction( i < lst.count() ? lst.at( i ) : 0, this );
340 setVisible( !isEmptyAction() );
344 \brief Get combo box created by this action group for the specified widget.
345 \param p widget this action group is added to
346 \return combo box if it was created for the specified widget or 0 otherwise
348 QtxComboBox* QtxActionGroup::createdWidget( QWidget* p )
351 QList<QWidget*> lst = createdWidgets();
352 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end() && !cb; ++it )
354 if ( (*it)->parent() == p )
355 cb = ::qobject_cast<QtxComboBox*>( *it );
361 \fn void QtxActionGroup::selected( QAction* a );
362 \brief Emitted when some child action is toggled by the user.
363 \param a action being toggled