Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / Qtx / QtxActionToolMgr.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
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.
7 // 
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.
12 //
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
16 //
17 // See http://www.salome-platform.org/
18 //
19 // File:      QtxActionToolMgr.cxx
20 // Author:    Alexander SOLOVYEV, Sergey TELKOV
21
22 #include "QtxActionToolMgr.h"
23
24 #include "QtxAction.h"
25 #include "QtxToolBar.h"
26
27 #include <qmainwindow.h>
28 #include <qobjectlist.h>
29
30 /*!
31   Constructor
32 */
33 QtxActionToolMgr::QtxActionToolMgr( QMainWindow* p )
34 : QtxActionMgr( p ),
35 myMainWindow( p )
36 {
37 }
38
39 /*!
40   Destructor
41 */
42 QtxActionToolMgr::~QtxActionToolMgr()
43 {
44 }
45
46 /*!
47   \return desktop
48 */
49 QMainWindow* QtxActionToolMgr::mainWindow() const
50 {
51   return myMainWindow;
52 }
53
54 /*!
55   Creates toolbar 
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)
59 */
60 int QtxActionToolMgr::createToolBar( const QString& name, const int tid )
61 {
62   static int _toolBarId = -1;
63
64   int tbId = -1;
65   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && tbId == -1; ++it )
66   {
67     if ( it.data().toolBar->label().lower() == name.lower() )
68       tbId = it.key();
69   }
70
71   if ( tbId != -1 )
72     return tbId;
73
74   QToolBar* tb = find( name, mainWindow() );
75
76   tbId = tid < 0 ? --_toolBarId : tid;
77
78   myToolBars.insert( tbId, ToolBarInfo() );
79   ToolBarInfo& tInfo = myToolBars[tbId];
80
81   if ( !tb )
82   {
83     tb = new QtxToolBar( true, mainWindow() );
84     tb->setLabel( name );
85   }
86
87   tInfo.toolBar = tb;
88   connect( tInfo.toolBar, SIGNAL( destroyed() ), this, SLOT( onToolBarDestroyed() ) );
89
90   return tbId;
91 }
92
93 /*!
94   \return toolbar by title
95   \param label - toolbar title
96   \param mw - desktop
97 */
98 QToolBar* QtxActionToolMgr::find( const QString& label, QMainWindow* mw ) const
99 {
100   if ( !mw )
101     return 0;
102
103   QString pattern = label.lower();
104
105   QToolBar* res = 0;
106   QPtrList<QDockWindow> lst = mw->dockWindows();
107   for ( QPtrListIterator<QDockWindow> it( lst ); it.current() && !res; ++it )
108   {
109     if ( !it.current()->inherits( "QToolBar" ) )
110       continue;
111
112     QToolBar* cur = (QToolBar*)it.current();
113     if ( cur->label().lower() == pattern )
114       res = cur;
115   }
116   return res;
117 }
118
119 /*!
120   Removes toolbar
121   \param tid - toolbar id
122 */
123 void QtxActionToolMgr::removeToolBar( const int tid )
124 {
125   if ( !myToolBars.contains( tid ) )
126     return;
127
128   delete myToolBars[tid].toolBar;
129   myToolBars.remove( tid );
130 }
131
132 /*!
133   Removes toolbar
134   \param tname - toolbar name
135 */
136 void QtxActionToolMgr::removeToolBar( const QString& tname )
137 {
138   removeToolBar( find( tname ) );
139 }
140
141 /*!
142   Insert action into toolbar
143   \param id - identificator of action
144   \param tId - identificator of toolbar
145   \param idx - position inside toolbar
146 */
147 int QtxActionToolMgr::insert( const int id, const int tid, const int idx )
148 {
149   if ( !contains( id ) || !hasToolBar( tid ) )
150     return -1;
151
152   if ( containsAction( id, tid ) )
153     remove( id, tid );
154
155   ToolNode node;
156   node.id = id;
157
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 );
162
163   return id;
164 }
165
166 /*!
167   Insert action into toolbar
168   \param act - action
169   \param tId - identificator of toolbar
170   \param pos - position inside toolbar
171 */
172 int QtxActionToolMgr::insert( QAction* act, const int tid, const int pos )
173 {
174   return insert( registerAction( act ), tid, pos );
175 }
176
177 /*!
178   Insert action into toolbar
179   \param id - identificator of action
180   \param tname - name of toolbar
181   \param pos - position inside toolbar
182 */
183 int QtxActionToolMgr::insert( const int id, const QString& tname, const int pos )
184 {
185   return insert( id, createToolBar( tname ), pos );
186 }
187
188 /*!
189   Insert action into toolbar
190   \param act - action
191   \param tname - name of toolbar
192   \param pos - position inside toolbar
193 */
194 int QtxActionToolMgr::insert( QAction* act, const QString& tname, const int pos )
195 {
196   return insert( registerAction( act ), createToolBar( tname ), pos );
197 }
198
199 /*!
200   Append action into toolbar as last toolbutton
201   \param id - identificator of action
202   \param tId - identificator of toolbar
203 */
204 int QtxActionToolMgr::append( const int id, const int tid )
205 {
206   return insert( id, tid );
207 }
208
209 /*!
210   Append action into toolbar as last toolbutton
211   \param act - action
212   \param tId - identificator of toolbar
213 */
214 int QtxActionToolMgr::append( QAction* act, const int tid )
215 {
216   return insert( act, tid );
217 }
218
219 /*!
220   Append action into toolbar as last toolbutton
221   \param id - identificator of action
222   \param tname - toolbar name
223 */
224 int QtxActionToolMgr::append( const int id, const QString& tname )
225 {
226   return insert( id, tname );
227 }
228
229 /*!
230   Append action into toolbar as last toolbutton
231   \param act - action
232   \param tname - toolbar name
233 */
234 int QtxActionToolMgr::append( QAction* act, const QString& tname )
235 {
236   return insert( act, tname );
237 }
238
239 /*!
240   Append action into toolbar as first toolbutton
241   \param id - identificator of action
242   \param tId - identificator of toolbar
243 */
244 int QtxActionToolMgr::prepend( const int id, const int tid )
245 {
246   return insert( id, tid, 0 );
247 }
248
249 /*!
250   Append action into toolbar as first toolbutton
251   \param act - action
252   \param tId - identificator of toolbar
253 */
254 int QtxActionToolMgr::prepend( QAction* act, const int tid )
255 {
256   return insert( act, tid, 0 );
257 }
258
259 /*!
260   Append action into toolbar as first toolbutton
261   \param id - identificator of action
262   \param tname - toolbar name
263 */
264 int QtxActionToolMgr::prepend( const int id, const QString& tname )
265 {
266   return insert( id, tname, 0 );
267 }
268
269 /*!
270   Append action into toolbar as first toolbutton
271   \param act - action
272   \param tname - toolbar name
273 */
274 int QtxActionToolMgr::prepend( QAction* act, const QString& tname )
275 {
276   return insert( act, tname, 0 );
277 }
278
279 /*!
280   Remove action from toolbar
281   \param id - identificator of action
282   \param tId - identificator of toolbar
283 */
284 void QtxActionToolMgr::remove( const int id, const int tid )
285 {
286   if ( !myToolBars.contains( tid ) )
287     return;
288
289   NodeList newList;
290   const NodeList& nodes = myToolBars[tid].nodes;
291   for ( NodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it )
292   {
293     if ( (*it).id != id )
294       newList.append( *it );
295   }
296
297   myToolBars[tid].nodes = newList;
298
299   updateToolBar( tid );
300 }
301
302 /*!
303   Remove action from toolbar
304   \param id - identificator of action
305   \param tname - name of toolbar
306 */
307 void QtxActionToolMgr::remove( const int id, const QString& tname )
308 {
309   remove( id, find( tname ) );
310 }
311
312 /*!
313   \return toolbar by it's id
314   \param tId - identificator of toolbar
315 */
316 QToolBar* QtxActionToolMgr::toolBar( const int tid ) const
317 {
318   QToolBar* tb = 0;
319   if ( myToolBars.contains( tid ) )
320     tb = myToolBars[tid].toolBar;
321   return tb;
322 }
323
324 /*!
325   \return toolbar by it's name
326   \param tname - name of toolbar
327 */
328 QToolBar* QtxActionToolMgr::toolBar( const QString& tname ) const
329 {
330   return toolBar( find( tname ) );
331 }
332
333 /*!
334   \return true if manager contains toolbar with such id
335   \param tId - identificator of toolbar
336 */
337 bool QtxActionToolMgr::hasToolBar( const int tid ) const
338 {
339   return myToolBars.contains( tid );
340 }
341
342 /*!
343   \return true if manager contains toolbar with such name
344   \param tname - name of toolbar
345 */
346 bool QtxActionToolMgr::hasToolBar( const QString& tname ) const
347 {
348   return find( tname ) != -1;
349 }
350
351 /*!
352   \return true if toolbar contains action
353   \param id - identificator of action
354   \param tId - identificator of toolbar
355 */
356 bool QtxActionToolMgr::containsAction( const int id, const int tid ) const
357 {
358   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
359   {
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 )
364           return true;
365     }
366   }
367   return false;
368 }
369
370 /*!
371   SLOT: called when toolbar is destroyed, removes just destroyed toolbar from map
372 */
373 void QtxActionToolMgr::onToolBarDestroyed()
374 {
375   myToolBars.remove( find( (QToolBar*)sender() ) );
376 }
377
378 /*!
379   \return id of toolbar by it's name
380   \param tname - name of toolbar
381 */
382 int QtxActionToolMgr::find( const QString& tname ) const
383 {
384   int id = -1;
385   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
386   {
387     if ( it.data().toolBar->label() == tname )
388       id = it.key();
389   }
390   return id;
391 }
392
393 /*!
394   \return id of toolbar
395   \param t - toolbar
396 */
397 int QtxActionToolMgr::find( QToolBar* t ) const
398 {
399   int id = -1;
400   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
401   {
402     if ( it.data().toolBar == t )
403       id = it.key();
404   }
405   return id;
406 }
407
408 /*!
409   Updates toolbar
410   \param tId - toolbar id
411 */
412 void QtxActionToolMgr::updateToolBar( const int tId )
413 {
414   if ( !isUpdatesEnabled() )
415     return;
416
417   if ( !myToolBars.contains( tId ) )
418     return;
419
420   QToolBar* tb = myToolBars[tId].toolBar;
421   const NodeList& list = myToolBars[tId].nodes;
422
423   for ( NodeList::const_iterator it = list.begin(); it != list.end(); ++it )
424   {
425     QAction* a = action( (*it).id );
426     if ( a )
427       a->removeFrom( tb );
428   }
429
430   tb->clear();
431
432   for ( NodeList::const_iterator itr = list.begin(); itr != list.end(); ++itr )
433   {
434     if ( !isVisible( (*itr).id, tId ) )
435       continue;
436
437     QAction* a = action( (*itr).id );
438     if ( a )
439       a->addTo( tb );
440   }
441
442   simplifySeparators( tb );
443 }
444
445 /*!
446   Updates all toolbars
447 */
448 void QtxActionToolMgr::internalUpdate()
449 {
450   for ( ToolBarMap::ConstIterator it1 = myToolBars.begin(); it1 != myToolBars.end(); ++it1 )
451     updateToolBar( it1.key() );
452 }
453
454 /*!
455   Removes excess separators from toolbar
456 */
457 void QtxActionToolMgr::simplifySeparators( QToolBar* t )
458 {
459   if ( t )
460     Qtx::simplifySeparators( t );
461 }
462
463 /*!
464   Shows action in all toolbars
465   \param actId - action id
466 */
467 void QtxActionToolMgr::show( const int actId )
468 {
469   setShown( actId, true );
470 }
471
472 /*!
473   Hides action in all toolbars
474   \param actId - action id
475 */
476 void QtxActionToolMgr::hide( const int actId )
477 {
478   setShown( actId, false );
479 }
480
481 /*!
482   Changes shown status of action in all toolbars
483   \param id - action id
484   \param on - new shown status
485 */
486 void QtxActionToolMgr::setShown( const int id, const bool on )
487 {
488   for ( ToolBarMap::Iterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
489     setVisible( id, it.key(), on );
490 }
491
492 /*!
493   \return true if action is shown in all toolbars
494   \param id - action id
495 */
496 bool QtxActionToolMgr::isShown( const int id ) const
497 {
498   QPtrList<ToolNode> nodes;
499   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
500   {
501     const NodeList& nl = it.data().nodes;
502     for ( NodeList::const_iterator itr = nl.begin(); itr != nl.end(); ++itr )
503     {
504       const ToolNode& node = *itr;
505       if ( node.id == id )
506         nodes.append( &node );
507     }
508   }
509
510   if ( nodes.isEmpty() )
511     return false;
512
513   bool vis = true;
514   for ( QPtrListIterator<ToolNode> itr( nodes ); itr.current() && vis; ++itr )
515     vis = itr.current()->visible;
516
517   return vis;
518 }
519
520 /*!
521   \return shown status of action in toolbar
522   \param id - action id
523   \param tId - toolbar id
524 */
525 bool QtxActionToolMgr::isVisible( const int id, const int tId ) const
526 {
527   if ( !myToolBars.contains( tId ) )
528     return false;
529
530   bool vis = false;
531   const NodeList& lst = myToolBars[tId].nodes;
532   for ( NodeList::const_iterator it = lst.begin(); it != lst.end() && !vis; ++it )
533   {
534     const ToolNode& node = *it;
535     if ( node.id == id )
536       vis = node.visible;
537   }
538   return vis;
539 }
540
541 /*!
542   Changes action shown status in certain toolbar
543   \param id - action id
544   \param tId - toolbar id
545   \param on - new shown status
546 */
547 void QtxActionToolMgr::setVisible( const int id, const int tId, const bool on )
548 {
549   if ( !myToolBars.contains( tId ) )
550     return;
551
552   bool changed = false;
553   NodeList& lst = myToolBars[tId].nodes;
554   for ( NodeList::iterator it = lst.begin(); it != lst.end(); ++it )
555   {
556     ToolNode& node = *it;
557     if ( node.id == id )
558     {
559       changed = changed || node.visible != on;
560       node.visible = on;
561     }
562   }
563
564   if ( changed )
565     updateToolBar( tId );
566 }
567
568 /*!
569   Loads toolbar content from file
570   \param fname - file name
571   \param r - reader
572 */
573 bool QtxActionToolMgr::load( const QString& fname, QtxActionMgr::Reader& r )
574 {
575   ToolCreator cr( &r, this );
576   return r.read( fname, cr );
577 }
578
579
580 /*!
581   Constructor
582 */
583 QtxActionToolMgr::ToolCreator::ToolCreator( QtxActionMgr::Reader* r,
584                                             QtxActionToolMgr* mgr )
585 : QtxActionMgr::Creator( r ),
586   myMgr( mgr )
587 {
588 }
589
590 /*!
591   Destructor
592 */
593 QtxActionToolMgr::ToolCreator::~ToolCreator()
594 {
595 }
596
597 /*!
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
603 */
604 int QtxActionToolMgr::ToolCreator::append( const QString& tag, const bool subMenu,
605                                            const ItemAttributes& attr, const int tId )
606 {  
607   if( !myMgr || !reader() )
608     return -1;
609
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"    );
619
620   int res = -1, actId = intValue( attr, id, -1 );
621   if( tId==-1 )
622     res = myMgr->createToolBar( strValue( attr, label ), intValue( attr, id, -1 ) );
623   else if( tag==sep )
624     res = myMgr->insert( separator(), tId, intValue( attr, pos, -1 ) );
625   else
626   {
627     QPixmap pix; QIconSet set;
628     QString name = strValue( attr, icon );
629     if( !name.isEmpty() && loadPixmap( name, pix ) )
630       set = QIconSet( pix );
631
632     QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set,
633                                        strValue( attr, label ), 
634                                        QKeySequence( strValue( attr, accel ) ),
635                                        myMgr );
636     QString toggleact = strValue( attr, toggle );
637     newAct->setToggleAction( !toggleact.isEmpty() );
638     newAct->setOn( toggleact.lower()=="true" );
639         
640     connect( newAct );
641     int aid = myMgr->registerAction( newAct, actId );
642     res = myMgr->insert( aid, tId, intValue( attr, pos, -1 ) );
643   }
644
645   return res;
646 }
647
648