Salome HOME
Initial version
[modules/gui.git] / src / CAM / CAM_Module.cxx
1 #include "CAM_Module.h"
2
3 #include "CAM_DataModel.h"
4 #include "CAM_Application.h"
5 #include "CAM_Study.h"
6
7 #include <QtxAction.h>
8 #include <QtxActionMenuMgr.h>
9 #include <QtxActionToolMgr.h>
10
11 static const char* ModuleIcon[] = {
12 "20 20 2 1",
13 "       c None",
14 ".      c #000000",
15 "                    ",
16 "                    ",
17 "                    ",
18 " .................. ",
19 " .                . ",
20 " .                . ",
21 " .                . ",
22 " .                . ",
23 " .                . ",
24 " .                . ",
25 " .                . ",
26 " .                . ",
27 " .................. ",
28 "    .     .     .   ",
29 "    .     .     .   ",
30 "   ...   ...   ...  ",
31 "  .. .. .. .. .. .. ",
32 "  .   . .   . .   . ",
33 "  .. .. .. .. .. .. ",
34 "   ...   ...   ...  "};
35
36 QPixmap MYPixmap( ModuleIcon );
37
38 CAM_Module::CAM_Module()
39 : QObject(),
40 myApp( 0 ),
41 myIcon( MYPixmap ),
42 myDataModel( 0 )
43 {
44 }
45
46 CAM_Module::CAM_Module( const QString& name )
47 : QObject(),
48 myApp( 0 ),
49 myName( name ),
50 myIcon( MYPixmap ),
51 myDataModel( 0 )
52 {
53 }
54
55 CAM_Module::~CAM_Module()
56 {
57   delete myDataModel;
58   myDataModel = 0;
59 }
60
61 void CAM_Module::initialize( CAM_Application* app )
62 {
63   myApp = app;
64 }
65
66 QPixmap CAM_Module::moduleIcon() const
67 {
68   return myIcon;
69 }
70
71 QString CAM_Module::moduleName() const
72 {
73   return myName;
74 }
75
76 CAM_DataModel* CAM_Module::dataModel() const
77 {
78   if ( !myDataModel )
79   {
80     CAM_Module* that = (CAM_Module*)this;
81     that->myDataModel = that->createDataModel();
82     that->myDataModel->initialize();
83   }
84   return myDataModel;
85 }
86
87 CAM_Application* CAM_Module::application() const
88 {
89   return myApp;
90 }
91
92 void CAM_Module::activateModule( SUIT_Study* study )
93 {
94 }
95
96 void CAM_Module::deactivateModule( SUIT_Study* )
97 {
98 }
99
100 void CAM_Module::studyClosed( SUIT_Study* study )
101 {
102   CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
103   if ( !camDoc ) 
104     return;
105
106   if ( camDoc->containsDataModel( dataModel() ) )
107     camDoc->removeDataModel( dataModel() );
108 }
109
110 void CAM_Module::studyChanged( SUIT_Study* , SUIT_Study* )
111 {
112 }
113
114 CAM_DataModel* CAM_Module::createDataModel()
115
116   return new CAM_DataModel( this );
117 }
118
119 void CAM_Module::setModuleName( const QString& name )
120 {
121   myName = name;
122 }
123
124 void CAM_Module::setModuleIcon( const QPixmap& icon )
125 {
126   myIcon = icon;
127 }
128
129 QtxActionMenuMgr* CAM_Module::menuMgr() const
130 {
131   QtxActionMenuMgr* mgr = 0;
132   if ( application() && application()->desktop() )
133     mgr = application()->desktop()->menuMgr();
134   return mgr;
135 }
136
137 QtxActionToolMgr* CAM_Module::toolMgr() const
138 {
139   QtxActionToolMgr* mgr = 0;
140   if ( application() && application()->desktop() )
141     mgr = application()->desktop()->toolMgr();
142   return mgr;
143 }
144
145 int CAM_Module::createTool( const QString& name )
146 {
147   if ( !toolMgr() )
148     return -1;
149
150   return toolMgr()->createToolBar( name );
151 }
152
153 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
154 {
155   if ( !toolMgr() )
156     return -1;
157
158   int regId = registerAction( id, a );
159   int intId = toolMgr()->insert( a, tBar, idx );
160   return intId != -1 ? regId : -1;
161 }
162
163 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
164 {
165   if ( !toolMgr() )
166     return -1;
167
168   int regId = registerAction( id, a );
169   int intId = toolMgr()->insert( a, tBar, idx );
170   return intId != -1 ? regId : -1;
171 }
172
173 int CAM_Module::createTool( const int id, const int tBar, const int idx )
174 {
175   if ( !toolMgr() )
176     return -1;
177
178   int intId = toolMgr()->insert( action( id ), tBar, idx );
179   return intId != -1 ? id : -1;
180 }
181
182 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
183 {
184   if ( !toolMgr() )
185     return -1;
186
187   int intId = toolMgr()->insert( action( id ), tBar, idx );
188   return intId != -1 ? id : -1;
189 }
190
191 int CAM_Module::createMenu( const QString& subMenu, const int menu,
192                             const int id, const int group, const int index )
193 {
194   if ( !menuMgr() )
195     return -1;
196
197   return menuMgr()->insert( subMenu, menu, group, index );
198 }
199
200 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
201                             const int id, const int group, const int index )
202 {
203   if ( !menuMgr() )
204     return -1;
205
206   return menuMgr()->insert( subMenu, menu, group, index );
207 }
208
209 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
210 {
211   if ( !a || !menuMgr() )
212     return -1;
213
214   int regId = registerAction( id, a );
215   int intId = menuMgr()->insert( a, menu, group, index );
216   return intId != -1 ? regId : -1;
217 }
218
219 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
220 {
221   if ( !a || !menuMgr() )
222     return -1;
223
224   int regId = registerAction( id, a );
225   int intId = menuMgr()->insert( a, menu, group, index );
226   return intId != -1 ? regId : -1;
227 }
228
229 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
230 {
231   if ( !menuMgr() )
232     return -1;
233
234   int intId = menuMgr()->insert( action( id ), menu, group, index );
235   return intId != -1 ? id : -1;
236 }
237
238 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
239 {
240   if ( !menuMgr() )
241     return -1;
242
243   int intId = menuMgr()->insert( action( id ), menu, group, index );
244   return intId != -1 ? id : -1;
245 }
246
247 void CAM_Module::setMenuShown( const bool on )
248 {
249   QtxActionMenuMgr* mMgr = menuMgr();
250   if ( !mMgr )
251     return;
252
253   bool upd = mMgr->isUpdatesEnabled();
254   mMgr->setUpdatesEnabled( false );
255
256   QAction* sep = separator();
257   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
258   {
259     if ( it.data() != sep )
260       mMgr->setShown( mMgr->actionId( it.data() ), on );
261   }
262
263   mMgr->setUpdatesEnabled( upd );
264   if ( upd )
265     mMgr->update();
266 }
267
268 void CAM_Module::setMenuShown( QAction* a, const bool on )
269 {
270   if ( menuMgr() )
271     menuMgr()->setShown( menuMgr()->actionId( a ), on );
272 }
273
274 void CAM_Module::setMenuShown( const int id, const bool on )
275 {
276   setMenuShown( action( id ), on );
277 }
278
279 void CAM_Module::setToolShown( const bool on )
280 {
281   QtxActionToolMgr* tMgr = toolMgr();
282   if ( !tMgr )
283     return;
284
285   bool upd = tMgr->isUpdatesEnabled();
286   tMgr->setUpdatesEnabled( false );
287
288   QAction* sep = separator();
289   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
290   {
291     if ( it.data() != sep )
292       tMgr->setShown( tMgr->actionId( it.data() ), on );
293   }
294
295   tMgr->setUpdatesEnabled( upd );
296   if ( upd )
297     tMgr->update();
298 }
299
300 void CAM_Module::setToolShown( QAction* a, const bool on )
301 {
302   if ( toolMgr() )
303     toolMgr()->setShown( toolMgr()->actionId( a ), on );
304 }
305
306 void CAM_Module::setToolShown( const int id, const bool on )
307 {
308   setToolShown( action( id ), on );
309 }
310
311 QAction* CAM_Module::action( const int id ) const
312 {
313   QAction* a = 0;
314   if ( myActionMap.contains( id ) )
315     a = myActionMap[id];
316   return a;
317 }
318
319 int CAM_Module::actionId( const QAction* a ) const
320 {
321   int id = -1;
322   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
323   {
324     if ( it.data() == a )
325       id = it.key();
326   }
327   return id;
328 }
329
330 QAction* CAM_Module::createAction( const int id, const QString& text, const QIconSet& icon,
331                                    const QString& menu, const QString& tip, const int key,
332                                    QObject* parent, const bool toggle, QObject* reciever, const char* member )
333 {
334   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
335   a->setStatusTip( tip );
336
337   if ( reciever && member )
338     connect( a, SIGNAL( activated() ), reciever, member );
339
340   registerAction( id, a );
341
342   return a;
343 }
344
345 int CAM_Module::registerAction( const int id, QAction* a )
346 {
347   int ident = -1;
348   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
349     if ( it.data() == a )
350       ident = it.key();
351
352   if ( ident != -1 )
353     return ident;
354
355   static int generatedId = -1;
356   ident = id < 0 ? --generatedId : id;
357
358   myActionMap.insert( id, a );
359
360   if ( menuMgr() )
361     menuMgr()->registerAction( a );
362
363   if ( toolMgr() )
364     toolMgr()->registerAction( a );
365
366   return ident;
367 }
368
369 QAction* CAM_Module::separator()
370 {
371   return QtxActionMgr::separator();
372 }