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