Salome HOME
Facilate action processing with ModuleBase_ActionInfo
[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 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
329 {
330   return addFeature(theWBName,
331                     theInfo.id,
332                     theInfo.text,
333                     theInfo.toolTip,
334                     theInfo.icon,
335                     theInfo.shortcut);
336 }
337
338
339 //******************************************************
340 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
341                                            const QString& theTip, const QIcon& theIcon,
342                                            const QKeySequence& theKeys, bool isCheckable,
343                                            const char* theMenuSourceText, const int theMenuPosition)
344 {
345   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
346
347   int aId = myActionsList.size();
348   myActionsList.append(theId);
349   SUIT_Desktop* aDesk = application()->desktop();
350   int aKeys = 0;
351   for (unsigned int i = 0; i < theKeys.count(); i++)
352     aKeys += theKeys[i];
353   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
354                                   isCheckable);
355   aAction->setData(theId);
356   createMenu(aId, aMenu, theMenuPosition);
357   return aAction;
358 }
359
360 //******************************************************
361 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
362 {
363   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
364   createMenu(separator(), aMenu, -1, theMenuPosition);
365 }
366
367 //******************************************************
368 QList<QAction*> NewGeom_Module::commandList() const
369 {
370   QList<QAction*> aActions;
371   for (int i = 0; i < myActionsList.size(); i++)
372     aActions.append(action(i));
373   return aActions;
374 }
375
376 //******************************************************
377 QStringList NewGeom_Module::commandIdList() const
378 {
379   return myActionsList;
380 }
381
382 //******************************************************
383 QMainWindow* NewGeom_Module::desktop() const
384 {
385   return application()->desktop();
386 }
387
388 //******************************************************
389 QString NewGeom_Module::commandId(const QAction* theCmd) const
390 {
391   int aId = actionId(theCmd);
392   if (aId < myActionsList.size())
393     return myActionsList[aId];
394   return QString();
395 }
396
397 //******************************************************
398 QAction* NewGeom_Module::command(const QString& theId) const
399 {
400   int aId = myActionsList.indexOf(theId);
401   if ((aId != -1) && (aId < myActionsList.size())) {
402     return action(aId);
403   }
404   return 0;
405 }
406
407 //******************************************************
408 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
409 {
410   myNestedActions[theId] = theActions;
411 }
412
413 //******************************************************
414 QStringList NewGeom_Module::nestedActions(const QString& theId) const
415 {
416   if (myNestedActions.contains(theId))
417     return myNestedActions[theId];
418   return QStringList();
419 }
420
421 //******************************************************
422 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
423 {
424   myDocumentType[theId] = theKind;
425 }
426
427 //******************************************************
428 QString NewGeom_Module::documentKind(const QString& theId) const
429 {
430   if (myDocumentType.contains(theId))
431     return myDocumentType[theId];
432   return QString();
433
434 }
435
436 //******************************************************
437 void NewGeom_Module::selectionChanged()
438 {
439   LightApp_Module::selectionChanged();
440   myWorkshop->salomeViewerSelectionChanged();
441 }
442
443 //******************************************************
444 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
445 {
446   myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
447   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
448 }
449
450
451 //******************************************************
452 void NewGeom_Module::createPreferences()
453 {
454   LightApp_Preferences* pref = preferences();
455   if (!pref)
456     return;
457   ModuleBase_Preferences::updateConfigByResources();
458   QString aModName = moduleName();
459
460   QtxPreferenceItem* item = pref->findItem(aModName, true );
461   if ( item && (!item->isEmpty() )) {
462     item->parentItem()->removeItem(item);
463     delete item;
464   }
465
466   int catId = pref->addPreference(aModName, -1 );
467   if ( catId == -1 )
468     return;
469   NewGeom_PrefMgr aMgr(pref, aModName);
470   ModuleBase_Preferences::createEditContent(&aMgr, catId);
471   pref->retrieve();
472 }
473
474 //******************************************************
475 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
476 {
477   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
478   QString aVal = aResMgr->stringValue(theSection, theParam);
479   Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
480   std::string aValue = aVal.toStdString();
481   if (aValue.empty()) {
482     aValue = aProp->defaultValue();
483     aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
484
485     LightApp_Preferences* pref = preferences();
486     if (pref)
487       pref->retrieve();
488   }
489   aProp->setValue(aValue);
490
491 }
492
493 void NewGeom_Module::inspectSalomeModules()
494 {
495   QStringList aModuleNames;
496   getApp()->modules(aModuleNames, false);
497   foreach(QString eachModule, aModuleNames) {
498     Config_ModuleReader::addDependencyModule(eachModule.toStdString());
499   }
500 }