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