From: vsr Date: Mon, 7 May 2007 15:18:40 +0000 (+0000) Subject: Porting to Qt4 X-Git-Url: http://git.salome-platform.org/gitweb/?a=commitdiff_plain;h=e791ddfc5ce627876db624b9bdc9de70be61c4c2;p=modules%2Fgui.git Porting to Qt4 --- diff --git a/src/Qtx/QtxAction.cxx b/src/Qtx/QtxAction.cxx index dbae4819f..ee45e1b1f 100755 --- a/src/Qtx/QtxAction.cxx +++ b/src/Qtx/QtxAction.cxx @@ -21,284 +21,182 @@ #include "QtxAction.h" -#include -#include +#include +#include +#include +#include /*! - Name: QtxAction [public] - Desc: Constructs an action with given parent and name. If toggle is true the - action will be a toggle action, otherwise it will be a command action. -*/ + \brief Constructor. + + Create an action. + \param parent parent object + \param name action name (in terms of QObject) + \param toggle if \c true the action is a toggle action +*/ QtxAction::QtxAction( QObject* parent, const char* name, bool toggle ) - : QAction( parent, name, toggle ) +: QAction( parent ) { + setObjectName( name ); + setCheckable( toggle ); + + QApplication::instance()->installEventFilter( this ); } /*! - Name: QtxAction [public] - Desc: This constructor creates an action with the following properties: the - description text, the icon or iconset icon, the menu text and keyboard - accelerator. It is a child of given parent and named specified name. - If toggle is true the action will be a toggle action, otherwise it will - be a command action. -*/ + \brief Constructor. + + Create an action. -QtxAction::QtxAction( const QString& text, const QIconSet& icon, + \param text tooltip text + \param icon iconset + \param menuText menu text + \param accel shortcut key sequence + \param parent parent object + \param name action name (in terms of QObject) + \param toggle if \c true the action is a toggle action +*/ +QtxAction::QtxAction( const QString& text, const QIcon& icon, const QString& menuText, int accel, QObject* parent, const char* name, bool toggle ) - : QAction( text, icon, menuText, accel, parent, name, toggle ) +: QAction( icon, menuText, parent ) { + setToolTip( text ); + setShortcut( accel ); + setObjectName( name ); + setCheckable( toggle ); + + QApplication::instance()->installEventFilter( this ); } /*! - Name: QtxAction [public] - Desc: This constructor creates an action with the following properties: the - description text, the menu text and keyboard accelerator. It is a child - of given parent and named specified name. If toggle is true the action - will be a toggle action, otherwise it will be a command action. -*/ + \brief Constructor. + + Create an action. + \param text tooltip text + \param menuText menu text + \param accel shortcut key sequence + \param parent parent object + \param name action name (in terms of QObject) + \param toggle if \c true the action is a toggle action +*/ QtxAction::QtxAction( const QString& text, const QString& menuText, int accel, QObject* parent, const char* name, bool toggle ) - : QAction( text, menuText, accel, parent, name, toggle ) +: QAction( menuText, parent ) { + setToolTip( text ); + setShortcut( accel ); + setObjectName( name ); + setCheckable( toggle ); + + QApplication::instance()->installEventFilter( this ); } /*! - Name: ~QtxAction [virtual public] - Desc: Destructor. + \brief Destructor. + + Does nothing for the moment. */ - QtxAction::~QtxAction() { } /*! - Name: addTo [virtual public] - Desc: Adds this action to widget. Returns true if the action was added - successfully and false otherwise. + \brief Event filter. + + Redefined from QObject. Calls virtual methods when the action is added to + the widget or removed from it in order to perform custom processing. + \param o object + \param e event + \return default implementation */ - -bool QtxAction::addTo( QWidget* w ) +bool QtxAction::eventFilter( QObject* o, QEvent* e ) { - if ( w->inherits( "QMenuBar" ) ) { - // --- Add action to the QMenuBar --- - // n.b. currently for the actions inserted to the menu bar - // the following properties are not supported: - // * tooltips - // * what's this info - // * toggle mode - QMenuBar* mb = (QMenuBar*)w; - if ( myMenuIds.find( w ) != myMenuIds.end() ) - return false; // already added - if ( name() == "qt_separator_action" ) // separator - myMenuIds[ w ] = mb->insertSeparator(); - else if ( iconSet().isNull() ) // has no icon - myMenuIds[ w ] = mb->insertItem( menuText(), this, SIGNAL( activated() ), accel() ); - else // has icon - myMenuIds[ w ] = mb->insertItem( iconSet(), menuText(), this, SIGNAL( activated() ), accel() ); - mb->setItemEnabled( myMenuIds[ w ], isEnabled() ); - mb->setItemVisible( myMenuIds[ w ], isVisible() ); - return true; + if ( o && o->isWidgetType() ) + { + if ( e->type() == QEvent::ActionAdded && ((QActionEvent*)e)->action() == this ) + addedTo( (QWidget*)o ); + if ( e->type() == QEvent::ActionRemoved && ((QActionEvent*)e)->action() == this ) + removedFrom( (QWidget*)o ); } - return QAction::addTo( w ); + return QAction::eventFilter( o, e ); } /*! - Name: addTo [virtual public] - Desc: Adds this action to widget. If widget is QPopupMenu given index will - be used for menu item inserting. Returns true if the action was added - successfully and false otherwise. + \brief Add action to widget. + \param w widget (menu or toolbar) + \return \c true if the action is added successfully and \c false otherwise. */ +bool QtxAction::addTo( QWidget* w ) +{ + if ( !w ) + return false; + + w->addAction( this ); + return true; +} + +/*! + \brief Add action to widget. + + The function adds the action to the menu or toolbar widget at the + specified \a index. If \a index is negative or greater than number of + items in the menu/toolbar, the action is added to the end of list. + \param w widget (menu or toolbar) + \param index index of the action in the action list + \return \c true if the action is added successfully and \c false otherwise. +*/ bool QtxAction::addTo( QWidget* w, const int index ) { - if ( !addTo( w ) ) + if ( !w ) return false; - if ( w->inherits( "QPopupMenu" ) ) { - // --- Add action to the QPopupMenu --- - QPopupMenu* popup = (QPopupMenu*)w; - if ( index >= 0 && index < (int)popup->count() - 1 ) { - int id = popup->idAt( popup->count() - 1 ); - if ( id != -1 ) { - QMenuItem* item = popup->findItem( id ); - if ( item && item->isSeparator() ) { - popup->removeItem( id ); - popup->insertSeparator( index ); - } - else { - QPopupMenu* p = item ? item->popup() : 0; - int accel = popup->accel( id ); - bool isOn = popup->isItemEnabled( id ); - bool isVisible = popup->isItemVisible( id ); - bool isChecked = popup->isItemChecked( id ); - QString text = popup->text( id ); - QIconSet icon; - if ( popup->iconSet( id ) ) - icon = *popup->iconSet( id ); - popup->removeItem( id ); - int pos; - if ( icon.isNull() ) - if ( p ) - pos = popup->indexOf( popup->insertItem( text, p, id, index ) ); - else - pos = popup->indexOf( popup->insertItem( text, id, index ) ); - else - if ( p ) - pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) ); - else - pos = popup->indexOf( popup->insertItem( icon, text, p, id, index ) ); - popup->setId( pos, id ); - popup->setAccel( accel, id ); - popup->setItemEnabled( id, isOn ); - popup->setItemVisible( id, isVisible ); - popup->setItemChecked( id, isChecked ); - if ( !whatsThis().isEmpty() ) - popup->setWhatsThis( id, whatsThis() ); - if ( !p ) - popup->connectItem( id, this, SLOT( internalActivation() ) ); - } - } - } - } - else if ( w->inherits( "QMenuBar" ) ) { - // --- Add action to the QMenuBar --- - QMenuBar* mb = (QMenuBar*)w; - if ( index >= 0 && index < (int)mb->count() - 1 ) { - int id = mb->idAt( mb->count() - 1 ); - if ( id != -1 ) { - QMenuItem* item = mb->findItem( id ); - if ( item && item->isSeparator() ) { - mb->removeItem( id ); - mb->insertSeparator( index ); - } - else { - QPopupMenu* p = item ? item->popup() : 0; - int accel = mb->accel( id ); - bool isOn = mb->isItemEnabled( id ); - bool isVisible = mb->isItemVisible( id ); - QString text = mb->text( id ); - QIconSet icon; - if ( mb->iconSet( id ) ) - icon = *mb->iconSet( id ); - mb->removeItem( id ); - int pos; - if ( icon.isNull() ) - if ( p ) - pos = mb->indexOf( mb->insertItem( text, p, id, index ) ); - else - pos = mb->indexOf( mb->insertItem( text, id, index ) ); - else - if ( p ) - pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) ); - else - pos = mb->indexOf( mb->insertItem( icon, text, p, id, index ) ); - mb->setId( pos, id ); - mb->setAccel( accel, id ); - mb->setItemEnabled( id, isOn ); - mb->setItemVisible( id, isVisible ); - if ( !p ) - mb->connectItem( id, this, SIGNAL( activated() ) ); - } - } - } - } + QAction* b = 0; + if ( 0 <= index && index < w->actions().count() ) + b = w->actions().at( index ); + + w->insertAction( b, this ); + return true; } /*! - Name: removeFrom [virtual public] - Desc: Removes this action from widget. Returns true if the action was removed - successfully and false otherwise. + \brief Remove action from widget. + \param w widget (menu or toolbar) + \return \c true if the action is removed successfully and \c false otherwise. */ - bool QtxAction::removeFrom( QWidget* w ) { - // check if widget is QMenuBar - if ( w->inherits( "QMenuBar" ) ) { - QMenuBar* mb = (QMenuBar*)w; - if ( myMenuIds.find( w ) == myMenuIds.end() ) - return false; // not yet added - mb->removeItem( myMenuIds[ w ] ); - myMenuIds.remove( w ); - return true; - } - return QAction::removeFrom( w ); + if ( !w ) + return false; + + w->removeAction( this ); + return true; } /*! - Name: setPopup [virtual public] - Desc: Set or unset the sub popup menu for item with specified id in the given popup. -*/ + \brief Called when the action is added to the widget. + + Base implementation does nothing. Can be redefined in the successor + class to customize the behavior. -void QtxAction::setPopup( QWidget* w, const int id, QPopupMenu* subPopup ) const + \param w widget (menu or toolbar) +*/ +void QtxAction::addedTo( QWidget* /*w*/ ) { - if ( !w ) - return; - - QMenuData* pmd = 0; - - if ( w->inherits( "QPopupMenu" ) ) - pmd = ::qt_cast( w ); - else if ( w->inherits( "QMenuBar" ) ) - pmd = ::qt_cast( w ); - - if ( !pmd ) - return; // bad widget - - QMenuData* md = 0; - QMenuItem* item = pmd->findItem( id, &md ); - if ( !item || md != pmd ) - return; // item is not found - - QPopupMenu* oldPopup = item->popup(); - if ( oldPopup == subPopup ) - return; // popup is not changed - - // get properties - int accel = pmd->accel( id ); - bool isOn = pmd->isItemEnabled( id ); - bool isVisible = pmd->isItemVisible( id ); - int pos = pmd->indexOf( id ); - QString text = pmd->text( id ); - QIconSet icon; - if ( pmd->iconSet( id ) ) - icon = *pmd->iconSet( id ); - - // remove previous item - pmd->removeItem( id ); - - // add new item - if ( w->inherits( "QPopupMenu" ) ) { - // --- QPopupMenu --- - QPopupMenu* popup = (QPopupMenu*)w; - if ( icon.isNull() ) - pos = popup->indexOf( subPopup ? popup->insertItem( text, subPopup, id, pos ) : - popup->insertItem( text, id, pos ) ); - else - pos = popup->indexOf( subPopup ? popup->insertItem( icon, text, subPopup, id, pos ) : - popup->insertItem( icon, text, id, pos ) ); - } - else { - // --- QMenuBar --- - QMenuBar* mb = (QMenuBar*)w; - if ( icon.isNull() ) - pos = mb->indexOf( subPopup ? mb->insertItem( text, subPopup, id, pos ) : - mb->insertItem( text, id, pos ) ); - else - pos = mb->indexOf( subPopup ? mb->insertItem( icon, text, subPopup, id, pos ) : - mb->insertItem( icon, text, id, pos ) ); - } +} - // restore properties - pmd->setId( pos, id ); // for sure (if id < 0) - pmd->setAccel( accel, id ); - pmd->setItemEnabled( id, isOn ); - pmd->setItemVisible( id, isVisible ); +/*! + \brief Called when the action is removed from the widget. - // delete old popup - delete oldPopup; -} + Base implementation does nothing. Can be redefined in the successor + class to customize the behavior. + \param w widget (menu or toolbar) +*/ +void QtxAction::removedFrom( QWidget* /*w*/ ) +{ +} diff --git a/src/Qtx/QtxAction.h b/src/Qtx/QtxAction.h index af0473f3f..f00b6f625 100755 --- a/src/Qtx/QtxAction.h +++ b/src/Qtx/QtxAction.h @@ -16,7 +16,7 @@ // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com // -// File: QtxAction.hxx +// File: QtxAction.h // Author: Sergey TELKOV #ifndef QTXACTION_H @@ -24,8 +24,9 @@ #include "Qtx.h" -#include -#include +#include +#include +#include #ifdef WIN32 #pragma warning ( disable:4251 ) @@ -33,23 +34,23 @@ class QTX_EXPORT QtxAction : public QAction { - Q_OBJECT + Q_OBJECT public: - QtxAction( QObject* = 0, const char* = 0, bool = false ); - QtxAction( const QString&, const QString&, int, QObject*, const char* = 0, bool = false ); - QtxAction( const QString&, const QIconSet&, const QString&, int, QObject*, const char* = 0, bool = false ); - virtual ~QtxAction(); + QtxAction( QObject* = 0, const char* = 0, bool = false ); + QtxAction( const QString&, const QString&, int, QObject*, const char* = 0, bool = false ); + QtxAction( const QString&, const QIcon&, const QString&, int, QObject*, const char* = 0, bool = false ); + virtual ~QtxAction(); - virtual bool addTo( QWidget* ); - virtual bool addTo( QWidget*, const int ); - virtual bool removeFrom( QWidget* ); + virtual bool eventFilter( QObject*, QEvent* ); -protected: - void setPopup( QWidget*, const int, QPopupMenu* ) const; + virtual bool addTo( QWidget* ); + virtual bool addTo( QWidget*, const int ); + virtual bool removeFrom( QWidget* ); -private: - QMap myMenuIds; +protected: + virtual void addedTo( QWidget* ); + virtual void removedFrom( QWidget* ); }; #ifdef WIN32