1 // Copyright (C) 2007-2020 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 : 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 += (*wit)->findChildren<QtxComboBox*>();
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 number of registered modules.
276 \return modules count
278 int LightApp_ModuleAction::count() const
280 return modules().count();
284 \brief Get list of modules.
285 \return modules names list
287 QStringList LightApp_ModuleAction::modules() const
291 QList<QAction*> alist = mySet->actions();
292 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
293 lst.append( (*it)->text() );
299 \brief Get module icon.
300 \param name module name
304 QIcon LightApp_ModuleAction::moduleIcon( const QString& name ) const
306 QAction* a = mySet->moduleAction( name );
307 return a ? a->icon() : QIcon();
311 \brief Set module icon.
312 \param name module name
313 \param ico new module icon
316 void LightApp_ModuleAction::setModuleIcon( const QString& name, const QIcon& ico )
318 QAction* a = mySet->moduleAction( name );
327 \brief Get module action.
328 \param name module name
330 QAction* LightApp_ModuleAction::moduleAction( const QString& name ) const
332 return mySet->moduleAction( name );
336 \brief Add module into the list.
337 \param name module name
338 \param ico module icon
339 \param idx position in the module list (if -1, the module is added to the end of list)
342 void LightApp_ModuleAction::insertModule( const QString& name, const QIcon& ico,
345 QtxAction* a = new QtxAction( name, ico, name, 0, this, true );
346 a->setStatusTip( tr( "ACTIVATE_MODULE_TOP" ).arg( name ) );
348 mySet->insertAction( a, -1, idx );
353 \brief Remove module from the list.
354 \param name module name
357 void LightApp_ModuleAction::removeModule( const QString& name )
359 int id = mySet->moduleId( name );
363 mySet->removeAction( id );
368 \brief Get active module.
370 If there is no active module ("neutral point"), then the null string
373 \return active module name
374 \sa setActiveModule()
376 QString LightApp_ModuleAction::activeModule() const
378 QAction* a = active();
379 return a ? a->text() : QString();
383 \brief Set active module.
385 To turn to the "neutral point" (no active module), pass empty string.
387 \param name new active module name
390 void LightApp_ModuleAction::setActiveModule( const QString& name )
392 if ( name == activeModule() )
395 int id = mySet->moduleId( name );
396 if ( name.isEmpty() || id != -1 )
397 activate( id, false );
401 \brief Set action display mode.
403 Action can be represented in the toolbar as
404 * combo box only (Qtx::ComboItem)
405 * set of modules buttons only (Qtx::Buttons)
406 * combo box followed by the set of modules buttons (Qtx::All)
409 \param mode action display mode
412 void LightApp_ModuleAction::setMode( const int mode )
419 \brief Get action display mode.
420 \param mode action display mode
423 int LightApp_ModuleAction::mode() const
429 \brief Called when the action is added to the widget.
430 \param w widget (not used)
432 void LightApp_ModuleAction::addedTo( QWidget* w )
434 if ( w->inherits( "QToolBar" ) )
435 w->insertAction( this, myCombo );
436 w->insertAction( this, mySet );
441 \brief Remove action from widget.
442 \param w widget (menu or toolbar)
443 \return \c true if the action is removed successfully and \c false otherwise.
446 void LightApp_ModuleAction::removedFrom( QWidget* w )
448 if ( w->inherits( "QToolBar" ) )
449 w->removeAction( myCombo );
450 w->removeAction( mySet );
454 \brief Perform delayed activation with specified id.
455 \param e custom event
456 \return \c true if the event was processed successfully and \c false otherwise.
458 bool LightApp_ModuleAction::event( QEvent* e )
460 if ( e->type() == QEvent::MaxUser ) {
461 activate( ((ActivateEvent*)e)->id(), false );
464 return QtxAction::event( e );
468 \fn void LightApp_ModuleAction::moduleActivated( const QString& name );
469 \brief Emitted when the module is activated
470 \param name module name (empty string for neutral point)
474 \brief Initialize an action,
477 void LightApp_ModuleAction::init()
482 myCombo = new ComboAction( this );
483 mySet = new ActionSet( this );
485 connect( this, SIGNAL( changed() ), this, SLOT( onChanged() ) );
486 connect( mySet, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) );
487 connect( myCombo, SIGNAL( activatedId( int ) ), this, SLOT( onComboActivated( int ) ) );
491 \brief Update an action.
494 void LightApp_ModuleAction::update()
496 QList<QtxComboBox*> lst = myCombo->widgets();
497 for ( QList<QtxComboBox*>::const_iterator it = lst.begin(); it != lst.end(); ++it )
500 myCombo->setVisible( myMode & ComboItem );
501 mySet->setVisible( myMode & Buttons );
505 \brief Update combo box.
509 void LightApp_ModuleAction::update( QtxComboBox* cb )
514 bool blocked = cb->blockSignals( true );
515 int curId = mySet->moduleId( active() );
516 QList<QAction*> alist = mySet->actions();
519 cb->addItem( icon(), text() );
522 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
525 int id = mySet->moduleId( a );
526 cb->addItem( a->icon(), a->text() );
527 cb->setId( cb->count() - 1, id );
530 cb->setCurrentId( curId );
531 cb->blockSignals( blocked );
535 \brief Get an action corresponding to the active module.
537 \return active module action or 0 if there is no active module
539 QAction* LightApp_ModuleAction::active() const
543 QList<QAction*> alist = mySet->actions();
544 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end() && !a; ++it )
546 if ( (*it)->isChecked() )
554 \brief Activate a module item.
556 \param id module identifier
557 \param fromAction \c true if function is called from the module button
559 void LightApp_ModuleAction::activate( int id, bool fromAction )
561 bool checked = false;
563 QList<QAction*> alist = mySet->actions();
564 for ( QList<QAction*>::const_iterator it = alist.begin(); it != alist.end(); ++it )
566 if ( mySet->moduleId( *it ) != id ) {
567 (*it)->setChecked( false );
571 (*it)->setChecked( true );
572 checked = (*it)->isChecked();
576 QList<QtxComboBox*> widgets = myCombo->widgets();
577 for ( QList<QtxComboBox*>::const_iterator wit = widgets.begin(); wit != widgets.end(); ++wit )
579 QtxComboBox* cb = *wit;
580 bool blocked = cb->signalsBlocked();
581 cb->blockSignals( true );
582 cb->setCurrentId( checked ? id : -1 );
583 cb->blockSignals( blocked );
586 emit moduleActivated( activeModule() );
590 \brief Called when module button is triggered.
592 \param id module identifier
594 void LightApp_ModuleAction::onTriggered( int id )
600 \brief Called when action state is changed.
603 This slot is used to prevent making the parent action visible.
605 void LightApp_ModuleAction::onChanged()
610 bool block = signalsBlocked();
611 blockSignals( true );
613 blockSignals( block );
617 \brief Called when combo box item is activated.
618 \param id module identifier
620 void LightApp_ModuleAction::onComboActivated( int id )
622 QApplication::postEvent( this, new ActivateEvent( QEvent::MaxUser, id ) );