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