1 // Copyright (C) 2007-2016 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File: QtxActionGroup.cxx
21 // Author: Sergey TELKOV
23 #include "QtxActionGroup.h"
25 #include "QtxComboBox.h"
29 #include <QActionGroup>
33 \brief The QtxActionGroup class groups actions together.
35 QtxActionGroup class operates with a list of actions in the similar way as it does QActionGroup class.
36 But in contrast to the Qt 4's class, QtxActrionGroup behaves rather like it was in Qt series 3x.
37 For example, it automatically shows exclusive combo box widget when action group is added to the toolbar
38 and if \a usesDropDown and \a exclusive flags are both set to \c true.
40 The setExclusive() function is used to ensure that only one action is active at any moment:
41 it should be used with actions which have their \a checkable state set to \c true.
43 Action group actions appear as individual menu options and toolbar buttons. For exclusive action
44 groups use setUsesDropDown() to display the actions in a subwidget of the toolbar or menu the action group
47 Actions can be added to the action group using add() function. Add the action group to the menu or
48 toolbar in the same way as for single action - using addAction() method of QMenu or QToolbar class.
54 The created action group is exclusive by default.
56 \param parent owner object
59 QtxActionGroup::QtxActionGroup( QObject* parent )
60 : QtxActionSet( parent ),
63 setMenu( new QMenu( 0 ) );
64 myActionGroup = new QActionGroup( this );
66 connect( myActionGroup, SIGNAL( triggered( QAction* ) ), this, SLOT( onTriggered( QAction* ) ) );
71 \param parent owner object
72 \param exclusive if \c true only one action in the group will ever be active
75 QtxActionGroup::QtxActionGroup( QObject* parent, const bool exclusive )
76 : QtxActionSet( parent ),
79 setMenu( new QMenu( 0 ) );
80 myActionGroup = new QActionGroup( this );
81 myActionGroup->setExclusive( exclusive );
83 connect( myActionGroup, SIGNAL( triggered( QAction* ) ), this, SIGNAL( selected( QAction* ) ) );
89 QtxActionGroup::~QtxActionGroup()
94 \brief Check if the action group is exclusive
95 \return \c true if the action group is exclusive and \c false otherwise
96 \sa setExclusive(), setUsesDropDown()
98 bool QtxActionGroup::isExclusive() const
100 return myActionGroup->isExclusive();
104 \brief Set/clear the action group exclusiveness
105 \param on if \c true the action group will be exclusive
106 \sa isExclusive(), setUsesDropDown()
108 void QtxActionGroup::setExclusive( const bool on )
110 if ( myActionGroup->isExclusive() == on )
113 bool e = isEmptyAction();
115 myActionGroup->setExclusive( on );
117 if ( e != isEmptyAction() )
122 \brief Check if action group should appear in a subwidget of parent widget
124 Note: for this option to take into effect, the \a exclusive flag should
125 be also set to \c true
127 \return \c true if the action group is shown in subwidget
128 \sa setUsesDropDown(), setExclusive()
130 bool QtxActionGroup::usesDropDown() const
136 \brief Defines a way how the group's actions should be displayed in parent widget
137 action group is added to - as a group of actions or in a subwidget (e.g. in the
139 \param on if \c true, action group will be shown in the subwidget
140 \sa usesDropDown(), setExclusive()
142 void QtxActionGroup::setUsesDropDown( const bool on )
144 if ( myDropDown == on )
147 bool e = isEmptyAction();
151 if ( e != isEmptyAction() )
156 \brief Append the specified action into group.
157 \a action action to be added to the action group
159 void QtxActionGroup::add( QAction* a )
165 \brief Called when some subwidget item is activated by the user.
166 \param id item identifier
168 void QtxActionGroup::onActivated( int id )
170 const QObject* s = sender();
172 QAction* a = action( id );
176 if ( a->isChecked() )
179 a->setChecked( true );
182 QList<QWidget*> lst = createdWidgets();
183 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
185 QtxComboBox* cb = ::qobject_cast<QtxComboBox*>( *it );
187 cb->setCurrentId( id );
192 \brief Called when some action owned by this action group is activated by the user
193 \param a action being activated
195 void QtxActionGroup::onTriggered( QAction* a )
197 int id = actionId( a );
199 QList<QWidget*> lst = createdWidgets();
200 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
202 QtxComboBox* cb = ::qobject_cast<QtxComboBox*>( *it );
204 cb->setCurrentId( id );
212 \brief Enable/disable action group
214 void QtxActionGroup::setEnabled( bool on )
216 QtxActionSet::setEnabled( on );
217 myActionGroup->setEnabled( on );
221 \brief Update action group for the specified widget.
222 \param w a widget this action group is added to
224 void QtxActionGroup::updateAction( QWidget* w )
226 if ( !::qobject_cast<QMenu*>( w ) && !::qobject_cast<QMenuBar*>( w ) ) {
227 QtxComboBox* cb = createdWidget( w );
229 QtxActionSet::updateAction( w );
234 QList<QAction*> lst = actions();
235 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
236 w->removeAction( *it );
241 if ( !usesDropDown() ) {
242 QtxActionSet::updateAction( w );
245 QList<QAction*> lst = actions();
246 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
247 w->removeAction( *it );
253 \brief Update action group for the specified combo box.
254 \param cb a combo box this action group is added to
256 void QtxActionGroup::updateAction( QtxComboBox* cb )
262 cb->setCleared( false );
265 QList<QAction*> lst = actions();
266 for ( QList<QAction*>::iterator it = lst.begin(); it != lst.end(); ++it )
269 cb->addItem( a->icon(), a->text() );
270 cb->setId( cb->count() - 1, actionId( a ) );
271 if ( a->isChecked() )
276 cb->setCurrentId( actionId( cur ) );
278 cb->setCleared( true );
282 \brief Create widget representing action group in the widget
283 this action group is added to.
284 \param p widget this action group is being added to
285 \return new widget representing this action group
287 QWidget* QtxActionGroup::createWidget( QWidget* p )
289 if ( ::qobject_cast<QMenu*>( p ) || ::qobject_cast<QMenuBar*>( p ) )
292 QtxComboBox* cb = !isEmptyAction() ? new QtxComboBox( p ) : 0;
294 connect( cb, SIGNAL( activatedId( int ) ), this, SLOT( onActivated( int ) ) );
299 \brief Check if the action itself should be invisible
300 (only child action are shown)
301 \return \c true if the action itself should be visible
303 bool QtxActionGroup::isEmptyAction() const
305 return !isExclusive() || !usesDropDown();
309 \brief Called when action is added to the action group
310 \param a action being added to the action group
312 void QtxActionGroup::actionAdded( QAction* a )
314 myActionGroup->addAction( a );
316 menu()->addAction( a );
320 \brief Called when action is removed from the action group
321 \param a action being removed from the action group
323 void QtxActionGroup::actionRemoved( QAction* a )
325 myActionGroup->removeAction( a );
327 menu()->removeAction( a );
331 \brief Internal update
333 void QtxActionGroup::updateType()
335 QList<QWidget*> lst = associatedWidgets();
336 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
339 QList<QAction*> lst = w->actions();
341 int i = lst.indexOf( this );
342 w->removeAction( this );
345 w->insertAction( i < lst.count() ? lst.at( i ) : 0, this );
347 setVisible( !isEmptyAction() );
351 \brief Get combo box created by this action group for the specified widget.
352 \param p widget this action group is added to
353 \return combo box if it was created for the specified widget or 0 otherwise
355 QtxComboBox* QtxActionGroup::createdWidget( QWidget* p )
358 QList<QWidget*> lst = createdWidgets();
359 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end() && !cb; ++it )
361 if ( (*it)->parent() == p )
362 cb = ::qobject_cast<QtxComboBox*>( *it );
368 \fn void QtxActionGroup::selected( QAction* a );
369 \brief Emitted when some child action is toggled by the user.
370 \param a action being toggled