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