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 : LightApp_ModuleAction.cxx
23 // Author : Sergey TELKOV, Vadim SANDLER
25 #include "LightApp_ModuleAction.h"
27 #include <QtxComboBox.h>
28 #include <QtxActionSet.h>
29 #include <QVBoxLayout>
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
188 \brief An action, representing the list of modules to be inserted to the
191 This action is represented in the toolbar as combo box and a set of buttons
192 for each module. In addition to the modules items, the combo box contains
193 an item corresponding to the "neutral point" of the application
194 (when there is no active module).
196 The action can be constructed with up to two parameters, defining the text
197 and icon to be displayed for the "neutral point".
199 Only one module can be active at the moment. It can be set programmatically
200 with setActiveModule() function. Use this method with empty string to turn
201 to the "neutral point". To get active module, use activeModule() function.
203 When user activates/deactivates any module, the signal moduleActivated()
206 The action can be represented in the toolbar in different modes:
207 * as combo box only (Qtx::ComboItem)
208 * as set of modules buttons only (Qtx::Buttons)
209 * as combo box followed by the set of modules buttons (Qtx::All)
210 * as none (Qtx::None)
211 By default, both combo box and buttons set are shown. Use method
212 setMode() to change this behavior.
214 An action can be also added to the popup menu, but combo box is never shown
215 in this case, only modules buttons.
221 Creates an module action with "neutral point" item described by \a text.
223 \param text "neutral point" item's text
224 \param parent parent object
226 LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, QObject* parent )
227 : QtxAction( parent )
236 Creates an module action with "neutral point" item described by \a text and \a ico.
238 \param text "neutral point" item's text
239 \param ico "neutral point" item's icon
240 \param parent parent object
242 LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, const QIcon& ico, QObject* parent )
243 : QtxAction( parent )
253 LightApp_ModuleAction::~LightApp_ModuleAction()
258 \brief Get list of modules.
259 \return modules names list
261 QStringList LightApp_ModuleAction::modules() const
265 QList<QAction*> alist = mySet->actions();
266 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
267 lst.append( (*it)->text() );
273 \brief Get module icon.
274 \param name module name
278 QIcon LightApp_ModuleAction::moduleIcon( const QString& name ) const
280 QAction* a = mySet->moduleAction( name );
281 return a ? a->icon() : QIcon();
285 \brief Set module icon.
286 \param name module name
287 \param ico new module icon
290 void LightApp_ModuleAction::setModuleIcon( const QString& name, const QIcon& ico )
292 QAction* a = mySet->moduleAction( name );
301 \brief Add module into the list.
302 \param name module name
303 \param ico module icon
304 \param idx position in the module list (if -1, the module is added to the end of list)
307 void LightApp_ModuleAction::insertModule( const QString& name, const QIcon& ico,
310 QtxAction* a = new QtxAction( name, ico, name, 0, this, true );
311 a->setStatusTip( tr( "Activate/deactivate %1 module" ).arg( name ) );
313 mySet->insertAction( a, -1, idx );
318 \brief Remove module from the list.
319 \param name module name
322 void LightApp_ModuleAction::removeModule( const QString& name )
324 int id = mySet->moduleId( name );
328 mySet->removeAction( id );
333 \brief Get active module.
335 If there is no active module ("neutral point"), then the null string
338 \return active module name
339 \sa setActiveModule()
341 QString LightApp_ModuleAction::activeModule() const
343 QAction* a = active();
344 return a ? a->text() : QString();
348 \brief Set active module.
350 To turn to the "neutral point" (no active module), pass empty string.
352 \param name new active module name
355 void LightApp_ModuleAction::setActiveModule( const QString& name )
357 if ( name == activeModule() )
360 int id = mySet->moduleId( name );
361 if ( name.isEmpty() || id != -1 )
362 activate( id, false );
366 \brief Set action display mode.
368 Action can be represented in the toolbar as
369 * combo box only (Qtx::ComboItem)
370 * set of modules buttons only (Qtx::Buttons)
371 * combo box followed by the set of modules buttons (Qtx::All)
374 \param mode action display mode
377 void LightApp_ModuleAction::setMode( const int mode )
384 \brief Get action display mode.
385 \param mode action display mode
388 int LightApp_ModuleAction::mode() const
394 \brief Called when the action is added to the widget.
395 \param w widget (not used)
397 void LightApp_ModuleAction::addedTo( QWidget* w )
399 if ( w->inherits( "QToolBar" ) )
400 w->insertAction( this, myCombo );
401 w->insertAction( this, mySet );
406 \brief Remove action from widget.
407 \param w widget (menu or toolbar)
408 \return \c true if the action is removed successfully and \c false otherwise.
411 void LightApp_ModuleAction::removedFrom( QWidget* w )
413 if ( w->inherits( "QToolBar" ) )
414 w->removeAction( myCombo );
415 w->removeAction( mySet );
419 \fn void LightApp_ModuleAction::moduleActivated( const QString& name );
420 \brief Emitted when the module is activated
421 \param name module name (empty string for neutral point)
425 \brief Initialize an action,
428 void LightApp_ModuleAction::init()
433 myCombo = new ComboAction( this );
434 mySet = new ActionSet( this );
436 connect( this, SIGNAL( changed() ), this, SLOT( onChanged() ) );
437 connect( mySet, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
438 connect( myCombo, SIGNAL( activatedId( int ) ), this, SLOT( onComboActivated( int ) ) );
442 \brief Update an action.
445 void LightApp_ModuleAction::update()
447 QList<QtxComboBox*> lst = myCombo->widgets();
448 for ( QList<QtxComboBox*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
451 myCombo->setVisible( myMode & ComboItem );
452 mySet->setVisible( myMode & Buttons );
456 \brief Update combo box.
460 void LightApp_ModuleAction::update( QtxComboBox* cb )
465 int curId = mySet->moduleId( active() );
466 QList<QAction*> alist = mySet->actions();
469 cb->addItem( icon(), text() );
472 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
475 int id = mySet->moduleId( a );
476 cb->addItem( a->icon(), a->text() );
477 cb->setId( cb->count() - 1, id );
480 cb->setCurrentId( curId );
484 \brief Get an action corresponding to the active module.
486 \return active module action or 0 if there is no active module
488 QAction* LightApp_ModuleAction::active() const
492 QList<QAction*> alist = mySet->actions();
493 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end() && !a; ++it )
495 if ( (*it)->isChecked() )
503 \brief Activate a module item.
505 \param id module identifier
506 \param fromAction \c true if function is called from the module button
508 void LightApp_ModuleAction::activate( int id, bool fromAction )
510 bool checked = false;
512 QList<QAction*> alist = mySet->actions();
513 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
515 if ( mySet->moduleId( *it ) != id ) {
516 (*it)->setChecked( false );
520 (*it)->setChecked( true );
521 checked = (*it)->isChecked();
525 QList<QtxComboBox*> widgets = myCombo->widgets();
526 for ( QList<QtxComboBox*>::const_iterator wit = widgets.begin(); wit != widgets.end(); ++wit )
528 QtxComboBox* cb = *wit;
529 bool blocked = cb->signalsBlocked();
530 cb->blockSignals( true );
531 cb->setCurrentId( checked ? id : -1 );
532 cb->blockSignals( blocked );
535 emit moduleActivated( activeModule() );
539 \brief Called when module button is triggered.
541 \param id module identifier
543 void LightApp_ModuleAction::onTriggered( int id )
549 \brief Called when action state is changed.
552 This slot is used to prevent making the parent action visible.
554 void LightApp_ModuleAction::onChanged()
559 bool block = signalsBlocked();
560 blockSignals( true );
562 blockSignals( block );
566 \brief Called when combo box item is activated.
567 \param id module identifier
569 void LightApp_ModuleAction::onComboActivated( int id )
571 activate( id, false );