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