1 // Copyright (C) 2007-2015 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: QtxActionSet.cxx
21 // Author: Sergey TELKOV
23 #include "QtxActionSet.h"
25 #include <QApplication>
29 \brief An action class which is represented in the menu bar (or toolbar) as
30 a group of items (which can be customized).
32 Example: Window menu in the MDI application with menu items:
43 \param parent parent object
45 QtxActionSet::QtxActionSet( QObject* parent )
48 connect( this, SIGNAL( changed() ), this, SLOT( onChanged() ) );
56 QtxActionSet::~QtxActionSet()
61 \brief Get list of child actions.
62 \return list of assigned actions
64 QList<QAction*> QtxActionSet::actions() const
70 \brief Assign child actions.
71 \param lst list of actions
73 void QtxActionSet::setActions( const QList<QAction*>& lst )
75 for ( ActionList::iterator it = mySet.begin(); it != mySet.end(); ++it )
77 if ( !lst.contains( *it ) )
87 \brief Insert actions at the specified position.
88 \param lst list of actions
89 \param index position in the action list (if < 0, items are appended to the end of list)
91 void QtxActionSet::insertActions( const QList<QAction*>& lst, const int index )
93 int idx = qMin( index < 0 ? mySet.count() : index, mySet.count() );
95 for ( QList<QAction*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
98 int ident = generateId();
100 a->setParent( this );
101 mySet.insert( idx++, a );
104 connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onActionTriggered( bool ) ) );
111 \brief Insert action at the specified position.
113 If \a id < 0, it is generated automatically.
115 \param a action being inserted
117 \param index position in the action list (if < 0, item is appended to the end of list)
118 \return action identifier
120 int QtxActionSet::insertAction( QAction* a, const int id, const int index )
125 int ident = id < 0 ? generateId() : id;
126 int idx = qMin( index < 0 ? mySet.count() : index, mySet.count() );
128 a->setParent( this );
129 mySet.insert( idx, a );
132 connect( a, SIGNAL( triggered( bool ) ), this, SLOT( onActionTriggered( bool ) ) );
142 \brief Insert action at the specified position.
144 If \a id < 0, it is generated automatically.
146 \param txt action text
148 \param index position in the action list (if < 0, item is appended to the end of list)
149 \return action identifier
151 int QtxActionSet::insertAction( const QString& txt, const int id, const int index )
153 return insertAction( new QtxAction( txt, txt, 0, this ), id, index );
157 \brief Insert action at the specified position.
159 If \a id < 0, it is generated automatically.
161 \param txt action text
162 \param icon action icon
164 \param index position in the action list (if < 0, item is appended to the end of list)
165 \return action identifier
167 int QtxActionSet::insertAction( const QString& txt, const QIcon& icon, const int id, const int index )
169 return insertAction( new QtxAction( txt, icon, txt, 0, this ), id, index );
173 \brief Remove specified action.
175 An action is removed from the action list and destroyed.
177 \param a action to be removed.
179 void QtxActionSet::removeAction( QAction* a )
181 if ( !mySet.contains( a ) )
184 mySet.removeAll( a );
190 \brief Remove specified action.
192 An action is removed from the action list and destroyed.
194 \param id action identifier
196 void QtxActionSet::removeAction( const int id )
198 removeAction( action( id ) );
202 \brief Remove all actions.
204 An actions list is cleared and all actions are destroyed.
206 void QtxActionSet::clear()
215 \brief Called when action is changed.
219 void QtxActionSet::onChanged()
221 QList<QWidget*> lst = createdWidgets();
222 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
223 (*it)->setEnabled( isEnabled() );
225 if ( !isVisible() || !isEmptyAction() )
228 bool block = signalsBlocked();
229 blockSignals( true );
231 blockSignals( block );
235 \brief Called when some action is activated by the user.
236 \param on toggled state (not used)
238 void QtxActionSet::onActionTriggered( bool /*on*/ )
240 QAction* a = ::qobject_cast<QAction*>( sender() );
244 int id = actionId( a );
246 emit triggered( id );
251 \brief Called when this action set is added to the menu bar (or toolbar).
252 \param w widget this action set is added to
254 void QtxActionSet::addedTo( QWidget* w )
256 QtxAction::addedTo( w );
262 \brief Called when this action set is removed from the menu bar (or toolbar).
263 \param w widget this action set is removed from
265 void QtxActionSet::removedFrom( QWidget* w )
267 QtxAction::removedFrom( w );
273 \brief Get action by specified identifier.
275 \return action or 0 if it is not found
277 QAction* QtxActionSet::action( int id ) const
280 for ( ActionList::const_iterator it = mySet.begin(); it != mySet.end() && !a; ++it )
282 if ( actionId( *it ) == id )
289 \brief Get action identifier for the action.
291 \return action ID or -1 if it is not found
293 int QtxActionSet::actionId( QAction* a ) const
296 if ( a && a->data().canConvert( QVariant::Int ) )
297 id = a->data().toInt();
302 \brief Set action identifier for the action.
304 \param id new action ID
306 void QtxActionSet::setActionId( QAction* a, const int id )
308 if ( !a || id == -1 )
315 \brief Notify that action was added
317 void QtxActionSet::actionAdded( QAction* )
322 \brief Notify that action was removed
324 void QtxActionSet::actionRemoved( QAction* )
329 \brief Getneration unique action identifier
330 \return generation action ID
332 int QtxActionSet::generateId() const
335 for ( ActionList::const_iterator it = mySet.begin(); it != mySet.end(); ++it )
336 map.insert( (*it)->data().toInt(), 0 );
339 while ( map.contains( id ) )
346 \brief Update action set.
348 void QtxActionSet::updateAction()
350 QList<QWidget*> lst = associatedWidgets();
351 for ( QList<QWidget*>::iterator it = lst.begin(); it != lst.end(); ++it )
356 \brief Update action set for the specified widget.
357 \param w a widget this action is added to
359 void QtxActionSet::updateAction( QWidget* w )
364 for ( ActionList::iterator it = mySet.begin(); it != mySet.end(); ++it )
365 w->removeAction( *it );
367 if ( !w->actions().contains( this ) )
371 for ( int i = 0; i < mySet.count(); i++ )
373 QAction* a = mySet.at( i );
376 w->insertAction( this, a );
380 QApplication::instance()->removeEventFilter( this );
382 w->insertAction( first, this );
384 QApplication::instance()->installEventFilter( this );
389 \brief Check if the action itself should be invisible
390 (only child action are shown)
391 \return \c true if the action itself should be visible
393 bool QtxActionSet::isEmptyAction() const
399 \fn void QtxActionSet::triggered( int id );
400 \brief Emitted when some child action is activated by the user.
405 \fn void QtxActionSet::triggered( QAction* a );
406 \brief Emitted when some child action is activated by the user.
407 \param a action being activated