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