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();
200 anOperation->abort();
202 // Delete selector because it has to be redefined on next activation
204 myProxyViewer->setSelector(0);
209 //myWorkshop->contextMenuMgr()->disconnectViewer();
211 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
212 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
213 getApp()->setEditEnabled(myIsEditEnabled);
215 return LightApp_Module::deactivateModule(theStudy);
218 //******************************************************
219 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
222 mySelector = createSelector(theMgr);
226 //******************************************************
227 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
230 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
231 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
232 if (mySelector->viewer() == aViewer) {
233 myWorkshop->displayer()->eraseAll(false);
234 myProxyViewer->setSelector(0);
242 //******************************************************
243 QtxPopupMgr* NewGeom_Module::popupMgr()
246 myPopupMgr = new QtxPopupMgr( 0, this );
250 //******************************************************
251 void NewGeom_Module::onDefaultPreferences()
253 ModuleBase_Preferences::resetConfig();
254 ModuleBase_Preferences::updateResourcesByConfig();
256 LightApp_Preferences* pref = preferences();
261 //******************************************************
262 void NewGeom_Module::onUpdateCommandStatus()
264 getApp()->updateActions();
267 //******************************************************
268 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
270 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
271 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
272 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
273 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
274 QList<SUIT_Selector*> aList;
275 aMgr->selectors(aList);
276 foreach(SUIT_Selector* aSel, aList)
278 aSel->setEnabled(aSel == aSelector);
280 myProxyViewer->setSelector(aSelector);
286 //******************************************************
287 CAM_DataModel* NewGeom_Module::createDataModel()
289 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
291 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
292 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
293 aStudy->addComponent(aDataModel);
298 //******************************************************
299 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
300 const QString& theTitle, const QString& theTip,
301 const QIcon& theIcon, const QKeySequence& theKeys,
304 int aMenu = createMenu(theWBName, -1, -1, 50);
305 int aTool = createTool(theWBName);
307 int aId = myActionsList.size();
308 myActionsList.append(theId);
309 SUIT_Desktop* aDesk = application()->desktop();
311 for (unsigned int i = 0; i < theKeys.count(); i++)
313 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
315 aAction->setData(theId);
316 int aItemId = createMenu(aId, aMenu, -1, 10);
317 int aToolId = createTool(aId, aTool);
321 //******************************************************
322 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
323 const QString& theTip, const QIcon& theIcon,
324 const QKeySequence& theKeys, bool isCheckable,
325 const char* theMenuSourceText, const int theMenuPosition)
327 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
329 int aId = myActionsList.size();
330 myActionsList.append(theId);
331 SUIT_Desktop* aDesk = application()->desktop();
333 for (unsigned int i = 0; i < theKeys.count(); i++)
335 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
337 aAction->setData(theId);
338 createMenu(aId, aMenu, theMenuPosition);
342 //******************************************************
343 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
345 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
346 createMenu(separator(), aMenu, -1, theMenuPosition);
349 //******************************************************
350 QList<QAction*> NewGeom_Module::commandList() const
352 QList<QAction*> aActions;
353 for (int i = 0; i < myActionsList.size(); i++)
354 aActions.append(action(i));
358 //******************************************************
359 QStringList NewGeom_Module::commandIdList() const
361 return myActionsList;
364 //******************************************************
365 QMainWindow* NewGeom_Module::desktop() const
367 return application()->desktop();
370 //******************************************************
371 QString NewGeom_Module::commandId(const QAction* theCmd) const
373 int aId = actionId(theCmd);
374 if (aId < myActionsList.size())
375 return myActionsList[aId];
379 //******************************************************
380 QAction* NewGeom_Module::command(const QString& theId) const
382 int aId = myActionsList.indexOf(theId);
383 if ((aId != -1) && (aId < myActionsList.size())) {
389 //******************************************************
390 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
392 myNestedActions[theId] = theActions;
395 //******************************************************
396 QStringList NewGeom_Module::nestedActions(const QString& theId) const
398 if (myNestedActions.contains(theId))
399 return myNestedActions[theId];
400 return QStringList();
403 //******************************************************
404 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
406 myDocumentType[theId] = theKind;
409 //******************************************************
410 QString NewGeom_Module::documentKind(const QString& theId) const
412 if (myDocumentType.contains(theId))
413 return myDocumentType[theId];
418 //******************************************************
419 void NewGeom_Module::selectionChanged()
421 LightApp_Module::selectionChanged();
422 myWorkshop->salomeViewerSelectionChanged();
425 //******************************************************
426 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
428 myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
429 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
433 //******************************************************
434 void NewGeom_Module::createPreferences()
436 LightApp_Preferences* pref = preferences();
439 ModuleBase_Preferences::updateConfigByResources();
440 QString aModName = moduleName();
442 QtxPreferenceItem* item = pref->findItem(aModName, true );
443 if ( item && (!item->isEmpty() )) {
444 item->parentItem()->removeItem(item);
448 int catId = pref->addPreference(aModName, -1 );
451 NewGeom_PrefMgr aMgr(pref, aModName);
452 ModuleBase_Preferences::createEditContent(&aMgr, catId);
456 //******************************************************
457 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
459 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
460 QString aVal = aResMgr->stringValue(theSection, theParam);
461 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
462 std::string aValue = aVal.toStdString();
463 if (aValue.empty()) {
464 aValue = aProp->defaultValue();
465 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
467 LightApp_Preferences* pref = preferences();
471 aProp->setValue(aValue);
475 void NewGeom_Module::inspectSalomeModules()
477 QStringList aModuleNames;
478 getApp()->modules(aModuleNames, false);
479 foreach(QString eachModule, aModuleNames) {
480 Config_ModuleReader::addDependencyModule(eachModule.toStdString());