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