From 788247fcd25c3c8f4f422b22312b4fb4d4ab21a9 Mon Sep 17 00:00:00 2001 From: vsr Date: Tue, 8 May 2007 10:18:46 +0000 Subject: [PATCH] Porting to Qt4 --- src/Qtx/Qtx.cxx | 2 +- src/Qtx/QtxActionMgr.cxx | 494 +++++++++++++++++++++++---------------- src/Qtx/QtxActionMgr.h | 38 ++- 3 files changed, 312 insertions(+), 222 deletions(-) diff --git a/src/Qtx/Qtx.cxx b/src/Qtx/Qtx.cxx index ddbc3b291..3505b0cfc 100755 --- a/src/Qtx/Qtx.cxx +++ b/src/Qtx/Qtx.cxx @@ -156,7 +156,7 @@ void Qtx::setTabOrder( const QWidgetList& widgets ) alignment flags \a alignFlags. \param src source widget (being aligned) \param ref reference widget (source widget being aligned to) - \param alignFlags alignment flags (Qtx::Alignment) + \param alignFlags alignment flags (Qtx::AlignmentFlags) */ void Qtx::alignWidget( QWidget* src, const QWidget* ref, const int alignFlags ) { diff --git a/src/Qtx/QtxActionMgr.cxx b/src/Qtx/QtxActionMgr.cxx index b157b835e..dc8ce8cc8 100644 --- a/src/Qtx/QtxActionMgr.cxx +++ b/src/Qtx/QtxActionMgr.cxx @@ -1,17 +1,17 @@ // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D -// +// // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public -// License as published by the Free Software Foundation; either +// License as published by the Free Software Foundation; either // version 2.1 of the License. -// -// This library is distributed in the hope that it will be useful -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// +// This library is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // -// You should have received a copy of the GNU Lesser General Public -// License along with this library; if not, write to the Free Software +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com @@ -23,25 +23,33 @@ #include "QtxActionMgr.h" #include "QtxAction.h" -#include -#include -#include -#include -#include -#include -#include +#include +#include + +#include +#include +#include +#include + +#include -static QAction* qtx_separator_action = 0; +typedef QList< QPointer > qtx_actionlist; +static qtx_actionlist qtx_separator_actions; +/*! + \internal + \brief Clean all cashed separator actions. + */ void qtxSeparatorActionCleanup() { - delete qtx_separator_action; - qtx_separator_action = 0; + for ( qtx_actionlist::iterator it = qtx_separator_actions.begin(); it != qtx_separator_actions.end(); ++it ) + delete *it; } /*! - Class: QtxActionMenuMgr::SeparatorAction - Level: Internal + \class QtxActionMgr::SeparatorAction + \internal + \brief Separator action class. */ class QtxActionMgr::SeparatorAction : public QtxAction @@ -49,132 +57,64 @@ class QtxActionMgr::SeparatorAction : public QtxAction public: SeparatorAction( QObject* = 0 ); virtual ~SeparatorAction(); - - virtual bool addTo( QWidget* ); - virtual bool removeFrom( QWidget* ); - -private: - QMap myMenus; - QMap myTools; }; /*! - Constructor + \brief Separator action constructor. + \param parent parent object */ QtxActionMgr::SeparatorAction::SeparatorAction( QObject* parent ) : QtxAction( parent ) { + setSeparator( true ); } /*! - Destructor -*/ -QtxActionMgr::SeparatorAction::~SeparatorAction() -{ -} + \brief Separator action destructor. -/*! - Adds action to widget - \param wid - widget + Does nothing for the moment. */ -bool QtxActionMgr::SeparatorAction::addTo( QWidget* wid ) +QtxActionMgr::SeparatorAction::~SeparatorAction() { - if ( !wid ) - return false; - - bool res = true; - if ( wid->inherits( "QPopupMenu" ) ) - { - QPopupMenu* popup = (QPopupMenu*)wid; - myMenus[popup].append( popup->insertSeparator() ); - } - else if ( wid->inherits( "QToolBar" ) ) - { - QToolBar* tb = (QToolBar*)wid; - tb->addSeparator(); - myTools[tb].append( (QWidget*)tb->children()->getLast() ); - } - else - res = false; - - return res; } -/*! - Removes action from widget - \param wid - widget -*/ -bool QtxActionMgr::SeparatorAction::removeFrom( QWidget* wid ) -{ - if ( !wid ) - return false; - - bool res = true; - if ( wid->inherits( "QPopupMenu" ) ) - { - QPopupMenu* popup = (QPopupMenu*)wid; - if ( myMenus.contains( popup ) ) - { - const QIntList& list = myMenus[popup]; - for ( QIntList::const_iterator it = list.begin(); it != list.end(); ++it ) - popup->removeItem( *it ); - - myMenus.remove( popup ); - } - } - else if ( wid->inherits( "QToolBar" ) ) - { - QToolBar* tb = (QToolBar*)wid; - if ( myTools.contains( tb ) ) - { - QMap childMap; - if ( tb->children() ) - { - for ( QObjectListIt it( *tb->children() ); it.current(); ++it ) - childMap.insert( it.current(), 0 ); - } - const QWidgetList& list = myTools[tb]; - for ( QWidgetListIt it( list ); it.current(); ++it ) - { - if ( childMap.contains( it.current() ) ) - delete it.current(); - } - - myTools.remove( tb ); - } - } - else - res = false; - - return res; -} /*! - Class: QtxActionMgr - Level: Public + \class QtxActionMgr + \brief Manages a set of actions accessible by unique ID. + + Base class for menu, toolbar action containers and popup menu creators. */ /*! - Constructor + \brief Action manager constructor. + \param parent parent object */ QtxActionMgr::QtxActionMgr( QObject* parent ) : QObject( parent ), -myUpdate( true ) + myUpdate( true ), + myUpdTimer( 0 ) { } /*! - Destructor + \brief Action manager destructor. + + Does nothing for the moment. */ QtxActionMgr::~QtxActionMgr() { } /*! - Stores action in internal map - If action with such id is registered already, then it will be unregistered - \param a - action to be registered - \param userId - proposed id (if it is less than 0, then id will be generated automatically) + \brief Register action in the internal map. + + If \a userId is less than 0, the ID for the action is generated automatically. + If action with given \a userId is already registered, it will be re-registered. + + \param a action to be registered + \param userId action ID + \return action ID (the same as userId or generated one) */ int QtxActionMgr::registerAction( QAction* a, const int userId ) { @@ -201,8 +141,8 @@ int QtxActionMgr::registerAction( QAction* a, const int userId ) } /*! - Removes action from internal map - \param id - action id + \brief Unregister action from internal map. + \param id action ID */ void QtxActionMgr::unRegisterAction( const int id ) { @@ -211,8 +151,9 @@ void QtxActionMgr::unRegisterAction( const int id ) } /*! - \return action by id - \param id - action id + \brief Get action by \a id. + \param id action ID + \return action (0 if action is not found) */ QAction* QtxActionMgr::action( const int id ) const { @@ -223,8 +164,9 @@ QAction* QtxActionMgr::action( const int id ) const } /*! - \return id by action - \param a - action + \brief Get action ID. + \param a action + \return action ID (-1 if action is not found) */ int QtxActionMgr::actionId( const QAction* a ) const { @@ -234,7 +176,7 @@ int QtxActionMgr::actionId( const QAction* a ) const int theId = -1; for ( ActionMap::ConstIterator it = myActions.begin(); it != myActions.end() && theId == -1; ++it ) { - if ( it.data() == a ) + if ( it.value() == a ) theId = it.key(); } @@ -242,8 +184,9 @@ int QtxActionMgr::actionId( const QAction* a ) const } /*! - \return true if internal map contains such id - \param id - action id + \brief Check if an action with given \a id is registered in the action manager. + \param id action ID + \return \c true if internal map contains action with such ID */ bool QtxActionMgr::contains( const int id ) const { @@ -251,7 +194,8 @@ bool QtxActionMgr::contains( const int id ) const } /*! - \return count of actions in internal map + \brief Get number of registered actions. + \return number of actions in the internal map */ int QtxActionMgr::count() const { @@ -259,7 +203,8 @@ int QtxActionMgr::count() const } /*! - \return true if internal map is empty + \brief Check if there are no actions registered in the action manager. + \return \c true if internal map is empty */ bool QtxActionMgr::isEmpty() const { @@ -267,7 +212,8 @@ bool QtxActionMgr::isEmpty() const } /*! - Fills list with ids of registered actions + \brief Get all registered actions IDs. + \param list of actions IDs to be fiiled in */ void QtxActionMgr::idList( QIntList& lst ) const { @@ -275,7 +221,8 @@ void QtxActionMgr::idList( QIntList& lst ) const } /*! - \return true if updates are enabled + \brief Check if update is enabled. + \return \c true if update is enabled */ bool QtxActionMgr::isUpdatesEnabled() const { @@ -283,8 +230,8 @@ bool QtxActionMgr::isUpdatesEnabled() const } /*! - Enables/disables updates - \param upd - new state + \brief Enable/disable update operation. + \param upd new state */ void QtxActionMgr::setUpdatesEnabled( const bool upd ) { @@ -292,39 +239,62 @@ void QtxActionMgr::setUpdatesEnabled( const bool upd ) } /*! - \return true if action is visible (by default \return always true) + \brief Check if an action with given ID \a actId is visible to + the parent action with given ID \a place. + + Base implementatin always returns \c true. + + \param actId action ID + \param place some parent action ID + \return \c true if an action is visible */ -bool QtxActionMgr::isVisible( const int, const int ) const +bool QtxActionMgr::isVisible( const int /*actId*/, const int /*place*/ ) const { return true; } /*! - Sets visibility of action (by default, empty implementation) + \brief Set action visibility flag. + + Base implementatin does nothing. + + \param actId action ID + \param place some parent action ID + \param on visibility state */ -void QtxActionMgr::setVisible( const int, const int, const bool ) +void QtxActionMgr::setVisible( const int /*actId*/, const int /*place*/, const bool /*on*/ ) { } /*! - Updates actions, check isUpdatesEnabled() and call internalUpdate() + \brief Update actions. + + Does nothing if update is disabled. + \sa isUpdatesEnabled(), internalUpdate() */ void QtxActionMgr::update() { - if ( isUpdatesEnabled() ) - internalUpdate(); + if ( !isUpdatesEnabled() ) + return; + + internalUpdate(); + if ( myUpdTimer ) + myUpdTimer->stop(); } /*! - Real update (to be redefined in successors) + \brief Internal update. + + This method is called by update() function and should be redefined in successors. */ void QtxActionMgr::internalUpdate() { } /*! - \return global free id + \brief Generate unique action ID. + \return new ID */ int QtxActionMgr::generateId() const { @@ -333,8 +303,9 @@ int QtxActionMgr::generateId() const } /*! - \return true if action is enabled - \param id - action id + \brief Check is action with given \a id is enabled. + \param id action ID + \return \c true if action is enabled */ bool QtxActionMgr::isEnabled( const int id ) const { @@ -346,56 +317,113 @@ bool QtxActionMgr::isEnabled( const int id ) const } /*! - Enables/disables action - \param id - action id - \param en - new state + Enable/disable action with given \a id. + \param id action ID + \param enable new state */ -void QtxActionMgr::setEnabled( const int id, const bool en ) +void QtxActionMgr::setEnabled( const int id, const bool enable ) { QAction* a = action( id ); if ( a ) - a->setEnabled( en ); + a->setEnabled( enable ); } /*! - \return action for separator - If this action doesn't exist, then it will be created - \param individual - if it is false, then action will be shared, otherwise it will be created on every call + \brief Create new separator action. + + If \a own is \c true, that the caller is responsible for the action + deleting. If \a own is \c false, new separator action is owned by the + action manager which destroys it on application exit. + + \param own ownership flag + \return new separator action */ -QAction* QtxActionMgr::separator( const bool individual ) +QAction* QtxActionMgr::separator( const bool own ) { - if ( individual ) + if ( own ) return new SeparatorAction(); - if ( !qtx_separator_action ) - { - qtx_separator_action = new SeparatorAction(); + if ( qtx_separator_actions.isEmpty() ) qAddPostRoutine( qtxSeparatorActionCleanup ); + + SeparatorAction* a = new SeparatorAction(); + qtx_separator_actions.append( a ); + + return a; +} + +/*! + \brief Perform delayed update. + + Does nothing if update is disabled. +*/ +void QtxActionMgr::triggerUpdate() +{ + if ( !isUpdatesEnabled() ) + return; + + if ( !myUpdTimer ) + { + myUpdTimer = new QTimer( this ); + myUpdTimer->setSingleShot( true ); + connect( myUpdTimer, SIGNAL( timeout() ), this, SLOT( onUpdateContent() ) ); } - return qtx_separator_action; + myUpdTimer->stop(); + // add timer event to event list + myUpdTimer->start( 0 ); +} + +/*! + \brief Internal content update operation. + + Called automatically by onUpdateContent() when the delayed update + id triggered. Default implementation does nothing. + + \sa triggerUpdate(), onUpdateContent() +*/ +void QtxActionMgr::updateContent() +{} + +/*! + \brief Called when delayed update is performed (via timer event). + + Calls virtual method updateContent() which can customize the + content update operation. +*/ +void QtxActionMgr::onUpdateContent() +{ + updateContent(); } /*! - Class: QtxActionMgr::Reader - Level: Public + \class QtxActionMgr::Reader + \brief Generic actions description files reader class. + + This class is used to read files of some format to create actions + and fill an action manager with actions automatically. */ /*! - Constructor + \brief Reader constructor. + + Does nothing for the moment. */ QtxActionMgr::Reader::Reader() { } /*! - Destructor + \brief Reader destructor + + Does nothing for the moment. */ QtxActionMgr::Reader::~Reader() { } /*! - \return list of options + \brief Get list of options. + \return options list */ QStringList QtxActionMgr::Reader::options() const { @@ -403,9 +431,13 @@ QStringList QtxActionMgr::Reader::options() const } /*! - \return value of option - \param name - option name - \param def - default option value (is returned, if there is no such option) + \brief Get option value. + + If there is no such option the default value is returned. + + \param name option name + \param def default option value + \return option value */ QString QtxActionMgr::Reader::option( const QString& name, const QString& def ) const { @@ -416,18 +448,41 @@ QString QtxActionMgr::Reader::option( const QString& name, const QString& def ) } /*! - Sets value of option - \param name - option name - \param value - option value + \brief Set option value. + \param name option name + \param value new option value */ void QtxActionMgr::Reader::setOption( const QString& name, const QString& value ) { myOptions[ name ] = value; } +/*! + \fn bool QtxActionMgr::Reader::read( const QString& fname, Creator& cr ) const + \brief Read the file and fill and action manager with actions + by using help actions creator. + + Default implementation is pure virtual. + + \param fname XML file name + \param cr actions creator + \return \c true in success and \c false in case of error +*/ + +/*! + \class QtxActionMgr::XMLReader + \brief XML file reader. + + This class is used to read files of XML format to create + actions and fill an action manager with actions automatically. +*/ /*! - Constructor + \brief XML reader constructor. + + \param root root XML tag name + \param item menu item XML tag name + \param dir resources directory (containing icons, etc) */ QtxActionMgr::XMLReader::XMLReader( const QString& root, const QString& item, @@ -449,25 +504,29 @@ QtxActionMgr::XMLReader::XMLReader( const QString& root, } /*! - Destructor + \brief XML reader destructor. + + Does nothing for the moment. */ QtxActionMgr::XMLReader::~XMLReader() { } /*! - Reads file and fills action manager with help of creator - \param fname - file name - \param cr - creator + \brief Read the file and fill and action manager with actions + by using help actions creator. + \param fname XML file name + \param cr actions creator + \return \c true in success and \c false in case of error */ bool QtxActionMgr::XMLReader::read( const QString& fname, Creator& cr ) const { - bool res = false; + bool res = false; #ifndef QT_NO_DOM QFile file( fname ); - if ( !file.open( IO_ReadOnly ) ) + if ( !file.open( QFile::ReadOnly ) ) return res; QDomDocument doc; @@ -501,10 +560,10 @@ bool QtxActionMgr::XMLReader::read( const QString& fname, Creator& cr ) const } /*! - Create item by xml node - \param parent_node - parent node - \param parent_id - parent id - \param cr - creator + \brief Read XML mode and create an item if requied. + \param parent_node parent XML file node + \param parent_id parent action ID + \param cr actions creator */ void QtxActionMgr::XMLReader::read( const QDomNode& parent_node, const int parent_id, @@ -513,7 +572,7 @@ void QtxActionMgr::XMLReader::read( const QDomNode& parent_node, if( parent_node.isNull() ) return; - QStringList items = QStringList::split( "|", option( QString( "menu_item" ) ) ); + QStringList items = option( "menu_item" ).split( "|", QString::SkipEmptyParts ); const QDomNodeList& children = parent_node.childNodes(); for( int i=0, n=children.count(); ioption( "icons_dir", "." ) ); + QStringList dirlist = reader()->option( "icons_dir", "." ).split( ";", QString::SkipEmptyParts ); QStringList::const_iterator anIt = dirlist.begin(), aLast = dirlist.end(); bool res = false; @@ -651,3 +737,19 @@ bool QtxActionMgr::Creator::loadPixmap( const QString& fname, QPixmap& pix ) con return res; } + +/*! + \fn int QtxActionMgr::Creator::append( const QString& tag, + const bool subMenu, + const ItemAttributes& attr, + const int pId ) + \brief Create (and probably append to the action manager) new action. + + Default implementation is pure virtual. + + \param tag item tag name + \param subMenu \c true if this item is submenu + \param attr attributes map + \param pId parent action ID + \return item (for example action) ID +*/ diff --git a/src/Qtx/QtxActionMgr.h b/src/Qtx/QtxActionMgr.h index e0ba1d0e6..f3b265a20 100644 --- a/src/Qtx/QtxActionMgr.h +++ b/src/Qtx/QtxActionMgr.h @@ -24,24 +24,19 @@ #include "Qtx.h" -#include -#include -#include +#include +#include +#include +class QTimer; class QAction; class QDomNode; - #ifdef WIN32 #pragma warning( disable:4251 ) #endif -/*! - \class QtxActionMgr - Contains set of actions accessible by id. - Base class for menu, popup creators and other action containers. -*/ class QTX_EXPORT QtxActionMgr : public QObject { Q_OBJECT @@ -87,22 +82,25 @@ protected: virtual void internalUpdate(); int generateId() const; + void triggerUpdate(); + virtual void updateContent(); + +private slots: + void onUpdateContent(); + private: - typedef QGuardedPtr ActionPtr; + typedef QPointer ActionPtr; typedef QMap ActionMap; private: bool myUpdate; ActionMap myActions; + QTimer* myUpdTimer; }; QTX_EXPORT typedef QMap ItemAttributes; -/*! - \class QtxActionMgr::Creator - Allows to fill automatically action manager with actions created by data from file -*/ class QtxActionMgr::Creator { public: @@ -120,16 +118,11 @@ public: protected: static int intValue( const ItemAttributes&, const QString&, const int ); static QString strValue( const ItemAttributes&, const QString&, - const QString& = QString::null ); + const QString& = QString::null ); private: QtxActionMgr::Reader* myReader; }; -/*! - \class QtxActionMgr::Reader - This class is used to read files of some format - to create actions and to fill action manager automatically -*/ class QtxActionMgr::Reader { public: @@ -146,11 +139,6 @@ private: QMap< QString, QString > myOptions; }; -/*! - \class QtxActionMgr::Reader - This class is used to read files of XML format - to create actions and to fill action manager automatically -*/ class QtxActionMgr::XMLReader : public Reader { public: -- 2.39.2