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