// 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
#include "QtxActionMgr.h"
#include "QtxAction.h"
-#include <qwidget.h>
-#include <qtoolbar.h>
-#include <qpopupmenu.h>
-#include <qwidgetlist.h>
-#include <qobjectlist.h>
-#include <qfile.h>
-#include <qdom.h>
+#include <QtCore/qfile.h>
+#include <QtCore/qtimer.h>
+
+#include <QtGui/qmenu.h>
+#include <QtGui/qwidget.h>
+#include <QtGui/qtoolbar.h>
+#include <QtGui/qapplication.h>
+
+#include <QtXml/qdom.h>
-static QAction* qtx_separator_action = 0;
+typedef QList< QPointer<QAction> > 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
public:
SeparatorAction( QObject* = 0 );
virtual ~SeparatorAction();
-
- virtual bool addTo( QWidget* );
- virtual bool removeFrom( QWidget* );
-
-private:
- QMap<QPopupMenu*, QIntList> myMenus;
- QMap<QToolBar*, QWidgetList> 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<QObject*, int> 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 )
{
}
/*!
- 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 )
{
}
/*!
- \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
{
}
/*!
- \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
{
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();
}
}
/*!
- \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
{
}
/*!
- \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
{
}
/*!
- \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
{
}
/*!
- 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
{
}
/*!
- \return true if updates are enabled
+ \brief Check if update is enabled.
+ \return \c true if update is enabled
*/
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 )
{
}
/*!
- \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
{
}
/*!
- \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
{
}
/*!
- 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
{
}
/*!
- \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
{
}
/*!
- 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,
}
/*!
- 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;
}
/*!
- 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,
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(); i<n; i++ )
}
/*!
- \return true if node satisfies pattern
+ \brief Check node name correspondance to some pattern.
+ \param node XML file node
+ \param pattern node name pattern
+ \return \c true if node satisfies pattern
*/
bool QtxActionMgr::XMLReader::isNodeSimilar( const QDomNode& node,
const QString& pattern ) const
{
if( node.nodeName()==pattern )
return true;
-
+
QDomDocument temp;
QString mes;
temp.setContent( pattern, true, &mes );
return ok;
}
+/*!
+ \class QtxActionMgr::Creator
+ \brief Generic actions creator class.
+
+ Used by Reader to create actions and fill in the action
+ manager with the actions.
+*/
/*!
- \return integer value by attributes
- \param attrs - attributes
- \param name - name of attribute
- \param def - default value (is returned on fail)
+ \brief Get integer attribute value from the attribute map.
+
+ Returns default value if the attribute is not found.
+
+ \param attrs attributes map
+ \param name attribute name
+ \param def default attribute value
+ \return attribute value
*/
int QtxActionMgr::Creator::intValue( const ItemAttributes& attrs,
const QString& name, int def )
{
if( attrs.contains( name ) )
{
- bool ok;
+ bool ok;
int res = attrs[ name ].toInt( &ok );
if( ok )
return res;
}
/*!
- \return string value by attributes
- \param attrs - attributes
- \param name - name of attribute
- \param def - default value (is returned on fail)
+ \brief Get string attribute value from the attribute map.
+
+ Returns default value if the attribute is not found.
+
+ \param attrs attributes map
+ \param name attribute name
+ \param def default attribute value
+ \return attribute value
*/
QString QtxActionMgr::Creator::strValue( const ItemAttributes& attrs,
const QString& name,
}
/*!
- Constructor
+ \brief Creator constructor.
+ \param r action reader
*/
QtxActionMgr::Creator::Creator( QtxActionMgr::Reader* r )
: myReader( r )
}
/*!
- Destructor
+ \brief Creator destructor.
+
+ Does nothing for the moment.
*/
QtxActionMgr::Creator::~Creator()
{
}
/*!
- \return corresponding reader
+ \brief Get actions reader.
+ \return actions reader
*/
QtxActionMgr::Reader* QtxActionMgr::Creator::reader() const
{
}
/*!
- Connects action to some slots (default implementation is empty)
+ \brief Connect action to some specific slot(s)
+
+ Default implementation does nothing.
+
+ \param a action
*/
-void QtxActionMgr::Creator::connect( QAction* ) const
+void QtxActionMgr::Creator::connect( QAction* /*a*/ ) const
{
}
/*!
- Loads pixmap
- \param fname - file name
- \param pix - to return loaded pixmap
+ \brief Load pixmap from the file.
+ \param fname file name
+ \param pix uaed to return pixmap
+ \return \c true if pixmap is loaded successfully and \c false in case of error
*/
bool QtxActionMgr::Creator::loadPixmap( const QString& fname, QPixmap& pix ) const
{
if( !reader() )
return false;
- QStringList dirlist = QStringList::split( ";", reader()->option( "icons_dir", "." ) );
+ QStringList dirlist = reader()->option( "icons_dir", "." ).split( ";", QString::SkipEmptyParts );
QStringList::const_iterator anIt = dirlist.begin(),
aLast = dirlist.end();
bool res = false;
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
+*/