Salome HOME
Update code documentation
[modules/shaper.git] / src / NewGeom / NewGeom_Module.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3
4 #include "NewGeom_Module.h"
5 #include "NewGeom_DataModel.h"
6 #include "NewGeom_OCCSelector.h"
7
8 #include <XGUI_Workshop.h>
9 #include <XGUI_PropertyPanel.h>
10 #include <XGUI_ContextMenuMgr.h>
11 #include <XGUI_ObjectsBrowser.h>
12 #include <XGUI_OperationMgr.h>
13 #include <XGUI_Displayer.h>
14
15 #include <ModuleBase_Operation.h>
16 #include <ModuleBase_Preferences.h>
17
18 #include <LightApp_Application.h>
19 #include <LightApp_SelectionMgr.h>
20 #include <LightApp_OCCSelector.h>
21 #include <LightApp_Study.h>
22 #include <OCCViewer_ViewModel.h>
23
24 #include <SUIT_Selector.h>
25 #include <SUIT_Desktop.h>
26 #include <SUIT_ViewManager.h>
27 #include <SUIT_ResourceMgr.h>
28
29 #include <QtxPopupMgr.h>
30 #include <QtxActionMenuMgr.h>
31 #include <QtxResourceMgr.h>
32
33 #include <Config_PropManager.h>
34 #include <Config_ModuleReader.h>
35
36 #include <QDockWidget>
37 #include <QAction>
38 #include <QTimer>
39
40
41 extern "C" {
42 NewGeom_EXPORT CAM_Module* createModule()
43 {
44   return new NewGeom_Module();
45 }
46
47 NewGeom_EXPORT char* getModuleVersion()
48 {
49   return "0.0";
50 }
51 }
52
53 /** 
54 * Class for preferences management
55 */
56 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
57 {
58 public:
59   /// Constructor
60   /// \param theMgr preferences manager of SALOME
61   /// \param theModName name of the module
62   NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
63
64   virtual int addPreference(const QString& theLbl, int pId, 
65                             SUIT_PreferenceMgr::PrefItemType theType,
66                             const QString& theSection, const QString& theName )
67   {
68     return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
69   }
70
71   virtual void setItemProperty(const QString& thePropName,
72                                const QVariant& theValue,
73                                const int theId = -1)
74   {
75     myMgr->setItemProperty(thePropName, theValue, theId);
76   }
77
78
79   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
80
81 private:
82   LightApp_Preferences* myMgr;
83   QString myModName;
84 };
85
86
87
88
89 //******************************************************
90 NewGeom_Module::NewGeom_Module()
91     : LightApp_Module("NewGeom"),
92       mySelector(0), myIsOpened(0), myPopupMgr(0)
93 {
94   myWorkshop = new XGUI_Workshop(this);
95   connect(myWorkshop, SIGNAL(commandStatusUpdated()),
96           this, SLOT(onUpdateCommandStatus()));
97
98   myProxyViewer = new NewGeom_SalomeViewer(this);
99
100   ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
101   ModuleBase_Preferences::loadCustomProps();
102 }
103
104 //******************************************************
105 NewGeom_Module::~NewGeom_Module()
106 {
107 }
108
109 //******************************************************
110 void NewGeom_Module::initialize(CAM_Application* theApp)
111 {
112   LightApp_Module::initialize(theApp);
113   inspectSalomeModules();
114
115   myWorkshop->startApplication();
116   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
117   if (anApp)
118     connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
119 }
120
121 //******************************************************
122 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
123 {
124   theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
125 }
126
127 //******************************************************
128 void NewGeom_Module::viewManagers(QStringList& theList) const
129 {
130   theList.append(OCCViewer_Viewer::Type());
131 }
132
133 //******************************************************
134 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
135 {
136   bool isDone = LightApp_Module::activateModule(theStudy);
137   if (isDone) {
138     setMenuShown(true);
139     setToolShown(true);
140
141     QObject* aObj = myWorkshop->objectBrowser()->parent();
142     QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
143     if (aObjDoc) {
144       QAction* aViewAct = aObjDoc->toggleViewAction();
145       aViewAct->setEnabled(true);
146       myWorkshop->objectBrowser()->setVisible(true);
147       aObjDoc->setVisible(true);
148     }
149
150     if (!mySelector) {
151       ViewManagerList OCCViewManagers;
152       application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
153       if (OCCViewManagers.size() > 0) {
154         mySelector = createSelector(OCCViewManagers.first());
155       }
156     }
157     //action(myEraseAll)->setEnabled(false);
158
159     if (myIsOpened) {
160       myWorkshop->objectBrowser()->rebuildDataTree();
161       myWorkshop->updateCommandStatus();
162       myIsOpened = false;
163       QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
164     }
165     else
166       myWorkshop->updateCommandStatus();
167   }
168   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
169   myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
170   myIsEditEnabled = getApp()->isEditEnabled();
171   getApp()->setEditEnabled(false);
172
173   // this following row is caused by #187 bug.
174   // SALOME saves the dock widget positions before deactivateModule() and
175   // load it after the module activation. So, if the panel is visible before
176   // deactivate, it becomes visible after activate.
177   // In order to avoid the visible property panel, the widget position save is
178   // switch off in this module
179   aResMgr->setValue("Study", "store_positions", false);
180
181   return isDone;
182 }
183
184 //******************************************************
185 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
186 {
187   setMenuShown(false);
188   setToolShown(false);
189
190   QObject* aObj = myWorkshop->objectBrowser()->parent();
191   QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
192   if (aObjDoc) {
193     aObjDoc->setVisible(false);
194     myWorkshop->objectBrowser()->setVisible(false);
195     QAction* aViewAct = aObjDoc->toggleViewAction();
196     aViewAct->setEnabled(false);
197   }
198
199   // the active operation should be stopped for the next activation.
200   // There should not be active operation and visualized preview.
201   // Abort operation should be performed before the selection's remove
202   // because the displayed objects should be removed from the viewer, but
203   // the AIS context is obtained from the selector.
204   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
205   while (anOperation) {
206     anOperation->abort();
207     anOperation = myWorkshop->operationMgr()->currentOperation();
208   }
209   // Delete selector because it has to be redefined on next activation
210   if (mySelector) {
211     myProxyViewer->setSelector(0);
212     delete mySelector;
213     mySelector = 0;
214   }
215
216   //myWorkshop->contextMenuMgr()->disconnectViewer();
217
218   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
219   aResMgr->setValue("Study", "store_positions", myIsStorePositions);
220   getApp()->setEditEnabled(myIsEditEnabled);
221
222   return LightApp_Module::deactivateModule(theStudy);
223 }
224
225 //******************************************************
226 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
227 {
228   if (!mySelector) {
229     mySelector = createSelector(theMgr);
230   }
231 }
232
233 //******************************************************
234 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
235 {
236   if (mySelector) {
237     if (theMgr->getType() == OCCViewer_Viewer::Type()) {
238       OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
239       if (mySelector->viewer() == aViewer) {
240         myWorkshop->displayer()->eraseAll(false);
241         myProxyViewer->setSelector(0);
242         delete mySelector;
243         mySelector = 0;
244       }
245     }
246   }
247 }
248
249 //******************************************************
250 QtxPopupMgr* NewGeom_Module::popupMgr()
251 {
252   if (!myPopupMgr)
253     myPopupMgr = new QtxPopupMgr( 0, this );
254   return myPopupMgr;
255 }
256
257 //******************************************************
258 void NewGeom_Module::onDefaultPreferences()
259 {
260   ModuleBase_Preferences::resetConfig();
261   ModuleBase_Preferences::updateResourcesByConfig();
262
263   LightApp_Preferences* pref = preferences();
264   if (pref)
265     pref->retrieve();
266 }
267
268 //******************************************************
269 void NewGeom_Module::onUpdateCommandStatus()
270 {
271   getApp()->updateActions();
272 }
273
274 //******************************************************
275 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
276 {
277   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
278     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
279     NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
280     LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
281     QList<SUIT_Selector*> aList;
282     aMgr->selectors(aList);
283     foreach(SUIT_Selector* aSel, aList)
284     {
285       aSel->setEnabled(aSel == aSelector);
286     }
287     myProxyViewer->setSelector(aSelector);
288     return aSelector;
289   }
290   return 0;
291 }
292
293 //******************************************************
294 CAM_DataModel* NewGeom_Module::createDataModel()
295 {
296   NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
297
298   // Calling addComponent() for persistent functionality work in the SalomeApp_Study
299   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
300   aStudy->addComponent(aDataModel);
301
302   return aDataModel;
303 }
304
305 //******************************************************
306 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
307                                     const QString& theTitle, const QString& theTip,
308                                     const QIcon& theIcon, const QKeySequence& theKeys,
309                                     bool isCheckable)
310 {
311   int aMenu = createMenu(theWBName, -1, -1, 50);
312   int aTool = createTool(theWBName);
313
314   int aId = myActionsList.size();
315   myActionsList.append(theId);
316   SUIT_Desktop* aDesk = application()->desktop();
317   int aKeys = 0;
318   for (unsigned int i = 0; i < theKeys.count(); i++)
319     aKeys += theKeys[i];
320   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
321                                   isCheckable);
322   aAction->setData(theId);
323   int aItemId = createMenu(aId, aMenu, -1, 10);
324   int aToolId = createTool(aId, aTool);
325   return aAction;
326 }
327
328 //******************************************************
329 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
330                                            const QString& theTip, const QIcon& theIcon,
331                                            const QKeySequence& theKeys, bool isCheckable,
332                                            const char* theMenuSourceText, const int theMenuPosition)
333 {
334   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
335
336   int aId = myActionsList.size();
337   myActionsList.append(theId);
338   SUIT_Desktop* aDesk = application()->desktop();
339   int aKeys = 0;
340   for (unsigned int i = 0; i < theKeys.count(); i++)
341     aKeys += theKeys[i];
342   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
343                                   isCheckable);
344   aAction->setData(theId);
345   createMenu(aId, aMenu, theMenuPosition);
346   return aAction;
347 }
348
349 //******************************************************
350 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
351 {
352   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
353   createMenu(separator(), aMenu, -1, theMenuPosition);
354 }
355
356 //******************************************************
357 QList<QAction*> NewGeom_Module::commandList() const
358 {
359   QList<QAction*> aActions;
360   for (int i = 0; i < myActionsList.size(); i++)
361     aActions.append(action(i));
362   return aActions;
363 }
364
365 //******************************************************
366 QStringList NewGeom_Module::commandIdList() const
367 {
368   return myActionsList;
369 }
370
371 //******************************************************
372 QMainWindow* NewGeom_Module::desktop() const
373 {
374   return application()->desktop();
375 }
376
377 //******************************************************
378 QString NewGeom_Module::commandId(const QAction* theCmd) const
379 {
380   int aId = actionId(theCmd);
381   if (aId < myActionsList.size())
382     return myActionsList[aId];
383   return QString();
384 }
385
386 //******************************************************
387 QAction* NewGeom_Module::command(const QString& theId) const
388 {
389   int aId = myActionsList.indexOf(theId);
390   if ((aId != -1) && (aId < myActionsList.size())) {
391     return action(aId);
392   }
393   return 0;
394 }
395
396 //******************************************************
397 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
398 {
399   myNestedActions[theId] = theActions;
400 }
401
402 //******************************************************
403 QStringList NewGeom_Module::nestedActions(const QString& theId) const
404 {
405   if (myNestedActions.contains(theId))
406     return myNestedActions[theId];
407   return QStringList();
408 }
409
410 //******************************************************
411 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
412 {
413   myDocumentType[theId] = theKind;
414 }
415
416 //******************************************************
417 QString NewGeom_Module::documentKind(const QString& theId) const
418 {
419   if (myDocumentType.contains(theId))
420     return myDocumentType[theId];
421   return QString();
422
423 }
424
425 //******************************************************
426 void NewGeom_Module::selectionChanged()
427 {
428   LightApp_Module::selectionChanged();
429   myWorkshop->salomeViewerSelectionChanged();
430 }
431
432 //******************************************************
433 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
434 {
435   myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
436   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
437 }
438
439
440 //******************************************************
441 void NewGeom_Module::createPreferences()
442 {
443   LightApp_Preferences* pref = preferences();
444   if (!pref)
445     return;
446   ModuleBase_Preferences::updateConfigByResources();
447   QString aModName = moduleName();
448
449   QtxPreferenceItem* item = pref->findItem(aModName, true );
450   if ( item && (!item->isEmpty() )) {
451     item->parentItem()->removeItem(item);
452     delete item;
453   }
454
455   int catId = pref->addPreference(aModName, -1 );
456   if ( catId == -1 )
457     return;
458   NewGeom_PrefMgr aMgr(pref, aModName);
459   ModuleBase_Preferences::createEditContent(&aMgr, catId);
460   pref->retrieve();
461 }
462
463 //******************************************************
464 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
465 {
466   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
467   QString aVal = aResMgr->stringValue(theSection, theParam);
468   Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
469   std::string aValue = aVal.toStdString();
470   if (aValue.empty()) {
471     aValue = aProp->defaultValue();
472     aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
473
474     LightApp_Preferences* pref = preferences();
475     if (pref)
476       pref->retrieve();
477   }
478   aProp->setValue(aValue);
479
480 }
481
482 void NewGeom_Module::inspectSalomeModules()
483 {
484   QStringList aModuleNames;
485   getApp()->modules(aModuleNames, false);
486   foreach(QString eachModule, aModuleNames) {
487     Config_ModuleReader::addDependencyModule(eachModule.toStdString());
488   }
489 }