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>
15 #include <XGUI_MenuMgr.h>
17 #include <ModuleBase_Operation.h>
18 #include <ModuleBase_Preferences.h>
19 #include <ModuleBase_ActionInfo.h>
20 #include <ModuleBase_IModule.h>
22 #include <LightApp_Application.h>
23 #include <LightApp_SelectionMgr.h>
24 #include <LightApp_OCCSelector.h>
25 #include <LightApp_Study.h>
26 #include <OCCViewer_ViewModel.h>
28 #include <SUIT_Selector.h>
29 #include <SUIT_Desktop.h>
30 #include <SUIT_ViewManager.h>
31 #include <SUIT_ResourceMgr.h>
32 #include <SUIT_DataBrowser.h>
34 #include <QtxPopupMgr.h>
35 #include <QtxActionMenuMgr.h>
36 #include <QtxActionToolMgr.h>
37 #include <QtxResourceMgr.h>
39 #include <Config_PropManager.h>
40 #include <Config_ModuleReader.h>
42 #include <AIS_ListOfInteractive.hxx>
43 #include <AIS_ListIteratorOfListOfInteractive.hxx>
45 #include <QDockWidget>
52 SHAPERGUI_EXPORT CAM_Module* createModule()
54 return new SHAPERGUI();
57 SHAPERGUI_EXPORT char* getModuleVersion()
64 * Class for preferences management
66 class SHAPERGUI_PrefMgr: public ModuleBase_IPrefMgr
70 /// \param theMgr preferences manager of SALOME
71 /// \param theModName name of the module
72 SHAPERGUI_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):
73 myMgr(theMgr), myModName(theModName) {}
75 virtual int addPreference(const QString& theLbl, int pId,
76 SUIT_PreferenceMgr::PrefItemType theType,
77 const QString& theSection, const QString& theName )
79 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
82 virtual void setItemProperty(const QString& thePropName,
83 const QVariant& theValue,
86 myMgr->setItemProperty(thePropName, theValue, theId);
90 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
93 LightApp_Preferences* myMgr;
100 //******************************************************
101 SHAPERGUI::SHAPERGUI()
102 : LightApp_Module("SHAPER"),
103 mySelector(0), myIsOpened(0), myPopupMgr(0)
105 myWorkshop = new XGUI_Workshop(this);
106 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
107 this, SLOT(onUpdateCommandStatus()));
109 myProxyViewer = new SHAPERGUI_SalomeViewer(this);
111 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
112 ModuleBase_Preferences::loadCustomProps();
115 //******************************************************
116 SHAPERGUI::~SHAPERGUI()
120 //******************************************************
121 void SHAPERGUI::initialize(CAM_Application* theApp)
123 LightApp_Module::initialize(theApp);
124 inspectSalomeModules();
126 myWorkshop->startApplication();
127 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
130 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
134 //******************************************************
135 void SHAPERGUI::windows(QMap<int, int>& theWndMap) const
137 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
140 //******************************************************
141 void SHAPERGUI::viewManagers(QStringList& theList) const
143 theList.append(OCCViewer_Viewer::Type());
146 //******************************************************
147 // We can not create selector in this method because it can be called when
148 // SHAPER module is not active. Take into account that creation of our selector
149 // leads to switching OFF all other selectors
150 //void SHAPERGUI::connectToStudy(CAM_Study* theStudy)
152 // // if there are created viewer managers, we should try to create viewer
153 // // selector and initialize viewer with it. It sets interactive contect to the
154 // // proxy viewer. If study is opened, CAM application calls this method before the open()
156 // // the SHAPER data model is specific and during open(load) redisplay signals are flushed, so
157 // // we need to connect to the viewer before it. Here,
158 // // it seems the most appropriate place for this
159 // // according to SALOME architecture.
160 // if (!mySelector) {
161 // ViewManagerList OCCViewManagers;
162 // application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
163 // if (OCCViewManagers.size() > 0) {
164 // mySelector = createSelector(OCCViewManagers.first());
167 // LightApp_Module::connectToStudy(theStudy);
170 //******************************************************
171 bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
173 bool isDone = LightApp_Module::activateModule(theStudy);
174 SHAPERGUI_DataModel* aDataModel = dynamic_cast<SHAPERGUI_DataModel*>(dataModel());
175 aDataModel->initRootObject();
181 QObject* aObj = myWorkshop->objectBrowser()->parent();
182 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
184 QAction* aViewAct = aObjDoc->toggleViewAction();
185 aViewAct->setEnabled(true);
186 myWorkshop->objectBrowser()->setVisible(true);
187 aObjDoc->setVisible(true);
188 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
192 ViewManagerList OCCViewManagers;
193 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
194 if (OCCViewManagers.size() > 0) {
195 mySelector = createSelector(OCCViewManagers.first());
198 // it should be pefromed after the selector creation in order to have AISContext
199 myWorkshop->activateModule();
200 //action(myEraseAll)->setEnabled(false);
203 myWorkshop->objectBrowser()->rebuildDataTree();
204 myWorkshop->updateCommandStatus();
208 myWorkshop->updateCommandStatus();
210 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
211 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
212 myIsEditEnabled = getApp()->isEditEnabled();
213 getApp()->setEditEnabled(false);
215 // this following row is caused by #187 bug.
216 // SALOME saves the dock widget positions before deactivateModule() and
217 // load it after the module activation. So, if the panel is visible before
218 // deactivate, it becomes visible after activate.
219 // In order to avoid the visible property panel, the widget position save is
220 // switch off in this module
221 aResMgr->setValue("Study", "store_positions", false);
223 // Synchronize displayed objects
224 Handle(AIS_InteractiveContext) aContext;
225 if (mySelector && mySelector->viewer())
226 aContext = mySelector->viewer()->getAISContext();
228 if (!aContext.IsNull()) {
229 XGUI_Displayer* aDisp = myWorkshop->displayer();
230 QObjectPtrList aObjList = aDisp->displayedObjects();
232 AIS_ListOfInteractive aList;
233 aContext->DisplayedObjects(aList);
234 AIS_ListIteratorOfListOfInteractive aLIt;
235 Handle(AIS_InteractiveObject) anAISIO;
236 foreach (ObjectPtr aObj, aObjList) {
237 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
238 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
240 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
241 anAISIO = aLIt.Value();
242 if (anAISIO.get() == aAIS.get()) {
248 aObj->setDisplayed(false);
249 //aDisp->erase(aObj, false);
252 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
254 myProxyViewer->activateViewer(true);
256 // Postrrocessing for LoadScriptId to remove created(if it was created) SALOME Object Browser
257 connect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
258 this, SLOT(onScriptLoaded()));
262 //******************************************************
263 bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
265 myProxyViewer->activateViewer(false);
269 myWorkshop->deactivateModule();
271 QObject* aObj = myWorkshop->objectBrowser()->parent();
272 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
274 aObjDoc->setVisible(false);
275 myWorkshop->objectBrowser()->setVisible(false);
276 QAction* aViewAct = aObjDoc->toggleViewAction();
277 aViewAct->setEnabled(false);
280 // the active operation should be stopped for the next activation.
281 // There should not be active operation and visualized preview.
282 // Abort operation should be performed before the selection's remove
283 // because the displayed objects should be removed from the viewer, but
284 // the AIS context is obtained from the selector.
285 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
286 while (anOperation) {
287 anOperation->abort();
288 anOperation = myWorkshop->operationMgr()->currentOperation();
290 // Delete selector because it has to be redefined on next activation
292 myProxyViewer->setSelector(0);
297 //myWorkshop->contextMenuMgr()->disconnectViewer();
299 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
300 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
301 getApp()->setEditEnabled(myIsEditEnabled);
303 // Postrrocessing for LoadScriptId to remove created(if it was created) SALOME Object Browser
304 disconnect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
305 this, SLOT(onScriptLoaded()));
307 return LightApp_Module::deactivateModule(theStudy);
310 //******************************************************
311 void SHAPERGUI::onViewManagerAdded(SUIT_ViewManager* theMgr)
314 mySelector = createSelector(theMgr);
315 myWorkshop->module()->activateSelectionFilters();
316 myWorkshop->synchronizeViewer();
320 //******************************************************
321 void SHAPERGUI::onViewManagerRemoved(SUIT_ViewManager* theMgr)
324 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
325 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
326 if (mySelector->viewer() == aViewer) {
327 XGUI_Displayer* aDisp = myWorkshop->displayer();
328 QObjectPtrList aObjects = aDisp->displayedObjects();
329 foreach(ObjectPtr aObj, aObjects)
330 aObj->setDisplayed(false);
331 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
332 myProxyViewer->setSelector(0);
336 myWorkshop->module()->clearViewer();
342 //******************************************************
343 QtxPopupMgr* SHAPERGUI::popupMgr()
346 myPopupMgr = new QtxPopupMgr( 0, this );
350 //******************************************************
351 void SHAPERGUI::onDefaultPreferences()
353 // reset main resources
354 ModuleBase_Preferences::resetResourcePreferences(preferences());
355 // reset plugin's resources
356 ModuleBase_Preferences::resetConfigPropPreferences(preferences());
358 myWorkshop->displayer()->redisplayObjects();
361 //******************************************************
362 void SHAPERGUI::onScriptLoaded()
364 // this slot is called after processing of the LoadScriptId action of SalomeApp Application
365 // Each dumped script contains updateObjBrowser() that creates a new instance of Object
366 // Browser. When SHAPER module is active, this browser should not be used. It might be removed
367 // as hidden by means of updateWindows() of SalomeApp_Application or to remove
368 // it manually (because this method of application is protected)
369 SUIT_DataBrowser* aBrowser = getApp()->objectBrowser();
374 //******************************************************
375 void SHAPERGUI::onUpdateCommandStatus()
377 getApp()->updateActions();
380 //******************************************************
381 SHAPERGUI_OCCSelector* SHAPERGUI::createSelector(SUIT_ViewManager* theMgr)
383 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
384 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
385 SHAPERGUI_OCCSelector* aSelector = new SHAPERGUI_OCCSelector(aViewer,
386 getApp()->selectionMgr());
387 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
388 QList<SUIT_Selector*> aList;
389 aMgr->selectors(aList);
390 foreach(SUIT_Selector* aSel, aList)
392 aSel->setEnabled(aSel == aSelector);
394 myProxyViewer->setSelector(aSelector);
400 //******************************************************
401 CAM_DataModel* SHAPERGUI::createDataModel()
403 return new SHAPERGUI_DataModel(this);
406 QAction* SHAPERGUI::addFeature(const QString& theWBName, const ActionInfo& theInfo,
407 const bool isAddSeparator)
409 return addFeature(theWBName,
412 //Issue #650: in the SALOME mode the tooltip should be same as text
421 //******************************************************
422 QAction* SHAPERGUI::addFeature(const QString& theWBName, const QString& theId,
423 const QString& theTitle, const QString& theTip,
424 const QIcon& theIcon, const QKeySequence& theKeys,
425 bool isCheckable, const bool isAddSeparator,
426 const QString& theStatusTip)
428 static QString aLastTool = "";
430 if (aLastTool.isEmpty())
431 aLastTool = theWBName;
432 else if (theWBName != aLastTool) {
433 aLastTool = theWBName;
435 desktop()->addToolBarBreak();
441 int aId = myActionsList.size();
442 myActionsList.append(theId);
443 SUIT_Desktop* aDesk = application()->desktop();
445 for (int i = 0; i < theKeys.count(); i++)
447 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
449 aAction->setStatusTip(theStatusTip);
451 aAction->setData(theId);
453 int aWBMenu = createMenu(theWBName, -1, -1, 30/*10-Window, 1000 - Help*/);
454 int aItemId = createMenu(aId, aWBMenu);
456 createMenu(separator(), aWBMenu);
458 int aWBTool = createTool(theWBName, theWBName);
459 int aToolId = createTool(aId, aWBTool);
461 createTool(separator(), aWBTool);
466 bool SHAPERGUI::isFeatureOfNested(const QAction* theAction)
468 return dynamic_cast<const SHAPERGUI_NestedButton*>(theAction);
471 QAction* SHAPERGUI::addFeatureOfNested(const QString& theWBName,
472 const ActionInfo& theInfo,
473 const QList<QAction*>& theNestedActions)
475 myActionsList.append(theInfo.id);
476 SUIT_Desktop* aDesk = application()->desktop();
477 SHAPERGUI_NestedButton* anAction = new SHAPERGUI_NestedButton(aDesk, theNestedActions);
478 anAction->setData(theInfo.id);
479 anAction->setCheckable(theInfo.checkable);
480 anAction->setChecked(theInfo.checked);
481 anAction->setEnabled(theInfo.enabled);
482 anAction->setVisible(theInfo.visible);
483 anAction->setIcon(theInfo.icon);
484 anAction->setText(theInfo.text);
485 anAction->setToolTip(theInfo.toolTip);
486 anAction->setShortcut(theInfo.shortcut);
487 anAction->setFont(theInfo.font);
489 int aWBMenu = createMenu(theWBName, -1, -1, 30);
490 int aItemId = createMenu(anAction, aWBMenu);
491 createMenu(separator(), aWBMenu); /// nested action is always separated of others
493 int aWBTool = createTool(theWBName, theWBName);
494 int aToolId = createTool(anAction, aWBTool);
495 createTool(separator(), aWBTool); /// nested action is always separated of others
501 //******************************************************
502 QAction* SHAPERGUI::addDesktopCommand(const QString& theId, const QString& theTitle,
503 const QString& theTip, const QIcon& theIcon,
504 const QKeySequence& theKeys, bool isCheckable,
505 const char* theMenuSourceText, const int theMenuPosition)
507 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
509 int aId = myActionsList.size();
510 myActionsList.append(theId);
511 SUIT_Desktop* aDesk = application()->desktop();
513 for (int i = 0; i < theKeys.count(); i++)
515 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
517 aAction->setStatusTip(theTip);
518 aAction->setData(theId);
519 createMenu(aId, aMenu, theMenuPosition);
523 //******************************************************
524 void SHAPERGUI::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
526 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
527 createMenu(separator(), aMenu, -1, theMenuPosition);
530 bool SHAPERGUI::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
535 SUIT_Desktop* aDesktop = application()->desktop();
539 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
543 aToolMgr->append( theAction, theToolBarTitle );
547 //******************************************************
548 QList<QAction*> SHAPERGUI::commandList() const
550 QList<QAction*> aActions;
551 for (int i = 0; i < myActionsList.size(); i++) {
552 QAction* aCmd = action(i);
553 if (aCmd && myActionsList.contains(aCmd->data().toString()))
554 aActions.append(aCmd);
560 //******************************************************
561 QMainWindow* SHAPERGUI::desktop() const
563 return application()->desktop();
566 void SHAPERGUI::setFeatureInfo(const QString& theFeatureId,
567 const std::shared_ptr<Config_FeatureMessage>& theMessage)
569 myFeaturesInfo.insert(theFeatureId, theMessage);
572 std::shared_ptr<Config_FeatureMessage> SHAPERGUI::featureInfo(const QString& theFeatureId)
574 std::shared_ptr<Config_FeatureMessage> aMessage;
575 if (myFeaturesInfo.contains(theFeatureId))
576 aMessage = myFeaturesInfo[theFeatureId];
580 //******************************************************
581 void SHAPERGUI::selectionChanged()
583 LightApp_Module::selectionChanged();
584 myWorkshop->salomeViewerSelectionChanged();
587 //******************************************************
588 void SHAPERGUI::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
590 myWorkshop->contextMenuMgr()->updateViewerMenu();
591 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
592 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
596 //******************************************************
597 void SHAPERGUI::createPreferences()
599 LightApp_Preferences* pref = preferences();
602 ModuleBase_Preferences::updateConfigByResources();
603 QString aModName = moduleName();
605 QtxPreferenceItem* item = pref->findItem(aModName, true );
606 if ( item && (!item->isEmpty() )) {
607 item->parentItem()->removeItem(item);
611 int catId = pref->addPreference(aModName, -1 );
614 SHAPERGUI_PrefMgr aMgr(pref, aModName);
615 ModuleBase_Preferences::createEditContent(&aMgr, catId);
617 int viewTab = pref->addItem(tr("Viewer"), catId);
618 // Create other parameters group in viewer tab
619 int otherGroup = pref->addItem(tr("Default selection"), viewTab);
620 pref->setItemProperty("columns", 3, otherGroup);
621 pref->addItem(tr("Faces"), otherGroup,
622 SUIT_PreferenceMgr::Bool,
623 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
624 pref->addItem(tr("Edges"), otherGroup,
625 SUIT_PreferenceMgr::Bool,
626 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
627 pref->addItem(tr("Vertices"), otherGroup,
628 SUIT_PreferenceMgr::Bool,
629 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
631 int sensitivityGroup = pref->addItem(tr("Selection sensitivity"), viewTab);
632 pref->setItemProperty("columns", 2, sensitivityGroup);
633 pref->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::Double,
634 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
635 pref->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::Double,
636 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
640 //******************************************************
641 void SHAPERGUI::preferencesChanged(const QString& theSection, const QString& theParam)
643 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
644 QString aVal = aResMgr->stringValue(theSection, theParam);
645 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(),
646 theParam.toStdString());
647 std::string aValue = aVal.toStdString();
648 if (aValue.empty()) {
649 aValue = aProp->defaultValue();
650 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
652 LightApp_Preferences* pref = preferences();
656 aProp->setValue(aValue);
658 myWorkshop->displayer()->redisplayObjects();
661 void SHAPERGUI::putInfo(const QString& theInfo, const int theMSecs)
663 application()->putInfo(theInfo, theMSecs);
666 void SHAPERGUI::inspectSalomeModules()
668 QStringList aModuleNames;
669 getApp()->modules(aModuleNames, false);
670 foreach(QString eachModule, aModuleNames) {
671 Config_ModuleReader::addDependencyModule(eachModule.toStdString());
675 bool SHAPERGUI::abortAllOperations()
677 return workshop()->operationMgr()->abortAllOperations();
680 void SHAPERGUI::createFeatureActions()
682 myWorkshop->menuMgr()->createFeatureActions();