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