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>
13 #include <XGUI_Displayer.h>
15 #include <ModuleBase_Operation.h>
16 #include <ModuleBase_Preferences.h>
18 #include <LightApp_Application.h>
19 #include <LightApp_SelectionMgr.h>
20 #include <LightApp_OCCSelector.h>
21 #include <LightApp_Study.h>
22 #include <OCCViewer_ViewModel.h>
24 #include <SUIT_Selector.h>
25 #include <SUIT_Desktop.h>
26 #include <SUIT_ViewManager.h>
27 #include <SUIT_ResourceMgr.h>
29 #include <QtxPopupMgr.h>
30 #include <QtxActionMenuMgr.h>
31 #include <QtxResourceMgr.h>
33 #include <Config_PropManager.h>
34 #include <Config_ModuleReader.h>
36 #include <QDockWidget>
42 NewGeom_EXPORT CAM_Module* createModule()
44 return new NewGeom_Module();
47 NewGeom_EXPORT char* getModuleVersion()
53 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
56 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
58 virtual int addPreference(const QString& theLbl, int pId,
59 SUIT_PreferenceMgr::PrefItemType theType,
60 const QString& theSection, const QString& theName )
62 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
65 virtual void setItemProperty(const QString& thePropName,
66 const QVariant& theValue,
69 myMgr->setItemProperty(thePropName, theValue, theId);
73 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
76 LightApp_Preferences* myMgr;
83 //******************************************************
84 NewGeom_Module::NewGeom_Module()
85 : LightApp_Module("NewGeom"),
86 mySelector(0), myIsOpened(0), myPopupMgr(0)
88 myWorkshop = new XGUI_Workshop(this);
89 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
90 this, SLOT(onUpdateCommandStatus()));
92 myProxyViewer = new NewGeom_SalomeViewer(this);
94 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
95 ModuleBase_Preferences::loadCustomProps();
98 //******************************************************
99 NewGeom_Module::~NewGeom_Module()
103 //******************************************************
104 void NewGeom_Module::initialize(CAM_Application* theApp)
106 LightApp_Module::initialize(theApp);
107 inspectSalomeModules();
109 myWorkshop->startApplication();
110 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
112 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
115 //******************************************************
116 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
118 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
121 //******************************************************
122 void NewGeom_Module::viewManagers(QStringList& theList) const
124 theList.append(OCCViewer_Viewer::Type());
127 //******************************************************
128 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
130 bool isDone = LightApp_Module::activateModule(theStudy);
135 QObject* aObj = myWorkshop->objectBrowser()->parent();
136 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
138 QAction* aViewAct = aObjDoc->toggleViewAction();
139 aViewAct->setEnabled(true);
140 myWorkshop->objectBrowser()->setVisible(true);
141 aObjDoc->setVisible(true);
145 ViewManagerList OCCViewManagers;
146 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
147 if (OCCViewManagers.size() > 0) {
148 mySelector = createSelector(OCCViewManagers.first());
151 //action(myEraseAll)->setEnabled(false);
154 myWorkshop->objectBrowser()->rebuildDataTree();
155 myWorkshop->updateCommandStatus();
157 QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
160 myWorkshop->updateCommandStatus();
162 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
163 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
164 myIsEditEnabled = getApp()->isEditEnabled();
165 getApp()->setEditEnabled(false);
167 // this following row is caused by #187 bug.
168 // SALOME saves the dock widget positions before deactivateModule() and
169 // load it after the module activation. So, if the panel is visible before
170 // deactivate, it becomes visible after activate.
171 // In order to avoid the visible property panel, the widget position save is
172 // switch off in this module
173 aResMgr->setValue("Study", "store_positions", false);
178 //******************************************************
179 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
184 QObject* aObj = myWorkshop->objectBrowser()->parent();
185 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
187 aObjDoc->setVisible(false);
188 myWorkshop->objectBrowser()->setVisible(false);
189 QAction* aViewAct = aObjDoc->toggleViewAction();
190 aViewAct->setEnabled(false);
193 // the active operation should be stopped for the next activation.
194 // There should not be active operation and visualized preview.
195 // Abort operation should be performed before the selection's remove
196 // because the displayed objects should be removed from the viewer, but
197 // the AIS context is obtained from the selector.
198 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
199 while (anOperation) {
200 anOperation->abort();
201 anOperation = myWorkshop->operationMgr()->currentOperation();
203 // Delete selector because it has to be redefined on next activation
205 myProxyViewer->setSelector(0);
210 //myWorkshop->contextMenuMgr()->disconnectViewer();
212 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
213 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
214 getApp()->setEditEnabled(myIsEditEnabled);
216 return LightApp_Module::deactivateModule(theStudy);
219 //******************************************************
220 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
223 mySelector = createSelector(theMgr);
227 //******************************************************
228 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
231 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
232 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
233 if (mySelector->viewer() == aViewer) {
234 myWorkshop->displayer()->eraseAll(false);
235 myProxyViewer->setSelector(0);
243 //******************************************************
244 QtxPopupMgr* NewGeom_Module::popupMgr()
247 myPopupMgr = new QtxPopupMgr( 0, this );
251 //******************************************************
252 void NewGeom_Module::onDefaultPreferences()
254 ModuleBase_Preferences::resetConfig();
255 ModuleBase_Preferences::updateResourcesByConfig();
257 LightApp_Preferences* pref = preferences();
262 //******************************************************
263 void NewGeom_Module::onUpdateCommandStatus()
265 getApp()->updateActions();
268 //******************************************************
269 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
271 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
272 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
273 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
274 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
275 QList<SUIT_Selector*> aList;
276 aMgr->selectors(aList);
277 foreach(SUIT_Selector* aSel, aList)
279 aSel->setEnabled(aSel == aSelector);
281 myProxyViewer->setSelector(aSelector);
287 //******************************************************
288 CAM_DataModel* NewGeom_Module::createDataModel()
290 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
292 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
293 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
294 aStudy->addComponent(aDataModel);
299 //******************************************************
300 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
301 const QString& theTitle, const QString& theTip,
302 const QIcon& theIcon, const QKeySequence& theKeys,
305 int aMenu = createMenu(theWBName, -1, -1, 50);
306 int aTool = createTool(theWBName);
308 int aId = myActionsList.size();
309 myActionsList.append(theId);
310 SUIT_Desktop* aDesk = application()->desktop();
312 for (unsigned int i = 0; i < theKeys.count(); i++)
314 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
316 aAction->setData(theId);
317 int aItemId = createMenu(aId, aMenu, -1, 10);
318 int aToolId = createTool(aId, aTool);
322 //******************************************************
323 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
324 const QString& theTip, const QIcon& theIcon,
325 const QKeySequence& theKeys, bool isCheckable,
326 const char* theMenuSourceText, const int theMenuPosition)
328 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
330 int aId = myActionsList.size();
331 myActionsList.append(theId);
332 SUIT_Desktop* aDesk = application()->desktop();
334 for (unsigned int i = 0; i < theKeys.count(); i++)
336 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
338 aAction->setData(theId);
339 createMenu(aId, aMenu, theMenuPosition);
343 //******************************************************
344 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
346 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
347 createMenu(separator(), aMenu, -1, theMenuPosition);
350 //******************************************************
351 QList<QAction*> NewGeom_Module::commandList() const
353 QList<QAction*> aActions;
354 for (int i = 0; i < myActionsList.size(); i++)
355 aActions.append(action(i));
359 //******************************************************
360 QStringList NewGeom_Module::commandIdList() const
362 return myActionsList;
365 //******************************************************
366 QMainWindow* NewGeom_Module::desktop() const
368 return application()->desktop();
371 //******************************************************
372 QString NewGeom_Module::commandId(const QAction* theCmd) const
374 int aId = actionId(theCmd);
375 if (aId < myActionsList.size())
376 return myActionsList[aId];
380 //******************************************************
381 QAction* NewGeom_Module::command(const QString& theId) const
383 int aId = myActionsList.indexOf(theId);
384 if ((aId != -1) && (aId < myActionsList.size())) {
390 //******************************************************
391 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
393 myNestedActions[theId] = theActions;
396 //******************************************************
397 QStringList NewGeom_Module::nestedActions(const QString& theId) const
399 if (myNestedActions.contains(theId))
400 return myNestedActions[theId];
401 return QStringList();
404 //******************************************************
405 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
407 myDocumentType[theId] = theKind;
410 //******************************************************
411 QString NewGeom_Module::documentKind(const QString& theId) const
413 if (myDocumentType.contains(theId))
414 return myDocumentType[theId];
419 //******************************************************
420 void NewGeom_Module::selectionChanged()
422 LightApp_Module::selectionChanged();
423 myWorkshop->salomeViewerSelectionChanged();
426 //******************************************************
427 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
429 myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
430 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
434 //******************************************************
435 void NewGeom_Module::createPreferences()
437 LightApp_Preferences* pref = preferences();
440 ModuleBase_Preferences::updateConfigByResources();
441 QString aModName = moduleName();
443 QtxPreferenceItem* item = pref->findItem(aModName, true );
444 if ( item && (!item->isEmpty() )) {
445 item->parentItem()->removeItem(item);
449 int catId = pref->addPreference(aModName, -1 );
452 NewGeom_PrefMgr aMgr(pref, aModName);
453 ModuleBase_Preferences::createEditContent(&aMgr, catId);
457 //******************************************************
458 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
460 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
461 QString aVal = aResMgr->stringValue(theSection, theParam);
462 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
463 std::string aValue = aVal.toStdString();
464 if (aValue.empty()) {
465 aValue = aProp->defaultValue();
466 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
468 LightApp_Preferences* pref = preferences();
472 aProp->setValue(aValue);
476 void NewGeom_Module::inspectSalomeModules()
478 QStringList aModuleNames;
479 getApp()->modules(aModuleNames, false);
480 foreach(QString eachModule, aModuleNames) {
481 Config_ModuleReader::addDependencyModule(eachModule.toStdString());