Salome HOME
Merge remote-tracking branch 'origin/Dev_0.6.1' into Dev_0.6.1
[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_ObjectsBrowser.h>
10 #include <XGUI_OperationMgr.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_Preferences.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 ModuleBase_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   ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
87   ModuleBase_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     else
151       myWorkshop->updateCommandStatus();
152   }
153   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
154   myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
155   myIsEditEnabled = getApp()->isEditEnabled();
156   getApp()->setEditEnabled(false);
157
158   // this following row is caused by #187 bug.
159   // SALOME saves the dock widget positions before deactivateModule() and
160   // load it after the module activation. So, if the panel is visible before
161   // deactivate, it becomes visible after activate.
162   // In order to avoid the visible property panel, the widget position save is
163   // switch off in this module
164   aResMgr->setValue("Study", "store_positions", false);
165
166   return isDone;
167 }
168
169 //******************************************************
170 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
171 {
172   setMenuShown(false);
173   setToolShown(false);
174
175   QObject* aObj = myWorkshop->objectBrowser()->parent();
176   QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
177   if (aObjDoc) {
178     aObjDoc->setVisible(false);
179     myWorkshop->objectBrowser()->setVisible(false);
180     QAction* aViewAct = aObjDoc->toggleViewAction();
181     aViewAct->setEnabled(false);
182   }
183
184   // the active operation should be stopped for the next activation.
185   // There should not be active operation and visualized preview.
186   // Abort operation should be performed before the selection's remove
187   // because the displayed objects should be removed from the viewer, but
188   // the AIS context is obtained from the selector.
189   ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
190   if (anOperation)
191     anOperation->abort();
192
193   // Delete selector because it has to be redefined on next activation
194   if (mySelector) {
195     myProxyViewer->setSelector(0);
196     delete mySelector;
197     mySelector = 0;
198   }
199
200   //myWorkshop->contextMenuMgr()->disconnectViewer();
201
202   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
203   aResMgr->setValue("Study", "store_positions", myIsStorePositions);
204   getApp()->setEditEnabled(myIsEditEnabled);
205
206   return LightApp_Module::deactivateModule(theStudy);
207 }
208
209 //******************************************************
210 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
211 {
212   if ((!mySelector)) {
213     mySelector = createSelector(theMgr);
214   }
215 }
216
217 //******************************************************
218 QtxPopupMgr* NewGeom_Module::popupMgr()
219 {
220   if (!myPopupMgr)
221     myPopupMgr = new QtxPopupMgr( 0, this );
222   return myPopupMgr;
223 }
224
225 //******************************************************
226 void NewGeom_Module::onDefaultPreferences()
227 {
228   ModuleBase_Preferences::resetConfig();
229   ModuleBase_Preferences::updateResourcesByConfig();
230
231   LightApp_Preferences* pref = preferences();
232   if (pref)
233     pref->retrieve();
234 }
235
236 //******************************************************
237 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
238 {
239   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
240     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
241     NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
242     LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
243     QList<SUIT_Selector*> aList;
244     aMgr->selectors(aList);
245     foreach(SUIT_Selector* aSel, aList)
246     {
247       aSel->setEnabled(aSel == aSelector);
248     }
249     myProxyViewer->setSelector(aSelector);
250     return aSelector;
251   }
252   return 0;
253 }
254
255 //******************************************************
256 CAM_DataModel* NewGeom_Module::createDataModel()
257 {
258   return new NewGeom_DataModel(this);
259 }
260
261 //******************************************************
262 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
263                                     const QString& theTitle, const QString& theTip,
264                                     const QIcon& theIcon, const QKeySequence& theKeys,
265                                     bool isCheckable)
266 {
267   int aMenu = createMenu(theWBName, -1, -1, 50);
268   int aTool = createTool(theWBName);
269
270   int aId = myActionsList.size();
271   myActionsList.append(theId);
272   SUIT_Desktop* aDesk = application()->desktop();
273   int aKeys = 0;
274   for (unsigned int i = 0; i < theKeys.count(); i++)
275     aKeys += theKeys[i];
276   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
277                                   isCheckable);
278   aAction->setData(theId);
279   int aItemId = createMenu(aId, aMenu, -1, 10);
280   int aToolId = createTool(aId, aTool);
281   return aAction;
282 }
283
284 //******************************************************
285 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
286                                            const QString& theTip, const QIcon& theIcon,
287                                            const QKeySequence& theKeys, bool isCheckable,
288                                            const char* theMenuSourceText, const int theMenuPosition)
289 {
290   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
291
292   int aId = myActionsList.size();
293   myActionsList.append(theId);
294   SUIT_Desktop* aDesk = application()->desktop();
295   int aKeys = 0;
296   for (unsigned int i = 0; i < theKeys.count(); i++)
297     aKeys += theKeys[i];
298   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
299                                   isCheckable);
300   aAction->setData(theId);
301   createMenu(aId, aMenu, theMenuPosition);
302   return aAction;
303 }
304
305 //******************************************************
306 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
307 {
308   int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
309   createMenu(separator(), aMenu, -1, theMenuPosition);
310 }
311
312 //******************************************************
313 QList<QAction*> NewGeom_Module::commandList() const
314 {
315   QList<QAction*> aActions;
316   for (int i = 0; i < myActionsList.size(); i++)
317     aActions.append(action(i));
318   return aActions;
319 }
320
321 //******************************************************
322 QStringList NewGeom_Module::commandIdList() const
323 {
324   return myActionsList;
325 }
326
327 //******************************************************
328 QMainWindow* NewGeom_Module::desktop() const
329 {
330   return application()->desktop();
331 }
332
333 //******************************************************
334 QString NewGeom_Module::commandId(const QAction* theCmd) const
335 {
336   int aId = actionId(theCmd);
337   if (aId < myActionsList.size())
338     return myActionsList[aId];
339   return QString();
340 }
341
342 //******************************************************
343 QAction* NewGeom_Module::command(const QString& theId) const
344 {
345   int aId = myActionsList.indexOf(theId);
346   if ((aId != -1) && (aId < myActionsList.size())) {
347     return action(aId);
348   }
349   return 0;
350 }
351
352 //******************************************************
353 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
354 {
355   myNestedActions[theId] = theActions;
356 }
357
358 //******************************************************
359 QStringList NewGeom_Module::nestedActions(const QString& theId) const
360 {
361   if (myNestedActions.contains(theId))
362     return myNestedActions[theId];
363   return QStringList();
364 }
365
366 //******************************************************
367 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
368 {
369   myDocumentType[theId] = theKind;
370 }
371
372 //******************************************************
373 QString NewGeom_Module::documentKind(const QString& theId) const
374 {
375   if (myDocumentType.contains(theId))
376     return myDocumentType[theId];
377   return QString();
378
379 }
380
381 //******************************************************
382 void NewGeom_Module::selectionChanged()
383 {
384   LightApp_Module::selectionChanged();
385   myWorkshop->salomeViewerSelectionChanged();
386 }
387
388 //******************************************************
389 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
390 {
391   myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
392   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
393 }
394
395
396 //******************************************************
397 void NewGeom_Module::createPreferences()
398 {
399   LightApp_Preferences* pref = preferences();
400   if (!pref)
401     return;
402   ModuleBase_Preferences::updateConfigByResources();
403   QString aModName = moduleName();
404
405   QtxPreferenceItem* item = pref->findItem(aModName, true );
406   if ( item && (!item->isEmpty() )) {
407     item->parentItem()->removeItem(item);
408     delete item;
409   }
410
411   int catId = pref->addPreference(aModName, -1 );
412   if ( catId == -1 )
413     return;
414   NewGeom_PrefMgr aMgr(pref, aModName);
415   ModuleBase_Preferences::createEditContent(&aMgr, catId);
416   pref->retrieve();
417 }
418
419 //******************************************************
420 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
421 {
422   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
423   QString aVal = aResMgr->stringValue(theSection, theParam);
424   Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
425   std::string aValue = aVal.toStdString();
426   if (aValue.empty()) {
427     aValue = aProp->defaultValue();
428     aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
429
430     LightApp_Preferences* pref = preferences();
431     if (pref)
432       pref->retrieve();
433   }
434   aProp->setValue(aValue);
435
436 }