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