Salome HOME
Process history pointer
[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 #include <NewGeom_NestedButton.h>
8
9 #include <XGUI_Workshop.h>
10 #include <XGUI_PropertyPanel.h>
11 #include <XGUI_ContextMenuMgr.h>
12 #include <XGUI_ObjectsBrowser.h>
13 #include <XGUI_OperationMgr.h>
14 #include <XGUI_Displayer.h>
15
16 #include <ModuleBase_Operation.h>
17 #include <ModuleBase_Preferences.h>
18 #include <ModuleBase_ActionInfo.h>
19
20 #include <LightApp_Application.h>
21 #include <LightApp_SelectionMgr.h>
22 #include <LightApp_OCCSelector.h>
23 #include <LightApp_Study.h>
24 #include <OCCViewer_ViewModel.h>
25
26 #include <SUIT_Selector.h>
27 #include <SUIT_Desktop.h>
28 #include <SUIT_ViewManager.h>
29 #include <SUIT_ResourceMgr.h>
30
31 #include <QtxPopupMgr.h>
32 #include <QtxActionMenuMgr.h>
33 #include <QtxResourceMgr.h>
34
35 #include <Config_PropManager.h>
36 #include <Config_ModuleReader.h>
37
38 #include <AIS_ListOfInteractive.hxx>
39 #include <AIS_ListIteratorOfListOfInteractive.hxx>
40
41 #include <QDockWidget>
42 #include <QAction>
43 #include <QTimer>
44 #include <QMenu>
45
46
47 extern "C" {
48 NewGeom_EXPORT CAM_Module* createModule()
49 {
50   return new NewGeom_Module();
51 }
52
53 NewGeom_EXPORT char* getModuleVersion()
54 {
55   return "0.0";
56 }
57 }
58
59 /** 
60 * Class for preferences management
61 */
62 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
63 {
64 public:
65   /// Constructor
66   /// \param theMgr preferences manager of SALOME
67   /// \param theModName name of the module
68   NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
69
70   virtual int addPreference(const QString& theLbl, int pId, 
71                             SUIT_PreferenceMgr::PrefItemType theType,
72                             const QString& theSection, const QString& theName )
73   {
74     return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
75   }
76
77   virtual void setItemProperty(const QString& thePropName,
78                                const QVariant& theValue,
79                                const int theId = -1)
80   {
81     myMgr->setItemProperty(thePropName, theValue, theId);
82   }
83
84
85   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
86
87 private:
88   LightApp_Preferences* myMgr;
89   QString myModName;
90 };
91
92
93
94
95 //******************************************************
96 NewGeom_Module::NewGeom_Module()
97     : LightApp_Module("NewGeom"),
98       mySelector(0), myIsOpened(0), myPopupMgr(0)
99 {
100   myWorkshop = new XGUI_Workshop(this);
101   connect(myWorkshop, SIGNAL(commandStatusUpdated()),
102           this, SLOT(onUpdateCommandStatus()));
103
104   myProxyViewer = new NewGeom_SalomeViewer(this);
105
106   ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
107   ModuleBase_Preferences::loadCustomProps();
108 }
109
110 //******************************************************
111 NewGeom_Module::~NewGeom_Module()
112 {
113 }
114
115 //******************************************************
116 void NewGeom_Module::initialize(CAM_Application* theApp)
117 {
118   LightApp_Module::initialize(theApp);
119   inspectSalomeModules();
120
121   myWorkshop->startApplication();
122   LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
123   if (anApp)
124     connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
125 }
126
127 //******************************************************
128 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
129 {
130   theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
131 }
132
133 //******************************************************
134 void NewGeom_Module::viewManagers(QStringList& theList) const
135 {
136   theList.append(OCCViewer_Viewer::Type());
137 }
138
139 //******************************************************
140 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
141 {
142   bool isDone = LightApp_Module::activateModule(theStudy);
143   if (isDone) {
144     setMenuShown(true);
145     setToolShown(true);
146
147     QObject* aObj = myWorkshop->objectBrowser()->parent();
148     QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
149     if (aObjDoc) {
150       QAction* aViewAct = aObjDoc->toggleViewAction();
151       aViewAct->setEnabled(true);
152       myWorkshop->objectBrowser()->setVisible(true);
153       aObjDoc->setVisible(true);
154       desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
155     }
156
157     if (!mySelector) {
158       ViewManagerList OCCViewManagers;
159       application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
160       if (OCCViewManagers.size() > 0) {
161         mySelector = createSelector(OCCViewManagers.first());
162       }
163     }
164     // it should be pefromed after the selector creation in order to have AISContext 
165     myWorkshop->activateModule();
166     //action(myEraseAll)->setEnabled(false);
167
168     if (myIsOpened) {
169       myWorkshop->objectBrowser()->rebuildDataTree();
170       myWorkshop->updateCommandStatus();
171       myIsOpened = false;
172       // the display all results is not necessary anymore, it was commented in XGUI_Workshop,
173       // so it should be commented here
174       //QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
175     }
176     else
177       myWorkshop->updateCommandStatus();
178   }
179   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
180   myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
181   myIsEditEnabled = getApp()->isEditEnabled();
182   getApp()->setEditEnabled(false);
183
184   // this following row is caused by #187 bug.
185   // SALOME saves the dock widget positions before deactivateModule() and
186   // load it after the module activation. So, if the panel is visible before
187   // deactivate, it becomes visible after activate.
188   // In order to avoid the visible property panel, the widget position save is
189   // switch off in this module
190   aResMgr->setValue("Study", "store_positions", false);
191
192   // Synchronize displayed objects
193   if (mySelector && mySelector->viewer()) {
194     Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
195     XGUI_Displayer* aDisp = myWorkshop->displayer();
196     QObjectPtrList aObjList = aDisp->displayedObjects();
197
198     AIS_ListOfInteractive aList;
199     aContext->DisplayedObjects(aList);
200     AIS_ListIteratorOfListOfInteractive aLIt;
201     Handle(AIS_InteractiveObject) anAISIO;
202     foreach (ObjectPtr aObj, aObjList) {
203       AISObjectPtr aPrs = aDisp->getAISObject(aObj);
204       Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
205       bool aFound = false;
206       for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
207         anAISIO = aLIt.Value();
208         if (anAISIO.Access() == aAIS.Access()) {
209           aFound = true;
210           break;
211         }
212       }
213       if (!aFound) {
214         aObj->setDisplayed(false);
215         //aDisp->erase(aObj, false);
216       }
217     }
218     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
219   }
220   myProxyViewer->activateViewer(true);
221   return isDone;
222 }
223
224 //******************************************************
225 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
226 {
227   myProxyViewer->activateViewer(false);
228   setMenuShown(false);
229   setToolShown(false);
230
231   myWorkshop->deactivateModule();
232
233   QObject* aObj = myWorkshop->objectBrowser()->parent();
234   QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
235   if (aObjDoc) {
236     aObjDoc->setVisible(false);
237     myWorkshop->objectBrowser()->setVisible(false);
238     QAction* aViewAct = aObjDoc->toggleViewAction();
239     aViewAct->setEnabled(false);
240   }
241
242   // the active operation should be stopped for the next activation.
243   // There should not be active operation and visualized preview.
244   // Abort operation should be performed before the selection's remove
245   // because the displayed objects should be removed from the viewer, but
246   // the AIS context is obtained from the selector.
247   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
248   while (anOperation) {
249     anOperation->abort();
250     anOperation = myWorkshop->operationMgr()->currentOperation();
251   }
252   // Delete selector because it has to be redefined on next activation
253   if (mySelector) {
254     myProxyViewer->setSelector(0);
255     delete mySelector;
256     mySelector = 0;
257   }
258
259   //myWorkshop->contextMenuMgr()->disconnectViewer();
260
261   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
262   aResMgr->setValue("Study", "store_positions", myIsStorePositions);
263   getApp()->setEditEnabled(myIsEditEnabled);
264
265   return LightApp_Module::deactivateModule(theStudy);
266 }
267
268 //******************************************************
269 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
270 {
271   if (!mySelector) {
272     mySelector = createSelector(theMgr);
273   }
274 }
275
276 //******************************************************
277 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
278 {
279   if (mySelector) {
280     if (theMgr->getType() == OCCViewer_Viewer::Type()) {
281       OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
282       if (mySelector->viewer() == aViewer) {
283         XGUI_Displayer* aDisp = myWorkshop->displayer();
284         QObjectPtrList aObjects = aDisp->displayedObjects();
285         foreach(ObjectPtr aObj, aObjects)
286           aObj->setDisplayed(false);
287         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
288         myProxyViewer->setSelector(0);
289         delete mySelector;
290         mySelector = 0;
291       }
292     }
293   }
294 }
295
296 //******************************************************
297 QtxPopupMgr* NewGeom_Module::popupMgr()
298 {
299   if (!myPopupMgr)
300     myPopupMgr = new QtxPopupMgr( 0, this );
301   return myPopupMgr;
302 }
303
304 //******************************************************
305 void NewGeom_Module::onDefaultPreferences()
306 {
307   ModuleBase_Preferences::resetConfig();
308   ModuleBase_Preferences::updateResourcesByConfig();
309
310   LightApp_Preferences* pref = preferences();
311   if (pref)
312     pref->retrieve();
313 }
314
315 //******************************************************
316 void NewGeom_Module::onUpdateCommandStatus()
317 {
318   getApp()->updateActions();
319 }
320
321 //******************************************************
322 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
323 {
324   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
325     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
326     NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
327     LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
328     QList<SUIT_Selector*> aList;
329     aMgr->selectors(aList);
330     foreach(SUIT_Selector* aSel, aList)
331     {
332       aSel->setEnabled(aSel == aSelector);
333     }
334     myProxyViewer->setSelector(aSelector);
335     return aSelector;
336   }
337   return 0;
338 }
339
340 //******************************************************
341 CAM_DataModel* NewGeom_Module::createDataModel()
342 {
343   NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
344
345   // Calling addComponent() for persistent functionality work in the SalomeApp_Study
346   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
347   aStudy->addComponent(aDataModel);
348
349   return aDataModel;
350 }
351
352 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
353 {
354   return addFeature(theWBName,
355                     theInfo.id,
356                     theInfo.text,
357                     theInfo.toolTip,
358                     theInfo.icon,
359                     theInfo.shortcut,
360                     theInfo.checkable);
361 }
362
363 //******************************************************
364 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
365                                     const QString& theTitle, const QString& theTip,
366                                     const QIcon& theIcon, const QKeySequence& theKeys,
367                                     bool isCheckable)
368 {
369   static QString aLastTool = "";
370   static int aNb = 0;
371   if (aLastTool.isEmpty())
372     aLastTool = theWBName;
373   else if (theWBName != aLastTool) {
374     aLastTool = theWBName;
375     if (aNb > 20) {
376       desktop()->addToolBarBreak();
377       aNb = 0;
378     }
379   }
380   aNb++;
381
382   int aMenu = createMenu(theWBName, -1, -1, 50);
383   int aTool = createTool(theWBName, theWBName);
384
385   int aId = myActionsList.size();
386   myActionsList.append(theId);
387   SUIT_Desktop* aDesk = application()->desktop();
388   int aKeys = 0;
389   for (unsigned int i = 0; i < theKeys.count(); i++)
390     aKeys += theKeys[i];
391   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
392                                   isCheckable);
393   aAction->setData(theId);
394   int aItemId = createMenu(aId, aMenu, -1, 10);
395   int aToolId = createTool(aId, aTool);
396
397   return aAction;
398 }
399
400
401 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
402                                           const ActionInfo& theInfo,
403                                           const QList<QAction*>& theNestedActions)
404 {
405   int aMenu = createMenu(theWBName, -1, -1, 50);
406   int aTool = createTool(theWBName, theWBName);
407
408   int aId = myActionsList.size();
409   myActionsList.append(theInfo.id);
410   SUIT_Desktop* aDesk = application()->desktop();
411   NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
412   anAction->setData(theInfo.id);
413   anAction->setCheckable(theInfo.checkable);
414   anAction->setChecked(theInfo.checked);
415   anAction->setEnabled(theInfo.enabled);
416   anAction->setVisible(theInfo.visible);
417   anAction->setIcon(theInfo.icon);
418   anAction->setText(theInfo.text);
419   anAction->setToolTip(theInfo.toolTip);
420   anAction->setShortcut(theInfo.shortcut);
421   anAction->setFont(theInfo.font);
422
423   //int aItemId = createMenu(aId, aMenu, -1, 10);
424   int aToolId = createTool(anAction, aTool, aId);
425
426   return anAction;
427 }
428
429
430 //******************************************************
431 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
432                                            const QString& theTip, const QIcon& theIcon,
433                                            const QKeySequence& theKeys, bool isCheckable,
434                                            const char* theMenuSourceText, const int theMenuPosition)
435 {
436   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
437
438   int aId = myActionsList.size();
439   myActionsList.append(theId);
440   SUIT_Desktop* aDesk = application()->desktop();
441   int aKeys = 0;
442   for (unsigned int i = 0; i < theKeys.count(); i++)
443     aKeys += theKeys[i];
444   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
445                                   isCheckable);
446   aAction->setData(theId);
447   createMenu(aId, aMenu, theMenuPosition);
448   return aAction;
449 }
450
451 //******************************************************
452 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
453 {
454   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
455   createMenu(separator(), aMenu, -1, theMenuPosition);
456 }
457
458 //******************************************************
459 QList<QAction*> NewGeom_Module::commandList() const
460 {
461   QList<QAction*> aActions;
462   for (int i = 0; i < myActionsList.size(); i++) {
463     QAction* aCmd = action(i);
464     if (aCmd && myActionsList.contains(aCmd->data().toString()))
465       aActions.append(aCmd);
466   }
467   return aActions;
468 }
469
470 //******************************************************
471 QStringList NewGeom_Module::commandIdList() const
472 {
473   return myActionsList;
474 }
475
476 //******************************************************
477 QMainWindow* NewGeom_Module::desktop() const
478 {
479   return application()->desktop();
480 }
481
482 //******************************************************
483 QString NewGeom_Module::commandId(const QAction* theCmd) const
484 {
485   int aId = actionId(theCmd);
486   if (aId < myActionsList.size())
487     return myActionsList[aId];
488   return QString();
489 }
490
491 //******************************************************
492 QAction* NewGeom_Module::command(const QString& theId) const
493 {
494   int aId = myActionsList.indexOf(theId);
495   if ((aId != -1) && (aId < myActionsList.size())) {
496     return action(aId);
497   }
498   return 0;
499 }
500
501 //******************************************************
502 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
503 {
504   myNestedActions[theId] = theActions;
505 }
506
507 //******************************************************
508 QStringList NewGeom_Module::nestedActions(const QString& theId) const
509 {
510   if (myNestedActions.contains(theId))
511     return myNestedActions[theId];
512   return QStringList();
513 }
514
515 //******************************************************
516 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
517 {
518   myDocumentType[theId] = theKind;
519 }
520
521 //******************************************************
522 QString NewGeom_Module::documentKind(const QString& theId) const
523 {
524   if (myDocumentType.contains(theId))
525     return myDocumentType[theId];
526   return QString();
527
528 }
529
530 //******************************************************
531 void NewGeom_Module::selectionChanged()
532 {
533   LightApp_Module::selectionChanged();
534   myWorkshop->salomeViewerSelectionChanged();
535 }
536
537 //******************************************************
538 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
539 {
540   myWorkshop->contextMenuMgr()->updateViewerMenu();
541   myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
542   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
543 }
544
545
546 //******************************************************
547 void NewGeom_Module::createPreferences()
548 {
549   LightApp_Preferences* pref = preferences();
550   if (!pref)
551     return;
552   ModuleBase_Preferences::updateConfigByResources();
553   QString aModName = moduleName();
554
555   QtxPreferenceItem* item = pref->findItem(aModName, true );
556   if ( item && (!item->isEmpty() )) {
557     item->parentItem()->removeItem(item);
558     delete item;
559   }
560
561   int catId = pref->addPreference(aModName, -1 );
562   if ( catId == -1 )
563     return;
564   NewGeom_PrefMgr aMgr(pref, aModName);
565   ModuleBase_Preferences::createEditContent(&aMgr, catId);
566   pref->retrieve();
567 }
568
569 //******************************************************
570 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
571 {
572   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
573   QString aVal = aResMgr->stringValue(theSection, theParam);
574   Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
575   std::string aValue = aVal.toStdString();
576   if (aValue.empty()) {
577     aValue = aProp->defaultValue();
578     aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
579
580     LightApp_Preferences* pref = preferences();
581     if (pref)
582       pref->retrieve();
583   }
584   aProp->setValue(aValue);
585
586 }
587
588 void NewGeom_Module::inspectSalomeModules()
589 {
590   QStringList aModuleNames;
591   getApp()->modules(aModuleNames, false);
592   foreach(QString eachModule, aModuleNames) {
593     Config_ModuleReader::addDependencyModule(eachModule.toStdString());
594   }
595 }