]> 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
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 }
91
92 //******************************************************
93 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
94 {
95   theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
96 }
97
98 //******************************************************
99 void NewGeom_Module::viewManagers(QStringList& theList) const
100 {
101   theList.append(OCCViewer_Viewer::Type());
102 }
103
104 //******************************************************
105 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
106 {
107   bool isDone = LightApp_Module::activateModule(theStudy);
108   if (isDone) {
109     setMenuShown(true);
110     setToolShown(true);
111
112     QObject* aObj = myWorkshop->objectBrowser()->parent();
113     QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
114     if (aObjDoc) {
115       QAction* aViewAct = aObjDoc->toggleViewAction();
116       aViewAct->setEnabled(true);
117       myWorkshop->objectBrowser()->setVisible(true);
118       aObjDoc->setVisible(true);
119     }
120
121     if (!mySelector) {
122       ViewManagerList OCCViewManagers;
123       application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
124       if (OCCViewManagers.size() > 0) {
125         mySelector = createSelector(OCCViewManagers.first());
126       }
127     }
128     myWorkshop->propertyPanel()->hide();
129     QtxPopupMgr* aMgr = popupMgr();  // Create popup manager
130     action(myEraseAll)->setEnabled(false);
131
132     if (myIsOpened) {
133       myWorkshop->objectBrowser()->rebuildDataTree();
134       myWorkshop->updateCommandStatus();
135       myIsOpened = false;
136       QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
137     }
138   }
139   return isDone;
140 }
141
142 //******************************************************
143 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
144 {
145   setMenuShown(false);
146   setToolShown(false);
147
148   myWorkshop->propertyPanel()->setVisible(false);
149   desktop()->removeDockWidget(myWorkshop->propertyPanel());
150
151   QObject* aObj = myWorkshop->objectBrowser()->parent();
152   QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
153   if (aObjDoc) {
154     aObjDoc->setVisible(false);
155     myWorkshop->objectBrowser()->setVisible(false);
156     QAction* aViewAct = aObjDoc->toggleViewAction();
157     aViewAct->setEnabled(false);
158   }
159
160   //myWorkshop->contextMenuMgr()->disconnectViewer();
161   return LightApp_Module::deactivateModule(theStudy);
162 }
163
164 //******************************************************
165 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
166 {
167   if ((!mySelector)) {
168     mySelector = createSelector(theMgr);
169   }
170 }
171
172 //******************************************************
173 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
174 {
175   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
176     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
177     NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
178     LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
179     QList<SUIT_Selector*> aList;
180     aMgr->selectors(aList);
181     foreach(SUIT_Selector* aSel, aList)
182     {
183       aSel->setEnabled(aSel == aSelector);
184     }
185     myProxyViewer->setSelector(aSelector);
186     return aSelector;
187   }
188   return 0;
189 }
190
191 //******************************************************
192 CAM_DataModel* NewGeom_Module::createDataModel()
193 {
194   return new NewGeom_DataModel(this);
195 }
196
197 //******************************************************
198 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
199                                     const QString& theTitle, const QString& theTip,
200                                     const QIcon& theIcon, const QKeySequence& theKeys,
201                                     bool isCheckable)
202 {
203   int aMenu = createMenu(theWBName, -1, -1, 50);
204   int aTool = createTool(theWBName);
205
206   int aId = myActionsList.size();
207   myActionsList.append(theId);
208   SUIT_Desktop* aDesk = application()->desktop();
209   int aKeys = 0;
210   for (unsigned int i = 0; i < theKeys.count(); i++)
211     aKeys += theKeys[i];
212   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
213                                   isCheckable);
214   aAction->setData(theId);
215   int aItemId = createMenu(aId, aMenu, -1, 10);
216   int aToolId = createTool(aId, aTool);
217   return aAction;
218 }
219
220 //******************************************************
221 QAction* NewGeom_Module::addEditCommand(const QString& theId, const QString& theTitle,
222                                         const QString& theTip, const QIcon& theIcon,
223                                         const QKeySequence& theKeys, bool isCheckable)
224 {
225   int aMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1);
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   createMenu(aId, aMenu, 10);
237   return aAction;
238 }
239
240 //******************************************************
241 void NewGeom_Module::addEditMenuSeparator()
242 {
243   int aMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1);
244   createMenu(separator(), aMenu, -1, 10);
245 }
246
247 //******************************************************
248 QList<QAction*> NewGeom_Module::commandList() const
249 {
250   QList<QAction*> aActions;
251   for (int i = 0; i < myActionsList.size(); i++)
252     aActions.append(action(i));
253   return aActions;
254 }
255
256 //******************************************************
257 QStringList NewGeom_Module::commandIdList() const
258 {
259   return myActionsList;
260 }
261
262 //******************************************************
263 QMainWindow* NewGeom_Module::desktop() const
264 {
265   return application()->desktop();
266 }
267
268 //******************************************************
269 QString NewGeom_Module::commandId(const QAction* theCmd) const
270 {
271   int aId = actionId(theCmd);
272   if (aId < myActionsList.size())
273     return myActionsList[aId];
274   return QString();
275 }
276
277 //******************************************************
278 QAction* NewGeom_Module::command(const QString& theId) const
279 {
280   int aId = myActionsList.indexOf(theId);
281   if ((aId != -1) && (aId < myActionsList.size())) {
282     return action(aId);
283   }
284   return 0;
285 }
286
287 //******************************************************
288 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
289 {
290   myNestedActions[theId] = theActions;
291 }
292
293 //******************************************************
294 QStringList NewGeom_Module::nestedActions(const QString& theId) const
295 {
296   if (myNestedActions.contains(theId))
297     return myNestedActions[theId];
298   return QStringList();
299 }
300
301 //******************************************************
302 void NewGeom_Module::selectionChanged()
303 {
304   LightApp_Module::selectionChanged();
305   myWorkshop->salomeViewerSelectionChanged();
306 }
307
308 //******************************************************
309 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
310 {
311   myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
312   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
313 }
314
315
316 //******************************************************
317 void NewGeom_Module::createPreferences()
318 {
319   LightApp_Preferences* pref = preferences();
320   if (!pref)
321     return;
322   XGUI_Preferences::updateCustomProps();
323   QString aModName = moduleName();
324
325   QtxPreferenceItem* item = pref->findItem(aModName, true );
326   if ( item && (!item->isEmpty() )) {
327     item->parentItem()->removeItem(item);
328     delete item;
329   }
330
331   int catId = pref->addPreference(aModName, -1 );
332   if ( catId == -1 )
333     return;
334   NewGeom_PrefMgr aMgr(pref, aModName);
335   XGUI_Preferences::createEditContent(&aMgr, catId);
336   pref->retrieve();
337 }
338
339 //******************************************************
340 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
341 {
342   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
343   QString aVal = aResMgr->stringValue(theSection, theParam);
344   if (!aVal.isNull()) {
345     Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
346     aProp->setValue(aVal.toStdString());
347   }
348 }