]> SALOME platform Git repositories - modules/shaper.git/blob - src/NewGeom/NewGeom_Module.cpp
Salome HOME
Issue #614: Set Property panel as a tab over the object browser
[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   int aMenu = createMenu(theWBName, -1, -1, 50);
370   int aTool = createTool(theWBName);
371
372   int aId = myActionsList.size();
373   myActionsList.append(theId);
374   SUIT_Desktop* aDesk = application()->desktop();
375   int aKeys = 0;
376   for (unsigned int i = 0; i < theKeys.count(); i++)
377     aKeys += theKeys[i];
378   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
379                                   isCheckable);
380   aAction->setData(theId);
381   int aItemId = createMenu(aId, aMenu, -1, 10);
382   int aToolId = createTool(aId, aTool);
383
384   return aAction;
385 }
386
387
388 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
389                                           const ActionInfo& theInfo,
390                                           const QList<QAction*>& theNestedActions)
391 {
392   int aMenu = createMenu(theWBName, -1, -1, 50);
393   int aTool = createTool(theWBName);
394
395   int aId = myActionsList.size();
396   myActionsList.append(theInfo.id);
397   SUIT_Desktop* aDesk = application()->desktop();
398   NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
399   anAction->setData(theInfo.id);
400   anAction->setCheckable(theInfo.checkable);
401   anAction->setChecked(theInfo.checked);
402   anAction->setEnabled(theInfo.enabled);
403   anAction->setVisible(theInfo.visible);
404   anAction->setIcon(theInfo.icon);
405   anAction->setText(theInfo.text);
406   anAction->setToolTip(theInfo.toolTip);
407   anAction->setShortcut(theInfo.shortcut);
408   anAction->setFont(theInfo.font);
409
410   //int aItemId = createMenu(aId, aMenu, -1, 10);
411   int aToolId = createTool(anAction, aTool, aId);
412
413   return anAction;
414 }
415
416
417 //******************************************************
418 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
419                                            const QString& theTip, const QIcon& theIcon,
420                                            const QKeySequence& theKeys, bool isCheckable,
421                                            const char* theMenuSourceText, const int theMenuPosition)
422 {
423   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
424
425   int aId = myActionsList.size();
426   myActionsList.append(theId);
427   SUIT_Desktop* aDesk = application()->desktop();
428   int aKeys = 0;
429   for (unsigned int i = 0; i < theKeys.count(); i++)
430     aKeys += theKeys[i];
431   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
432                                   isCheckable);
433   aAction->setData(theId);
434   createMenu(aId, aMenu, theMenuPosition);
435   return aAction;
436 }
437
438 //******************************************************
439 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
440 {
441   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
442   createMenu(separator(), aMenu, -1, theMenuPosition);
443 }
444
445 //******************************************************
446 QList<QAction*> NewGeom_Module::commandList() const
447 {
448   QList<QAction*> aActions;
449   for (int i = 0; i < myActionsList.size(); i++) {
450     QAction* aCmd = action(i);
451     if (aCmd && myActionsList.contains(aCmd->data().toString()))
452       aActions.append(aCmd);
453   }
454   return aActions;
455 }
456
457 //******************************************************
458 QStringList NewGeom_Module::commandIdList() const
459 {
460   return myActionsList;
461 }
462
463 //******************************************************
464 QMainWindow* NewGeom_Module::desktop() const
465 {
466   return application()->desktop();
467 }
468
469 //******************************************************
470 QString NewGeom_Module::commandId(const QAction* theCmd) const
471 {
472   int aId = actionId(theCmd);
473   if (aId < myActionsList.size())
474     return myActionsList[aId];
475   return QString();
476 }
477
478 //******************************************************
479 QAction* NewGeom_Module::command(const QString& theId) const
480 {
481   int aId = myActionsList.indexOf(theId);
482   if ((aId != -1) && (aId < myActionsList.size())) {
483     return action(aId);
484   }
485   return 0;
486 }
487
488 //******************************************************
489 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
490 {
491   myNestedActions[theId] = theActions;
492 }
493
494 //******************************************************
495 QStringList NewGeom_Module::nestedActions(const QString& theId) const
496 {
497   if (myNestedActions.contains(theId))
498     return myNestedActions[theId];
499   return QStringList();
500 }
501
502 //******************************************************
503 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
504 {
505   myDocumentType[theId] = theKind;
506 }
507
508 //******************************************************
509 QString NewGeom_Module::documentKind(const QString& theId) const
510 {
511   if (myDocumentType.contains(theId))
512     return myDocumentType[theId];
513   return QString();
514
515 }
516
517 //******************************************************
518 void NewGeom_Module::selectionChanged()
519 {
520   LightApp_Module::selectionChanged();
521   myWorkshop->salomeViewerSelectionChanged();
522 }
523
524 //******************************************************
525 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
526 {
527   QMenu* aMenu = myWorkshop->contextMenuMgr()->viewerMenu();
528   theMenu->addActions(aMenu->actions());
529   //myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
530   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
531   delete aMenu; // Delete menu because it is not controlled by menu manager
532 }
533
534
535 //******************************************************
536 void NewGeom_Module::createPreferences()
537 {
538   LightApp_Preferences* pref = preferences();
539   if (!pref)
540     return;
541   ModuleBase_Preferences::updateConfigByResources();
542   QString aModName = moduleName();
543
544   QtxPreferenceItem* item = pref->findItem(aModName, true );
545   if ( item && (!item->isEmpty() )) {
546     item->parentItem()->removeItem(item);
547     delete item;
548   }
549
550   int catId = pref->addPreference(aModName, -1 );
551   if ( catId == -1 )
552     return;
553   NewGeom_PrefMgr aMgr(pref, aModName);
554   ModuleBase_Preferences::createEditContent(&aMgr, catId);
555   pref->retrieve();
556 }
557
558 //******************************************************
559 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
560 {
561   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
562   QString aVal = aResMgr->stringValue(theSection, theParam);
563   Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
564   std::string aValue = aVal.toStdString();
565   if (aValue.empty()) {
566     aValue = aProp->defaultValue();
567     aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
568
569     LightApp_Preferences* pref = preferences();
570     if (pref)
571       pref->retrieve();
572   }
573   aProp->setValue(aValue);
574
575 }
576
577 void NewGeom_Module::inspectSalomeModules()
578 {
579   QStringList aModuleNames;
580   getApp()->modules(aModuleNames, false);
581   foreach(QString eachModule, aModuleNames) {
582     Config_ModuleReader::addDependencyModule(eachModule.toStdString());
583   }
584 }