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/
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;
61 QtxActionMgr::SeparatorAction::SeparatorAction( QObject* parent )
66 QtxActionMgr::SeparatorAction::~SeparatorAction()
70 bool QtxActionMgr::SeparatorAction::addTo( QWidget* wid )
76 if ( wid->inherits( "QPopupMenu" ) )
78 QPopupMenu* popup = (QPopupMenu*)wid;
79 myMenus[popup].append( popup->insertSeparator() );
81 else if ( wid->inherits( "QToolBar" ) )
83 QToolBar* tb = (QToolBar*)wid;
85 myTools[tb].append( (QWidget*)tb->children()->getLast() );
93 bool QtxActionMgr::SeparatorAction::removeFrom( QWidget* wid )
99 if ( wid->inherits( "QPopupMenu" ) )
101 QPopupMenu* popup = (QPopupMenu*)wid;
102 if ( myMenus.contains( popup ) )
104 const QIntList& list = myMenus[popup];
105 for ( QIntList::const_iterator it = list.begin(); it != list.end(); ++it )
106 popup->removeItem( *it );
108 myMenus.remove( popup );
111 else if ( wid->inherits( "QToolBar" ) )
113 QToolBar* tb = (QToolBar*)wid;
114 if ( myTools.contains( tb ) )
116 QMap<QObject*, int> childMap;
117 if ( tb->children() )
119 for ( QObjectListIt it( *tb->children() ); it.current(); ++it )
120 childMap.insert( it.current(), 0 );
122 const QWidgetList& list = myTools[tb];
123 for ( QWidgetListIt it( list ); it.current(); ++it )
125 if ( childMap.contains( it.current() ) )
129 myTools.remove( tb );
143 QtxActionMgr::QtxActionMgr( QObject* parent )
149 QtxActionMgr::~QtxActionMgr()
153 int QtxActionMgr::registerAction( QAction* a, const int userId )
158 int theId = userId < 0 ? generateId() : userId;
160 if ( contains( theId ) )
161 unRegisterAction( theId );
163 int cur = actionId( a );
169 unRegisterAction( cur );
172 myActions.insert( theId, a );
177 void QtxActionMgr::unRegisterAction( const int id )
180 myActions.remove( id );
183 QAction* QtxActionMgr::action( const int id ) const
185 if ( contains( id ) )
186 return myActions[ id ];
191 int QtxActionMgr::actionId( const QAction* a ) const
197 for ( ActionMap::ConstIterator it = myActions.begin(); it != myActions.end() && theId == -1; ++it )
199 if ( it.data() == a )
206 bool QtxActionMgr::contains( const int id ) const
208 return myActions.contains( id );
211 int QtxActionMgr::count() const
213 return myActions.count();
216 bool QtxActionMgr::isEmpty() const
218 return myActions.isEmpty();
221 void QtxActionMgr::idList( QIntList& lst ) const
223 lst = myActions.keys();
226 bool QtxActionMgr::isUpdatesEnabled() const
231 void QtxActionMgr::setUpdatesEnabled( const bool upd )
236 bool QtxActionMgr::isVisible( const int, const int ) const
241 void QtxActionMgr::setVisible( const int, const int, const bool )
245 void QtxActionMgr::update()
247 if ( isUpdatesEnabled() )
251 void QtxActionMgr::internalUpdate()
255 int QtxActionMgr::generateId() const
261 bool QtxActionMgr::isEnabled( const int id ) const
263 QAction* a = action( id );
265 return a->isEnabled();
270 void QtxActionMgr::setEnabled( const int id, const bool en )
272 QAction* a = action( id );
277 QAction* QtxActionMgr::separator( const bool individual )
280 return new SeparatorAction();
282 if ( !qtx_separator_action )
284 qtx_separator_action = new SeparatorAction();
285 qAddPostRoutine( qtxSeparatorActionCleanup );
287 return qtx_separator_action;
291 Class: QtxActionMgr::Reader
295 QtxActionMgr::Reader::Reader()
299 QtxActionMgr::Reader::~Reader()
303 QStringList QtxActionMgr::Reader::options() const
305 return myOptions.keys();
308 QString QtxActionMgr::Reader::option( const QString& name, const QString& def ) const
310 if( myOptions.contains( name ) )
311 return myOptions[ name ];
316 void QtxActionMgr::Reader::setOption( const QString& name, const QString& value )
318 myOptions[ name ] = value;
323 Class: QtxActionMgr::XMLReader
326 QtxActionMgr::XMLReader::XMLReader( const QString& root,
331 setOption( QString( "root_tag" ), root );
332 setOption( QString( "menu_item" ), item );
333 setOption( QString( "icons_dir" ), dir );
334 setOption( QString( "id" ), QString( "item-id" ) );
335 setOption( QString( "pos" ), QString( "pos-id" ) );
336 setOption( QString( "group" ), QString( "group-id" ) );
337 setOption( QString( "label" ), QString( "label-id" ) );
338 setOption( QString( "tooltip" ), QString( "tooltip-id" ) );
339 setOption( QString( "accel" ), QString( "accel-id" ) );
340 setOption( QString( "separator" ), QString( "separator" ) );
341 setOption( QString( "icon" ), QString( "icon-id" ) );
342 setOption( QString( "toggle" ), QString( "toggle-id" ) );
345 QtxActionMgr::XMLReader::~XMLReader()
349 bool QtxActionMgr::XMLReader::read( const QString& fname, Creator& cr ) const
356 if ( !file.open( IO_ReadOnly ) )
361 res = doc.setContent( &file );
367 QString root = option( "root_tag" );
368 for( QDomNode cur = doc.documentElement(); !cur.isNull(); )
370 if( cur.isElement() && isNodeSimilar( cur, root ) )
372 else if( cur.hasChildNodes() )
374 cur = cur.firstChild();
378 while( !cur.isNull() && cur.nextSibling().isNull() )
379 cur = cur.parentNode();
381 cur = cur.nextSibling();
389 void QtxActionMgr::XMLReader::read( const QDomNode& parent_node,
393 if( parent_node.isNull() )
396 QStringList items = QStringList::split( "|", option( QString( "menu_item" ) ) );
398 const QDomNodeList& children = parent_node.childNodes();
399 for( int i=0, n=children.count(); i<n; i++ )
401 QDomNode node = children.item( i );
402 //QString n = node.nodeName();
403 if( node.isElement() /*&& node.hasAttributes()*/ &&
404 ( items.contains( node.nodeName() ) || node.nodeName()==option( "separator" ) ) )
406 QDomNamedNodeMap map = node.attributes();
407 ItemAttributes attrs;
409 for( int i=0, n=map.count(); i<n; i++ )
410 if( map.item( i ).isAttr() )
412 QDomAttr a = map.item( i ).toAttr();
413 attrs.insert( a.name(), a.value() );
416 int newId = cr.append( node.nodeName(), node.hasChildNodes(), attrs, parent_id );
417 if( node.hasChildNodes() )
418 read( node, newId, cr );
423 bool QtxActionMgr::XMLReader::isNodeSimilar( const QDomNode& node,
424 const QString& pattern ) const
426 if( node.nodeName()==pattern )
431 temp.setContent( pattern, true, &mes );
433 const QDomNamedNodeMap &temp_map = temp.documentElement().attributes(),
434 &cur_map = node.attributes();
435 bool ok = temp_map.count()>0;
436 for( int i=0, n=temp_map.count(); i<n && ok; i++ )
438 QDomAttr a = temp_map.item( i ).toAttr(),
439 b = cur_map.namedItem( a.name() ).toAttr();
440 ok = !b.isNull() && a.name()==b.name() && a.value()==b.value();
448 Class: QtxActionMgr::Creator
451 int QtxActionMgr::Creator::intValue( const ItemAttributes& attrs,
452 const QString& name, int def )
454 if( attrs.contains( name ) )
457 int res = attrs[ name ].toInt( &ok );
464 QString QtxActionMgr::Creator::strValue( const ItemAttributes& attrs,
468 if( attrs.contains( name ) )
469 return attrs[ name ];
474 QtxActionMgr::Creator::Creator( QtxActionMgr::Reader* r )
479 QtxActionMgr::Creator::~Creator()
483 QtxActionMgr::Reader* QtxActionMgr::Creator::reader() const
488 void QtxActionMgr::Creator::connect( QAction* ) const
492 bool QtxActionMgr::Creator::loadPixmap( const QString& fname, QPixmap& pix ) const
497 QStringList dirlist = QStringList::split( ";", reader()->option( "icons_dir", "." ) );
498 QStringList::const_iterator anIt = dirlist.begin(),
499 aLast = dirlist.end();
501 for( ; anIt!=aLast && !res; anIt++ )
502 res = pix.load( Qtx::addSlash( *anIt ) + fname );