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