Salome HOME
Merge branch 'master' of newgeom:newgeom.git
[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
11 #include <LightApp_Application.h>
12 #include <LightApp_SelectionMgr.h>
13 #include <LightApp_OCCSelector.h>
14 #include <OCCViewer_ViewModel.h>
15
16 #include <SUIT_Selector.h>
17 #include <SUIT_Desktop.h>
18 #include <SUIT_ViewManager.h>
19 #include <SUIT_ResourceMgr.h>
20
21 #include <QtxPopupMgr.h>
22 #include <QtxActionMenuMgr.h>
23 #include <QtxResourceMgr.h>
24
25 #include <Config_PropManager.h>
26
27 #include <QDockWidget>
28 #include <QAction>
29
30 extern "C" {
31 NewGeom_EXPORT CAM_Module* createModule()
32 {
33   return new NewGeom_Module();
34 }
35
36 NewGeom_EXPORT char* getModuleVersion()
37 {
38   return "0.0";
39 }
40 }
41
42 class NewGeom_PrefMgr: public XGUI_IPrefMgr
43 {
44 public:
45   NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
46
47   virtual int addPreference(const QString& theLbl, int pId, 
48                             SUIT_PreferenceMgr::PrefItemType theType,
49                             const QString& theSection, const QString& theName )
50   {
51     return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
52   }
53
54   virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
55
56 private:
57   LightApp_Preferences* myMgr;
58   QString myModName;
59 };
60
61
62
63
64 //******************************************************
65 NewGeom_Module::NewGeom_Module()
66     : LightApp_Module("NewGeom"),
67       mySelector(0)
68 {
69   myWorkshop = new XGUI_Workshop(this);
70   myProxyViewer = new NewGeom_SalomeViewer(this);
71
72   XGUI_Preferences::setResourceMgr(application()->resourceMgr());
73   XGUI_Preferences::loadCustomProps();
74 }
75
76 //******************************************************
77 NewGeom_Module::~NewGeom_Module()
78 {
79 }
80
81 //******************************************************
82 void NewGeom_Module::initialize(CAM_Application* theApp)
83 {
84   LightApp_Module::initialize(theApp);
85
86   myWorkshop->startApplication();
87 }
88
89 //******************************************************
90 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
91 {
92   theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
93 }
94
95 //******************************************************
96 void NewGeom_Module::viewManagers(QStringList& theList) const
97 {
98   theList.append(OCCViewer_Viewer::Type());
99 }
100
101 //******************************************************
102 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
103 {
104   bool isDone = LightApp_Module::activateModule(theStudy);
105   if (isDone) {
106     setMenuShown(true);
107     setToolShown(true);
108
109     if (!mySelector) {
110       ViewManagerList OCCViewManagers;
111       application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
112       if (OCCViewManagers.size() > 0) {
113         mySelector = createSelector(OCCViewManagers.first());
114       }
115     }
116     myWorkshop->propertyPanel()->hide();
117     QtxPopupMgr* aMgr = popupMgr();  // Create popup manager
118     action(myEraseAll)->setEnabled(false);
119   }
120   return isDone;
121 }
122
123 //******************************************************
124 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
125 {
126   setMenuShown(false);
127   setToolShown(false);
128   //myWorkshop->contextMenuMgr()->disconnectViewer();
129   return LightApp_Module::deactivateModule(theStudy);
130 }
131
132 //******************************************************
133 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
134 {
135   if ((!mySelector)) {
136     mySelector = createSelector(theMgr);
137   }
138 }
139
140 //******************************************************
141 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
142 {
143   if (theMgr->getType() == OCCViewer_Viewer::Type()) {
144     OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
145     NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
146     LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
147     QList<SUIT_Selector*> aList;
148     aMgr->selectors(aList);
149     foreach(SUIT_Selector* aSel, aList)
150     {
151       aSel->setEnabled(aSel == aSelector);
152     }
153     myProxyViewer->setSelector(aSelector);
154     return aSelector;
155   }
156   return 0;
157 }
158
159 //******************************************************
160 CAM_DataModel* NewGeom_Module::createDataModel()
161 {
162   return new NewGeom_DataModel(this);
163 }
164
165 //******************************************************
166 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
167                                     const QString& theTitle, const QString& theTip,
168                                     const QIcon& theIcon, const QKeySequence& theKeys,
169                                     bool isCheckable)
170 {
171   int aMenu = createMenu(theWBName, -1, -1, 50);
172   int aTool = createTool(theWBName);
173
174   int aId = myActionsList.size();
175   myActionsList.append(theId);
176   SUIT_Desktop* aDesk = application()->desktop();
177   int aKeys = 0;
178   for (unsigned int i = 0; i < theKeys.count(); i++)
179     aKeys += theKeys[i];
180   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
181                                   isCheckable);
182   aAction->setData(theId);
183   int aItemId = createMenu(aId, aMenu, -1, 10);
184   int aToolId = createTool(aId, aTool);
185   return aAction;
186 }
187
188 //******************************************************
189 QAction* NewGeom_Module::addEditCommand(const QString& theId, const QString& theTitle,
190                                         const QString& theTip, const QIcon& theIcon,
191                                         const QKeySequence& theKeys, bool isCheckable)
192 {
193   int aMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1);
194
195   int aId = myActionsList.size();
196   myActionsList.append(theId);
197   SUIT_Desktop* aDesk = application()->desktop();
198   int aKeys = 0;
199   for (unsigned int i = 0; i < theKeys.count(); i++)
200     aKeys += theKeys[i];
201   QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
202                                   isCheckable);
203   aAction->setData(theId);
204   createMenu(aId, aMenu, 10);
205   return aAction;
206 }
207
208 //******************************************************
209 void NewGeom_Module::addEditMenuSeparator()
210 {
211   int aMenu = createMenu(tr("MEN_DESK_EDIT"), -1, -1);
212   createMenu(separator(), aMenu, -1, 10);
213 }
214
215 //******************************************************
216 QList<QAction*> NewGeom_Module::commandList() const
217 {
218   QList<QAction*> aActions;
219   for (int i = 0; i < myActionsList.size(); i++)
220     aActions.append(action(i));
221   return aActions;
222 }
223
224 //******************************************************
225 QStringList NewGeom_Module::commandIdList() const
226 {
227   return myActionsList;
228 }
229
230 //******************************************************
231 QMainWindow* NewGeom_Module::desktop() const
232 {
233   return application()->desktop();
234 }
235
236 //******************************************************
237 QString NewGeom_Module::commandId(const QAction* theCmd) const
238 {
239   int aId = actionId(theCmd);
240   if (aId < myActionsList.size())
241     return myActionsList[aId];
242   return QString();
243 }
244
245 //******************************************************
246 QAction* NewGeom_Module::command(const QString& theId) const
247 {
248   int aId = myActionsList.indexOf(theId);
249   if ((aId != -1) && (aId < myActionsList.size())) {
250     return action(aId);
251   }
252   return 0;
253 }
254
255 //******************************************************
256 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
257 {
258   myNestedActions[theId] = theActions;
259 }
260
261 //******************************************************
262 QStringList NewGeom_Module::nestedActions(const QString& theId) const
263 {
264   if (myNestedActions.contains(theId))
265     return myNestedActions[theId];
266   return QStringList();
267 }
268
269 //******************************************************
270 void NewGeom_Module::selectionChanged()
271 {
272   LightApp_Module::selectionChanged();
273   myWorkshop->salomeViewerSelectionChanged();
274 }
275
276 //******************************************************
277 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
278 {
279   myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
280   LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
281 }
282
283
284 //******************************************************
285 void NewGeom_Module::createPreferences()
286 {
287   XGUI_Preferences::updateCustomProps();
288   LightApp_Preferences* pref = preferences();
289   int catId = pref->addPreference( moduleName(), -1 );
290   if ( catId == -1 )
291     return;
292   NewGeom_PrefMgr aMgr(pref, moduleName());
293   XGUI_Preferences::createEditContent(&aMgr, catId);
294   pref->retrieve();
295 }
296
297 //******************************************************
298 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
299 {
300   SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
301   QString aVal = aResMgr->stringValue(theSection, theParam);
302   if (!aVal.isNull()) {
303     Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
304     aProp->setValue(aVal.toStdString());
305   }
306 }