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>
33 QtxActionToolMgr::QtxActionToolMgr( QMainWindow* p )
42 QtxActionToolMgr::~QtxActionToolMgr()
49 QMainWindow* QtxActionToolMgr::mainWindow() const
56 \return id of just created toolbar
57 \param name - name of toolbar
58 \param tid - proposed id (if such id is used already, then it will be returned without creation)
60 int QtxActionToolMgr::createToolBar( const QString& name, const int tid )
62 static int _toolBarId = -1;
65 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && tbId == -1; ++it )
67 if ( it.data().toolBar->label().lower() == name.lower() )
74 QToolBar* tb = find( name, mainWindow() );
76 tbId = tid < 0 ? --_toolBarId : tid;
78 myToolBars.insert( tbId, ToolBarInfo() );
79 ToolBarInfo& tInfo = myToolBars[tbId];
83 tb = new QtxToolBar( true, mainWindow() );
88 connect( tInfo.toolBar, SIGNAL( destroyed() ), this, SLOT( onToolBarDestroyed() ) );
94 \return toolbar by title
95 \param label - toolbar title
98 QToolBar* QtxActionToolMgr::find( const QString& label, QMainWindow* mw ) const
103 QString pattern = label.lower();
106 QPtrList<QDockWindow> lst = mw->dockWindows();
107 for ( QPtrListIterator<QDockWindow> it( lst ); it.current() && !res; ++it )
109 if ( !it.current()->inherits( "QToolBar" ) )
112 QToolBar* cur = (QToolBar*)it.current();
113 if ( cur->label().lower() == pattern )
121 \param tid - toolbar id
123 void QtxActionToolMgr::removeToolBar( const int tid )
125 if ( !myToolBars.contains( tid ) )
128 delete myToolBars[tid].toolBar;
129 myToolBars.remove( tid );
134 \param tname - toolbar name
136 void QtxActionToolMgr::removeToolBar( const QString& tname )
138 removeToolBar( find( tname ) );
142 Insert action into toolbar
143 \param id - identificator of action
144 \param tId - identificator of toolbar
145 \param idx - position inside toolbar
147 int QtxActionToolMgr::insert( const int id, const int tid, const int idx )
149 if ( !contains( id ) || !hasToolBar( tid ) )
152 if ( containsAction( id, tid ) )
158 NodeList& list = myToolBars[tid].nodes;
159 int index = idx < 0 ? list.count() : QMIN( idx, (int)list.count() );
160 list.insert( list.at( index ), node );
161 updateToolBar( tid );
167 Insert action into toolbar
169 \param tId - identificator of toolbar
170 \param pos - position inside toolbar
172 int QtxActionToolMgr::insert( QAction* act, const int tid, const int pos )
174 return insert( registerAction( act ), tid, pos );
178 Insert action into toolbar
179 \param id - identificator of action
180 \param tname - name of toolbar
181 \param pos - position inside toolbar
183 int QtxActionToolMgr::insert( const int id, const QString& tname, const int pos )
185 return insert( id, createToolBar( tname ), pos );
189 Insert action into toolbar
191 \param tname - name of toolbar
192 \param pos - position inside toolbar
194 int QtxActionToolMgr::insert( QAction* act, const QString& tname, const int pos )
196 return insert( registerAction( act ), createToolBar( tname ), pos );
200 Append action into toolbar as last toolbutton
201 \param id - identificator of action
202 \param tId - identificator of toolbar
204 int QtxActionToolMgr::append( const int id, const int tid )
206 return insert( id, tid );
210 Append action into toolbar as last toolbutton
212 \param tId - identificator of toolbar
214 int QtxActionToolMgr::append( QAction* act, const int tid )
216 return insert( act, tid );
220 Append action into toolbar as last toolbutton
221 \param id - identificator of action
222 \param tname - toolbar name
224 int QtxActionToolMgr::append( const int id, const QString& tname )
226 return insert( id, tname );
230 Append action into toolbar as last toolbutton
232 \param tname - toolbar name
234 int QtxActionToolMgr::append( QAction* act, const QString& tname )
236 return insert( act, tname );
240 Append action into toolbar as first toolbutton
241 \param id - identificator of action
242 \param tId - identificator of toolbar
244 int QtxActionToolMgr::prepend( const int id, const int tid )
246 return insert( id, tid, 0 );
250 Append action into toolbar as first toolbutton
252 \param tId - identificator of toolbar
254 int QtxActionToolMgr::prepend( QAction* act, const int tid )
256 return insert( act, tid, 0 );
260 Append action into toolbar as first toolbutton
261 \param id - identificator of action
262 \param tname - toolbar name
264 int QtxActionToolMgr::prepend( const int id, const QString& tname )
266 return insert( id, tname, 0 );
270 Append action into toolbar as first toolbutton
272 \param tname - toolbar name
274 int QtxActionToolMgr::prepend( QAction* act, const QString& tname )
276 return insert( act, tname, 0 );
280 Remove action from toolbar
281 \param id - identificator of action
282 \param tId - identificator of toolbar
284 void QtxActionToolMgr::remove( const int id, const int tid )
286 if ( !myToolBars.contains( tid ) )
290 const NodeList& nodes = myToolBars[tid].nodes;
291 for ( NodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it )
293 if ( (*it).id != id )
294 newList.append( *it );
297 myToolBars[tid].nodes = newList;
299 updateToolBar( tid );
303 Remove action from toolbar
304 \param id - identificator of action
305 \param tname - name of toolbar
307 void QtxActionToolMgr::remove( const int id, const QString& tname )
309 remove( id, find( tname ) );
313 \return toolbar by it's id
314 \param tId - identificator of toolbar
316 QToolBar* QtxActionToolMgr::toolBar( const int tid ) const
319 if ( myToolBars.contains( tid ) )
320 tb = myToolBars[tid].toolBar;
325 \return toolbar by it's name
326 \param tname - name of toolbar
328 QToolBar* QtxActionToolMgr::toolBar( const QString& tname ) const
330 return toolBar( find( tname ) );
334 \return true if manager contains toolbar with such id
335 \param tId - identificator of toolbar
337 bool QtxActionToolMgr::hasToolBar( const int tid ) const
339 return myToolBars.contains( tid );
343 \return true if manager contains toolbar with such name
344 \param tname - name of toolbar
346 bool QtxActionToolMgr::hasToolBar( const QString& tname ) const
348 return find( tname ) != -1;
352 \return true if toolbar contains action
353 \param id - identificator of action
354 \param tId - identificator of toolbar
356 bool QtxActionToolMgr::containsAction( const int id, const int tid ) const
358 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
360 if ( tid == -1 || it.key() == tid ) {
361 const NodeList& list = it.data().nodes;
362 for ( NodeList::const_iterator nit = list.begin(); nit != list.end(); ++nit )
363 if ( (*nit).id == id )
371 SLOT: called when toolbar is destroyed, removes just destroyed toolbar from map
373 void QtxActionToolMgr::onToolBarDestroyed()
375 myToolBars.remove( find( (QToolBar*)sender() ) );
379 \return id of toolbar by it's name
380 \param tname - name of toolbar
382 int QtxActionToolMgr::find( const QString& tname ) const
385 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
387 if ( it.data().toolBar->label() == tname )
394 \return id of toolbar
397 int QtxActionToolMgr::find( QToolBar* t ) const
400 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
402 if ( it.data().toolBar == t )
410 \param tId - toolbar id
412 void QtxActionToolMgr::updateToolBar( const int tId )
414 if ( !isUpdatesEnabled() )
417 if ( !myToolBars.contains( tId ) )
420 QToolBar* tb = myToolBars[tId].toolBar;
421 const NodeList& list = myToolBars[tId].nodes;
423 for ( NodeList::const_iterator it = list.begin(); it != list.end(); ++it )
425 QAction* a = action( (*it).id );
432 for ( NodeList::const_iterator itr = list.begin(); itr != list.end(); ++itr )
434 if ( !isVisible( (*itr).id, tId ) )
437 QAction* a = action( (*itr).id );
442 simplifySeparators( tb );
448 void QtxActionToolMgr::internalUpdate()
450 for ( ToolBarMap::ConstIterator it1 = myToolBars.begin(); it1 != myToolBars.end(); ++it1 )
451 updateToolBar( it1.key() );
455 Removes excess separators from toolbar
457 void QtxActionToolMgr::simplifySeparators( QToolBar* t )
460 Qtx::simplifySeparators( t );
464 Shows action in all toolbars
465 \param actId - action id
467 void QtxActionToolMgr::show( const int actId )
469 setShown( actId, true );
473 Hides action in all toolbars
474 \param actId - action id
476 void QtxActionToolMgr::hide( const int actId )
478 setShown( actId, false );
482 Changes shown status of action in all toolbars
483 \param id - action id
484 \param on - new shown status
486 void QtxActionToolMgr::setShown( const int id, const bool on )
488 for ( ToolBarMap::Iterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
489 setVisible( id, it.key(), on );
493 \return true if action is shown in all toolbars
494 \param id - action id
496 bool QtxActionToolMgr::isShown( const int id ) const
498 QPtrList<ToolNode> nodes;
499 for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
501 const NodeList& nl = it.data().nodes;
502 for ( NodeList::const_iterator itr = nl.begin(); itr != nl.end(); ++itr )
504 const ToolNode& node = *itr;
506 nodes.append( &node );
510 if ( nodes.isEmpty() )
514 for ( QPtrListIterator<ToolNode> itr( nodes ); itr.current() && vis; ++itr )
515 vis = itr.current()->visible;
521 \return shown status of action in toolbar
522 \param id - action id
523 \param tId - toolbar id
525 bool QtxActionToolMgr::isVisible( const int id, const int tId ) const
527 if ( !myToolBars.contains( tId ) )
531 const NodeList& lst = myToolBars[tId].nodes;
532 for ( NodeList::const_iterator it = lst.begin(); it != lst.end() && !vis; ++it )
534 const ToolNode& node = *it;
542 Changes action shown status in certain toolbar
543 \param id - action id
544 \param tId - toolbar id
545 \param on - new shown status
547 void QtxActionToolMgr::setVisible( const int id, const int tId, const bool on )
549 if ( !myToolBars.contains( tId ) )
552 bool changed = false;
553 NodeList& lst = myToolBars[tId].nodes;
554 for ( NodeList::iterator it = lst.begin(); it != lst.end(); ++it )
556 ToolNode& node = *it;
559 changed = changed || node.visible != on;
565 updateToolBar( tId );
569 Loads toolbar content from file
570 \param fname - file name
573 bool QtxActionToolMgr::load( const QString& fname, QtxActionMgr::Reader& r )
575 ToolCreator cr( &r, this );
576 return r.read( fname, cr );
583 QtxActionToolMgr::ToolCreator::ToolCreator( QtxActionMgr::Reader* r,
584 QtxActionToolMgr* mgr )
585 : QtxActionMgr::Creator( r ),
593 QtxActionToolMgr::ToolCreator::~ToolCreator()
598 Appends new tool buttons
599 \param tag - tag of toolmenu
600 \param subMenu - it has submenu (not used here)
601 \param attr - list of attributes
602 \param pId - id of action corresponding to parent item
604 int QtxActionToolMgr::ToolCreator::append( const QString& tag, const bool subMenu,
605 const ItemAttributes& attr, const int tId )
607 if( !myMgr || !reader() )
610 QString label = reader()->option( "label", "label" ),
611 id = reader()->option( "id", "id" ),
612 pos = reader()->option( "pos", "pos" ),
613 group = reader()->option( "group", "group" ),
614 tooltip = reader()->option( "tooltip", "tooltip" ),
615 sep = reader()->option( "separator", "separator" ),
616 accel = reader()->option( "accel", "accel" ),
617 icon = reader()->option( "icon", "icon" ),
618 toggle = reader()->option( "toggle", "toggle" );
620 int res = -1, actId = intValue( attr, id, -1 );
622 res = myMgr->createToolBar( strValue( attr, label ), intValue( attr, id, -1 ) );
624 res = myMgr->insert( separator(), tId, intValue( attr, pos, -1 ) );
627 QPixmap pix; QIconSet set;
628 QString name = strValue( attr, icon );
629 if( !name.isEmpty() && loadPixmap( name, pix ) )
630 set = QIconSet( pix );
632 QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set,
633 strValue( attr, label ),
634 QKeySequence( strValue( attr, accel ) ),
636 QString toggleact = strValue( attr, toggle );
637 newAct->setToggleAction( !toggleact.isEmpty() );
638 newAct->setOn( toggleact.lower()=="true" );
641 int aid = myMgr->registerAction( newAct, actId );
642 res = myMgr->insert( aid, tId, intValue( attr, pos, -1 ) );