1 // File: QtxActionToolMgr.cxx
2 // Author: Alexander SOLOVYEV, Sergey TELKOV
4 #include "QtxActionToolMgr.h"
7 #include "QtxToolBar.h"
9 #include <qmainwindow.h>
10 #include <qobjectlist.h>
12 QtxActionToolMgr::QtxActionToolMgr( QMainWindow* p )
18 QtxActionToolMgr::~QtxActionToolMgr()
22 QMainWindow* QtxActionToolMgr::mainWindow() const
27 int QtxActionToolMgr::createToolBar( const QString& name, const int tid )
29 static int _toolBarId = -1;
32 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && tbId == -1; ++it )
34 if ( it.data().toolBar->label().lower() == name.lower() )
41 QToolBar* tb = find( name, mainWindow() );
43 tbId = tid < 0 ? --_toolBarId : tid;
45 myToolBars.insert( tbId, ToolBarInfo() );
46 ToolBarInfo& tInfo = myToolBars[tbId];
50 tb = new QtxToolBar( true, mainWindow() );
55 connect( tInfo.toolBar, SIGNAL( destroyed() ), this, SLOT( onToolBarDestroyed() ) );
60 QToolBar* QtxActionToolMgr::find( const QString& label, QMainWindow* mw ) const
65 QString pattern = label.lower();
68 QPtrList<QDockWindow> lst = mw->dockWindows();
69 for ( QPtrListIterator<QDockWindow> it( lst ); it.current() && !res; ++it )
71 if ( !it.current()->inherits( "QToolBar" ) )
74 QToolBar* cur = (QToolBar*)it.current();
75 if ( cur->label().lower() == pattern )
81 void QtxActionToolMgr::removeToolBar( const int tid )
83 if ( !myToolBars.contains( tid ) )
86 delete myToolBars[tid].toolBar;
87 myToolBars.remove( tid );
90 void QtxActionToolMgr::removeToolBar( const QString& tname )
92 removeToolBar( find( tname ) );
95 int QtxActionToolMgr::insert( const int id, const int tid, const int idx )
97 if ( !contains( id ) || !hasToolBar( tid ) )
103 NodeList& list = myToolBars[tid].nodes;
104 int index = idx < 0 ? list.count() : QMIN( idx, (int)list.count() );
105 list.insert( list.at( index ), node );
106 updateToolBar( tid );
111 int QtxActionToolMgr::insert( QAction* act, const int tid, const int pos )
113 return insert( registerAction( act ), tid, pos );
116 int QtxActionToolMgr::insert( const int id, const QString& tname, const int pos )
118 return insert( id, createToolBar( tname ), pos );
121 int QtxActionToolMgr::insert( QAction* act, const QString& tname, const int pos )
123 return insert( registerAction( act ), createToolBar( tname ), pos );
126 int QtxActionToolMgr::append( const int id, const int tid )
128 return insert( id, tid );
131 int QtxActionToolMgr::append( QAction* act, const int tid )
133 return insert( act, tid );
136 int QtxActionToolMgr::append( const int id, const QString& tname )
138 return insert( id, tname );
141 int QtxActionToolMgr::append( QAction* act, const QString& tname )
143 return insert( act, tname );
146 int QtxActionToolMgr::prepend( const int id, const int tid )
148 return insert( id, tid, 0 );
151 int QtxActionToolMgr::prepend( QAction* act, const int tid )
153 return insert( act, tid, 0 );
156 int QtxActionToolMgr::prepend( const int id, const QString& tname )
158 return insert( id, tname, 0 );
161 int QtxActionToolMgr::prepend( QAction* act, const QString& tname )
163 return insert( act, tname, 0 );
166 void QtxActionToolMgr::remove( const int id, const int tid )
168 if ( !myToolBars.contains( tid ) )
172 const NodeList& nodes = myToolBars[tid].nodes;
173 for ( NodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it )
175 if ( (*it).id != id )
176 newList.append( *it );
179 myToolBars[tid].nodes = newList;
181 updateToolBar( tid );
184 void QtxActionToolMgr::remove( const int id, const QString& tname )
186 remove( id, find( tname ) );
189 QToolBar* QtxActionToolMgr::toolBar( const int tid ) const
192 if ( myToolBars.contains( tid ) )
193 tb = myToolBars[tid].toolBar;
197 QToolBar* QtxActionToolMgr::toolBar( const QString& tname ) const
199 return toolBar( find( tname ) );
202 bool QtxActionToolMgr::hasToolBar( const int tid ) const
204 return myToolBars.contains( tid );
207 bool QtxActionToolMgr::hasToolBar( const QString& tname ) const
209 return find( tname ) != -1;
212 void QtxActionToolMgr::onToolBarDestroyed()
214 myToolBars.remove( find( (QToolBar*)sender() ) );
217 int QtxActionToolMgr::find( const QString& tname ) const
220 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
222 if ( it.data().toolBar->label() == tname )
228 int QtxActionToolMgr::find( QToolBar* t ) const
231 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
233 if ( it.data().toolBar == t )
239 void QtxActionToolMgr::updateToolBar( const int tId )
241 if ( !isUpdatesEnabled() )
244 if ( !myToolBars.contains( tId ) )
247 QToolBar* tb = myToolBars[tId].toolBar;
248 const NodeList& list = myToolBars[tId].nodes;
250 for ( NodeList::const_iterator it = list.begin(); it != list.end(); ++it )
252 QAction* a = action( (*it).id );
259 for ( NodeList::const_iterator itr = list.begin(); itr != list.end(); ++itr )
261 if ( !isVisible( (*itr).id, tId ) )
264 QAction* a = action( (*itr).id );
269 simplifySeparators( tb );
272 void QtxActionToolMgr::internalUpdate()
274 for ( ToolBarMap::ConstIterator it1 = myToolBars.begin(); it1 != myToolBars.end(); ++it1 )
275 updateToolBar( it1.key() );
278 void QtxActionToolMgr::simplifySeparators( QToolBar* t )
281 Qtx::simplifySeparators( t );
284 void QtxActionToolMgr::show( const int actId )
286 setShown( actId, true );
289 void QtxActionToolMgr::hide( const int actId )
291 setShown( actId, false );
294 void QtxActionToolMgr::setShown( const int id, const bool on )
296 for ( ToolBarMap::Iterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
297 setVisible( id, it.key(), on );
300 bool QtxActionToolMgr::isShown( const int id ) const
302 QPtrList<ToolNode> nodes;
303 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
305 const NodeList& nl = it.data().nodes;
306 for ( NodeList::const_iterator itr = nl.begin(); itr != nl.end(); ++itr )
308 const ToolNode& node = *itr;
310 nodes.append( &node );
314 if ( nodes.isEmpty() )
318 for ( QPtrListIterator<ToolNode> itr( nodes ); itr.current() && vis; ++itr )
319 vis = itr.current()->visible;
324 bool QtxActionToolMgr::isVisible( const int id, const int tId ) const
326 if ( !myToolBars.contains( tId ) )
330 const NodeList& lst = myToolBars[tId].nodes;
331 for ( NodeList::const_iterator it = lst.begin(); it != lst.end() && !vis; ++it )
333 const ToolNode& node = *it;
340 void QtxActionToolMgr::setVisible( const int id, const int tId, const bool on )
342 if ( !myToolBars.contains( tId ) )
345 bool changed = false;
346 NodeList& lst = myToolBars[tId].nodes;
347 for ( NodeList::iterator it = lst.begin(); it != lst.end(); ++it )
349 ToolNode& node = *it;
352 changed = changed || node.visible != on;
358 updateToolBar( tId );
361 bool QtxActionToolMgr::load( const QString& fname, QtxActionMgr::Reader& r )
363 ToolCreator cr( &r, this );
364 return r.read( fname, cr );
369 Class: QtxActionToolMgr::ToolCreator
372 QtxActionToolMgr::ToolCreator::ToolCreator( QtxActionMgr::Reader* r,
373 QtxActionToolMgr* mgr )
374 : QtxActionMgr::Creator( r ),
379 QtxActionToolMgr::ToolCreator::~ToolCreator()
383 int QtxActionToolMgr::ToolCreator::append( const QString& tag, const bool subMenu,
384 const ItemAttributes& attr, const int tId )
386 if( !myMgr || !reader() )
389 QString label = reader()->option( "label", "label" ),
390 id = reader()->option( "id", "id" ),
391 pos = reader()->option( "pos", "pos" ),
392 group = reader()->option( "group", "group" ),
393 tooltip = reader()->option( "tooltip", "tooltip" ),
394 sep = reader()->option( "separator", "separator" ),
395 accel = reader()->option( "accel", "accel" ),
396 icon = reader()->option( "icon", "icon" ),
397 toggle = reader()->option( "toggle", "toggle" );
399 int res = -1, actId = intValue( attr, id, -1 );
401 res = myMgr->createToolBar( strValue( attr, label ), intValue( attr, id, -1 ) );
403 res = myMgr->insert( separator(), tId, intValue( attr, pos, -1 ) );
406 QPixmap pix; QIconSet set;
407 QString name = strValue( attr, icon );
408 if( !name.isEmpty() && loadPixmap( name, pix ) )
409 set = QIconSet( pix );
411 QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set,
412 strValue( attr, label ),
413 QKeySequence( strValue( attr, accel ) ),
415 QString toggleact = strValue( attr, toggle );
416 newAct->setToggleAction( !toggleact.isEmpty() );
417 newAct->setOn( toggleact.lower()=="true" );
420 int aid = myMgr->registerAction( newAct, actId );
421 res = myMgr->insert( aid, tId, intValue( attr, pos, -1 ) );