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