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