1 // File: QtxActionMgr.cxx
2 // Author: Alexander SOLOVYEV, Sergey TELKOV
5 #include "QtxActionMgr.h"
10 #include <qpopupmenu.h>
11 #include <qwidgetlist.h>
12 #include <qobjectlist.h>
16 static QAction* qtx_separator_action = 0;
18 void qtxSeparatorActionCleanup()
20 delete qtx_separator_action;
21 qtx_separator_action = 0;
25 Class: QtxActionMenuMgr::SeparatorAction
29 class QtxActionMgr::SeparatorAction : public QtxAction
32 SeparatorAction( QObject* = 0 );
33 virtual ~SeparatorAction();
35 virtual bool addTo( QWidget* );
36 virtual bool removeFrom( QWidget* );
39 QMap<QPopupMenu*, QIntList> myMenus;
40 QMap<QToolBar*, QWidgetList> myTools;
43 QtxActionMgr::SeparatorAction::SeparatorAction( QObject* parent )
48 QtxActionMgr::SeparatorAction::~SeparatorAction()
52 bool QtxActionMgr::SeparatorAction::addTo( QWidget* wid )
58 if ( wid->inherits( "QPopupMenu" ) )
60 QPopupMenu* popup = (QPopupMenu*)wid;
61 myMenus[popup].append( popup->insertSeparator() );
63 else if ( wid->inherits( "QToolBar" ) )
65 QToolBar* tb = (QToolBar*)wid;
67 myTools[tb].append( (QWidget*)tb->children()->getLast() );
75 bool QtxActionMgr::SeparatorAction::removeFrom( QWidget* wid )
81 if ( wid->inherits( "QPopupMenu" ) )
83 QPopupMenu* popup = (QPopupMenu*)wid;
84 if ( myMenus.contains( popup ) )
86 const QIntList& list = myMenus[popup];
87 for ( QIntList::const_iterator it = list.begin(); it != list.end(); ++it )
88 popup->removeItem( *it );
90 myMenus.remove( popup );
93 else if ( wid->inherits( "QToolBar" ) )
95 QToolBar* tb = (QToolBar*)wid;
96 if ( myTools.contains( tb ) )
98 QMap<QObject*, int> childMap;
101 for ( QObjectListIt it( *tb->children() ); it.current(); ++it )
102 childMap.insert( it.current(), 0 );
104 const QWidgetList& list = myTools[tb];
105 for ( QWidgetListIt it( list ); it.current(); ++it )
107 if ( childMap.contains( it.current() ) )
111 myTools.remove( tb );
125 QtxActionMgr::QtxActionMgr( QObject* parent )
131 QtxActionMgr::~QtxActionMgr()
135 int QtxActionMgr::registerAction( QAction* a, const int userId )
140 int theId = userId < 0 ? generateId() : userId;
142 if ( contains( theId ) )
143 unRegisterAction( theId );
145 int cur = actionId( a );
151 unRegisterAction( cur );
154 myActions.insert( theId, a );
159 void QtxActionMgr::unRegisterAction( const int id )
162 myActions.remove( id );
165 QAction* QtxActionMgr::action( const int id ) const
167 if ( contains( id ) )
168 return myActions[ id ];
173 int QtxActionMgr::actionId( const QAction* a ) const
179 for ( ActionMap::ConstIterator it = myActions.begin(); it != myActions.end() && theId == -1; ++it )
181 if ( it.data() == a )
188 bool QtxActionMgr::contains( const int id ) const
190 return myActions.contains( id );
193 int QtxActionMgr::count() const
195 return myActions.count();
198 bool QtxActionMgr::isEmpty() const
200 return myActions.isEmpty();
203 void QtxActionMgr::idList( QIntList& lst ) const
205 lst = myActions.keys();
208 bool QtxActionMgr::isUpdatesEnabled() const
213 void QtxActionMgr::setUpdatesEnabled( const bool upd )
218 bool QtxActionMgr::isVisible( const int, const int ) const
223 void QtxActionMgr::setVisible( const int, const int, const bool )
227 void QtxActionMgr::update()
229 if ( isUpdatesEnabled() )
233 void QtxActionMgr::internalUpdate()
237 int QtxActionMgr::generateId() const
243 bool QtxActionMgr::isEnabled( const int id ) const
245 QAction* a = action( id );
247 return a->isEnabled();
252 void QtxActionMgr::setEnabled( const int id, const bool en )
254 QAction* a = action( id );
259 QAction* QtxActionMgr::separator( const bool individual )
262 return new SeparatorAction();
264 if ( !qtx_separator_action )
266 qtx_separator_action = new SeparatorAction();
267 qAddPostRoutine( qtxSeparatorActionCleanup );
269 return qtx_separator_action;
273 Class: QtxActionMgr::Reader
277 QtxActionMgr::Reader::Reader()
281 QtxActionMgr::Reader::~Reader()
285 QStringList QtxActionMgr::Reader::options() const
287 return myOptions.keys();
290 QString QtxActionMgr::Reader::option( const QString& name, const QString& def ) const
292 if( myOptions.contains( name ) )
293 return myOptions[ name ];
298 void QtxActionMgr::Reader::setOption( const QString& name, const QString& value )
300 myOptions[ name ] = value;
305 Class: QtxActionMgr::XMLReader
308 QtxActionMgr::XMLReader::XMLReader( const QString& root,
313 setOption( QString( "root_tag" ), root );
314 setOption( QString( "menu_item" ), item );
315 setOption( QString( "icons_dir" ), dir );
316 setOption( QString( "id" ), QString( "item-id" ) );
317 setOption( QString( "pos" ), QString( "pos-id" ) );
318 setOption( QString( "group" ), QString( "group-id" ) );
319 setOption( QString( "label" ), QString( "label-id" ) );
320 setOption( QString( "tooltip" ), QString( "tooltip-id" ) );
321 setOption( QString( "accel" ), QString( "accel-id" ) );
322 setOption( QString( "separator" ), QString( "separator" ) );
323 setOption( QString( "icon" ), QString( "icon-id" ) );
324 setOption( QString( "toggle" ), QString( "toggle-id" ) );
327 QtxActionMgr::XMLReader::~XMLReader()
331 bool QtxActionMgr::XMLReader::read( const QString& fname, Creator& cr ) const
338 if ( !file.open( IO_ReadOnly ) )
343 res = doc.setContent( &file );
349 QString root = option( "root_tag" );
350 for( QDomNode cur = doc.documentElement(); !cur.isNull(); )
352 if( cur.isElement() && isNodeSimilar( cur, root ) )
354 else if( cur.hasChildNodes() )
356 cur = cur.firstChild();
360 while( !cur.isNull() && cur.nextSibling().isNull() )
361 cur = cur.parentNode();
363 cur = cur.nextSibling();
371 void QtxActionMgr::XMLReader::read( const QDomNode& parent_node,
375 if( parent_node.isNull() )
378 QStringList items = QStringList::split( "|", option( QString( "menu_item" ) ) );
380 const QDomNodeList& children = parent_node.childNodes();
381 for( int i=0, n=children.count(); i<n; i++ )
383 QDomNode node = children.item( i );
384 QString n = node.nodeName();
385 if( node.isElement() /*&& node.hasAttributes()*/ &&
386 ( items.contains( node.nodeName() ) || node.nodeName()==option( "separator" ) ) )
388 QDomNamedNodeMap map = node.attributes();
389 ItemAttributes attrs;
391 for( int i=0, n=map.count(); i<n; i++ )
392 if( map.item( i ).isAttr() )
394 QDomAttr a = map.item( i ).toAttr();
395 attrs.insert( a.name(), a.value() );
398 int newId = cr.append( node.nodeName(), node.hasChildNodes(), attrs, parent_id );
399 if( node.hasChildNodes() )
400 read( node, newId, cr );
405 bool QtxActionMgr::XMLReader::isNodeSimilar( const QDomNode& node,
406 const QString& pattern ) const
408 if( node.nodeName()==pattern )
413 temp.setContent( pattern, true, &mes );
415 const QDomNamedNodeMap &temp_map = temp.documentElement().attributes(),
416 &cur_map = node.attributes();
417 bool ok = temp_map.count()>0;
418 for( int i=0, n=temp_map.count(); i<n && ok; i++ )
420 QDomAttr a = temp_map.item( i ).toAttr(),
421 b = cur_map.namedItem( a.name() ).toAttr();
422 ok = !b.isNull() && a.name()==b.name() && a.value()==b.value();
430 Class: QtxActionMgr::Creator
433 int QtxActionMgr::Creator::intValue( const ItemAttributes& attrs,
434 const QString& name, int def )
436 if( attrs.contains( name ) )
439 int res = attrs[ name ].toInt( &ok );
446 QString QtxActionMgr::Creator::strValue( const ItemAttributes& attrs,
450 if( attrs.contains( name ) )
451 return attrs[ name ];
456 QtxActionMgr::Creator::Creator( QtxActionMgr::Reader* r )
461 QtxActionMgr::Creator::~Creator()
465 QtxActionMgr::Reader* QtxActionMgr::Creator::reader() const
470 void QtxActionMgr::Creator::connect( QAction* ) const
474 bool QtxActionMgr::Creator::loadPixmap( const QString& fname, QPixmap& pix ) const
479 QStringList dirlist = QStringList::split( ";", reader()->option( "icons_dir", "." ) );
480 QStringList::const_iterator anIt = dirlist.begin(),
481 aLast = dirlist.end();
483 for( ; anIt!=aLast && !res; anIt++ )
484 res = pix.load( Qtx::addSlash( *anIt ) + fname );