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