From 2623233c00a8725cc60d5b263c97e0f1754730ff Mon Sep 17 00:00:00 2001 From: vsr Date: Wed, 20 Jun 2007 12:42:58 +0000 Subject: [PATCH] Porting to Qt 4 --- src/LightApp/LightApp_ModuleAction.cxx | 458 ++++++++++++++++++------- src/LightApp/LightApp_ModuleAction.h | 49 ++- 2 files changed, 366 insertions(+), 141 deletions(-) diff --git a/src/LightApp/LightApp_ModuleAction.cxx b/src/LightApp/LightApp_ModuleAction.cxx index 3f75df418..4e929fa12 100755 --- a/src/LightApp/LightApp_ModuleAction.cxx +++ b/src/LightApp/LightApp_ModuleAction.cxx @@ -23,6 +23,161 @@ #include "LightApp_ModuleAction.h" #include +#include +#include + +/*! + \class LightApp_ModuleAction::ActionSet + \brief Internal class to represent list of modules buttons. + \internal +*/ + +class LightApp_ModuleAction::ActionSet : public QtxActionSet +{ +public: + ActionSet( QObject* ); + QAction* moduleAction( const QString& ) const; + int moduleId( const QString& ) const; + int moduleId( QAction* ) const; + void setVisible( bool ); +}; + +/*! + \brief Constructor. + \internal + \param parent parent object +*/ +LightApp_ModuleAction::ActionSet::ActionSet( QObject* parent ) +: QtxActionSet( parent ) +{ +} + +/*! + \brief Get action corresponding to the specified module. + \internal + \param name module name + \return module action or 0 if \a name is invalid +*/ +QAction* LightApp_ModuleAction::ActionSet::moduleAction( const QString& name ) const +{ + QAction* a = 0; + + QList alist = actions(); + for ( QList::const_iterator it = alist.begin(); it != alist.end() && !a; ++it ) + { + if ( (*it)->text() == name ) + a = *it; + } + + return a; +} + +/*! + \brief Get module action identifier. + \internal + \param name module name + \return module action ID or -1 if \a name is invalid +*/ +int LightApp_ModuleAction::ActionSet::moduleId( const QString& name ) const +{ + int id = -1; + + QList alist = actions(); + for ( QList::const_iterator it = alist.begin(); it != alist.end() && id == -1; ++it ) + { + if ( (*it)->text() == name ) + id = actionId( *it ); + } + + return id; +} + +/*! + \brief Get module action identifier. + \internal + \param a module action + \return module action ID or -1 if \a a is null or invalid +*/ +int LightApp_ModuleAction::ActionSet::moduleId( QAction* a ) const +{ + return actionId( a ); +} + +/*! + \brief Show/hide modules actions. + \internal + \param on new visibility state +*/ +void LightApp_ModuleAction::ActionSet::setVisible( bool on ) +{ + QList alist = actions(); + for ( QList::const_iterator it = alist.begin(); it != alist.end(); ++it ) + (*it)->setVisible( on ); + + QtxActionSet::setVisible( on ); +} + +/*! + \class LightApp_ModuleAction::ComboAction + \brief Internal class to represent combo box with the list of modules in the toolbar. + \internal +*/ + +/*! + \brief Constructor. + \internal + \param parent parent object +*/ +LightApp_ModuleAction::ComboAction::ComboAction( QObject* parent ) +: QtxAction( parent ) +{ +} + +/*! + \brief Get list of associated widgets. + \internal + \return list of created widgets (QtxComboBox) +*/ +QList LightApp_ModuleAction::ComboAction::widgets() const +{ + QList lst; + + QList wlist = createdWidgets(); + for ( QList::const_iterator wit = wlist.begin(); wit != wlist.end(); ++wit ) + lst += qFindChildren(*wit); + + return lst; +} + +/*! + \brief Create combo box widget by request from the toolbar. + \internal + \param parent parent widget (should be QToolBar or its successor) + \return new custom widget, containing combo box +*/ +QWidget* LightApp_ModuleAction::ComboAction::createWidget( QWidget* parent ) +{ + if ( !parent->inherits( "QToolBar" ) ) + return 0; + + QWidget* dumb = new QWidget( parent ); + QHBoxLayout* l = new QHBoxLayout( dumb );l->setSpacing(0);l->setMargin(0); + QtxComboBox* cb = new QtxComboBox( dumb ); + cb->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); + cb->setFocusPolicy( Qt::NoFocus ); + l->addWidget( cb ); + + connect( cb, SIGNAL( activatedId( int ) ), this, SIGNAL( activatedId( int ) ) ); + + return dumb; +} + +/*! + \fn void LightApp_ModuleAction::ComboAction::activatedId( int id ); + \internal + \brief Emitted when the combo box item is activated + \param item identifier +*/ /*! \class LightApp_ModuleAction @@ -30,8 +185,9 @@ toolbar. This action is represented in the toolbar as combo box and a set of buttons - for each module. In addition, the combo box contains an item, corresponding - to the "neutral point" of the application (where there is no active module). + for each module. In addition to the modules items, the combo box contains + an item corresponding to the "neutral point" of the application + (when there is no active module). The action can be constructed with up to two parameters, defining the text and icon to be displayed for the "neutral point". @@ -42,6 +198,17 @@ When user activates/deactivates any module, the signal moduleActivated() is emitted. + + The action can be represented in the toolbar in different modes: + * as combo box only (Qtx::ComboItem) + * as set of modules buttons only (Qtx::Buttons) + * as combo box followed by the set of modules buttons (Qtx::All) + * as none (Qtx::None) + By default, both combo box and buttons set are shown. Use method + setMode() to change this behavior. + + An action can be also added to the popup menu, but combo box is never shown + in this case, only modules buttons. */ /*! @@ -53,7 +220,7 @@ \param parent parent object */ LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, QObject* parent ) -: QtxActionSet( parent ) +: QtxAction( parent ) { setText( text ); init(); @@ -69,7 +236,7 @@ LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, QObject* pare \param parent parent object */ LightApp_ModuleAction::LightApp_ModuleAction( const QString& text, const QIcon& ico, QObject* parent ) -: QtxActionSet( parent ) +: QtxAction( parent ) { setText( text ); setIcon( ico ); @@ -90,31 +257,35 @@ LightApp_ModuleAction::~LightApp_ModuleAction() QStringList LightApp_ModuleAction::modules() const { QStringList lst; - QList alist = actions(); + + QList alist = mySet->actions(); for ( QList::const_iterator it = alist.begin(); it != alist.end(); ++it ) lst.append( (*it)->text() ); + return lst; } /*! - \brief Get module icon + \brief Get module icon. \param name module name \return module icon + \sa setModuleIcon() */ QIcon LightApp_ModuleAction::moduleIcon( const QString& name ) const { - QAction* a = moduleAction( name ); + QAction* a = mySet->moduleAction( name ); return a ? a->icon() : QIcon(); } /*! - \brief Set module icon + \brief Set module icon. \param name module name \param ico new module icon + \sa moduleIcon() */ void LightApp_ModuleAction::setModuleIcon( const QString& name, const QIcon& ico ) { - QAction* a = moduleAction( name ); + QAction* a = mySet->moduleAction( name ); if ( !a ) return; @@ -127,6 +298,7 @@ void LightApp_ModuleAction::setModuleIcon( const QString& name, const QIcon& ico \param name module name \param ico module icon \param idx position in the module list (if -1, the module is added to the end of list) + \sa removeModule() */ void LightApp_ModuleAction::insertModule( const QString& name, const QIcon& ico, const int idx ) @@ -134,21 +306,22 @@ void LightApp_ModuleAction::insertModule( const QString& name, const QIcon& ico, QtxAction* a = new QtxAction( name, ico, name, 0, this, true ); a->setStatusTip( tr( "Activate/deactivate %1 module" ).arg( name ) ); - int id = insertAction( a, -1, idx ); + mySet->insertAction( a, -1, idx ); update(); } /*! \brief Remove module from the list. \param name module name + \sa insertModule() */ void LightApp_ModuleAction::removeModule( const QString& name ) { - int id = moduleId( name ); + int id = mySet->moduleId( name ); if ( id == -1 ) return; - removeAction( id ); + mySet->removeAction( id ); update(); } @@ -159,6 +332,7 @@ void LightApp_ModuleAction::removeModule( const QString& name ) is returned. \return active module name + \sa setActiveModule() */ QString LightApp_ModuleAction::activeModule() const { @@ -172,125 +346,104 @@ QString LightApp_ModuleAction::activeModule() const To turn to the "neutral point" (no active module), pass empty string. \param name new active module name + \sa activeModule() */ void LightApp_ModuleAction::setActiveModule( const QString& name ) { - int id = moduleId( name ); + if ( name == activeModule() ) + return; + + int id = mySet->moduleId( name ); if ( name.isEmpty() || id != -1 ) activate( id, false ); } /*! - \brief Called when module button is triggered. - \internal - \param id module identifier + \brief Set action display mode. + + Action can be represented in the toolbar as + * combo box only (Qtx::ComboItem) + * set of modules buttons only (Qtx::Buttons) + * combo box followed by the set of modules buttons (Qtx::All) + * none (Qtx::None) + + \param mode action display mode + \sa mode() */ -void LightApp_ModuleAction::onTriggered( int id ) +void LightApp_ModuleAction::setMode( const int mode ) { - activate( id ); + myMode = mode; + update(); } /*! - \brief Activate a module item. - \internal - \param id module identifier - \param fromAction \c true if function is called from the module button + \brief Get action display mode. + \param mode action display mode + \sa setMode() */ -void LightApp_ModuleAction::activate( int id, bool fromAction ) +int LightApp_ModuleAction::mode() const { - bool checked = false; - - QList alist = actions(); - for ( QList::const_iterator it = alist.begin(); it != alist.end(); ++it ) - { - if ( actionId( *it ) != id ) { - (*it)->setChecked( false ); - } - else { - if ( !fromAction ) - (*it)->setChecked( true ); - checked = (*it)->isChecked(); - } - } - - QList widgets = createdWidgets(); - for ( QList::const_iterator wit = widgets.begin(); wit != widgets.end(); ++wit ) - { - QtxComboBox* cb = ::qobject_cast( *wit ); - if ( cb ) { - bool blocked = cb->signalsBlocked(); - cb->blockSignals( true ); - cb->setCurrentId( checked ? id : -1 ); - cb->blockSignals( blocked ); - } - } - - emit moduleActivated( activeModule() ); + return myMode; } /*! - \brief Called when combo box item is activated. - \param id module identifier + \brief Add action to the widget. + \param w widget (menu or toolbar) + \return \c true if the action is added successfully and \c false otherwise. + \sa removeFrom() */ -void LightApp_ModuleAction::onComboActivated( int id ) +bool LightApp_ModuleAction::addTo( QWidget* w ) { - QList alist = actions(); - for ( QList::const_iterator it = alist.begin(); it != alist.end(); ++it ) - { - (*it)->setChecked( actionId( *it ) == id ); - } - - emit moduleActivated( activeModule() ); + bool ok = QtxAction::addTo( w ); + if ( w->inherits( "QToolBar" ) ) + ok = ok && myCombo->addTo( w ); + return ok && mySet->addTo( w ); } /*! - \brief Create combo box widget by request from tool bar. - \param parent parent widget (should be QToolBar or its successor) - \return new combo box widget + \brief Remove action from widget. + \param w widget (menu or toolbar) + \return \c true if the action is removed successfully and \c false otherwise. + \sa addTo() */ -QWidget* LightApp_ModuleAction::createWidget( QWidget* parent ) +bool LightApp_ModuleAction::removeFrom( QWidget* w ) { - if ( !parent->inherits( "QToolBar" ) ) - return 0; - - QtxComboBox* cb = new QtxComboBox( parent ); - cb->setSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ); - cb->setFocusPolicy( Qt::NoFocus ); - - update( cb ); - cb->blockSignals( true ); - cb->setCurrentId( actionId( active() ) ); - cb->blockSignals( false ); - - connect( cb, SIGNAL( activatedId( int ) ), this, SLOT( onComboActivated( int ) ) ); - - return cb; + bool ok = mySet->removeFrom( w ); + if ( w->inherits( "QToolBar" ) ) + ok = ok && myCombo->removeFrom( w ); + return ok && QtxAction::removeFrom( w ); } /*! - \brief Return \c false if the action contains custom widgets. - \return \c false (action provides combo box as custom widget) + \brief Called when the action is added to the widget. + \param w widget (not used) */ -bool LightApp_ModuleAction::isEmptyAction() const +void LightApp_ModuleAction::addedTo( QWidget* /*w*/ ) { - return false; + update(); } /*! - \brief Get an action corresponding to the active module. + \fn void LightApp_ModuleAction::moduleActivated( const QString& name ); + \brief Emitted when the module is activated + \param name module name (empty string for neutral point) +*/ + +/*! + \brief Initialize an action, \internal - \return active module action or 0 if there is no active module */ -QAction* LightApp_ModuleAction::active() const +void LightApp_ModuleAction::init() { - QAction* a = 0; - QList alist = actions(); - for ( QList::const_iterator it = alist.begin(); it != alist.end() && !a; ++it ) - { - if ( (*it)->isChecked() ) - a = *it; - } - return a; + setVisible( false ); + + myMode = All; + myCombo = new ComboAction( this ); + mySet = new ActionSet( this ); + + connect( this, SIGNAL( changed() ), this, SLOT( onChanged() ) ); + connect( mySet, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) ); + connect( myCombo, SIGNAL( activatedId( int ) ), this, SLOT( onComboActivated( int ) ) ); } /*! @@ -299,9 +452,12 @@ QAction* LightApp_ModuleAction::active() const */ void LightApp_ModuleAction::update() { - QList lst = createdWidgets(); - for ( QList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) - update( ::qobject_cast( *itr ) ); + QList lst = myCombo->widgets(); + for ( QList::const_iterator it = lst.begin(); it != lst.end(); ++it ) + update( *it ); + + myCombo->setVisible( myMode & ComboItem ); + mySet->setVisible( myMode & Buttons ); } /*! @@ -314,18 +470,17 @@ void LightApp_ModuleAction::update( QtxComboBox* cb ) if ( !cb ) return; - int curId = cb->currentId(); - - QList alist = actions(); + int curId = mySet->moduleId( active() ); + QList alist = mySet->actions(); cb->clear(); - + cb->addItem( icon(), text() ); cb->setId( 0, -1 ); for ( QList::const_iterator it = alist.begin(); it != alist.end(); ++it ) { QAction* a = *it; - int id = actionId( a ); + int id = mySet->moduleId( a ); cb->addItem( a->icon(), a->text() ); cb->setId( cb->count() - 1, id ); } @@ -334,53 +489,92 @@ void LightApp_ModuleAction::update( QtxComboBox* cb ) } /*! - \brief Initialize an action, - \internal -*/ -void LightApp_ModuleAction::init() -{ - setVisible( true ); - connect( this, SIGNAL( triggered( int ) ), this, SLOT( onTriggered( int ) ) ); -} - -/*! - \brief Get action corresponding to the specified module. + \brief Get an action corresponding to the active module. \internal - \param name module name - \return module action or 0 if \a name is invalid + \return active module action or 0 if there is no active module */ -QAction* LightApp_ModuleAction::moduleAction( const QString& name ) const +QAction* LightApp_ModuleAction::active() const { QAction* a = 0; - QList alist = actions(); + + QList alist = mySet->actions(); for ( QList::const_iterator it = alist.begin(); it != alist.end() && !a; ++it ) { - if ( (*it)->text() == name ) + if ( (*it)->isChecked() ) a = *it; } + return a; } /*! - \brief Get module action identifier. + \brief Activate a module item. \internal - \param name module name - \return module action ID or 0 if \a name is invalid + \param id module identifier + \param fromAction \c true if function is called from the module button */ -int LightApp_ModuleAction::moduleId( const QString& name ) const +void LightApp_ModuleAction::activate( int id, bool fromAction ) { - int id = -1; - QList alist = actions(); - for ( QList::const_iterator it = alist.begin(); it != alist.end() && id == -1; ++it ) + bool checked = false; + + QList alist = mySet->actions(); + for ( QList::const_iterator it = alist.begin(); it != alist.end(); ++it ) { - if ( (*it)->text() == name ) - id = actionId( *it ); + if ( mySet->moduleId( *it ) != id ) { + (*it)->setChecked( false ); + } + else { + if ( !fromAction ) + (*it)->setChecked( true ); + checked = (*it)->isChecked(); + } } - return id; + + QList widgets = myCombo->widgets(); + for ( QList::const_iterator wit = widgets.begin(); wit != widgets.end(); ++wit ) + { + QtxComboBox* cb = *wit; + bool blocked = cb->signalsBlocked(); + cb->blockSignals( true ); + cb->setCurrentId( checked ? id : -1 ); + cb->blockSignals( blocked ); + } + + emit moduleActivated( activeModule() ); } /*! - \fn void LightApp_ModuleAction::moduleActivated( const QString& name ); - \brief Emitted when the module is activated - \param name module name (empty string for neutral point) + \brief Called when module button is triggered. + \internal + \param id module identifier +*/ +void LightApp_ModuleAction::onTriggered( int id ) +{ + activate( id ); +} + +/*! + \brief Called when action state is changed. + \internal + + This slot is used to prevent making the parent action visible. +*/ +void LightApp_ModuleAction::onChanged() +{ + if ( !isVisible() ) + return; + + bool block = signalsBlocked(); + blockSignals( true ); + setVisible( false ); + blockSignals( block ); +} + +/*! + \brief Called when combo box item is activated. + \param id module identifier */ +void LightApp_ModuleAction::onComboActivated( int id ) +{ + activate( id, false ); +} diff --git a/src/LightApp/LightApp_ModuleAction.h b/src/LightApp/LightApp_ModuleAction.h index db00ff605..333c265e2 100755 --- a/src/LightApp/LightApp_ModuleAction.h +++ b/src/LightApp/LightApp_ModuleAction.h @@ -25,7 +25,7 @@ #include "LightApp.h" -#include +#include #ifdef WIN32 #pragma warning ( disable:4251 ) @@ -33,10 +33,17 @@ class QtxComboBox; -class LIGHTAPP_EXPORT LightApp_ModuleAction : public QtxActionSet +class LIGHTAPP_EXPORT LightApp_ModuleAction : public QtxAction { Q_OBJECT +private: + class ActionSet; + class ComboAction; + +public: + enum { None = 0x00, Buttons = 0x01, ComboItem = 0x02, All = Buttons | ComboItem }; + public: LightApp_ModuleAction( const QString&, QObject* = 0 ); LightApp_ModuleAction( const QString&, const QIcon&, QObject* = 0 ); @@ -53,19 +60,21 @@ public: QString activeModule() const; void setActiveModule( const QString& ); -signals: - void moduleActivated( const QString& ); + void setMode( const int ); + int mode() const; + + virtual bool addTo( QWidget* ); + virtual bool removeFrom( QWidget* ); protected: - virtual QWidget* createWidget( QWidget* ); - virtual bool isEmptyAction() const; + virtual void addedTo( QWidget* ); + +signals: + void moduleActivated( const QString& ); private: void init(); - QAction* moduleAction( const QString& ) const; - int moduleId( const QString& ) const; - void update(); void update( QtxComboBox* ); @@ -73,8 +82,30 @@ private: void activate( int, bool = true ); private slots: + void onChanged(); void onTriggered( int ); void onComboActivated( int ); + +private: + ComboAction* myCombo; + ActionSet* mySet; + int myMode; +}; + +class LightApp_ModuleAction::ComboAction : public QtxAction +{ + Q_OBJECT + +public: + ComboAction( QObject* parent ); + + QList widgets() const; + +protected: + virtual QWidget* createWidget( QWidget* ); + +signals: + void activatedId( int ); }; #endif // LIGHTAPP_MODULEACTION_H -- 2.39.2