Salome HOME
Dialog SetupCurveDlg has been added
[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 bool CAM_Module::activateModule( SUIT_Study* study )
93 {
94   return true;
95 }
96
97 bool CAM_Module::deactivateModule( SUIT_Study* )
98 {
99   return true;
100 }
101
102 void CAM_Module::studyClosed( SUIT_Study* study )
103 {
104   CAM_Study* camDoc = dynamic_cast<CAM_Study*>( study );
105   if ( !camDoc ) 
106     return;
107
108   if ( camDoc->containsDataModel( dataModel() ) )
109     camDoc->removeDataModel( dataModel() );
110 }
111
112 void CAM_Module::studyChanged( SUIT_Study* , SUIT_Study* )
113 {
114 }
115
116 CAM_DataModel* CAM_Module::createDataModel()
117
118   return new CAM_DataModel( this );
119 }
120
121 void CAM_Module::setModuleName( const QString& name )
122 {
123   myName = name;
124 }
125
126 void CAM_Module::setModuleIcon( const QPixmap& icon )
127 {
128   myIcon = icon;
129 }
130
131 QtxActionMenuMgr* CAM_Module::menuMgr() const
132 {
133   QtxActionMenuMgr* mgr = 0;
134   if ( application() && application()->desktop() )
135     mgr = application()->desktop()->menuMgr();
136   return mgr;
137 }
138
139 QtxActionToolMgr* CAM_Module::toolMgr() const
140 {
141   QtxActionToolMgr* mgr = 0;
142   if ( application() && application()->desktop() )
143     mgr = application()->desktop()->toolMgr();
144   return mgr;
145 }
146
147 int CAM_Module::createTool( const QString& name )
148 {
149   if ( !toolMgr() )
150     return -1;
151
152   return toolMgr()->createToolBar( name );
153 }
154
155 int CAM_Module::createTool( QAction* a, const int tBar, const int id, const int idx )
156 {
157   if ( !toolMgr() )
158     return -1;
159
160   int regId = registerAction( id, a );
161   int intId = toolMgr()->insert( a, tBar, idx );
162   return intId != -1 ? regId : -1;
163 }
164
165 int CAM_Module::createTool( QAction* a, const QString& tBar, const int id, const int idx )
166 {
167   if ( !toolMgr() )
168     return -1;
169
170   int regId = registerAction( id, a );
171   int intId = toolMgr()->insert( a, tBar, idx );
172   return intId != -1 ? regId : -1;
173 }
174
175 int CAM_Module::createTool( const int id, const int tBar, const int idx )
176 {
177   if ( !toolMgr() )
178     return -1;
179
180   int intId = toolMgr()->insert( action( id ), tBar, idx );
181   return intId != -1 ? id : -1;
182 }
183
184 int CAM_Module::createTool( const int id, const QString& tBar, const int idx )
185 {
186   if ( !toolMgr() )
187     return -1;
188
189   int intId = toolMgr()->insert( action( id ), tBar, idx );
190   return intId != -1 ? id : -1;
191 }
192
193 int CAM_Module::createMenu( const QString& subMenu, const int menu,
194                             const int id, const int group, const int index )
195 {
196   if ( !menuMgr() )
197     return -1;
198
199   return menuMgr()->insert( subMenu, menu, group, index );
200 }
201
202 int CAM_Module::createMenu( const QString& subMenu, const QString& menu,
203                             const int id, const int group, const int index )
204 {
205   if ( !menuMgr() )
206     return -1;
207
208   return menuMgr()->insert( subMenu, menu, group, index );
209 }
210
211 int CAM_Module::createMenu( QAction* a, const int menu, const int id, const int group, const int index )
212 {
213   if ( !a || !menuMgr() )
214     return -1;
215
216   int regId = registerAction( id, a );
217   int intId = menuMgr()->insert( a, menu, group, index );
218   return intId != -1 ? regId : -1;
219 }
220
221 int CAM_Module::createMenu( QAction* a, const QString& menu, const int id, const int group, const int index )
222 {
223   if ( !a || !menuMgr() )
224     return -1;
225
226   int regId = registerAction( id, a );
227   int intId = menuMgr()->insert( a, menu, group, index );
228   return intId != -1 ? regId : -1;
229 }
230
231 int CAM_Module::createMenu( const int id, const int menu, const int group, const int index )
232 {
233   if ( !menuMgr() )
234     return -1;
235
236   int intId = menuMgr()->insert( action( id ), menu, group, index );
237   return intId != -1 ? id : -1;
238 }
239
240 int CAM_Module::createMenu( const int id, const QString& menu, const int group, const int index )
241 {
242   if ( !menuMgr() )
243     return -1;
244
245   int intId = menuMgr()->insert( action( id ), menu, group, index );
246   return intId != -1 ? id : -1;
247 }
248
249 void CAM_Module::setMenuShown( const bool on )
250 {
251   QtxActionMenuMgr* mMgr = menuMgr();
252   if ( !mMgr )
253     return;
254
255   bool upd = mMgr->isUpdatesEnabled();
256   mMgr->setUpdatesEnabled( false );
257
258   QAction* sep = separator();
259   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
260   {
261     if ( it.data() != sep )
262       mMgr->setShown( mMgr->actionId( it.data() ), on );
263   }
264
265   mMgr->setUpdatesEnabled( upd );
266   if ( upd )
267     mMgr->update();
268 }
269
270 void CAM_Module::setMenuShown( QAction* a, const bool on )
271 {
272   if ( menuMgr() )
273     menuMgr()->setShown( menuMgr()->actionId( a ), on );
274 }
275
276 void CAM_Module::setMenuShown( const int id, const bool on )
277 {
278   setMenuShown( action( id ), on );
279 }
280
281 void CAM_Module::setToolShown( const bool on )
282 {
283   QtxActionToolMgr* tMgr = toolMgr();
284   if ( !tMgr )
285     return;
286
287   bool upd = tMgr->isUpdatesEnabled();
288   tMgr->setUpdatesEnabled( false );
289
290   QAction* sep = separator();
291   for ( QMap<int, QAction*>::Iterator it = myActionMap.begin(); it != myActionMap.end(); ++it )
292   {
293     if ( it.data() != sep )
294       tMgr->setShown( tMgr->actionId( it.data() ), on );
295   }
296
297   tMgr->setUpdatesEnabled( upd );
298   if ( upd )
299     tMgr->update();
300 }
301
302 void CAM_Module::setToolShown( QAction* a, const bool on )
303 {
304   if ( toolMgr() )
305     toolMgr()->setShown( toolMgr()->actionId( a ), on );
306 }
307
308 void CAM_Module::setToolShown( const int id, const bool on )
309 {
310   setToolShown( action( id ), on );
311 }
312
313 QAction* CAM_Module::action( const int id ) const
314 {
315   QAction* a = 0;
316   if ( myActionMap.contains( id ) )
317     a = myActionMap[id];
318   return a;
319 }
320
321 int CAM_Module::actionId( const QAction* a ) const
322 {
323   int id = -1;
324   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && id == -1; ++it )
325   {
326     if ( it.data() == a )
327       id = it.key();
328   }
329   return id;
330 }
331
332 QAction* CAM_Module::createAction( const int id, const QString& text, const QIconSet& icon,
333                                    const QString& menu, const QString& tip, const int key,
334                                    QObject* parent, const bool toggle, QObject* reciever, const char* member )
335 {
336   QtxAction* a = new QtxAction( text, icon, menu, key, parent, 0, toggle );
337   a->setStatusTip( tip );
338
339   if ( reciever && member )
340     connect( a, SIGNAL( activated() ), reciever, member );
341
342   registerAction( id, a );
343
344   return a;
345 }
346
347 int CAM_Module::registerAction( const int id, QAction* a )
348 {
349   int ident = -1;
350   for ( QMap<int, QAction*>::ConstIterator it = myActionMap.begin(); it != myActionMap.end() && ident == -1; ++it )
351     if ( it.data() == a )
352       ident = it.key();
353
354   if ( ident != -1 )
355     return ident;
356
357   static int generatedId = -1;
358   ident = id < 0 ? --generatedId : id;
359
360   myActionMap.insert( id, a );
361
362   if ( menuMgr() )
363     menuMgr()->registerAction( a );
364
365   if ( toolMgr() )
366     toolMgr()->registerAction( a );
367
368   return ident;
369 }
370
371 QAction* CAM_Module::separator()
372 {
373   return QtxActionMgr::separator();
374 }