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