Salome HOME
Make concealment of results working on compsolids: if at least one sub-body is concea...
[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
402 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
403                                           const ActionInfo& theInfo,
404                                           const QList<QAction*>& theNestedActions)
405 {
406   int aMenu = createMenu(theWBName, -1, -1, 50);
407   int aTool = createTool(theWBName, theWBName);
408
409   int aId = myActionsList.size();
410   myActionsList.append(theInfo.id);
411   SUIT_Desktop* aDesk = application()->desktop();
412   NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
413   anAction->setData(theInfo.id);
414   anAction->setCheckable(theInfo.checkable);
415   anAction->setChecked(theInfo.checked);
416   anAction->setEnabled(theInfo.enabled);
417   anAction->setVisible(theInfo.visible);
418   anAction->setIcon(theInfo.icon);
419   anAction->setText(theInfo.text);
420   anAction->setToolTip(theInfo.toolTip);
421   anAction->setShortcut(theInfo.shortcut);
422   anAction->setFont(theInfo.font);
423
424   //int aItemId = createMenu(aId, aMenu, -1, 10);
425   int aToolId = createTool(anAction, aTool, aId);
426
427   return anAction;
428 }
429
430
431 //******************************************************
432 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
433                                            const QString& theTip, const QIcon& theIcon,
434                                            const QKeySequence& theKeys, bool isCheckable,
435                                            const char* theMenuSourceText, const int theMenuPosition)
436 {
437   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
438
439   int aId = myActionsList.size();
440   myActionsList.append(theId);
441   SUIT_Desktop* aDesk = application()->desktop();
442   int aKeys = 0;
443   for (unsigned int i = 0; i < theKeys.count(); i++)
444     aKeys += theKeys[i];
445   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
446                                   isCheckable);
447   aAction->setData(theId);
448   createMenu(aId, aMenu, theMenuPosition);
449   return aAction;
450 }
451
452 //******************************************************
453 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
454 {
455   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
456   createMenu(separator(), aMenu, -1, theMenuPosition);
457 }
458
459 bool NewGeom_Module::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
460 {
461   if( !theAction )
462     return false;
463
464   SUIT_Desktop* aDesktop = application()->desktop();
465   if( !aDesktop )
466     return false;
467
468   QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
469   if( !aToolMgr )
470     return false;
471
472   aToolMgr->append( theAction, theToolBarTitle );
473   return true;
474 }
475
476 //******************************************************
477 QList<QAction*> NewGeom_Module::commandList() const
478 {
479   QList<QAction*> aActions;
480   for (int i = 0; i < myActionsList.size(); i++) {
481     QAction* aCmd = action(i);
482     if (aCmd && myActionsList.contains(aCmd->data().toString()))
483       aActions.append(aCmd);
484   }
485   return aActions;
486 }
487
488 //******************************************************
489 QStringList NewGeom_Module::commandIdList() const
490 {
491   return myActionsList;
492 }
493
494 //******************************************************
495 QMainWindow* NewGeom_Module::desktop() const
496 {
497   return application()->desktop();
498 }
499
500 //******************************************************
501 QString NewGeom_Module::commandId(const QAction* theCmd) const
502 {
503   int aId = actionId(theCmd);
504   if (aId < myActionsList.size())
505     return myActionsList[aId];
506   return QString();
507 }
508
509 //******************************************************
510 QAction* NewGeom_Module::command(const QString& theId) const
511 {
512   int aId = myActionsList.indexOf(theId);
513   if ((aId != -1) && (aId < myActionsList.size())) {
514     return action(aId);
515   }
516   return 0;
517 }
518
519 //******************************************************
520 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
521 {
522   myNestedActions[theId] = theActions;
523 }
524
525 //******************************************************
526 QStringList NewGeom_Module::nestedActions(const QString& theId) const
527 {
528   if (myNestedActions.contains(theId))
529     return myNestedActions[theId];
530   return QStringList();
531 }
532
533 //******************************************************
534 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
535 {
536   myDocumentType[theId] = theKind;
537 }
538
539 //******************************************************
540 QString NewGeom_Module::documentKind(const QString& theId) const
541 {
542   if (myDocumentType.contains(theId))
543     return myDocumentType[theId];
544   return QString();
545
546 }
547
548 //******************************************************
549 void NewGeom_Module::selectionChanged()
550 {
551   LightApp_Module::selectionChanged();
552   myWorkshop->salomeViewerSelectionChanged();
553 }
554
555 //******************************************************
556 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
557 {
558   myWorkshop->contextMenuMgr()->updateViewerMenu();
559   myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
560   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
561 }
562
563
564 //******************************************************
565 void NewGeom_Module::createPreferences()
566 {
567   LightApp_Preferences* pref = preferences();
568   if (!pref)
569     return;
570   ModuleBase_Preferences::updateConfigByResources();
571   QString aModName = moduleName();
572
573   QtxPreferenceItem* item = pref->findItem(aModName, true );
574   if ( item && (!item->isEmpty() )) {
575     item->parentItem()->removeItem(item);
576     delete item;
577   }
578
579   int catId = pref->addPreference(aModName, -1 );
580   if ( catId == -1 )
581     return;
582   NewGeom_PrefMgr aMgr(pref, aModName);
583   ModuleBase_Preferences::createEditContent(&aMgr, catId);
584   pref->retrieve();
585 }
586
587 //******************************************************
588 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
589 {
590   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
591   QString aVal = aResMgr->stringValue(theSection, theParam);
592   Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
593   std::string aValue = aVal.toStdString();
594   if (aValue.empty()) {
595     aValue = aProp->defaultValue();
596     aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
597
598     LightApp_Preferences* pref = preferences();
599     if (pref)
600       pref->retrieve();
601   }
602   aProp->setValue(aValue);
603
604 }
605
606 void NewGeom_Module::inspectSalomeModules()
607 {
608   QStringList aModuleNames;
609   getApp()->modules(aModuleNames, false);
610   foreach(QString eachModule, aModuleNames) {
611     Config_ModuleReader::addDependencyModule(eachModule.toStdString());
612   }
613 }