1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
4 #include "NewGeom_Module.h"
5 #include "NewGeom_DataModel.h"
6 #include "NewGeom_OCCSelector.h"
8 #include <XGUI_Workshop.h>
9 #include <XGUI_PropertyPanel.h>
10 #include <XGUI_ContextMenuMgr.h>
11 #include <XGUI_ObjectsBrowser.h>
12 #include <XGUI_OperationMgr.h>
14 #include <ModuleBase_Operation.h>
15 #include <ModuleBase_Preferences.h>
17 #include <LightApp_Application.h>
18 #include <LightApp_SelectionMgr.h>
19 #include <LightApp_OCCSelector.h>
20 #include <LightApp_Study.h>
21 #include <OCCViewer_ViewModel.h>
23 #include <SUIT_Selector.h>
24 #include <SUIT_Desktop.h>
25 #include <SUIT_ViewManager.h>
26 #include <SUIT_ResourceMgr.h>
28 #include <QtxPopupMgr.h>
29 #include <QtxActionMenuMgr.h>
30 #include <QtxResourceMgr.h>
32 #include <Config_PropManager.h>
34 #include <QDockWidget>
40 NewGeom_EXPORT CAM_Module* createModule()
42 return new NewGeom_Module();
45 NewGeom_EXPORT char* getModuleVersion()
51 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
54 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
56 virtual int addPreference(const QString& theLbl, int pId,
57 SUIT_PreferenceMgr::PrefItemType theType,
58 const QString& theSection, const QString& theName )
60 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
63 virtual void setItemProperty(const QString& thePropName,
64 const QVariant& theValue,
67 myMgr->setItemProperty(thePropName, theValue, theId);
71 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
74 LightApp_Preferences* myMgr;
81 //******************************************************
82 NewGeom_Module::NewGeom_Module()
83 : LightApp_Module("NewGeom"),
84 mySelector(0), myIsOpened(0), myPopupMgr(0)
86 myWorkshop = new XGUI_Workshop(this);
87 myProxyViewer = new NewGeom_SalomeViewer(this);
89 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
90 ModuleBase_Preferences::loadCustomProps();
93 //******************************************************
94 NewGeom_Module::~NewGeom_Module()
98 //******************************************************
99 void NewGeom_Module::initialize(CAM_Application* theApp)
101 LightApp_Module::initialize(theApp);
103 myWorkshop->startApplication();
104 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
106 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
109 //******************************************************
110 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
112 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
115 //******************************************************
116 void NewGeom_Module::viewManagers(QStringList& theList) const
118 theList.append(OCCViewer_Viewer::Type());
121 //******************************************************
122 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
124 bool isDone = LightApp_Module::activateModule(theStudy);
129 QObject* aObj = myWorkshop->objectBrowser()->parent();
130 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
132 QAction* aViewAct = aObjDoc->toggleViewAction();
133 aViewAct->setEnabled(true);
134 myWorkshop->objectBrowser()->setVisible(true);
135 aObjDoc->setVisible(true);
139 ViewManagerList OCCViewManagers;
140 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
141 if (OCCViewManagers.size() > 0) {
142 mySelector = createSelector(OCCViewManagers.first());
145 //action(myEraseAll)->setEnabled(false);
148 myWorkshop->objectBrowser()->rebuildDataTree();
149 myWorkshop->updateCommandStatus();
151 QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
154 myWorkshop->updateCommandStatus();
156 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
157 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
158 myIsEditEnabled = getApp()->isEditEnabled();
159 getApp()->setEditEnabled(false);
161 // this following row is caused by #187 bug.
162 // SALOME saves the dock widget positions before deactivateModule() and
163 // load it after the module activation. So, if the panel is visible before
164 // deactivate, it becomes visible after activate.
165 // In order to avoid the visible property panel, the widget position save is
166 // switch off in this module
167 aResMgr->setValue("Study", "store_positions", false);
172 //******************************************************
173 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
178 QObject* aObj = myWorkshop->objectBrowser()->parent();
179 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
181 aObjDoc->setVisible(false);
182 myWorkshop->objectBrowser()->setVisible(false);
183 QAction* aViewAct = aObjDoc->toggleViewAction();
184 aViewAct->setEnabled(false);
187 // the active operation should be stopped for the next activation.
188 // There should not be active operation and visualized preview.
189 // Abort operation should be performed before the selection's remove
190 // because the displayed objects should be removed from the viewer, but
191 // the AIS context is obtained from the selector.
192 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
194 anOperation->abort();
196 // Delete selector because it has to be redefined on next activation
198 myProxyViewer->setSelector(0);
203 //myWorkshop->contextMenuMgr()->disconnectViewer();
205 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
206 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
207 getApp()->setEditEnabled(myIsEditEnabled);
209 return LightApp_Module::deactivateModule(theStudy);
212 //******************************************************
213 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
216 mySelector = createSelector(theMgr);
220 //******************************************************
221 QtxPopupMgr* NewGeom_Module::popupMgr()
224 myPopupMgr = new QtxPopupMgr( 0, this );
228 //******************************************************
229 void NewGeom_Module::onDefaultPreferences()
231 ModuleBase_Preferences::resetConfig();
232 ModuleBase_Preferences::updateResourcesByConfig();
234 LightApp_Preferences* pref = preferences();
239 //******************************************************
240 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
242 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
243 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
244 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
245 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
246 QList<SUIT_Selector*> aList;
247 aMgr->selectors(aList);
248 foreach(SUIT_Selector* aSel, aList)
250 aSel->setEnabled(aSel == aSelector);
252 myProxyViewer->setSelector(aSelector);
258 //******************************************************
259 CAM_DataModel* NewGeom_Module::createDataModel()
261 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
263 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
264 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
265 aStudy->addComponent(aDataModel);
270 //******************************************************
271 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
272 const QString& theTitle, const QString& theTip,
273 const QIcon& theIcon, const QKeySequence& theKeys,
276 int aMenu = createMenu(theWBName, -1, -1, 50);
277 int aTool = createTool(theWBName);
279 int aId = myActionsList.size();
280 myActionsList.append(theId);
281 SUIT_Desktop* aDesk = application()->desktop();
283 for (unsigned int i = 0; i < theKeys.count(); i++)
285 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
287 aAction->setData(theId);
288 int aItemId = createMenu(aId, aMenu, -1, 10);
289 int aToolId = createTool(aId, aTool);
293 //******************************************************
294 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
295 const QString& theTip, const QIcon& theIcon,
296 const QKeySequence& theKeys, bool isCheckable,
297 const char* theMenuSourceText, const int theMenuPosition)
299 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
301 int aId = myActionsList.size();
302 myActionsList.append(theId);
303 SUIT_Desktop* aDesk = application()->desktop();
305 for (unsigned int i = 0; i < theKeys.count(); i++)
307 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
309 aAction->setData(theId);
310 createMenu(aId, aMenu, theMenuPosition);
314 //******************************************************
315 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
317 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
318 createMenu(separator(), aMenu, -1, theMenuPosition);
321 //******************************************************
322 QList<QAction*> NewGeom_Module::commandList() const
324 QList<QAction*> aActions;
325 for (int i = 0; i < myActionsList.size(); i++)
326 aActions.append(action(i));
330 //******************************************************
331 QStringList NewGeom_Module::commandIdList() const
333 return myActionsList;
336 //******************************************************
337 QMainWindow* NewGeom_Module::desktop() const
339 return application()->desktop();
342 //******************************************************
343 QString NewGeom_Module::commandId(const QAction* theCmd) const
345 int aId = actionId(theCmd);
346 if (aId < myActionsList.size())
347 return myActionsList[aId];
351 //******************************************************
352 QAction* NewGeom_Module::command(const QString& theId) const
354 int aId = myActionsList.indexOf(theId);
355 if ((aId != -1) && (aId < myActionsList.size())) {
361 //******************************************************
362 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
364 myNestedActions[theId] = theActions;
367 //******************************************************
368 QStringList NewGeom_Module::nestedActions(const QString& theId) const
370 if (myNestedActions.contains(theId))
371 return myNestedActions[theId];
372 return QStringList();
375 //******************************************************
376 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
378 myDocumentType[theId] = theKind;
381 //******************************************************
382 QString NewGeom_Module::documentKind(const QString& theId) const
384 if (myDocumentType.contains(theId))
385 return myDocumentType[theId];
390 //******************************************************
391 void NewGeom_Module::selectionChanged()
393 LightApp_Module::selectionChanged();
394 myWorkshop->salomeViewerSelectionChanged();
397 //******************************************************
398 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
400 myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
401 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
405 //******************************************************
406 void NewGeom_Module::createPreferences()
408 LightApp_Preferences* pref = preferences();
411 ModuleBase_Preferences::updateConfigByResources();
412 QString aModName = moduleName();
414 QtxPreferenceItem* item = pref->findItem(aModName, true );
415 if ( item && (!item->isEmpty() )) {
416 item->parentItem()->removeItem(item);
420 int catId = pref->addPreference(aModName, -1 );
423 NewGeom_PrefMgr aMgr(pref, aModName);
424 ModuleBase_Preferences::createEditContent(&aMgr, catId);
428 //******************************************************
429 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
431 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
432 QString aVal = aResMgr->stringValue(theSection, theParam);
433 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
434 std::string aValue = aVal.toStdString();
435 if (aValue.empty()) {
436 aValue = aProp->defaultValue();
437 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
439 LightApp_Preferences* pref = preferences();
443 aProp->setValue(aValue);