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