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