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"
7 #include <NewGeom_NestedButton.h>
9 #include <XGUI_Workshop.h>
10 #include <XGUI_PropertyPanel.h>
11 #include <XGUI_ContextMenuMgr.h>
12 #include <XGUI_ObjectsBrowser.h>
13 #include <XGUI_OperationMgr.h>
14 #include <XGUI_Displayer.h>
16 #include <ModuleBase_Operation.h>
17 #include <ModuleBase_Preferences.h>
18 #include <ModuleBase_ActionInfo.h>
20 #include <LightApp_Application.h>
21 #include <LightApp_SelectionMgr.h>
22 #include <LightApp_OCCSelector.h>
23 #include <LightApp_Study.h>
24 #include <OCCViewer_ViewModel.h>
26 #include <SUIT_Selector.h>
27 #include <SUIT_Desktop.h>
28 #include <SUIT_ViewManager.h>
29 #include <SUIT_ResourceMgr.h>
31 #include <QtxPopupMgr.h>
32 #include <QtxActionMenuMgr.h>
33 #include <QtxResourceMgr.h>
35 #include <Config_PropManager.h>
36 #include <Config_ModuleReader.h>
38 #include <QDockWidget>
44 NewGeom_EXPORT CAM_Module* createModule()
46 return new NewGeom_Module();
49 NewGeom_EXPORT char* getModuleVersion()
56 * Class for preferences management
58 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
62 /// \param theMgr preferences manager of SALOME
63 /// \param theModName name of the module
64 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
66 virtual int addPreference(const QString& theLbl, int pId,
67 SUIT_PreferenceMgr::PrefItemType theType,
68 const QString& theSection, const QString& theName )
70 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
73 virtual void setItemProperty(const QString& thePropName,
74 const QVariant& theValue,
77 myMgr->setItemProperty(thePropName, theValue, theId);
81 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
84 LightApp_Preferences* myMgr;
91 //******************************************************
92 NewGeom_Module::NewGeom_Module()
93 : LightApp_Module("NewGeom"),
94 mySelector(0), myIsOpened(0), myPopupMgr(0)
96 myWorkshop = new XGUI_Workshop(this);
97 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
98 this, SLOT(onUpdateCommandStatus()));
100 myProxyViewer = new NewGeom_SalomeViewer(this);
102 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
103 ModuleBase_Preferences::loadCustomProps();
106 //******************************************************
107 NewGeom_Module::~NewGeom_Module()
111 //******************************************************
112 void NewGeom_Module::initialize(CAM_Application* theApp)
114 LightApp_Module::initialize(theApp);
115 inspectSalomeModules();
117 myWorkshop->startApplication();
118 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
120 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
123 //******************************************************
124 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
126 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
129 //******************************************************
130 void NewGeom_Module::viewManagers(QStringList& theList) const
132 theList.append(OCCViewer_Viewer::Type());
135 //******************************************************
136 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
138 bool isDone = LightApp_Module::activateModule(theStudy);
143 QObject* aObj = myWorkshop->objectBrowser()->parent();
144 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
146 QAction* aViewAct = aObjDoc->toggleViewAction();
147 aViewAct->setEnabled(true);
148 myWorkshop->objectBrowser()->setVisible(true);
149 aObjDoc->setVisible(true);
153 ViewManagerList OCCViewManagers;
154 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
155 if (OCCViewManagers.size() > 0) {
156 mySelector = createSelector(OCCViewManagers.first());
159 //action(myEraseAll)->setEnabled(false);
162 myWorkshop->objectBrowser()->rebuildDataTree();
163 myWorkshop->updateCommandStatus();
165 QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
168 myWorkshop->updateCommandStatus();
170 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
171 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
172 myIsEditEnabled = getApp()->isEditEnabled();
173 getApp()->setEditEnabled(false);
175 // this following row is caused by #187 bug.
176 // SALOME saves the dock widget positions before deactivateModule() and
177 // load it after the module activation. So, if the panel is visible before
178 // deactivate, it becomes visible after activate.
179 // In order to avoid the visible property panel, the widget position save is
180 // switch off in this module
181 aResMgr->setValue("Study", "store_positions", false);
186 //******************************************************
187 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
192 QObject* aObj = myWorkshop->objectBrowser()->parent();
193 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
195 aObjDoc->setVisible(false);
196 myWorkshop->objectBrowser()->setVisible(false);
197 QAction* aViewAct = aObjDoc->toggleViewAction();
198 aViewAct->setEnabled(false);
201 // the active operation should be stopped for the next activation.
202 // There should not be active operation and visualized preview.
203 // Abort operation should be performed before the selection's remove
204 // because the displayed objects should be removed from the viewer, but
205 // the AIS context is obtained from the selector.
206 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
207 while (anOperation) {
208 anOperation->abort();
209 anOperation = myWorkshop->operationMgr()->currentOperation();
211 // Delete selector because it has to be redefined on next activation
213 myProxyViewer->setSelector(0);
218 //myWorkshop->contextMenuMgr()->disconnectViewer();
220 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
221 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
222 getApp()->setEditEnabled(myIsEditEnabled);
224 return LightApp_Module::deactivateModule(theStudy);
227 //******************************************************
228 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
231 mySelector = createSelector(theMgr);
235 //******************************************************
236 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
239 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
240 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
241 if (mySelector->viewer() == aViewer) {
242 myWorkshop->displayer()->eraseAll(false);
243 myProxyViewer->setSelector(0);
251 //******************************************************
252 QtxPopupMgr* NewGeom_Module::popupMgr()
255 myPopupMgr = new QtxPopupMgr( 0, this );
259 //******************************************************
260 void NewGeom_Module::onDefaultPreferences()
262 ModuleBase_Preferences::resetConfig();
263 ModuleBase_Preferences::updateResourcesByConfig();
265 LightApp_Preferences* pref = preferences();
270 //******************************************************
271 void NewGeom_Module::onUpdateCommandStatus()
273 getApp()->updateActions();
276 //******************************************************
277 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
279 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
280 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
281 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
282 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
283 QList<SUIT_Selector*> aList;
284 aMgr->selectors(aList);
285 foreach(SUIT_Selector* aSel, aList)
287 aSel->setEnabled(aSel == aSelector);
289 myProxyViewer->setSelector(aSelector);
295 //******************************************************
296 CAM_DataModel* NewGeom_Module::createDataModel()
298 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
300 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
301 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
302 aStudy->addComponent(aDataModel);
307 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
309 return addFeature(theWBName,
317 //******************************************************
318 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
319 const QString& theTitle, const QString& theTip,
320 const QIcon& theIcon, const QKeySequence& theKeys,
323 int aMenu = createMenu(theWBName, -1, -1, 50);
324 int aTool = createTool(theWBName);
326 int aId = myActionsList.size();
327 myActionsList.append(theId);
328 SUIT_Desktop* aDesk = application()->desktop();
330 for (unsigned int i = 0; i < theKeys.count(); i++)
332 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
334 aAction->setData(theId);
335 int aItemId = createMenu(aId, aMenu, -1, 10);
336 int aToolId = createTool(aId, aTool);
342 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
343 const ActionInfo& theInfo,
344 const QList<QAction*>& theNestedActions)
346 int aMenu = createMenu(theWBName, -1, -1, 50);
347 int aTool = createTool(theWBName);
349 int aId = myActionsList.size();
350 myActionsList.append(theInfo.id);
351 SUIT_Desktop* aDesk = application()->desktop();
352 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
353 anAction->setData(theInfo.id);
354 anAction->setCheckable(theInfo.checkable);
355 anAction->setChecked(theInfo.checked);
356 anAction->setEnabled(theInfo.enabled);
357 anAction->setVisible(theInfo.visible);
358 anAction->setIcon(theInfo.icon);
359 anAction->setText(theInfo.text);
360 anAction->setToolTip(theInfo.toolTip);
361 anAction->setShortcut(theInfo.shortcut);
362 anAction->setFont(theInfo.font);
364 //int aItemId = createMenu(aId, aMenu, -1, 10);
365 int aToolId = createTool(anAction, aTool, aId);
371 //******************************************************
372 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
373 const QString& theTip, const QIcon& theIcon,
374 const QKeySequence& theKeys, bool isCheckable,
375 const char* theMenuSourceText, const int theMenuPosition)
377 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
379 int aId = myActionsList.size();
380 myActionsList.append(theId);
381 SUIT_Desktop* aDesk = application()->desktop();
383 for (unsigned int i = 0; i < theKeys.count(); i++)
385 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
387 aAction->setData(theId);
388 createMenu(aId, aMenu, theMenuPosition);
392 //******************************************************
393 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
395 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
396 createMenu(separator(), aMenu, -1, theMenuPosition);
399 //******************************************************
400 QList<QAction*> NewGeom_Module::commandList() const
402 QList<QAction*> aActions;
403 for (int i = 0; i < myActionsList.size(); i++)
404 aActions.append(action(i));
408 //******************************************************
409 QStringList NewGeom_Module::commandIdList() const
411 return myActionsList;
414 //******************************************************
415 QMainWindow* NewGeom_Module::desktop() const
417 return application()->desktop();
420 //******************************************************
421 QString NewGeom_Module::commandId(const QAction* theCmd) const
423 int aId = actionId(theCmd);
424 if (aId < myActionsList.size())
425 return myActionsList[aId];
429 //******************************************************
430 QAction* NewGeom_Module::command(const QString& theId) const
432 int aId = myActionsList.indexOf(theId);
433 if ((aId != -1) && (aId < myActionsList.size())) {
439 //******************************************************
440 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
442 myNestedActions[theId] = theActions;
445 //******************************************************
446 QStringList NewGeom_Module::nestedActions(const QString& theId) const
448 if (myNestedActions.contains(theId))
449 return myNestedActions[theId];
450 return QStringList();
453 //******************************************************
454 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
456 myDocumentType[theId] = theKind;
459 //******************************************************
460 QString NewGeom_Module::documentKind(const QString& theId) const
462 if (myDocumentType.contains(theId))
463 return myDocumentType[theId];
468 //******************************************************
469 void NewGeom_Module::selectionChanged()
471 LightApp_Module::selectionChanged();
472 myWorkshop->salomeViewerSelectionChanged();
475 //******************************************************
476 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
478 myWorkshop->contextMenuMgr()->addViewerItems(theMenu);
479 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
483 //******************************************************
484 void NewGeom_Module::createPreferences()
486 LightApp_Preferences* pref = preferences();
489 ModuleBase_Preferences::updateConfigByResources();
490 QString aModName = moduleName();
492 QtxPreferenceItem* item = pref->findItem(aModName, true );
493 if ( item && (!item->isEmpty() )) {
494 item->parentItem()->removeItem(item);
498 int catId = pref->addPreference(aModName, -1 );
501 NewGeom_PrefMgr aMgr(pref, aModName);
502 ModuleBase_Preferences::createEditContent(&aMgr, catId);
506 //******************************************************
507 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
509 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
510 QString aVal = aResMgr->stringValue(theSection, theParam);
511 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
512 std::string aValue = aVal.toStdString();
513 if (aValue.empty()) {
514 aValue = aProp->defaultValue();
515 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
517 LightApp_Preferences* pref = preferences();
521 aProp->setValue(aValue);
525 void NewGeom_Module::inspectSalomeModules()
527 QStringList aModuleNames;
528 getApp()->modules(aModuleNames, false);
529 foreach(QString eachModule, aModuleNames) {
530 Config_ModuleReader::addDependencyModule(eachModule.toStdString());