1 // Copyright (C) 2007-2012 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.
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 : LightApp_ModuleAction.cxx
21 // Author : Sergey TELKOV, Vadim SANDLER
23 #include "LightApp_ModuleAction.h"
25 #include <QtxComboBox.h>
26 #include <QtxActionSet.h>
27 #include <QVBoxLayout>
28 #include <QApplication>
32 \class LightApp_ModuleAction::ActionSet
33 \brief Internal class to represent list of modules buttons.
37 class LightApp_ModuleAction::ActionSet : public QtxActionSet
40 ActionSet( QObject* );
41 QAction* moduleAction( const QString& ) const;
42 int moduleId( const QString& ) const;
43 int moduleId( QAction* ) const;
44 void setVisible( bool );
50 \param parent parent object
52 LightApp_ModuleAction::ActionSet::ActionSet( QObject* parent )
53 : QtxActionSet( parent )
58 \brief Get action corresponding to the specified module.
60 \param name module name
61 \return module action or 0 if \a name is invalid
63 QAction* LightApp_ModuleAction::ActionSet::moduleAction( const QString& name ) const
67 QList<QAction*> alist = actions();
68 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end() && !a; ++it )
70 if ( (*it)->text() == name )
78 \brief Get module action identifier.
80 \param name module name
81 \return module action ID or -1 if \a name is invalid
83 int LightApp_ModuleAction::ActionSet::moduleId( const QString& name ) const
87 QList<QAction*> alist = actions();
88 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end() && id == -1; ++it )
90 if ( (*it)->text() == name )
98 \brief Get module action identifier.
100 \param a module action
101 \return module action ID or -1 if \a a is null or invalid
103 int LightApp_ModuleAction::ActionSet::moduleId( QAction* a ) const
105 return actionId( a );
109 \brief Show/hide modules actions.
111 \param on new visibility state
113 void LightApp_ModuleAction::ActionSet::setVisible( bool on )
115 QList<QAction*> alist = actions();
116 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
117 (*it)->setVisible( on );
119 QtxActionSet::setVisible( on );
123 \class LightApp_ModuleAction::ComboAction
124 \brief Internal class to represent combo box with the list of modules in the toolbar.
131 \param parent parent object
133 LightApp_ModuleAction::ComboAction::ComboAction( QObject* parent )
134 : QtxAction( parent )
139 \brief Get list of associated widgets.
141 \return list of created widgets (QtxComboBox)
143 QList<QtxComboBox*> LightApp_ModuleAction::ComboAction::widgets() const
145 QList<QtxComboBox*> lst;
147 QList<QWidget*> wlist = createdWidgets();
148 for ( QList<QWidget*>::const_iterator wit = wlist.begin(); wit != wlist.end(); ++wit )
149 lst += qFindChildren<QtxComboBox *>(*wit);
155 \brief Create combo box widget by request from the toolbar.
157 \param parent parent widget (should be QToolBar or its successor)
158 \return new custom widget, containing combo box
160 QWidget* LightApp_ModuleAction::ComboAction::createWidget( QWidget* parent )
162 if ( !parent->inherits( "QToolBar" ) )
165 QWidget* dumb = new QWidget( parent );
166 QVBoxLayout* l = new QVBoxLayout( dumb );
167 l->setSpacing( 0 ); l->setMargin( 0 );
168 QtxComboBox* cb = new QtxComboBox( dumb );
169 cb->setSizeAdjustPolicy( QComboBox::AdjustToContents );
170 cb->setFocusPolicy( Qt::NoFocus );
174 connect( cb, SIGNAL( activatedId( int ) ), this, SIGNAL( activatedId( int ) ) );
180 \fn void LightApp_ModuleAction::ComboAction::activatedId( int id );
182 \brief Emitted when the combo box item is activated
183 \param item identifier
187 \class LightApp_ModuleAction::ActivateEvent
188 \brief Internal class to represent custom event for transfer the activation item id.
191 class LightApp_ModuleAction::ActivateEvent : public QEvent
194 ActivateEvent( QEvent::Type type, int id ) : QEvent( type ), myId( id ) {};
197 int id() const { return myId; }
204 \class LightApp_ModuleAction
205 \brief An action, representing the list of modules to be inserted to the
208 This action is represented in the toolbar as combo box and a set of buttons
209 for each module. In addition to the modules items, the combo box contains
210 an item corresponding to the "neutral point" of the application
211 (when there is no active module).
213 The action can be constructed with up to two parameters, defining the text
214 and icon to be displayed for the "neutral point".
216 Only one module can be active at the moment. It can be set programmatically
217 with setActiveModule() function. Use this method with empty string to turn
218 to the "neutral point". To get active module, use activeModule() function.
220 When user activates/deactivates any module, the signal moduleActivated()
223 The action can be represented in the toolbar in different modes:
224 * as combo box only (Qtx::ComboItem)
225 * as set of modules buttons only (Qtx::Buttons)
226 * as combo box followed by the set of modules buttons (Qtx::All)
227 * as none (Qtx::None)
228 By default, both combo box and buttons set are shown. Use method
229 setMode() to change this behavior.
231 An action can be also added to the popup menu, but combo box is never shown
232 in this case, only modules buttons.
238 Creates an module action with "neutral point" item described by \a text.
240 \param text "neutral point" item's text
241 \param parent parent object
243 LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, QObject* parent )
244 : QtxAction( parent )
253 Creates an module action with "neutral point" item described by \a text and \a ico.
255 \param text "neutral point" item's text
256 \param ico "neutral point" item's icon
257 \param parent parent object
259 LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, const QIcon& ico, QObject* parent )
260 : QtxAction( parent )
270 LightApp_ModuleAction::~LightApp_ModuleAction()
275 \brief Get list of modules.
276 \return modules names list
278 QStringList LightApp_ModuleAction::modules() const
282 QList<QAction*> alist = mySet->actions();
283 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
284 lst.append( (*it)->text() );
290 \brief Get module icon.
291 \param name module name
295 QIcon LightApp_ModuleAction::moduleIcon( const QString& name ) const
297 QAction* a = mySet->moduleAction( name );
298 return a ? a->icon() : QIcon();
302 \brief Set module icon.
303 \param name module name
304 \param ico new module icon
307 void LightApp_ModuleAction::setModuleIcon( const QString& name, const QIcon& ico )
309 QAction* a = mySet->moduleAction( name );
318 \brief Add module into the list.
319 \param name module name
320 \param ico module icon
321 \param idx position in the module list (if -1, the module is added to the end of list)
324 void LightApp_ModuleAction::insertModule( const QString& name, const QIcon& ico,
327 QtxAction* a = new QtxAction( name, ico, name, 0, this, true );
328 a->setStatusTip( tr( "ACTIVATE_MODULE_TOP" ).arg( name ) );
330 mySet->insertAction( a, -1, idx );
335 \brief Remove module from the list.
336 \param name module name
339 void LightApp_ModuleAction::removeModule( const QString& name )
341 int id = mySet->moduleId( name );
345 mySet->removeAction( id );
350 \brief Get active module.
352 If there is no active module ("neutral point"), then the null string
355 \return active module name
356 \sa setActiveModule()
358 QString LightApp_ModuleAction::activeModule() const
360 QAction* a = active();
361 return a ? a->text() : QString();
365 \brief Set active module.
367 To turn to the "neutral point" (no active module), pass empty string.
369 \param name new active module name
372 void LightApp_ModuleAction::setActiveModule( const QString& name )
374 if ( name == activeModule() )
377 int id = mySet->moduleId( name );
378 if ( name.isEmpty() || id != -1 )
379 activate( id, false );
383 \brief Set action display mode.
385 Action can be represented in the toolbar as
386 * combo box only (Qtx::ComboItem)
387 * set of modules buttons only (Qtx::Buttons)
388 * combo box followed by the set of modules buttons (Qtx::All)
391 \param mode action display mode
394 void LightApp_ModuleAction::setMode( const int mode )
401 \brief Get action display mode.
402 \param mode action display mode
405 int LightApp_ModuleAction::mode() const
411 \brief Called when the action is added to the widget.
412 \param w widget (not used)
414 void LightApp_ModuleAction::addedTo( QWidget* w )
416 if ( w->inherits( "QToolBar" ) )
417 w->insertAction( this, myCombo );
418 w->insertAction( this, mySet );
423 \brief Remove action from widget.
424 \param w widget (menu or toolbar)
425 \return \c true if the action is removed successfully and \c false otherwise.
428 void LightApp_ModuleAction::removedFrom( QWidget* w )
430 if ( w->inherits( "QToolBar" ) )
431 w->removeAction( myCombo );
432 w->removeAction( mySet );
436 \brief Perform delayed activation with specified id.
437 \param e custom event
438 \return \c true if the event was processed successfully and \c false otherwise.
440 bool LightApp_ModuleAction::event( QEvent* e )
442 if ( e->type() == QEvent::MaxUser ) {
443 activate( ((ActivateEvent*)e)->id(), false );
446 return QtxAction::event( e );
450 \fn void LightApp_ModuleAction::moduleActivated( const QString& name );
451 \brief Emitted when the module is activated
452 \param name module name (empty string for neutral point)
456 \brief Initialize an action,
459 void LightApp_ModuleAction::init()
464 myCombo = new ComboAction( this );
465 mySet = new ActionSet( this );
467 connect( this, SIGNAL( changed() ), this, SLOT( onChanged() ) );
468 connect( mySet, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
469 connect( myCombo, SIGNAL( activatedId( int ) ), this, SLOT( onComboActivated( int ) ) );
473 \brief Update an action.
476 void LightApp_ModuleAction::update()
478 QList<QtxComboBox*> lst = myCombo->widgets();
479 for ( QList<QtxComboBox*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
482 myCombo->setVisible( myMode & ComboItem );
483 mySet->setVisible( myMode & Buttons );
487 \brief Update combo box.
491 void LightApp_ModuleAction::update( QtxComboBox* cb )
496 bool blocked = cb->blockSignals( true );
497 int curId = mySet->moduleId( active() );
498 QList<QAction*> alist = mySet->actions();
501 cb->addItem( icon(), text() );
504 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
507 int id = mySet->moduleId( a );
508 cb->addItem( a->icon(), a->text() );
509 cb->setId( cb->count() - 1, id );
512 cb->setCurrentId( curId );
513 cb->blockSignals( blocked );
517 \brief Get an action corresponding to the active module.
519 \return active module action or 0 if there is no active module
521 QAction* LightApp_ModuleAction::active() const
525 QList<QAction*> alist = mySet->actions();
526 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end() && !a; ++it )
528 if ( (*it)->isChecked() )
536 \brief Activate a module item.
538 \param id module identifier
539 \param fromAction \c true if function is called from the module button
541 void LightApp_ModuleAction::activate( int id, bool fromAction )
543 bool checked = false;
545 QList<QAction*> alist = mySet->actions();
546 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
548 if ( mySet->moduleId( *it ) != id ) {
549 (*it)->setChecked( false );
553 (*it)->setChecked( true );
554 checked = (*it)->isChecked();
558 QList<QtxComboBox*> widgets = myCombo->widgets();
559 for ( QList<QtxComboBox*>::const_iterator wit = widgets.begin(); wit != widgets.end(); ++wit )
561 QtxComboBox* cb = *wit;
562 bool blocked = cb->signalsBlocked();
563 cb->blockSignals( true );
564 cb->setCurrentId( checked ? id : -1 );
565 cb->blockSignals( blocked );
568 emit moduleActivated( activeModule() );
572 \brief Called when module button is triggered.
574 \param id module identifier
576 void LightApp_ModuleAction::onTriggered( int id )
582 \brief Called when action state is changed.
585 This slot is used to prevent making the parent action visible.
587 void LightApp_ModuleAction::onChanged()
592 bool block = signalsBlocked();
593 blockSignals( true );
595 blockSignals( block );
599 \brief Called when combo box item is activated.
600 \param id module identifier
602 void LightApp_ModuleAction::onComboActivated( int id )
604 QApplication::postEvent( this, new ActivateEvent( QEvent::MaxUser, id ) );