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()
54 * Class for preferences management
56 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
60 /// \param theMgr preferences manager of SALOME
61 /// \param theModName name of the module
62 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
64 virtual int addPreference(const QString& theLbl, int pId,
65 SUIT_PreferenceMgr::PrefItemType theType,
66 const QString& theSection, const QString& theName )
68 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
71 virtual void setItemProperty(const QString& thePropName,
72 const QVariant& theValue,
75 myMgr->setItemProperty(thePropName, theValue, theId);
79 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
82 LightApp_Preferences* myMgr;
89 //******************************************************
90 NewGeom_Module::NewGeom_Module()
91 : LightApp_Module("NewGeom"),
92 mySelector(0), myIsOpened(0), myPopupMgr(0)
94 myWorkshop = new XGUI_Workshop(this);
95 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
96 this, SLOT(onUpdateCommandStatus()));
98 myProxyViewer = new NewGeom_SalomeViewer(this);
100 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
101 ModuleBase_Preferences::loadCustomProps();
104 //******************************************************
105 NewGeom_Module::~NewGeom_Module()
109 //******************************************************
110 void NewGeom_Module::initialize(CAM_Application* theApp)
112 LightApp_Module::initialize(theApp);
113 inspectSalomeModules();
115 myWorkshop->startApplication();
116 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
118 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
121 //******************************************************
122 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
124 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
127 //******************************************************
128 void NewGeom_Module::viewManagers(QStringList& theList) const
130 theList.append(OCCViewer_Viewer::Type());
133 //******************************************************
134 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
136 bool isDone = LightApp_Module::activateModule(theStudy);
141 QObject* aObj = myWorkshop->objectBrowser()->parent();
142 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
144 QAction* aViewAct = aObjDoc->toggleViewAction();
145 aViewAct->setEnabled(true);
146 myWorkshop->objectBrowser()->setVisible(true);
147 aObjDoc->setVisible(true);
151 ViewManagerList OCCViewManagers;
152 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
153 if (OCCViewManagers.size() > 0) {
154 mySelector = createSelector(OCCViewManagers.first());
157 //action(myEraseAll)->setEnabled(false);
160 myWorkshop->objectBrowser()->rebuildDataTree();
161 myWorkshop->updateCommandStatus();
163 QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
166 myWorkshop->updateCommandStatus();
168 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
169 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
170 myIsEditEnabled = getApp()->isEditEnabled();
171 getApp()->setEditEnabled(false);
173 // this following row is caused by #187 bug.
174 // SALOME saves the dock widget positions before deactivateModule() and
175 // load it after the module activation. So, if the panel is visible before
176 // deactivate, it becomes visible after activate.
177 // In order to avoid the visible property panel, the widget position save is
178 // switch off in this module
179 aResMgr->setValue("Study", "store_positions", false);
184 //******************************************************
185 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
190 QObject* aObj = myWorkshop->objectBrowser()->parent();
191 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
193 aObjDoc->setVisible(false);
194 myWorkshop->objectBrowser()->setVisible(false);
195 QAction* aViewAct = aObjDoc->toggleViewAction();
196 aViewAct->setEnabled(false);
199 // the active operation should be stopped for the next activation.
200 // There should not be active operation and visualized preview.
201 // Abort operation should be performed before the selection's remove
202 // because the displayed objects should be removed from the viewer, but
203 // the AIS context is obtained from the selector.
204 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
205 while (anOperation) {
206 anOperation->abort();
207 anOperation = myWorkshop->operationMgr()->currentOperation();
209 // Delete selector because it has to be redefined on next activation
211 myProxyViewer->setSelector(0);
216 //myWorkshop->contextMenuMgr()->disconnectViewer();
218 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
219 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
220 getApp()->setEditEnabled(myIsEditEnabled);
222 return LightApp_Module::deactivateModule(theStudy);
225 //******************************************************
226 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
229 mySelector = createSelector(theMgr);
233 //******************************************************
234 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
237 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
238 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
239 if (mySelector->viewer() == aViewer) {
240 myWorkshop->displayer()->eraseAll(false);
241 myProxyViewer->setSelector(0);
249 //******************************************************
250 QtxPopupMgr* NewGeom_Module::popupMgr()
253 myPopupMgr = new QtxPopupMgr( 0, this );
257 //******************************************************
258 void NewGeom_Module::onDefaultPreferences()
260 ModuleBase_Preferences::resetConfig();
261 ModuleBase_Preferences::updateResourcesByConfig();
263 LightApp_Preferences* pref = preferences();
268 //******************************************************
269 void NewGeom_Module::onUpdateCommandStatus()
271 getApp()->updateActions();
274 //******************************************************
275 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
277 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
278 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
279 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
280 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
281 QList<SUIT_Selector*> aList;
282 aMgr->selectors(aList);
283 foreach(SUIT_Selector* aSel, aList)
285 aSel->setEnabled(aSel == aSelector);
287 myProxyViewer->setSelector(aSelector);
293 //******************************************************
294 CAM_DataModel* NewGeom_Module::createDataModel()
296 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
298 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
299 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
300 aStudy->addComponent(aDataModel);
305 //******************************************************
306 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
307 const QString& theTitle, const QString& theTip,
308 const QIcon& theIcon, const QKeySequence& theKeys,
311 int aMenu = createMenu(theWBName, -1, -1, 50);
312 int aTool = createTool(theWBName);
314 int aId = myActionsList.size();
315 myActionsList.append(theId);
316 SUIT_Desktop* aDesk = application()->desktop();
318 for (unsigned int i = 0; i < theKeys.count(); i++)
320 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
322 aAction->setData(theId);
323 int aItemId = createMenu(aId, aMenu, -1, 10);
324 int aToolId = createTool(aId, aTool);
328 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
330 return addFeature(theWBName,
339 //******************************************************
340 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
341 const QString& theTip, const QIcon& theIcon,
342 const QKeySequence& theKeys, bool isCheckable,
343 const char* theMenuSourceText, const int theMenuPosition)
345 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
347 int aId = myActionsList.size();
348 myActionsList.append(theId);
349 SUIT_Desktop* aDesk = application()->desktop();
351 for (unsigned int i = 0; i < theKeys.count(); i++)
353 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
355 aAction->setData(theId);
356 createMenu(aId, aMenu, theMenuPosition);
360 //******************************************************
361 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
363 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
364 createMenu(separator(), aMenu, -1, theMenuPosition);
367 //******************************************************
368 QList<QAction*> NewGeom_Module::commandList() const
370 QList<QAction*> aActions;
371 for (int i = 0; i < myActionsList.size(); i++)
372 aActions.append(action(i));
376 //******************************************************
377 QStringList NewGeom_Module::commandIdList() const
379 return myActionsList;
382 //******************************************************
383 QMainWindow* NewGeom_Module::desktop() const
385 return application()->desktop();
388 //******************************************************
389 QString NewGeom_Module::commandId(const QAction* theCmd) const
391 int aId = actionId(theCmd);
392 if (aId < myActionsList.size())
393 return myActionsList[aId];
397 //******************************************************
398 QAction* NewGeom_Module::command(const QString& theId) const
400 int aId = myActionsList.indexOf(theId);
401 if ((aId != -1) && (aId < myActionsList.size())) {
407 //******************************************************
408 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
410 myNestedActions[theId] = theActions;
413 //******************************************************
414 QStringList NewGeom_Module::nestedActions(const QString& theId) const
416 if (myNestedActions.contains(theId))
417 return myNestedActions[theId];
418 return QStringList();
421 //******************************************************
422 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
424 myDocumentType[theId] = theKind;
427 //******************************************************
428 QString NewGeom_Module::documentKind(const QString& theId) const
430 if (myDocumentType.contains(theId))
431 return myDocumentType[theId];
436 //******************************************************
437 void NewGeom_Module::selectionChanged()
439 LightApp_Module::selectionChanged();
440 myWorkshop->salomeViewerSelectionChanged();
443 //******************************************************
444 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
446 myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
447 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
451 //******************************************************
452 void NewGeom_Module::createPreferences()
454 LightApp_Preferences* pref = preferences();
457 ModuleBase_Preferences::updateConfigByResources();
458 QString aModName = moduleName();
460 QtxPreferenceItem* item = pref->findItem(aModName, true );
461 if ( item && (!item->isEmpty() )) {
462 item->parentItem()->removeItem(item);
466 int catId = pref->addPreference(aModName, -1 );
469 NewGeom_PrefMgr aMgr(pref, aModName);
470 ModuleBase_Preferences::createEditContent(&aMgr, catId);
474 //******************************************************
475 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
477 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
478 QString aVal = aResMgr->stringValue(theSection, theParam);
479 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
480 std::string aValue = aVal.toStdString();
481 if (aValue.empty()) {
482 aValue = aProp->defaultValue();
483 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
485 LightApp_Preferences* pref = preferences();
489 aProp->setValue(aValue);
493 void NewGeom_Module::inspectSalomeModules()
495 QStringList aModuleNames;
496 getApp()->modules(aModuleNames, false);
497 foreach(QString eachModule, aModuleNames) {
498 Config_ModuleReader::addDependencyModule(eachModule.toStdString());