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