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