Salome HOME
Bug fixed: if 'splash' parameter is not set, then application crashed. Now OK.
[modules/gui.git] / src / Qtx / QtxActionToolMgr.cxx
1 // File:      QtxActionToolMgr.cxx
2 // Author:    Alexander SOLOVYEV, Sergey TELKOV
3
4 #include "QtxActionToolMgr.h"
5
6 #include "QtxAction.h"
7 #include "QtxToolBar.h"
8
9 #include <qmainwindow.h>
10 #include <qobjectlist.h>
11
12 QtxActionToolMgr::QtxActionToolMgr( QMainWindow* p )
13 : QtxActionMgr( p ),
14 myMainWindow( p )
15 {
16 }
17
18 QtxActionToolMgr::~QtxActionToolMgr()
19 {
20 }
21
22 QMainWindow* QtxActionToolMgr::mainWindow() const
23 {
24   return myMainWindow;
25 }
26
27 int QtxActionToolMgr::createToolBar( const QString& name, const int tid )
28 {
29   static int _toolBarId = -1;
30
31   int tbId = -1;
32   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && tbId == -1; ++it )
33   {
34     if ( it.data().toolBar->label().lower() == name.lower() )
35       tbId = it.key();
36   }
37
38   if ( tbId != -1 )
39     return tbId;
40
41   QToolBar* tb = find( name, mainWindow() );
42
43   tbId = tid < 0 ? --_toolBarId : tid;
44
45   myToolBars.insert( tbId, ToolBarInfo() );
46   ToolBarInfo& tInfo = myToolBars[tbId];
47
48   if ( !tb )
49   {
50     tb = new QtxToolBar( true, mainWindow() );
51     tb->setLabel( name );
52   }
53
54   tInfo.toolBar = tb;
55   connect( tInfo.toolBar, SIGNAL( destroyed() ), this, SLOT( onToolBarDestroyed() ) );
56
57   return tbId;
58 }
59
60 QToolBar* QtxActionToolMgr::find( const QString& label, QMainWindow* mw ) const
61 {
62   if ( !mw )
63     return 0;
64
65   QString pattern = label.lower();
66
67   QToolBar* res = 0;
68   QPtrList<QDockWindow> lst = mw->dockWindows();
69   for ( QPtrListIterator<QDockWindow> it( lst ); it.current() && !res; ++it )
70   {
71     if ( !it.current()->inherits( "QToolBar" ) )
72       continue;
73
74     QToolBar* cur = (QToolBar*)it.current();
75     if ( cur->label().lower() == pattern )
76       res = cur;
77   }
78   return res;
79 }
80
81 void QtxActionToolMgr::removeToolBar( const int tid )
82 {
83   if ( !myToolBars.contains( tid ) )
84     return;
85
86   delete myToolBars[tid].toolBar;
87   myToolBars.remove( tid );
88 }
89
90 void QtxActionToolMgr::removeToolBar( const QString& tname )
91 {
92   removeToolBar( find( tname ) );
93 }
94
95 int QtxActionToolMgr::insert( const int id, const int tid, const int idx )
96 {
97   if ( !contains( id ) || !hasToolBar( tid ) )
98     return -1;
99
100   ToolNode node;
101   node.id = id;
102
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 );
107
108   return id;
109 }
110
111 int QtxActionToolMgr::insert( QAction* act, const int tid, const int pos )
112 {
113   return insert( registerAction( act ), tid, pos );
114 }
115
116 int QtxActionToolMgr::insert( const int id, const QString& tname, const int pos )
117 {
118   return insert( id, createToolBar( tname ), pos );
119 }
120
121 int QtxActionToolMgr::insert( QAction* act, const QString& tname, const int pos )
122 {
123   return insert( registerAction( act ), createToolBar( tname ), pos );
124 }
125
126 int QtxActionToolMgr::append( const int id, const int tid )
127 {
128   return insert( id, tid );
129 }
130
131 int QtxActionToolMgr::append( QAction* act, const int tid )
132 {
133   return insert( act, tid );
134 }
135
136 int QtxActionToolMgr::append( const int id, const QString& tname )
137 {
138   return insert( id, tname );
139 }
140
141 int QtxActionToolMgr::append( QAction* act, const QString& tname )
142 {
143   return insert( act, tname );
144 }
145
146 int QtxActionToolMgr::prepend( const int id, const int tid )
147 {
148   return insert( id, tid, 0 );
149 }
150
151 int QtxActionToolMgr::prepend( QAction* act, const int tid )
152 {
153   return insert( act, tid, 0 );
154 }
155
156 int QtxActionToolMgr::prepend( const int id, const QString& tname )
157 {
158   return insert( id, tname, 0 );
159 }
160
161 int QtxActionToolMgr::prepend( QAction* act, const QString& tname )
162 {
163   return insert( act, tname, 0 );
164 }
165
166 void QtxActionToolMgr::remove( const int id, const int tid )
167 {
168   if ( !myToolBars.contains( tid ) )
169     return;
170
171   NodeList newList;
172   const NodeList& nodes = myToolBars[tid].nodes;
173   for ( NodeList::const_iterator it = nodes.begin(); it != nodes.end(); ++it )
174   {
175     if ( (*it).id != id )
176       newList.append( *it );
177   }
178
179   myToolBars[tid].nodes = newList;
180
181   updateToolBar( tid );
182 }
183
184 void QtxActionToolMgr::remove( const int id, const QString& tname )
185 {
186   remove( id, find( tname ) );
187 }
188
189 QToolBar* QtxActionToolMgr::toolBar( const int tid ) const
190 {
191   QToolBar* tb = 0;
192   if ( myToolBars.contains( tid ) )
193     tb = myToolBars[tid].toolBar;
194   return tb;
195 }
196
197 QToolBar* QtxActionToolMgr::toolBar( const QString& tname ) const
198 {
199   return toolBar( find( tname ) );
200 }
201
202 bool QtxActionToolMgr::hasToolBar( const int tid ) const
203 {
204   return myToolBars.contains( tid );
205 }
206
207 bool QtxActionToolMgr::hasToolBar( const QString& tname ) const
208 {
209   return find( tname ) != -1;
210 }
211
212 void QtxActionToolMgr::onToolBarDestroyed()
213 {
214   myToolBars.remove( find( (QToolBar*)sender() ) );
215 }
216
217 int QtxActionToolMgr::find( const QString& tname ) const
218 {
219   int id = -1;
220   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
221   {
222     if ( it.data().toolBar->label() == tname )
223       id = it.key();
224   }
225   return id;
226 }
227
228 int QtxActionToolMgr::find( QToolBar* t ) const
229 {
230   int id = -1;
231   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end() && id == -1; ++it )
232   {
233     if ( it.data().toolBar == t )
234       id = it.key();
235   }
236   return id;
237 }
238
239 void QtxActionToolMgr::updateToolBar( const int tId )
240 {
241   if ( !isUpdatesEnabled() )
242     return;
243
244   if ( !myToolBars.contains( tId ) )
245     return;
246
247   QToolBar* tb = myToolBars[tId].toolBar;
248   const NodeList& list = myToolBars[tId].nodes;
249
250   for ( NodeList::const_iterator it = list.begin(); it != list.end(); ++it )
251   {
252     QAction* a = action( (*it).id );
253     if ( a )
254       a->removeFrom( tb );
255   }
256
257   tb->clear();
258
259   for ( NodeList::const_iterator itr = list.begin(); itr != list.end(); ++itr )
260   {
261     if ( !isVisible( (*itr).id, tId ) )
262       continue;
263
264     QAction* a = action( (*itr).id );
265     if ( a )
266       a->addTo( tb );
267   }
268
269   simplifySeparators( tb );
270 }
271
272 void QtxActionToolMgr::internalUpdate()
273 {
274   for ( ToolBarMap::ConstIterator it1 = myToolBars.begin(); it1 != myToolBars.end(); ++it1 )
275     updateToolBar( it1.key() );
276 }
277
278 void QtxActionToolMgr::simplifySeparators( QToolBar* t )
279 {
280   if ( t )
281     Qtx::simplifySeparators( t );
282 }
283
284 void QtxActionToolMgr::show( const int actId )
285 {
286   setShown( actId, true );
287 }
288
289 void QtxActionToolMgr::hide( const int actId )
290 {
291   setShown( actId, false );
292 }
293
294 void QtxActionToolMgr::setShown( const int id, const bool on )
295 {
296   for ( ToolBarMap::Iterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
297     setVisible( id, it.key(), on );
298 }
299
300 bool QtxActionToolMgr::isShown( const int id ) const
301 {
302   QPtrList<ToolNode> nodes;
303   for ( ToolBarMap::ConstIterator it = myToolBars.begin(); it != myToolBars.end(); ++it )
304   {
305     const NodeList& nl = it.data().nodes;
306     for ( NodeList::const_iterator itr = nl.begin(); itr != nl.end(); ++itr )
307     {
308       const ToolNode& node = *itr;
309       if ( node.id == id )
310         nodes.append( &node );
311     }
312   }
313
314   if ( nodes.isEmpty() )
315     return false;
316
317   bool vis = true;
318   for ( QPtrListIterator<ToolNode> itr( nodes ); itr.current() && vis; ++itr )
319     vis = itr.current()->visible;
320
321   return vis;
322 }
323
324 bool QtxActionToolMgr::isVisible( const int id, const int tId ) const
325 {
326   if ( !myToolBars.contains( tId ) )
327     return false;
328
329   bool vis = false;
330   const NodeList& lst = myToolBars[tId].nodes;
331   for ( NodeList::const_iterator it = lst.begin(); it != lst.end() && !vis; ++it )
332   {
333     const ToolNode& node = *it;
334     if ( node.id == id )
335       vis = node.visible;
336   }
337   return vis;
338 }
339
340 void QtxActionToolMgr::setVisible( const int id, const int tId, const bool on )
341 {
342   if ( !myToolBars.contains( tId ) )
343     return;
344
345   bool changed = false;
346   NodeList& lst = myToolBars[tId].nodes;
347   for ( NodeList::iterator it = lst.begin(); it != lst.end(); ++it )
348   {
349     ToolNode& node = *it;
350     if ( node.id == id )
351     {
352       changed = changed || node.visible != on;
353       node.visible = on;
354     }
355   }
356
357   if ( changed )
358     updateToolBar( tId );
359 }
360
361 bool QtxActionToolMgr::load( const QString& fname, QtxActionMgr::Reader& r )
362 {
363   ToolCreator cr( &r, this );
364   return r.read( fname, cr );
365 }
366
367
368 /*!
369         Class: QtxActionToolMgr::ToolCreator
370         Level: Public
371 */
372 QtxActionToolMgr::ToolCreator::ToolCreator( QtxActionMgr::Reader* r,
373                                             QtxActionToolMgr* mgr )
374 : QtxActionMgr::Creator( r ),
375   myMgr( mgr )
376 {
377 }
378
379 QtxActionToolMgr::ToolCreator::~ToolCreator()
380 {
381 }
382
383 int QtxActionToolMgr::ToolCreator::append( const QString& tag, const bool subMenu,
384                                            const ItemAttributes& attr, const int tId )
385 {  
386   if( !myMgr || !reader() )
387     return -1;
388
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"    );
398
399   int res = -1, actId = intValue( attr, id, -1 );
400   if( tId==-1 )
401     res = myMgr->createToolBar( strValue( attr, label ), intValue( attr, id, -1 ) );
402   else if( tag==sep )
403     res = myMgr->insert( separator(), tId, intValue( attr, pos, -1 ) );
404   else
405   {
406     QPixmap pix; QIconSet set;
407     QString name = strValue( attr, icon );
408     if( !name.isEmpty() && loadPixmap( name, pix ) )
409       set = QIconSet( pix );
410
411     QtxAction* newAct = new QtxAction( strValue( attr, tooltip ), set,
412                                        strValue( attr, label ), 
413                                        QKeySequence( strValue( attr, accel ) ),
414                                        myMgr );
415     QString toggleact = strValue( attr, toggle );
416     newAct->setToggleAction( !toggleact.isEmpty() );
417     newAct->setOn( toggleact.lower()=="true" );
418         
419     connect( newAct );
420     int aid = myMgr->registerAction( newAct, actId );
421     res = myMgr->insert( aid, tId, intValue( attr, pos, -1 ) );
422   }
423
424   return res;
425 }
426
427