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: QtxActionToolMgr.cxx
20 // Author: Alexander SOLOVYEV, Sergey TELKOV
22 #include "QtxActionToolMgr.h"
24 #include "QtxAction.h"
25 #include "QtxToolBar.h"
27 #include <qmainwindow.h>
28 #include <qobjectlist.h>
30 QtxActionToolMgr::QtxActionToolMgr( QMainWindow* p )
36 QtxActionToolMgr::~QtxActionToolMgr()
40 QMainWindow* QtxActionToolMgr::mainWindow() const
45 int QtxActionToolMgr::createToolBar( const QString& name, const int tid )
47 static int _toolBarId = -1;
50 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && tbId == -1; ++it )
52 if ( it.data().toolBar->label().lower() == name.lower() )
59 QToolBar* tb = find( name, mainWindow() );
61 tbId = tid < 0 ? --_toolBarId : tid;
63 myToolBars.insert( tbId, ToolBarInfo() );
64 ToolBarInfo& tInfo = myToolBars[tbId];
68 tb = new QtxToolBar( true, mainWindow() );
73 connect( tInfo.toolBar, SIGNAL( destroyed() ), this, SLOT( onToolBarDestroyed() ) );
78 QToolBar* QtxActionToolMgr::find( const QString& label, QMainWindow* mw ) const
83 QString pattern = label.lower();
86 QPtrList<QDockWindow> lst = mw->dockWindows();
87 for ( QPtrListIterator<QDockWindow> it( lst ); it.current() && !res; ++it )
89 if ( !it.current()->inherits( "QToolBar" ) )
92 QToolBar* cur = (QToolBar*)it.current();
93 if ( cur->label().lower() == pattern )
99 void QtxActionToolMgr::removeToolBar( const int tid )
101 if ( !myToolBars.contains( tid ) )
104 delete myToolBars[tid].toolBar;
105 myToolBars.remove( tid );
108 void QtxActionToolMgr::removeToolBar( const QString& tname )
110 removeToolBar( find( tname ) );
113 int QtxActionToolMgr::insert( const int id, const int tid, const int idx )
115 if ( !contains( id ) || !hasToolBar( tid ) )
121 NodeList& list = myToolBars[tid].nodes;
122 int index = idx < 0 ? list.count() : QMIN( idx, (int)list.count() );
123 list.insert( list.at( index ), node );
124 updateToolBar( tid );
129 int QtxActionToolMgr::insert( QAction* act, const int tid, const int pos )
131 return insert( registerAction( act ), tid, pos );
134 int QtxActionToolMgr::insert( const int id, const QString& tname, const int pos )
136 return insert( id, createToolBar( tname ), pos );
139 int QtxActionToolMgr::insert( QAction* act, const QString& tname, const int pos )
141 return insert( registerAction( act ), createToolBar( tname ), pos );
144 int QtxActionToolMgr::append( const int id, const int tid )
146 return insert( id, tid );
149 int QtxActionToolMgr::append( QAction* act, const int tid )
151 return insert( act, tid );
154 int QtxActionToolMgr::append( const int id, const QString& tname )
156 return insert( id, tname );
159 int QtxActionToolMgr::append( QAction* act, const QString& tname )
161 return insert( act, tname );
164 int QtxActionToolMgr::prepend( const int id, const int tid )
166 return insert( id, tid, 0 );
169 int QtxActionToolMgr::prepend( QAction* act, const int tid )
171 return insert( act, tid, 0 );
174 int QtxActionToolMgr::prepend( const int id, const QString& tname )
176 return insert( id, tname, 0 );
179 int QtxActionToolMgr::prepend( QAction* act, const QString& tname )
181 return insert( act, tname, 0 );
184 void QtxActionToolMgr::remove( const int id, const int tid )
186 if ( !myToolBars.contains( tid ) )
190 const NodeList& nodes = myToolBars[tid].nodes;
191 for ( NodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it )
193 if ( (*it).id != id )
194 newList.append( *it );
197 myToolBars[tid].nodes = newList;
199 updateToolBar( tid );
202 void QtxActionToolMgr::remove( const int id, const QString& tname )
204 remove( id, find( tname ) );
207 QToolBar* QtxActionToolMgr::toolBar( const int tid ) const
210 if ( myToolBars.contains( tid ) )
211 tb = myToolBars[tid].toolBar;
215 QToolBar* QtxActionToolMgr::toolBar( const QString& tname ) const
217 return toolBar( find( tname ) );
220 bool QtxActionToolMgr::hasToolBar( const int tid ) const
222 return myToolBars.contains( tid );
225 bool QtxActionToolMgr::hasToolBar( const QString& tname ) const
227 return find( tname ) != -1;
230 bool QtxActionToolMgr::containsAction( const int id, const int tid ) const
232 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
234 if ( tid == -1 || it.key() == tid ) {
235 const NodeList& list = it.data().nodes;
236 for ( NodeList::const_iterator nit = list.begin(); nit != list.end(); ++nit )
237 if ( (*nit).id == id )
244 void QtxActionToolMgr::onToolBarDestroyed()
246 myToolBars.remove( find( (QToolBar*)sender() ) );
249 int QtxActionToolMgr::find( const QString& tname ) const
252 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
254 if ( it.data().toolBar->label() == tname )
260 int QtxActionToolMgr::find( QToolBar* t ) const
263 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
265 if ( it.data().toolBar == t )
271 void QtxActionToolMgr::updateToolBar( const int tId )
273 if ( !isUpdatesEnabled() )
276 if ( !myToolBars.contains( tId ) )
279 QToolBar* tb = myToolBars[tId].toolBar;
280 const NodeList& list = myToolBars[tId].nodes;
282 for ( NodeList::const_iterator it = list.begin(); it != list.end(); ++it )
284 QAction* a = action( (*it).id );
291 for ( NodeList::const_iterator itr = list.begin(); itr != list.end(); ++itr )
293 if ( !isVisible( (*itr).id, tId ) )
296 QAction* a = action( (*itr).id );
301 simplifySeparators( tb );
304 void QtxActionToolMgr::internalUpdate()
306 for ( ToolBarMap::ConstIterator it1 = myToolBars.begin(); it1 != myToolBars.end(); ++it1 )
307 updateToolBar( it1.key() );
310 void QtxActionToolMgr::simplifySeparators( QToolBar* t )
313 Qtx::simplifySeparators( t );
316 void QtxActionToolMgr::show( const int actId )
318 setShown( actId, true );
321 void QtxActionToolMgr::hide( const int actId )
323 setShown( actId, false );
326 void QtxActionToolMgr::setShown( const int id, const bool on )
328 for ( ToolBarMap::Iterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
329 setVisible( id, it.key(), on );
332 bool QtxActionToolMgr::isShown( const int id ) const
334 QPtrList<ToolNode> nodes;
335 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
337 const NodeList& nl = it.data().nodes;
338 for ( NodeList::const_iterator itr = nl.begin(); itr != nl.end(); ++itr )
340 const ToolNode& node = *itr;
342 nodes.append( &node );
346 if ( nodes.isEmpty() )
350 for ( QPtrListIterator<ToolNode> itr( nodes ); itr.current() && vis; ++itr )
351 vis = itr.current()->visible;
356 bool QtxActionToolMgr::isVisible( const int id, const int tId ) const
358 if ( !myToolBars.contains( tId ) )
362 const NodeList& lst = myToolBars[tId].nodes;
363 for ( NodeList::const_iterator it = lst.begin(); it != lst.end() && !vis; ++it )
365 const ToolNode& node = *it;
372 void QtxActionToolMgr::setVisible( const int id, const int tId, const bool on )
374 if ( !myToolBars.contains( tId ) )
377 bool changed = false;
378 NodeList& lst = myToolBars[tId].nodes;
379 for ( NodeList::iterator it = lst.begin(); it != lst.end(); ++it )
381 ToolNode& node = *it;
384 changed = changed || node.visible != on;
390 updateToolBar( tId );
393 bool QtxActionToolMgr::load( const QString& fname, QtxActionMgr::Reader& r )
395 ToolCreator cr( &r, this );
396 return r.read( fname, cr );
401 Class: QtxActionToolMgr::ToolCreator
404 QtxActionToolMgr::ToolCreator::ToolCreator( QtxActionMgr::Reader* r,
405 QtxActionToolMgr* mgr )
406 : QtxActionMgr::Creator( r ),
411 QtxActionToolMgr::ToolCreator::~ToolCreator()
415 int QtxActionToolMgr::ToolCreator::append( const QString& tag, const bool subMenu,
416 const ItemAttributes& attr, const int tId )
418 if( !myMgr || !reader() )
421 QString label = reader()->option( "label", "label" ),
422 id = reader()->option( "id", "id" ),
423 pos = reader()->option( "pos", "pos" ),
424 group = reader()->option( "group", "group" ),
425 tooltip = reader()->option( "tooltip", "tooltip" ),
426 sep = reader()->option( "separator", "separator" ),
427 accel = reader()->option( "accel", "accel" ),
428 icon = reader()->option( "icon", "icon" ),
429 toggle = reader()->option( "toggle", "toggle" );
431 int res = -1, actId = intValue( attr, id, -1 );
433 res = myMgr->createToolBar( strValue( attr, label ), intValue( attr, id, -1 ) );
435 res = myMgr->insert( separator(), tId, intValue( attr, pos, -1 ) );
438 QPixmap pix; QIconSet set;
439 QString name = strValue( attr, icon );
440 if( !name.isEmpty() && loadPixmap( name, pix ) )
441 set = QIconSet( pix );
443 QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set,
444 strValue( attr, label ),
445 QKeySequence( strValue( attr, accel ) ),
447 QString toggleact = strValue( attr, toggle );
448 newAct->setToggleAction( !toggleact.isEmpty() );
449 newAct->setOn( toggleact.lower()=="true" );
452 int aid = myMgr->registerAction( newAct, actId );
453 res = myMgr->insert( aid, tId, intValue( attr, pos, -1 ) );