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