Salome HOME
Issue #596: Redesign of pop-up menu management
[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     }
155
156     if (!mySelector) {
157       ViewManagerList OCCViewManagers;
158       application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
159       if (OCCViewManagers.size() > 0) {
160         mySelector = createSelector(OCCViewManagers.first());
161       }
162     }
163     // it should be pefromed after the selector creation in order to have AISContext 
164     myWorkshop->activateModule();
165     //action(myEraseAll)->setEnabled(false);
166
167     if (myIsOpened) {
168       myWorkshop->objectBrowser()->rebuildDataTree();
169       myWorkshop->updateCommandStatus();
170       myIsOpened = false;
171       // the display all results is not necessary anymore, it was commented in XGUI_Workshop,
172       // so it should be commented here
173       //QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
174     }
175     else
176       myWorkshop->updateCommandStatus();
177   }
178   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
179   myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
180   myIsEditEnabled = getApp()->isEditEnabled();
181   getApp()->setEditEnabled(false);
182
183   // this following row is caused by #187 bug.
184   // SALOME saves the dock widget positions before deactivateModule() and
185   // load it after the module activation. So, if the panel is visible before
186   // deactivate, it becomes visible after activate.
187   // In order to avoid the visible property panel, the widget position save is
188   // switch off in this module
189   aResMgr->setValue("Study", "store_positions", false);
190
191   // Synchronize displayed objects
192   if (mySelector && mySelector->viewer()) {
193     Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
194     XGUI_Displayer* aDisp = myWorkshop->displayer();
195     QObjectPtrList aObjList = aDisp->displayedObjects();
196
197     AIS_ListOfInteractive aList;
198     aContext->DisplayedObjects(aList);
199     AIS_ListIteratorOfListOfInteractive aLIt;
200     Handle(AIS_InteractiveObject) anAISIO;
201     foreach (ObjectPtr aObj, aObjList) {
202       AISObjectPtr aPrs = aDisp->getAISObject(aObj);
203       Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
204       bool aFound = false;
205       for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
206         anAISIO = aLIt.Value();
207         if (anAISIO.Access() == aAIS.Access()) {
208           aFound = true;
209           break;
210         }
211       }
212       if (!aFound) {
213         aObj->setDisplayed(false);
214         //aDisp->erase(aObj, false);
215       }
216     }
217     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
218   }
219   myProxyViewer->activateViewer(true);
220   return isDone;
221 }
222
223 //******************************************************
224 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
225 {
226   myProxyViewer->activateViewer(false);
227   setMenuShown(false);
228   setToolShown(false);
229
230   myWorkshop->deactivateModule();
231
232   QObject* aObj = myWorkshop->objectBrowser()->parent();
233   QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
234   if (aObjDoc) {
235     aObjDoc->setVisible(false);
236     myWorkshop->objectBrowser()->setVisible(false);
237     QAction* aViewAct = aObjDoc->toggleViewAction();
238     aViewAct->setEnabled(false);
239   }
240
241   // the active operation should be stopped for the next activation.
242   // There should not be active operation and visualized preview.
243   // Abort operation should be performed before the selection's remove
244   // because the displayed objects should be removed from the viewer, but
245   // the AIS context is obtained from the selector.
246   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
247   while (anOperation) {
248     anOperation->abort();
249     anOperation = myWorkshop->operationMgr()->currentOperation();
250   }
251   // Delete selector because it has to be redefined on next activation
252   if (mySelector) {
253     myProxyViewer->setSelector(0);
254     delete mySelector;
255     mySelector = 0;
256   }
257
258   //myWorkshop->contextMenuMgr()->disconnectViewer();
259
260   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
261   aResMgr->setValue("Study", "store_positions", myIsStorePositions);
262   getApp()->setEditEnabled(myIsEditEnabled);
263
264   return LightApp_Module::deactivateModule(theStudy);
265 }
266
267 //******************************************************
268 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
269 {
270   if (!mySelector) {
271     mySelector = createSelector(theMgr);
272   }
273 }
274
275 //******************************************************
276 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
277 {
278   if (mySelector) {
279     if (theMgr->getType() == OCCViewer_Viewer::Type()) {
280       OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
281       if (mySelector->viewer() == aViewer) {
282         XGUI_Displayer* aDisp = myWorkshop->displayer();
283         QObjectPtrList aObjects = aDisp->displayedObjects();
284         foreach(ObjectPtr aObj, aObjects)
285           aObj->setDisplayed(false);
286         Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
287         myProxyViewer->setSelector(0);
288         delete mySelector;
289         mySelector = 0;
290       }
291     }
292   }
293 }
294
295 //******************************************************
296 QtxPopupMgr* NewGeom_Module::popupMgr()
297 {
298   if (!myPopupMgr)
299     myPopupMgr = new QtxPopupMgr( 0, this );
300   return myPopupMgr;
301 }
302
303 //******************************************************
304 void NewGeom_Module::onDefaultPreferences()
305 {
306   ModuleBase_Preferences::resetConfig();
307   ModuleBase_Preferences::updateResourcesByConfig();
308
309   LightApp_Preferences* pref = preferences();
310   if (pref)
311     pref->retrieve();
312 }
313
314 //******************************************************
315 void NewGeom_Module::onUpdateCommandStatus()
316 {
317   getApp()->updateActions();
318 }
319
320 //******************************************************
321 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
322 {
323   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
324     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
325     NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
326     LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
327     QList<SUIT_Selector*> aList;
328     aMgr->selectors(aList);
329     foreach(SUIT_Selector* aSel, aList)
330     {
331       aSel->setEnabled(aSel == aSelector);
332     }
333     myProxyViewer->setSelector(aSelector);
334     return aSelector;
335   }
336   return 0;
337 }
338
339 //******************************************************
340 CAM_DataModel* NewGeom_Module::createDataModel()
341 {
342   NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
343
344   // Calling addComponent() for persistent functionality work in the SalomeApp_Study
345   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
346   aStudy->addComponent(aDataModel);
347
348   return aDataModel;
349 }
350
351 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
352 {
353   return addFeature(theWBName,
354                     theInfo.id,
355                     theInfo.text,
356                     theInfo.toolTip,
357                     theInfo.icon,
358                     theInfo.shortcut,
359                     theInfo.checkable);
360 }
361
362 //******************************************************
363 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
364                                     const QString& theTitle, const QString& theTip,
365                                     const QIcon& theIcon, const QKeySequence& theKeys,
366                                     bool isCheckable)
367 {
368   int aMenu = createMenu(theWBName, -1, -1, 50);
369   int aTool = createTool(theWBName);
370
371   int aId = myActionsList.size();
372   myActionsList.append(theId);
373   SUIT_Desktop* aDesk = application()->desktop();
374   int aKeys = 0;
375   for (unsigned int i = 0; i < theKeys.count(); i++)
376     aKeys += theKeys[i];
377   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
378                                   isCheckable);
379   aAction->setData(theId);
380   int aItemId = createMenu(aId, aMenu, -1, 10);
381   int aToolId = createTool(aId, aTool);
382
383   return aAction;
384 }
385
386
387 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
388                                           const ActionInfo& theInfo,
389                                           const QList<QAction*>& theNestedActions)
390 {
391   int aMenu = createMenu(theWBName, -1, -1, 50);
392   int aTool = createTool(theWBName);
393
394   int aId = myActionsList.size();
395   myActionsList.append(theInfo.id);
396   SUIT_Desktop* aDesk = application()->desktop();
397   NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
398   anAction->setData(theInfo.id);
399   anAction->setCheckable(theInfo.checkable);
400   anAction->setChecked(theInfo.checked);
401   anAction->setEnabled(theInfo.enabled);
402   anAction->setVisible(theInfo.visible);
403   anAction->setIcon(theInfo.icon);
404   anAction->setText(theInfo.text);
405   anAction->setToolTip(theInfo.toolTip);
406   anAction->setShortcut(theInfo.shortcut);
407   anAction->setFont(theInfo.font);
408
409   //int aItemId = createMenu(aId, aMenu, -1, 10);
410   int aToolId = createTool(anAction, aTool, aId);
411
412   return anAction;
413 }
414
415
416 //******************************************************
417 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
418                                            const QString& theTip, const QIcon& theIcon,
419                                            const QKeySequence& theKeys, bool isCheckable,
420                                            const char* theMenuSourceText, const int theMenuPosition)
421 {
422   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
423
424   int aId = myActionsList.size();
425   myActionsList.append(theId);
426   SUIT_Desktop* aDesk = application()->desktop();
427   int aKeys = 0;
428   for (unsigned int i = 0; i < theKeys.count(); i++)
429     aKeys += theKeys[i];
430   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
431                                   isCheckable);
432   aAction->setData(theId);
433   createMenu(aId, aMenu, theMenuPosition);
434   return aAction;
435 }
436
437 //******************************************************
438 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
439 {
440   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
441   createMenu(separator(), aMenu, -1, theMenuPosition);
442 }
443
444 //******************************************************
445 QList<QAction*> NewGeom_Module::commandList() const
446 {
447   QList<QAction*> aActions;
448   for (int i = 0; i < myActionsList.size(); i++) {
449     QAction* aCmd = action(i);
450     if (aCmd && myActionsList.contains(aCmd->data().toString()))
451       aActions.append(aCmd);
452   }
453   return aActions;
454 }
455
456 //******************************************************
457 QStringList NewGeom_Module::commandIdList() const
458 {
459   return myActionsList;
460 }
461
462 //******************************************************
463 QMainWindow* NewGeom_Module::desktop() const
464 {
465   return application()->desktop();
466 }
467
468 //******************************************************
469 QString NewGeom_Module::commandId(const QAction* theCmd) const
470 {
471   int aId = actionId(theCmd);
472   if (aId < myActionsList.size())
473     return myActionsList[aId];
474   return QString();
475 }
476
477 //******************************************************
478 QAction* NewGeom_Module::command(const QString& theId) const
479 {
480   int aId = myActionsList.indexOf(theId);
481   if ((aId != -1) && (aId < myActionsList.size())) {
482     return action(aId);
483   }
484   return 0;
485 }
486
487 //******************************************************
488 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
489 {
490   myNestedActions[theId] = theActions;
491 }
492
493 //******************************************************
494 QStringList NewGeom_Module::nestedActions(const QString& theId) const
495 {
496   if (myNestedActions.contains(theId))
497     return myNestedActions[theId];
498   return QStringList();
499 }
500
501 //******************************************************
502 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
503 {
504   myDocumentType[theId] = theKind;
505 }
506
507 //******************************************************
508 QString NewGeom_Module::documentKind(const QString& theId) const
509 {
510   if (myDocumentType.contains(theId))
511     return myDocumentType[theId];
512   return QString();
513
514 }
515
516 //******************************************************
517 void NewGeom_Module::selectionChanged()
518 {
519   LightApp_Module::selectionChanged();
520   myWorkshop->salomeViewerSelectionChanged();
521 }
522
523 //******************************************************
524 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
525 {
526   theMenu->addActions(myWorkshop->contextMenuMgr()->viewerMenu()->actions());
527   //myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
528   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
529 }
530
531
532 //******************************************************
533 void NewGeom_Module::createPreferences()
534 {
535   LightApp_Preferences* pref = preferences();
536   if (!pref)
537     return;
538   ModuleBase_Preferences::updateConfigByResources();
539   QString aModName = moduleName();
540
541   QtxPreferenceItem* item = pref->findItem(aModName, true );
542   if ( item && (!item->isEmpty() )) {
543     item->parentItem()->removeItem(item);
544     delete item;
545   }
546
547   int catId = pref->addPreference(aModName, -1 );
548   if ( catId == -1 )
549     return;
550   NewGeom_PrefMgr aMgr(pref, aModName);
551   ModuleBase_Preferences::createEditContent(&aMgr, catId);
552   pref->retrieve();
553 }
554
555 //******************************************************
556 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
557 {
558   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
559   QString aVal = aResMgr->stringValue(theSection, theParam);
560   Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
561   std::string aValue = aVal.toStdString();
562   if (aValue.empty()) {
563     aValue = aProp->defaultValue();
564     aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
565
566     LightApp_Preferences* pref = preferences();
567     if (pref)
568       pref->retrieve();
569   }
570   aProp->setValue(aValue);
571
572 }
573
574 void NewGeom_Module::inspectSalomeModules()
575 {
576   QStringList aModuleNames;
577   getApp()->modules(aModuleNames, false);
578   foreach(QString eachModule, aModuleNames) {
579     Config_ModuleReader::addDependencyModule(eachModule.toStdString());
580   }
581 }