Salome HOME
Create Presentation for rigid constraint
[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   // Delete selector because it has to be redefined on next activation
161   if (mySelector) {
162     myProxyViewer->setSelector(0);
163     delete mySelector;
164     mySelector = 0;
165   }
166
167   //myWorkshop->contextMenuMgr()->disconnectViewer();
168   return LightApp_Module::deactivateModule(theStudy);
169 }
170
171 //******************************************************
172 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
173 {
174   if ((!mySelector)) {
175     mySelector = createSelector(theMgr);
176   }
177 }
178
179 //******************************************************
180 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
181 {
182   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
183     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
184     NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
185     LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
186     QList<SUIT_Selector*> aList;
187     aMgr->selectors(aList);
188     foreach(SUIT_Selector* aSel, aList)
189     {
190       aSel->setEnabled(aSel == aSelector);
191     }
192     myProxyViewer->setSelector(aSelector);
193     return aSelector;
194   }
195   return 0;
196 }
197
198 //******************************************************
199 CAM_DataModel* NewGeom_Module::createDataModel()
200 {
201   return new NewGeom_DataModel(this);
202 }
203
204 //******************************************************
205 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
206                                     const QString& theTitle, const QString& theTip,
207                                     const QIcon& theIcon, const QKeySequence& theKeys,
208                                     bool isCheckable)
209 {
210   int aMenu = createMenu(theWBName, -1, -1, 50);
211   int aTool = createTool(theWBName);
212
213   int aId = myActionsList.size();
214   myActionsList.append(theId);
215   SUIT_Desktop* aDesk = application()->desktop();
216   int aKeys = 0;
217   for (unsigned int i = 0; i < theKeys.count(); i++)
218     aKeys += theKeys[i];
219   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
220                                   isCheckable);
221   aAction->setData(theId);
222   int aItemId = createMenu(aId, aMenu, -1, 10);
223   int aToolId = createTool(aId, aTool);
224   return aAction;
225 }
226
227 //******************************************************
228 QAction* NewGeom_Module::addEditCommand(const QString& theId, const QString& theTitle,
229                                         const QString& theTip, const QIcon& theIcon,
230                                         const QKeySequence& theKeys, bool isCheckable)
231 {
232   int aMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1);
233
234   int aId = myActionsList.size();
235   myActionsList.append(theId);
236   SUIT_Desktop* aDesk = application()->desktop();
237   int aKeys = 0;
238   for (unsigned int i = 0; i < theKeys.count(); i++)
239     aKeys += theKeys[i];
240   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
241                                   isCheckable);
242   aAction->setData(theId);
243   createMenu(aId, aMenu, 10);
244   return aAction;
245 }
246
247 //******************************************************
248 void NewGeom_Module::addEditMenuSeparator()
249 {
250   int aMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1);
251   createMenu(separator(), aMenu, -1, 10);
252 }
253
254 //******************************************************
255 QList<QAction*> NewGeom_Module::commandList() const
256 {
257   QList<QAction*> aActions;
258   for (int i = 0; i < myActionsList.size(); i++)
259     aActions.append(action(i));
260   return aActions;
261 }
262
263 //******************************************************
264 QStringList NewGeom_Module::commandIdList() const
265 {
266   return myActionsList;
267 }
268
269 //******************************************************
270 QMainWindow* NewGeom_Module::desktop() const
271 {
272   return application()->desktop();
273 }
274
275 //******************************************************
276 QString NewGeom_Module::commandId(const QAction* theCmd) const
277 {
278   int aId = actionId(theCmd);
279   if (aId < myActionsList.size())
280     return myActionsList[aId];
281   return QString();
282 }
283
284 //******************************************************
285 QAction* NewGeom_Module::command(const QString& theId) const
286 {
287   int aId = myActionsList.indexOf(theId);
288   if ((aId != -1) && (aId < myActionsList.size())) {
289     return action(aId);
290   }
291   return 0;
292 }
293
294 //******************************************************
295 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
296 {
297   myNestedActions[theId] = theActions;
298 }
299
300 //******************************************************
301 QStringList NewGeom_Module::nestedActions(const QString& theId) const
302 {
303   if (myNestedActions.contains(theId))
304     return myNestedActions[theId];
305   return QStringList();
306 }
307
308 //******************************************************
309 void NewGeom_Module::selectionChanged()
310 {
311   LightApp_Module::selectionChanged();
312   myWorkshop->salomeViewerSelectionChanged();
313 }
314
315 //******************************************************
316 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
317 {
318   myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
319   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
320 }
321
322
323 //******************************************************
324 void NewGeom_Module::createPreferences()
325 {
326   LightApp_Preferences* pref = preferences();
327   if (!pref)
328     return;
329   XGUI_Preferences::updateCustomProps();
330   QString aModName = moduleName();
331
332   QtxPreferenceItem* item = pref->findItem(aModName, true );
333   if ( item && (!item->isEmpty() )) {
334     item->parentItem()->removeItem(item);
335     delete item;
336   }
337
338   int catId = pref->addPreference(aModName, -1 );
339   if ( catId == -1 )
340     return;
341   NewGeom_PrefMgr aMgr(pref, aModName);
342   XGUI_Preferences::createEditContent(&aMgr, catId);
343   pref->retrieve();
344 }
345
346 //******************************************************
347 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
348 {
349   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
350   QString aVal = aResMgr->stringValue(theSection, theParam);
351   if (!aVal.isNull()) {
352     Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
353     aProp->setValue(aVal.toStdString());
354   }
355 }