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 <AIS_ListOfInteractive.hxx>
39 #include <AIS_ListIteratorOfListOfInteractive.hxx>
41 #include <QDockWidget>
48 NewGeom_EXPORT CAM_Module* createModule()
50 return new NewGeom_Module();
53 NewGeom_EXPORT char* getModuleVersion()
60 * Class for preferences management
62 class NewGeom_PrefMgr: public ModuleBase_IPrefMgr
66 /// \param theMgr preferences manager of SALOME
67 /// \param theModName name of the module
68 NewGeom_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
70 virtual int addPreference(const QString& theLbl, int pId,
71 SUIT_PreferenceMgr::PrefItemType theType,
72 const QString& theSection, const QString& theName )
74 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
77 virtual void setItemProperty(const QString& thePropName,
78 const QVariant& theValue,
81 myMgr->setItemProperty(thePropName, theValue, theId);
85 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
88 LightApp_Preferences* myMgr;
95 //******************************************************
96 NewGeom_Module::NewGeom_Module()
97 : LightApp_Module("NewGeom"),
98 mySelector(0), myIsOpened(0), myPopupMgr(0)
100 myWorkshop = new XGUI_Workshop(this);
101 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
102 this, SLOT(onUpdateCommandStatus()));
104 myProxyViewer = new NewGeom_SalomeViewer(this);
106 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
107 ModuleBase_Preferences::loadCustomProps();
110 //******************************************************
111 NewGeom_Module::~NewGeom_Module()
115 //******************************************************
116 void NewGeom_Module::initialize(CAM_Application* theApp)
118 LightApp_Module::initialize(theApp);
119 inspectSalomeModules();
121 myWorkshop->startApplication();
122 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
124 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
127 //******************************************************
128 void NewGeom_Module::windows(QMap<int, int>& theWndMap) const
130 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
133 //******************************************************
134 void NewGeom_Module::viewManagers(QStringList& theList) const
136 theList.append(OCCViewer_Viewer::Type());
139 //******************************************************
140 bool NewGeom_Module::activateModule(SUIT_Study* theStudy)
142 bool isDone = LightApp_Module::activateModule(theStudy);
147 QObject* aObj = myWorkshop->objectBrowser()->parent();
148 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
150 QAction* aViewAct = aObjDoc->toggleViewAction();
151 aViewAct->setEnabled(true);
152 myWorkshop->objectBrowser()->setVisible(true);
153 aObjDoc->setVisible(true);
154 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
158 ViewManagerList OCCViewManagers;
159 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
160 if (OCCViewManagers.size() > 0) {
161 mySelector = createSelector(OCCViewManagers.first());
164 // it should be pefromed after the selector creation in order to have AISContext
165 myWorkshop->activateModule();
166 //action(myEraseAll)->setEnabled(false);
169 myWorkshop->objectBrowser()->rebuildDataTree();
170 myWorkshop->updateCommandStatus();
172 // the display all results is not necessary anymore, it was commented in XGUI_Workshop,
173 // so it should be commented here
174 //QTimer::singleShot(1000, myWorkshop, SLOT(displayAllResults()));
177 myWorkshop->updateCommandStatus();
179 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
180 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
181 myIsEditEnabled = getApp()->isEditEnabled();
182 getApp()->setEditEnabled(false);
184 // this following row is caused by #187 bug.
185 // SALOME saves the dock widget positions before deactivateModule() and
186 // load it after the module activation. So, if the panel is visible before
187 // deactivate, it becomes visible after activate.
188 // In order to avoid the visible property panel, the widget position save is
189 // switch off in this module
190 aResMgr->setValue("Study", "store_positions", false);
192 // Synchronize displayed objects
193 if (mySelector && mySelector->viewer()) {
194 Handle(AIS_InteractiveContext) aContext = mySelector->viewer()->getAISContext();
195 XGUI_Displayer* aDisp = myWorkshop->displayer();
196 QObjectPtrList aObjList = aDisp->displayedObjects();
198 AIS_ListOfInteractive aList;
199 aContext->DisplayedObjects(aList);
200 AIS_ListIteratorOfListOfInteractive aLIt;
201 Handle(AIS_InteractiveObject) anAISIO;
202 foreach (ObjectPtr aObj, aObjList) {
203 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
204 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
206 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
207 anAISIO = aLIt.Value();
208 if (anAISIO.Access() == aAIS.Access()) {
214 aObj->setDisplayed(false);
215 //aDisp->erase(aObj, false);
218 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
220 myProxyViewer->activateViewer(true);
224 //******************************************************
225 bool NewGeom_Module::deactivateModule(SUIT_Study* theStudy)
227 myProxyViewer->activateViewer(false);
231 myWorkshop->deactivateModule();
233 QObject* aObj = myWorkshop->objectBrowser()->parent();
234 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
236 aObjDoc->setVisible(false);
237 myWorkshop->objectBrowser()->setVisible(false);
238 QAction* aViewAct = aObjDoc->toggleViewAction();
239 aViewAct->setEnabled(false);
242 // the active operation should be stopped for the next activation.
243 // There should not be active operation and visualized preview.
244 // Abort operation should be performed before the selection's remove
245 // because the displayed objects should be removed from the viewer, but
246 // the AIS context is obtained from the selector.
247 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
248 while (anOperation) {
249 anOperation->abort();
250 anOperation = myWorkshop->operationMgr()->currentOperation();
252 // Delete selector because it has to be redefined on next activation
254 myProxyViewer->setSelector(0);
259 //myWorkshop->contextMenuMgr()->disconnectViewer();
261 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
262 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
263 getApp()->setEditEnabled(myIsEditEnabled);
265 return LightApp_Module::deactivateModule(theStudy);
268 //******************************************************
269 void NewGeom_Module::onViewManagerAdded(SUIT_ViewManager* theMgr)
272 mySelector = createSelector(theMgr);
276 //******************************************************
277 void NewGeom_Module::onViewManagerRemoved(SUIT_ViewManager* theMgr)
280 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
281 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
282 if (mySelector->viewer() == aViewer) {
283 XGUI_Displayer* aDisp = myWorkshop->displayer();
284 QObjectPtrList aObjects = aDisp->displayedObjects();
285 foreach(ObjectPtr aObj, aObjects)
286 aObj->setDisplayed(false);
287 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
288 myProxyViewer->setSelector(0);
296 //******************************************************
297 QtxPopupMgr* NewGeom_Module::popupMgr()
300 myPopupMgr = new QtxPopupMgr( 0, this );
304 //******************************************************
305 void NewGeom_Module::onDefaultPreferences()
307 ModuleBase_Preferences::resetConfig();
308 ModuleBase_Preferences::updateResourcesByConfig();
310 LightApp_Preferences* pref = preferences();
315 //******************************************************
316 void NewGeom_Module::onUpdateCommandStatus()
318 getApp()->updateActions();
321 //******************************************************
322 NewGeom_OCCSelector* NewGeom_Module::createSelector(SUIT_ViewManager* theMgr)
324 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
325 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
326 NewGeom_OCCSelector* aSelector = new NewGeom_OCCSelector(aViewer, getApp()->selectionMgr());
327 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
328 QList<SUIT_Selector*> aList;
329 aMgr->selectors(aList);
330 foreach(SUIT_Selector* aSel, aList)
332 aSel->setEnabled(aSel == aSelector);
334 myProxyViewer->setSelector(aSelector);
340 //******************************************************
341 CAM_DataModel* NewGeom_Module::createDataModel()
343 NewGeom_DataModel* aDataModel = new NewGeom_DataModel(this);
345 // Calling addComponent() for persistent functionality work in the SalomeApp_Study
346 LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>(application()->activeStudy() );
347 aStudy->addComponent(aDataModel);
352 QAction* NewGeom_Module::addFeature(const QString& theWBName, const ActionInfo& theInfo)
354 return addFeature(theWBName,
363 //******************************************************
364 QAction* NewGeom_Module::addFeature(const QString& theWBName, const QString& theId,
365 const QString& theTitle, const QString& theTip,
366 const QIcon& theIcon, const QKeySequence& theKeys,
369 int aMenu = createMenu(theWBName, -1, -1, 50);
370 int aTool = createTool(theWBName);
372 int aId = myActionsList.size();
373 myActionsList.append(theId);
374 SUIT_Desktop* aDesk = application()->desktop();
376 for (unsigned int i = 0; i < theKeys.count(); i++)
378 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
380 aAction->setData(theId);
381 int aItemId = createMenu(aId, aMenu, -1, 10);
382 int aToolId = createTool(aId, aTool);
388 QAction* NewGeom_Module::addNestedFeature(const QString& theWBName,
389 const ActionInfo& theInfo,
390 const QList<QAction*>& theNestedActions)
392 int aMenu = createMenu(theWBName, -1, -1, 50);
393 int aTool = createTool(theWBName);
395 int aId = myActionsList.size();
396 myActionsList.append(theInfo.id);
397 SUIT_Desktop* aDesk = application()->desktop();
398 NewGeom_NestedButton* anAction = new NewGeom_NestedButton(aDesk, theNestedActions);
399 anAction->setData(theInfo.id);
400 anAction->setCheckable(theInfo.checkable);
401 anAction->setChecked(theInfo.checked);
402 anAction->setEnabled(theInfo.enabled);
403 anAction->setVisible(theInfo.visible);
404 anAction->setIcon(theInfo.icon);
405 anAction->setText(theInfo.text);
406 anAction->setToolTip(theInfo.toolTip);
407 anAction->setShortcut(theInfo.shortcut);
408 anAction->setFont(theInfo.font);
410 //int aItemId = createMenu(aId, aMenu, -1, 10);
411 int aToolId = createTool(anAction, aTool, aId);
417 //******************************************************
418 QAction* NewGeom_Module::addDesktopCommand(const QString& theId, const QString& theTitle,
419 const QString& theTip, const QIcon& theIcon,
420 const QKeySequence& theKeys, bool isCheckable,
421 const char* theMenuSourceText, const int theMenuPosition)
423 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
425 int aId = myActionsList.size();
426 myActionsList.append(theId);
427 SUIT_Desktop* aDesk = application()->desktop();
429 for (unsigned int i = 0; i < theKeys.count(); i++)
431 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
433 aAction->setData(theId);
434 createMenu(aId, aMenu, theMenuPosition);
438 //******************************************************
439 void NewGeom_Module::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
441 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
442 createMenu(separator(), aMenu, -1, theMenuPosition);
445 //******************************************************
446 QList<QAction*> NewGeom_Module::commandList() const
448 QList<QAction*> aActions;
449 for (int i = 0; i < myActionsList.size(); i++) {
450 QAction* aCmd = action(i);
451 if (aCmd && myActionsList.contains(aCmd->data().toString()))
452 aActions.append(aCmd);
457 //******************************************************
458 QStringList NewGeom_Module::commandIdList() const
460 return myActionsList;
463 //******************************************************
464 QMainWindow* NewGeom_Module::desktop() const
466 return application()->desktop();
469 //******************************************************
470 QString NewGeom_Module::commandId(const QAction* theCmd) const
472 int aId = actionId(theCmd);
473 if (aId < myActionsList.size())
474 return myActionsList[aId];
478 //******************************************************
479 QAction* NewGeom_Module::command(const QString& theId) const
481 int aId = myActionsList.indexOf(theId);
482 if ((aId != -1) && (aId < myActionsList.size())) {
488 //******************************************************
489 void NewGeom_Module::setNestedActions(const QString& theId, const QStringList& theActions)
491 myNestedActions[theId] = theActions;
494 //******************************************************
495 QStringList NewGeom_Module::nestedActions(const QString& theId) const
497 if (myNestedActions.contains(theId))
498 return myNestedActions[theId];
499 return QStringList();
502 //******************************************************
503 void NewGeom_Module::setDocumentKind(const QString& theId, const QString& theKind)
505 myDocumentType[theId] = theKind;
508 //******************************************************
509 QString NewGeom_Module::documentKind(const QString& theId) const
511 if (myDocumentType.contains(theId))
512 return myDocumentType[theId];
517 //******************************************************
518 void NewGeom_Module::selectionChanged()
520 LightApp_Module::selectionChanged();
521 myWorkshop->salomeViewerSelectionChanged();
524 //******************************************************
525 void NewGeom_Module::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
527 QMenu* aMenu = myWorkshop->contextMenuMgr()->viewerMenu();
528 theMenu->addActions(aMenu->actions());
529 //myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
530 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
531 delete aMenu; // Delete menu because it is not controlled by menu manager
535 //******************************************************
536 void NewGeom_Module::createPreferences()
538 LightApp_Preferences* pref = preferences();
541 ModuleBase_Preferences::updateConfigByResources();
542 QString aModName = moduleName();
544 QtxPreferenceItem* item = pref->findItem(aModName, true );
545 if ( item && (!item->isEmpty() )) {
546 item->parentItem()->removeItem(item);
550 int catId = pref->addPreference(aModName, -1 );
553 NewGeom_PrefMgr aMgr(pref, aModName);
554 ModuleBase_Preferences::createEditContent(&aMgr, catId);
558 //******************************************************
559 void NewGeom_Module::preferencesChanged(const QString& theSection, const QString& theParam)
561 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
562 QString aVal = aResMgr->stringValue(theSection, theParam);
563 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
564 std::string aValue = aVal.toStdString();
565 if (aValue.empty()) {
566 aValue = aProp->defaultValue();
567 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
569 LightApp_Preferences* pref = preferences();
573 aProp->setValue(aValue);
577 void NewGeom_Module::inspectSalomeModules()
579 QStringList aModuleNames;
580 getApp()->modules(aModuleNames, false);
581 foreach(QString eachModule, aModuleNames) {
582 Config_ModuleReader::addDependencyModule(eachModule.toStdString());