1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
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.
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
19 // File: QtxActionMgr.cxx
20 // Author: Alexander SOLOVYEV, Sergey TELKOV
23 #include "QtxActionMgr.h"
24 #include "QtxAction.h"
28 #include <qpopupmenu.h>
29 #include <qwidgetlist.h>
30 #include <qobjectlist.h>
34 static QAction* qtx_separator_action = 0;
36 void qtxSeparatorActionCleanup()
38 delete qtx_separator_action;
39 qtx_separator_action = 0;
43 Class: QtxActionMenuMgr::SeparatorAction
47 class QtxActionMgr::SeparatorAction : public QtxAction
50 SeparatorAction( QObject* = 0 );
51 virtual ~SeparatorAction();
53 virtual bool addTo( QWidget* );
54 virtual bool removeFrom( QWidget* );
57 QMap<QPopupMenu*, QIntList> myMenus;
58 QMap<QToolBar*, QWidgetList> myTools;
64 QtxActionMgr::SeparatorAction::SeparatorAction( QObject* parent )
72 QtxActionMgr::SeparatorAction::~SeparatorAction()
80 bool QtxActionMgr::SeparatorAction::addTo( QWidget* wid )
86 if ( wid->inherits( "QPopupMenu" ) )
88 QPopupMenu* popup = (QPopupMenu*)wid;
89 myMenus[popup].append( popup->insertSeparator() );
91 else if ( wid->inherits( "QToolBar" ) )
93 QToolBar* tb = (QToolBar*)wid;
95 myTools[tb].append( (QWidget*)tb->children()->getLast() );
104 Removes action from widget
107 bool QtxActionMgr::SeparatorAction::removeFrom( QWidget* wid )
113 if ( wid->inherits( "QPopupMenu" ) )
115 QPopupMenu* popup = (QPopupMenu*)wid;
116 if ( myMenus.contains( popup ) )
118 const QIntList& list = myMenus[popup];
119 for ( QIntList::const_iterator it = list.begin(); it != list.end(); ++it )
120 popup->removeItem( *it );
122 myMenus.remove( popup );
125 else if ( wid->inherits( "QToolBar" ) )
127 QToolBar* tb = (QToolBar*)wid;
128 if ( myTools.contains( tb ) )
130 QMap<QObject*, int> childMap;
131 if ( tb->children() )
133 for ( QObjectListIt it( *tb->children() ); it.current(); ++it )
134 childMap.insert( it.current(), 0 );
136 const QWidgetList& list = myTools[tb];
137 for ( QWidgetListIt it( list ); it.current(); ++it )
139 if ( childMap.contains( it.current() ) )
143 myTools.remove( tb );
160 QtxActionMgr::QtxActionMgr( QObject* parent )
169 QtxActionMgr::~QtxActionMgr()
174 Stores action in internal map
175 If action with such id is registered already, then it will be unregistered
176 \param a - action to be registered
177 \param userId - proposed id (if it is less than 0, then id will be generated automatically)
179 int QtxActionMgr::registerAction( QAction* a, const int userId )
184 int theId = userId < 0 ? generateId() : userId;
186 if ( contains( theId ) )
187 unRegisterAction( theId );
189 int cur = actionId( a );
195 unRegisterAction( cur );
198 myActions.insert( theId, a );
204 Removes action from internal map
205 \param id - action id
207 void QtxActionMgr::unRegisterAction( const int id )
210 myActions.remove( id );
215 \param id - action id
217 QAction* QtxActionMgr::action( const int id ) const
219 if ( contains( id ) )
220 return myActions[ id ];
229 int QtxActionMgr::actionId( const QAction* a ) const
235 for ( ActionMap::ConstIterator it = myActions.begin(); it != myActions.end() && theId == -1; ++it )
237 if ( it.data() == a )
245 \return true if internal map contains such id
246 \param id - action id
248 bool QtxActionMgr::contains( const int id ) const
250 return myActions.contains( id );
254 \return count of actions in internal map
256 int QtxActionMgr::count() const
258 return myActions.count();
262 \return true if internal map is empty
264 bool QtxActionMgr::isEmpty() const
266 return myActions.isEmpty();
270 Fills list with ids of registered actions
272 void QtxActionMgr::idList( QIntList& lst ) const
274 lst = myActions.keys();
278 \return true if updates are enabled
280 bool QtxActionMgr::isUpdatesEnabled() const
286 Enables/disables updates
287 \param upd - new state
289 void QtxActionMgr::setUpdatesEnabled( const bool upd )
295 \return true if action is visible (by default \return always true)
297 bool QtxActionMgr::isVisible( const int, const int ) const
303 Sets visibility of action (by default, empty implementation)
305 void QtxActionMgr::setVisible( const int, const int, const bool )
310 Updates actions, check isUpdatesEnabled() and call internalUpdate()
311 \sa isUpdatesEnabled(), internalUpdate()
313 void QtxActionMgr::update()
315 if ( isUpdatesEnabled() )
320 Real update (to be redefined in successors)
322 void QtxActionMgr::internalUpdate()
327 \return global free id
329 int QtxActionMgr::generateId() const
336 \return true if action is enabled
337 \param id - action id
339 bool QtxActionMgr::isEnabled( const int id ) const
341 QAction* a = action( id );
343 return a->isEnabled();
349 Enables/disables action
350 \param id - action id
351 \param en - new state
353 void QtxActionMgr::setEnabled( const int id, const bool en )
355 QAction* a = action( id );
361 \return action for separator
362 If this action doesn't exist, then it will be created
363 \param individual - if it is false, then action will be shared, otherwise it will be created on every call
365 QAction* QtxActionMgr::separator( const bool individual )
368 return new SeparatorAction();
370 if ( !qtx_separator_action )
372 qtx_separator_action = new SeparatorAction();
373 qAddPostRoutine( qtxSeparatorActionCleanup );
375 return qtx_separator_action;
379 Class: QtxActionMgr::Reader
386 QtxActionMgr::Reader::Reader()
393 QtxActionMgr::Reader::~Reader()
398 \return list of options
400 QStringList QtxActionMgr::Reader::options() const
402 return myOptions.keys();
406 \return value of option
407 \param name - option name
408 \param def - default option value (is returned, if there is no such option)
410 QString QtxActionMgr::Reader::option( const QString& name, const QString& def ) const
412 if( myOptions.contains( name ) )
413 return myOptions[ name ];
420 \param name - option name
421 \param value - option value
423 void QtxActionMgr::Reader::setOption( const QString& name, const QString& value )
425 myOptions[ name ] = value;
432 QtxActionMgr::XMLReader::XMLReader( const QString& root,
437 setOption( QString( "root_tag" ), root );
438 setOption( QString( "menu_item" ), item );
439 setOption( QString( "icons_dir" ), dir );
440 setOption( QString( "id" ), QString( "item-id" ) );
441 setOption( QString( "pos" ), QString( "pos-id" ) );
442 setOption( QString( "group" ), QString( "group-id" ) );
443 setOption( QString( "label" ), QString( "label-id" ) );
444 setOption( QString( "tooltip" ), QString( "tooltip-id" ) );
445 setOption( QString( "accel" ), QString( "accel-id" ) );
446 setOption( QString( "separator" ), QString( "separator" ) );
447 setOption( QString( "icon" ), QString( "icon-id" ) );
448 setOption( QString( "toggle" ), QString( "toggle-id" ) );
454 QtxActionMgr::XMLReader::~XMLReader()
459 Reads file and fills action manager with help of creator
460 \param fname - file name
463 bool QtxActionMgr::XMLReader::read( const QString& fname, Creator& cr ) const
470 if ( !file.open( IO_ReadOnly ) )
475 res = doc.setContent( &file );
481 QString root = option( "root_tag" );
482 for( QDomNode cur = doc.documentElement(); !cur.isNull(); )
484 if( cur.isElement() && isNodeSimilar( cur, root ) )
486 else if( cur.hasChildNodes() )
488 cur = cur.firstChild();
492 while( !cur.isNull() && cur.nextSibling().isNull() )
493 cur = cur.parentNode();
495 cur = cur.nextSibling();
504 Create item by xml node
505 \param parent_node - parent node
506 \param parent_id - parent id
509 void QtxActionMgr::XMLReader::read( const QDomNode& parent_node,
513 if( parent_node.isNull() )
516 QStringList items = QStringList::split( "|", option( QString( "menu_item" ) ) );
518 const QDomNodeList& children = parent_node.childNodes();
519 for( int i=0, n=children.count(); i<n; i++ )
521 QDomNode node = children.item( i );
522 //QString n = node.nodeName();
523 if( node.isElement() /*&& node.hasAttributes()*/ &&
524 ( items.contains( node.nodeName() ) || node.nodeName()==option( "separator" ) ) )
526 QDomNamedNodeMap map = node.attributes();
527 ItemAttributes attrs;
529 for( int i=0, n=map.count(); i<n; i++ )
530 if( map.item( i ).isAttr() )
532 QDomAttr a = map.item( i ).toAttr();
533 attrs.insert( a.name(), a.value() );
536 int newId = cr.append( node.nodeName(), node.hasChildNodes(), attrs, parent_id );
537 if( node.hasChildNodes() )
538 read( node, newId, cr );
544 \return true if node satisfies pattern
546 bool QtxActionMgr::XMLReader::isNodeSimilar( const QDomNode& node,
547 const QString& pattern ) const
549 if( node.nodeName()==pattern )
554 temp.setContent( pattern, true, &mes );
556 const QDomNamedNodeMap &temp_map = temp.documentElement().attributes(),
557 &cur_map = node.attributes();
558 bool ok = temp_map.count()>0;
559 for( int i=0, n=temp_map.count(); i<n && ok; i++ )
561 QDomAttr a = temp_map.item( i ).toAttr(),
562 b = cur_map.namedItem( a.name() ).toAttr();
563 ok = !b.isNull() && a.name()==b.name() && a.value()==b.value();
571 \return integer value by attributes
572 \param attrs - attributes
573 \param name - name of attribute
574 \param def - default value (is returned on fail)
576 int QtxActionMgr::Creator::intValue( const ItemAttributes& attrs,
577 const QString& name, int def )
579 if( attrs.contains( name ) )
582 int res = attrs[ name ].toInt( &ok );
590 \return string value by attributes
591 \param attrs - attributes
592 \param name - name of attribute
593 \param def - default value (is returned on fail)
595 QString QtxActionMgr::Creator::strValue( const ItemAttributes& attrs,
599 if( attrs.contains( name ) )
600 return attrs[ name ];
608 QtxActionMgr::Creator::Creator( QtxActionMgr::Reader* r )
616 QtxActionMgr::Creator::~Creator()
621 \return corresponding reader
623 QtxActionMgr::Reader* QtxActionMgr::Creator::reader() const
629 Connects action to some slots (default implementation is empty)
631 void QtxActionMgr::Creator::connect( QAction* ) const
637 \param fname - file name
638 \param pix - to return loaded pixmap
640 bool QtxActionMgr::Creator::loadPixmap( const QString& fname, QPixmap& pix ) const
645 QStringList dirlist = QStringList::split( ";", reader()->option( "icons_dir", "." ) );
646 QStringList::const_iterator anIt = dirlist.begin(),
647 aLast = dirlist.end();
649 for( ; anIt!=aLast && !res; anIt++ )
650 res = pix.load( Qtx::addSlash( *anIt ) + fname );