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: QtxActionSet.cxx
23 // Author: Sergey TELKOV
25 #include "QtxActionSet.h"
27 #include <QApplication>
31 \brief An action class which is represented in the menu bar (or toolbar) as
32 a group of items (which can be customized).
34 Example: Window menu in the MDI application with menu items:
45 \param parent parent object
47 QtxActionSet::QtxActionSet( QObject* parent )
50 connect( this, SIGNAL( changed() ), this, SLOT( onChanged() ) );
58 QtxActionSet::~QtxActionSet()
63 \brief Get list of child actions.
64 \return list of assigned actions
66 QList<QAction*> QtxActionSet::actions() const
72 \brief Assign child actions.
73 \param lst list of actions
75 void QtxActionSet::setActions( const QList<QAction*>& lst )
77 for ( ActionList::iterator it = mySet.begin(); it != mySet.end(); ++it )
79 if ( !lst.contains( *it ) )
89 \brief Insert actions at the specified position.
90 \param lst list of actions
91 \param index position in the action list (if < 0, items are appended to the end of list)
93 void QtxActionSet::insertActions( const QList<QAction*>& lst, const int index )
95 int idx = qMin( index < 0 ? mySet.count() : index, mySet.count() );
97 for ( QList<QAction*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
100 int ident = generateId();
102 a->setParent( this );
103 mySet.insert( idx++, a );
106 connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onActionTriggered( bool ) ) );
113 \brief Insert action at the specified position.
115 If \a id < 0, it is generated automatically.
117 \param a action being inserted
119 \param index position in the action list (if < 0, item is appended to the end of list)
120 \return action identifier
122 int QtxActionSet::insertAction( QAction* a, const int id, const int index )
127 int ident = id < 0 ? generateId() : id;
128 int idx = qMin( index < 0 ? mySet.count() : index, mySet.count() );
130 a->setParent( this );
131 mySet.insert( idx, a );
134 connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onActionTriggered( bool ) ) );
144 \brief Insert action at the specified position.
146 If \a id < 0, it is generated automatically.
148 \param txt action text
150 \param index position in the action list (if < 0, item is appended to the end of list)
151 \return action identifier
153 int QtxActionSet::insertAction( const QString& txt, const int id, const int index )
155 return insertAction( new QtxAction( txt, txt, 0, this ), id, index );
159 \brief Insert action at the specified position.
161 If \a id < 0, it is generated automatically.
163 \param txt action text
164 \param icon action icon
166 \param index position in the action list (if < 0, item is appended to the end of list)
167 \return action identifier
169 int QtxActionSet::insertAction( const QString& txt, const QIcon& icon, const int id, const int index )
171 return insertAction( new QtxAction( txt, icon, txt, 0, this ), id, index );
175 \brief Remove specified action.
177 An action is removed from the action list and destroyed.
179 \param a action to be removed.
181 void QtxActionSet::removeAction( QAction* a )
183 if ( !mySet.contains( a ) )
186 mySet.removeAll( a );
192 \brief Remove specified action.
194 An action is removed from the action list and destroyed.
196 \param id action identifier
198 void QtxActionSet::removeAction( const int id )
200 removeAction( action( id ) );
204 \brief Remove all actions.
206 An actions list is cleared and all actions are destroyed.
208 void QtxActionSet::clear()
217 \brief Called when action is changed.
221 void QtxActionSet::onChanged()
223 QList<QWidget*> lst = createdWidgets();
224 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
225 (*it)->setEnabled( isEnabled() );
227 if ( !isVisible() || !isEmptyAction() )
230 bool block = signalsBlocked();
231 blockSignals( true );
233 blockSignals( block );
237 \brief Called when some action is activated by the user.
238 \param on toggled state (not used)
240 void QtxActionSet::onActionTriggered( bool /*on*/ )
242 QAction* a = ::qobject_cast<QAction*>( sender() );
246 int id = actionId( a );
248 emit triggered( id );
253 \brief Called when this action set is added to the menu bar (or toolbar).
254 \param w widget this action set is added to
256 void QtxActionSet::addedTo( QWidget* w )
258 QtxAction::addedTo( w );
264 \brief Called when this action set is removed from the menu bar (or toolbar).
265 \param w widget this action set is removed from
267 void QtxActionSet::removedFrom( QWidget* w )
269 QtxAction::removedFrom( w );
275 \brief Get action by specified identifier.
277 \return action or 0 if it is not found
279 QAction* QtxActionSet::action( int id ) const
282 for ( ActionList::const_iterator it = mySet.begin(); it != mySet.end() && !a; ++it )
284 if ( actionId( *it ) == id )
291 \brief Get action identifier for the action.
293 \return action ID or -1 if it is not found
295 int QtxActionSet::actionId( QAction* a ) const
298 if ( a && a->data().canConvert( QVariant::Int ) )
299 id = a->data().toInt();
304 \brief Set action identifier for the action.
306 \param id new action ID
308 void QtxActionSet::setActionId( QAction* a, const int id )
310 if ( !a || id == -1 )
317 \brief Notify that action was added
319 void QtxActionSet::actionAdded( QAction* )
324 \brief Notify that action was removed
326 void QtxActionSet::actionRemoved( QAction* )
331 \brief Getneration unique action identifier
332 \return generation action ID
334 int QtxActionSet::generateId() const
337 for ( ActionList::const_iterator it = mySet.begin(); it != mySet.end(); ++it )
338 map.insert( (*it)->data().toInt(), 0 );
341 while ( map.contains( id ) )
348 \brief Update action set.
350 void QtxActionSet::updateAction()
352 QList<QWidget*> lst = associatedWidgets();
353 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
358 \brief Update action set for the specified widget.
359 \param w a widget this action is added to
361 void QtxActionSet::updateAction( QWidget* w )
366 for ( ActionList::iterator it = mySet.begin(); it != mySet.end(); ++it )
367 w->removeAction( *it );
369 if ( !w->actions().contains( this ) )
373 for ( int i = 0; i < mySet.count(); i++ )
375 QAction* a = mySet.at( i );
378 w->insertAction( this, a );
382 QApplication::instance()->removeEventFilter( this );
384 w->insertAction( first, this );
386 QApplication::instance()->installEventFilter( this );
391 \brief Check if the action itself should be invisible
392 (only child action are shown)
393 \return \c true if the action itself should be visible
395 bool QtxActionSet::isEmptyAction() const
401 \fn void QtxActionSet::triggered( int id );
402 \brief Emitted when some child action is activated by the user.
407 \fn void QtxActionSet::triggered( QAction* a );
408 \brief Emitted when some child action is activated by the user.
409 \param a action being activated