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