1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
5 #include "SHAPERGUI_DataModel.h"
6 #include "SHAPERGUI_OCCSelector.h"
7 #include <SHAPERGUI_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>
19 #include <ModuleBase_IModule.h>
21 #include <LightApp_Application.h>
22 #include <LightApp_SelectionMgr.h>
23 #include <LightApp_OCCSelector.h>
24 #include <LightApp_Study.h>
25 #include <OCCViewer_ViewModel.h>
27 #include <SUIT_Selector.h>
28 #include <SUIT_Desktop.h>
29 #include <SUIT_ViewManager.h>
30 #include <SUIT_ResourceMgr.h>
32 #include <QtxPopupMgr.h>
33 #include <QtxActionMenuMgr.h>
34 #include <QtxActionToolMgr.h>
35 #include <QtxResourceMgr.h>
37 #include <Config_PropManager.h>
38 #include <Config_ModuleReader.h>
40 #include <AIS_ListOfInteractive.hxx>
41 #include <AIS_ListIteratorOfListOfInteractive.hxx>
43 #include <QDockWidget>
50 SHAPERGUI_EXPORT CAM_Module* createModule()
52 return new SHAPERGUI();
55 SHAPERGUI_EXPORT char* getModuleVersion()
62 * Class for preferences management
64 class SHAPERGUI_PrefMgr: public ModuleBase_IPrefMgr
68 /// \param theMgr preferences manager of SALOME
69 /// \param theModName name of the module
70 SHAPERGUI_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
72 virtual int addPreference(const QString& theLbl, int pId,
73 SUIT_PreferenceMgr::PrefItemType theType,
74 const QString& theSection, const QString& theName )
76 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
79 virtual void setItemProperty(const QString& thePropName,
80 const QVariant& theValue,
83 myMgr->setItemProperty(thePropName, theValue, theId);
87 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
90 LightApp_Preferences* myMgr;
97 //******************************************************
98 SHAPERGUI::SHAPERGUI()
99 : LightApp_Module("SHAPER"),
100 mySelector(0), myIsOpened(0), myPopupMgr(0)
102 myWorkshop = new XGUI_Workshop(this);
103 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
104 this, SLOT(onUpdateCommandStatus()));
106 myProxyViewer = new SHAPERGUI_SalomeViewer(this);
108 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
109 ModuleBase_Preferences::loadCustomProps();
112 //******************************************************
113 SHAPERGUI::~SHAPERGUI()
117 //******************************************************
118 void SHAPERGUI::initialize(CAM_Application* theApp)
120 LightApp_Module::initialize(theApp);
121 inspectSalomeModules();
123 myWorkshop->startApplication();
124 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
127 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
131 //******************************************************
132 void SHAPERGUI::windows(QMap<int, int>& theWndMap) const
134 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
137 //******************************************************
138 void SHAPERGUI::viewManagers(QStringList& theList) const
140 theList.append(OCCViewer_Viewer::Type());
143 //******************************************************
144 void SHAPERGUI::connectToStudy(CAM_Study* theStudy)
146 // if there are created viewer managers, we should try to create viewer
147 // selector and initialize viewer with it. It sets interactive contect to the
148 // proxy viewer. If study is opened, CAM application calls this method before the open() of data model
149 // the SHAPER data model is specific and during open(load) redisplay signals are flushed, so
150 // we need to connect to the viewer before it. Here, it seems the most appropriate place for this
151 // according to SALOME architecture.
153 ViewManagerList OCCViewManagers;
154 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
155 if (OCCViewManagers.size() > 0) {
156 mySelector = createSelector(OCCViewManagers.first());
159 LightApp_Module::connectToStudy(theStudy);
162 //******************************************************
163 bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
165 bool isDone = LightApp_Module::activateModule(theStudy);
166 SHAPERGUI_DataModel* aDataModel = dynamic_cast<SHAPERGUI_DataModel*>(dataModel());
167 aDataModel->initRootObject();
173 QObject* aObj = myWorkshop->objectBrowser()->parent();
174 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
176 QAction* aViewAct = aObjDoc->toggleViewAction();
177 aViewAct->setEnabled(true);
178 myWorkshop->objectBrowser()->setVisible(true);
179 aObjDoc->setVisible(true);
180 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
184 ViewManagerList OCCViewManagers;
185 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
186 if (OCCViewManagers.size() > 0) {
187 mySelector = createSelector(OCCViewManagers.first());
190 // it should be pefromed after the selector creation in order to have AISContext
191 myWorkshop->activateModule();
192 //action(myEraseAll)->setEnabled(false);
195 myWorkshop->objectBrowser()->rebuildDataTree();
196 myWorkshop->updateCommandStatus();
200 myWorkshop->updateCommandStatus();
202 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
203 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
204 myIsEditEnabled = getApp()->isEditEnabled();
205 getApp()->setEditEnabled(false);
207 // this following row is caused by #187 bug.
208 // SALOME saves the dock widget positions before deactivateModule() and
209 // load it after the module activation. So, if the panel is visible before
210 // deactivate, it becomes visible after activate.
211 // In order to avoid the visible property panel, the widget position save is
212 // switch off in this module
213 aResMgr->setValue("Study", "store_positions", false);
215 // Synchronize displayed objects
216 Handle(AIS_InteractiveContext) aContext;
217 if (mySelector && mySelector->viewer())
218 aContext = mySelector->viewer()->getAISContext();
220 if (!aContext.IsNull()) {
221 XGUI_Displayer* aDisp = myWorkshop->displayer();
222 QObjectPtrList aObjList = aDisp->displayedObjects();
224 AIS_ListOfInteractive aList;
225 aContext->DisplayedObjects(aList);
226 AIS_ListIteratorOfListOfInteractive aLIt;
227 Handle(AIS_InteractiveObject) anAISIO;
228 foreach (ObjectPtr aObj, aObjList) {
229 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
230 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
232 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
233 anAISIO = aLIt.Value();
234 if (anAISIO.Access() == aAIS.Access()) {
240 aObj->setDisplayed(false);
241 //aDisp->erase(aObj, false);
244 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
246 myProxyViewer->activateViewer(true);
250 //******************************************************
251 bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
253 myProxyViewer->activateViewer(false);
257 myWorkshop->deactivateModule();
259 QObject* aObj = myWorkshop->objectBrowser()->parent();
260 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
262 aObjDoc->setVisible(false);
263 myWorkshop->objectBrowser()->setVisible(false);
264 QAction* aViewAct = aObjDoc->toggleViewAction();
265 aViewAct->setEnabled(false);
268 // the active operation should be stopped for the next activation.
269 // There should not be active operation and visualized preview.
270 // Abort operation should be performed before the selection's remove
271 // because the displayed objects should be removed from the viewer, but
272 // the AIS context is obtained from the selector.
273 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
274 while (anOperation) {
275 anOperation->abort();
276 anOperation = myWorkshop->operationMgr()->currentOperation();
278 // Delete selector because it has to be redefined on next activation
280 myProxyViewer->setSelector(0);
285 //myWorkshop->contextMenuMgr()->disconnectViewer();
287 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
288 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
289 getApp()->setEditEnabled(myIsEditEnabled);
291 return LightApp_Module::deactivateModule(theStudy);
294 //******************************************************
295 void SHAPERGUI::onViewManagerAdded(SUIT_ViewManager* theMgr)
298 mySelector = createSelector(theMgr);
299 myWorkshop->module()->activateSelectionFilters();
300 myWorkshop->synchronizeViewer();
304 //******************************************************
305 void SHAPERGUI::onViewManagerRemoved(SUIT_ViewManager* theMgr)
308 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
309 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
310 if (mySelector->viewer() == aViewer) {
311 XGUI_Displayer* aDisp = myWorkshop->displayer();
312 QObjectPtrList aObjects = aDisp->displayedObjects();
313 foreach(ObjectPtr aObj, aObjects)
314 aObj->setDisplayed(false);
315 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
316 myProxyViewer->setSelector(0);
320 myWorkshop->module()->clearViewer();
326 //******************************************************
327 QtxPopupMgr* SHAPERGUI::popupMgr()
330 myPopupMgr = new QtxPopupMgr( 0, this );
334 //******************************************************
335 void SHAPERGUI::onDefaultPreferences()
337 // reset main resources
338 ModuleBase_Preferences::resetResourcePreferences(preferences());
339 // reset plugin's resources
340 ModuleBase_Preferences::resetConfigPropPreferences(preferences());
342 myWorkshop->displayer()->redisplayObjects();
345 //******************************************************
346 void SHAPERGUI::onUpdateCommandStatus()
348 getApp()->updateActions();
351 //******************************************************
352 SHAPERGUI_OCCSelector* SHAPERGUI::createSelector(SUIT_ViewManager* theMgr)
354 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
355 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
356 SHAPERGUI_OCCSelector* aSelector = new SHAPERGUI_OCCSelector(aViewer, getApp()->selectionMgr());
357 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
358 QList<SUIT_Selector*> aList;
359 aMgr->selectors(aList);
360 foreach(SUIT_Selector* aSel, aList)
362 aSel->setEnabled(aSel == aSelector);
364 myProxyViewer->setSelector(aSelector);
370 //******************************************************
371 CAM_DataModel* SHAPERGUI::createDataModel()
373 return new SHAPERGUI_DataModel(this);
376 QAction* SHAPERGUI::addFeature(const QString& theWBName, const ActionInfo& theInfo)
378 return addFeature(theWBName,
387 //******************************************************
388 QAction* SHAPERGUI::addFeature(const QString& theWBName, const QString& theId,
389 const QString& theTitle, const QString& theTip,
390 const QIcon& theIcon, const QKeySequence& theKeys,
393 static QString aLastTool = "";
395 if (aLastTool.isEmpty())
396 aLastTool = theWBName;
397 else if (theWBName != aLastTool) {
398 aLastTool = theWBName;
400 desktop()->addToolBarBreak();
406 int aMenu = createMenu(theWBName, -1, -1, 50);
407 int aTool = createTool(theWBName, theWBName);
409 int aId = myActionsList.size();
410 myActionsList.append(theId);
411 SUIT_Desktop* aDesk = application()->desktop();
413 for (unsigned int i = 0; i < theKeys.count(); i++)
415 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
417 aAction->setData(theId);
418 int aItemId = createMenu(aId, aMenu, -1, 10);
419 int aToolId = createTool(aId, aTool);
424 bool SHAPERGUI::isFeatureOfNested(const QAction* theAction)
426 return dynamic_cast<const SHAPERGUI_NestedButton*>(theAction);
429 QAction* SHAPERGUI::addFeatureOfNested(const QString& theWBName,
430 const ActionInfo& theInfo,
431 const QList<QAction*>& theNestedActions)
433 int aMenu = createMenu(theWBName, -1, -1, 50);
434 int aTool = createTool(theWBName, theWBName);
436 int aId = myActionsList.size();
437 myActionsList.append(theInfo.id);
438 SUIT_Desktop* aDesk = application()->desktop();
439 SHAPERGUI_NestedButton* anAction = new SHAPERGUI_NestedButton(aDesk, theNestedActions);
440 anAction->setData(theInfo.id);
441 anAction->setCheckable(theInfo.checkable);
442 anAction->setChecked(theInfo.checked);
443 anAction->setEnabled(theInfo.enabled);
444 anAction->setVisible(theInfo.visible);
445 anAction->setIcon(theInfo.icon);
446 anAction->setText(theInfo.text);
447 anAction->setToolTip(theInfo.toolTip);
448 anAction->setShortcut(theInfo.shortcut);
449 anAction->setFont(theInfo.font);
451 //int aItemId = createMenu(aId, aMenu, -1, 10);
452 int aToolId = createTool(anAction, aTool, aId);
458 //******************************************************
459 QAction* SHAPERGUI::addDesktopCommand(const QString& theId, const QString& theTitle,
460 const QString& theTip, const QIcon& theIcon,
461 const QKeySequence& theKeys, bool isCheckable,
462 const char* theMenuSourceText, const int theMenuPosition)
464 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
466 int aId = myActionsList.size();
467 myActionsList.append(theId);
468 SUIT_Desktop* aDesk = application()->desktop();
470 for (unsigned int i = 0; i < theKeys.count(); i++)
472 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
474 aAction->setData(theId);
475 createMenu(aId, aMenu, theMenuPosition);
479 //******************************************************
480 void SHAPERGUI::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
482 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
483 createMenu(separator(), aMenu, -1, theMenuPosition);
486 bool SHAPERGUI::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
491 SUIT_Desktop* aDesktop = application()->desktop();
495 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
499 aToolMgr->append( theAction, theToolBarTitle );
503 //******************************************************
504 QList<QAction*> SHAPERGUI::commandList() const
506 QList<QAction*> aActions;
507 for (int i = 0; i < myActionsList.size(); i++) {
508 QAction* aCmd = action(i);
509 if (aCmd && myActionsList.contains(aCmd->data().toString()))
510 aActions.append(aCmd);
515 //******************************************************
516 QStringList SHAPERGUI::commandIdList() const
518 return myActionsList;
521 //******************************************************
522 QMainWindow* SHAPERGUI::desktop() const
524 return application()->desktop();
527 //******************************************************
528 QString SHAPERGUI::commandId(const QAction* theCmd) const
530 int aId = actionId(theCmd);
531 if (aId < myActionsList.size())
532 return myActionsList[aId];
536 //******************************************************
537 QAction* SHAPERGUI::command(const QString& theId) const
539 int aId = myActionsList.indexOf(theId);
540 if ((aId != -1) && (aId < myActionsList.size())) {
546 void SHAPERGUI::setFeatureInfo(const QString& theFeatureId,
547 const std::shared_ptr<Config_FeatureMessage>& theMessage)
549 myFeaturesInfo.insert(theFeatureId, theMessage);
552 std::shared_ptr<Config_FeatureMessage> SHAPERGUI::featureInfo(const QString& theFeatureId)
554 std::shared_ptr<Config_FeatureMessage> aMessage;
555 if (myFeaturesInfo.contains(theFeatureId))
556 aMessage = myFeaturesInfo[theFeatureId];
560 //******************************************************
561 void SHAPERGUI::selectionChanged()
563 LightApp_Module::selectionChanged();
564 myWorkshop->salomeViewerSelectionChanged();
567 //******************************************************
568 void SHAPERGUI::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
570 myWorkshop->contextMenuMgr()->updateViewerMenu();
571 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
572 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
576 //******************************************************
577 void SHAPERGUI::createPreferences()
579 LightApp_Preferences* pref = preferences();
582 ModuleBase_Preferences::updateConfigByResources();
583 QString aModName = moduleName();
585 QtxPreferenceItem* item = pref->findItem(aModName, true );
586 if ( item && (!item->isEmpty() )) {
587 item->parentItem()->removeItem(item);
591 int catId = pref->addPreference(aModName, -1 );
594 SHAPERGUI_PrefMgr aMgr(pref, aModName);
595 ModuleBase_Preferences::createEditContent(&aMgr, catId);
597 int viewTab = pref->addItem(tr("Viewer"), catId);
598 // Create other parameters group in viewer tab
599 int otherGroup = pref->addItem(tr("Other parameters"), viewTab);
600 int selId = pref->addItem(tr("Default selection type"), otherGroup,
601 SUIT_PreferenceMgr::Selector,
602 ModuleBase_Preferences::VIEWER_SECTION, "selection");
603 QStringList aSelectionList;
604 aSelectionList.append( tr("Vertices") );
605 aSelectionList.append( tr("Edges") );
606 aSelectionList.append( tr("Faces") );
607 aSelectionList.append( tr("Results") );
609 QList<QVariant> anIndexesList;
610 anIndexesList.append(TopAbs_VERTEX);
611 anIndexesList.append(TopAbs_EDGE);
612 anIndexesList.append(TopAbs_FACE);
613 anIndexesList.append(-1);
615 pref->setItemProperty( "strings", aSelectionList, selId );
616 pref->setItemProperty( "indexes", anIndexesList, selId );
621 //******************************************************
622 void SHAPERGUI::preferencesChanged(const QString& theSection, const QString& theParam)
624 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
625 QString aVal = aResMgr->stringValue(theSection, theParam);
626 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
627 std::string aValue = aVal.toStdString();
628 if (aValue.empty()) {
629 aValue = aProp->defaultValue();
630 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
632 LightApp_Preferences* pref = preferences();
636 aProp->setValue(aValue);
638 myWorkshop->displayer()->redisplayObjects();
641 void SHAPERGUI::inspectSalomeModules()
643 QStringList aModuleNames;
644 getApp()->modules(aModuleNames, false);
645 foreach(QString eachModule, aModuleNames) {
646 Config_ModuleReader::addDependencyModule(eachModule.toStdString());
650 bool SHAPERGUI::abortAllOperations()
652 return workshop()->operationMgr()->abortAllOperations();