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 void SHAPERGUI::connectToStudy(CAM_Study* theStudy)
149 // if there are created viewer managers, we should try to create viewer
150 // selector and initialize viewer with it. It sets interactive contect to the
151 // proxy viewer. If study is opened, CAM application calls this method before the open()
153 // the SHAPER data model is specific and during open(load) redisplay signals are flushed, so
154 // we need to connect to the viewer before it. Here, it seems the most appropriate place for this
155 // according to SALOME architecture.
157 ViewManagerList OCCViewManagers;
158 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
159 if (OCCViewManagers.size() > 0) {
160 mySelector = createSelector(OCCViewManagers.first());
163 LightApp_Module::connectToStudy(theStudy);
166 //******************************************************
167 bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
169 bool isDone = LightApp_Module::activateModule(theStudy);
170 SHAPERGUI_DataModel* aDataModel = dynamic_cast<SHAPERGUI_DataModel*>(dataModel());
171 aDataModel->initRootObject();
177 QObject* aObj = myWorkshop->objectBrowser()->parent();
178 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
180 QAction* aViewAct = aObjDoc->toggleViewAction();
181 aViewAct->setEnabled(true);
182 myWorkshop->objectBrowser()->setVisible(true);
183 aObjDoc->setVisible(true);
184 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
188 ViewManagerList OCCViewManagers;
189 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
190 if (OCCViewManagers.size() > 0) {
191 mySelector = createSelector(OCCViewManagers.first());
194 // it should be pefromed after the selector creation in order to have AISContext
195 myWorkshop->activateModule();
196 //action(myEraseAll)->setEnabled(false);
199 myWorkshop->objectBrowser()->rebuildDataTree();
200 myWorkshop->updateCommandStatus();
204 myWorkshop->updateCommandStatus();
206 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
207 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
208 myIsEditEnabled = getApp()->isEditEnabled();
209 getApp()->setEditEnabled(false);
211 // this following row is caused by #187 bug.
212 // SALOME saves the dock widget positions before deactivateModule() and
213 // load it after the module activation. So, if the panel is visible before
214 // deactivate, it becomes visible after activate.
215 // In order to avoid the visible property panel, the widget position save is
216 // switch off in this module
217 aResMgr->setValue("Study", "store_positions", false);
219 // Synchronize displayed objects
220 Handle(AIS_InteractiveContext) aContext;
221 if (mySelector && mySelector->viewer())
222 aContext = mySelector->viewer()->getAISContext();
224 if (!aContext.IsNull()) {
225 XGUI_Displayer* aDisp = myWorkshop->displayer();
226 QObjectPtrList aObjList = aDisp->displayedObjects();
228 AIS_ListOfInteractive aList;
229 aContext->DisplayedObjects(aList);
230 AIS_ListIteratorOfListOfInteractive aLIt;
231 Handle(AIS_InteractiveObject) anAISIO;
232 foreach (ObjectPtr aObj, aObjList) {
233 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
234 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
236 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
237 anAISIO = aLIt.Value();
238 if (anAISIO.Access() == aAIS.Access()) {
244 aObj->setDisplayed(false);
245 //aDisp->erase(aObj, false);
248 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
250 myProxyViewer->activateViewer(true);
252 // Postrrocessing for LoadScriptId to remove created(if it was created) SALOME Object Browser
253 connect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
254 this, SLOT(onScriptLoaded()));
258 //******************************************************
259 bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
261 myProxyViewer->activateViewer(false);
265 myWorkshop->deactivateModule();
267 QObject* aObj = myWorkshop->objectBrowser()->parent();
268 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
270 aObjDoc->setVisible(false);
271 myWorkshop->objectBrowser()->setVisible(false);
272 QAction* aViewAct = aObjDoc->toggleViewAction();
273 aViewAct->setEnabled(false);
276 // the active operation should be stopped for the next activation.
277 // There should not be active operation and visualized preview.
278 // Abort operation should be performed before the selection's remove
279 // because the displayed objects should be removed from the viewer, but
280 // the AIS context is obtained from the selector.
281 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
282 while (anOperation) {
283 anOperation->abort();
284 anOperation = myWorkshop->operationMgr()->currentOperation();
286 // Delete selector because it has to be redefined on next activation
288 myProxyViewer->setSelector(0);
293 //myWorkshop->contextMenuMgr()->disconnectViewer();
295 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
296 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
297 getApp()->setEditEnabled(myIsEditEnabled);
299 // Postrrocessing for LoadScriptId to remove created(if it was created) SALOME Object Browser
300 disconnect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
301 this, SLOT(onScriptLoaded()));
303 return LightApp_Module::deactivateModule(theStudy);
306 //******************************************************
307 void SHAPERGUI::onViewManagerAdded(SUIT_ViewManager* theMgr)
310 mySelector = createSelector(theMgr);
311 myWorkshop->module()->activateSelectionFilters();
312 myWorkshop->synchronizeViewer();
316 //******************************************************
317 void SHAPERGUI::onViewManagerRemoved(SUIT_ViewManager* theMgr)
320 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
321 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
322 if (mySelector->viewer() == aViewer) {
323 XGUI_Displayer* aDisp = myWorkshop->displayer();
324 QObjectPtrList aObjects = aDisp->displayedObjects();
325 foreach(ObjectPtr aObj, aObjects)
326 aObj->setDisplayed(false);
327 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
328 myProxyViewer->setSelector(0);
332 myWorkshop->module()->clearViewer();
338 //******************************************************
339 QtxPopupMgr* SHAPERGUI::popupMgr()
342 myPopupMgr = new QtxPopupMgr( 0, this );
346 //******************************************************
347 void SHAPERGUI::onDefaultPreferences()
349 // reset main resources
350 ModuleBase_Preferences::resetResourcePreferences(preferences());
351 // reset plugin's resources
352 ModuleBase_Preferences::resetConfigPropPreferences(preferences());
354 myWorkshop->displayer()->redisplayObjects();
357 //******************************************************
358 void SHAPERGUI::onScriptLoaded()
360 // this slot is called after processing of the LoadScriptId action of SalomeApp Application
361 // Each dumped script contains updateObjBrowser() that creates a new instance of Object
362 // Browser. When SHAPER module is active, this browser should not be used. It might be removed
363 // as hidden by means of updateWindows() of SalomeApp_Application or to remove
364 // it manually (because this method of application is protected)
365 SUIT_DataBrowser* aBrowser = getApp()->objectBrowser();
370 //******************************************************
371 void SHAPERGUI::onUpdateCommandStatus()
373 getApp()->updateActions();
376 //******************************************************
377 SHAPERGUI_OCCSelector* SHAPERGUI::createSelector(SUIT_ViewManager* theMgr)
379 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
380 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
381 SHAPERGUI_OCCSelector* aSelector = new SHAPERGUI_OCCSelector(aViewer,
382 getApp()->selectionMgr());
383 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
384 QList<SUIT_Selector*> aList;
385 aMgr->selectors(aList);
386 foreach(SUIT_Selector* aSel, aList)
388 aSel->setEnabled(aSel == aSelector);
390 myProxyViewer->setSelector(aSelector);
396 //******************************************************
397 CAM_DataModel* SHAPERGUI::createDataModel()
399 return new SHAPERGUI_DataModel(this);
402 QAction* SHAPERGUI::addFeature(const QString& theWBName, const ActionInfo& theInfo,
403 const bool isAddSeparator)
405 return addFeature(theWBName,
408 //Issue #650: in the SALOME mode the tooltip should be same as text
417 //******************************************************
418 QAction* SHAPERGUI::addFeature(const QString& theWBName, const QString& theId,
419 const QString& theTitle, const QString& theTip,
420 const QIcon& theIcon, const QKeySequence& theKeys,
421 bool isCheckable, const bool isAddSeparator,
422 const QString& theStatusTip)
424 static QString aLastTool = "";
426 if (aLastTool.isEmpty())
427 aLastTool = theWBName;
428 else if (theWBName != aLastTool) {
429 aLastTool = theWBName;
431 desktop()->addToolBarBreak();
437 int aId = myActionsList.size();
438 myActionsList.append(theId);
439 SUIT_Desktop* aDesk = application()->desktop();
441 for (unsigned int i = 0; i < theKeys.count(); i++)
443 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
445 aAction->setStatusTip(theStatusTip);
447 aAction->setData(theId);
449 int aWBMenu = createMenu(theWBName, -1, -1, 30/*10-Window, 1000 - Help*/);
450 int aItemId = createMenu(aId, aWBMenu);
452 createMenu(separator(), aWBMenu);
454 int aWBTool = createTool(theWBName, theWBName);
455 int aToolId = createTool(aId, aWBTool);
457 createTool(separator(), aWBTool);
462 bool SHAPERGUI::isFeatureOfNested(const QAction* theAction)
464 return dynamic_cast<const SHAPERGUI_NestedButton*>(theAction);
467 QAction* SHAPERGUI::addFeatureOfNested(const QString& theWBName,
468 const ActionInfo& theInfo,
469 const QList<QAction*>& theNestedActions)
471 myActionsList.append(theInfo.id);
472 SUIT_Desktop* aDesk = application()->desktop();
473 SHAPERGUI_NestedButton* anAction = new SHAPERGUI_NestedButton(aDesk, theNestedActions);
474 anAction->setData(theInfo.id);
475 anAction->setCheckable(theInfo.checkable);
476 anAction->setChecked(theInfo.checked);
477 anAction->setEnabled(theInfo.enabled);
478 anAction->setVisible(theInfo.visible);
479 anAction->setIcon(theInfo.icon);
480 anAction->setText(theInfo.text);
481 anAction->setToolTip(theInfo.toolTip);
482 anAction->setShortcut(theInfo.shortcut);
483 anAction->setFont(theInfo.font);
485 int aWBMenu = createMenu(theWBName, -1, -1, 30);
486 int aItemId = createMenu(anAction, aWBMenu);
487 createMenu(separator(), aWBMenu); /// nested action is always separated of others
489 int aWBTool = createTool(theWBName, theWBName);
490 int aToolId = createTool(anAction, aWBTool);
491 createTool(separator(), aWBTool); /// nested action is always separated of others
497 //******************************************************
498 QAction* SHAPERGUI::addDesktopCommand(const QString& theId, const QString& theTitle,
499 const QString& theTip, const QIcon& theIcon,
500 const QKeySequence& theKeys, bool isCheckable,
501 const char* theMenuSourceText, const int theMenuPosition)
503 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
505 int aId = myActionsList.size();
506 myActionsList.append(theId);
507 SUIT_Desktop* aDesk = application()->desktop();
509 for (unsigned int i = 0; i < theKeys.count(); i++)
511 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
513 aAction->setStatusTip(theTip);
514 aAction->setData(theId);
515 createMenu(aId, aMenu, theMenuPosition);
519 //******************************************************
520 void SHAPERGUI::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
522 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
523 createMenu(separator(), aMenu, -1, theMenuPosition);
526 bool SHAPERGUI::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
531 SUIT_Desktop* aDesktop = application()->desktop();
535 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
539 aToolMgr->append( theAction, theToolBarTitle );
543 //******************************************************
544 QList<QAction*> SHAPERGUI::commandList() const
546 QList<QAction*> aActions;
547 for (int i = 0; i < myActionsList.size(); i++) {
548 QAction* aCmd = action(i);
549 if (aCmd && myActionsList.contains(aCmd->data().toString()))
550 aActions.append(aCmd);
556 //******************************************************
557 QMainWindow* SHAPERGUI::desktop() const
559 return application()->desktop();
562 void SHAPERGUI::setFeatureInfo(const QString& theFeatureId,
563 const std::shared_ptr<Config_FeatureMessage>& theMessage)
565 myFeaturesInfo.insert(theFeatureId, theMessage);
568 std::shared_ptr<Config_FeatureMessage> SHAPERGUI::featureInfo(const QString& theFeatureId)
570 std::shared_ptr<Config_FeatureMessage> aMessage;
571 if (myFeaturesInfo.contains(theFeatureId))
572 aMessage = myFeaturesInfo[theFeatureId];
576 //******************************************************
577 void SHAPERGUI::selectionChanged()
579 LightApp_Module::selectionChanged();
580 myWorkshop->salomeViewerSelectionChanged();
583 //******************************************************
584 void SHAPERGUI::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
586 myWorkshop->contextMenuMgr()->updateViewerMenu();
587 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
588 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
592 //******************************************************
593 void SHAPERGUI::createPreferences()
595 LightApp_Preferences* pref = preferences();
598 ModuleBase_Preferences::updateConfigByResources();
599 QString aModName = moduleName();
601 QtxPreferenceItem* item = pref->findItem(aModName, true );
602 if ( item && (!item->isEmpty() )) {
603 item->parentItem()->removeItem(item);
607 int catId = pref->addPreference(aModName, -1 );
610 SHAPERGUI_PrefMgr aMgr(pref, aModName);
611 ModuleBase_Preferences::createEditContent(&aMgr, catId);
613 int viewTab = pref->addItem(tr("Viewer"), catId);
614 // Create other parameters group in viewer tab
615 int otherGroup = pref->addItem(tr("Default selection"), viewTab);
616 pref->setItemProperty("columns", 3, otherGroup);
617 pref->addItem(tr("Faces"), otherGroup,
618 SUIT_PreferenceMgr::Bool,
619 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
620 pref->addItem(tr("Edges"), otherGroup,
621 SUIT_PreferenceMgr::Bool,
622 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
623 pref->addItem(tr("Vertices"), otherGroup,
624 SUIT_PreferenceMgr::Bool,
625 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
627 int sensitivityGroup = pref->addItem(tr("Selection sensitivity"), viewTab);
628 pref->setItemProperty("columns", 2, sensitivityGroup);
629 pref->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::Double,
630 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
631 pref->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::Double,
632 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
636 //******************************************************
637 void SHAPERGUI::preferencesChanged(const QString& theSection, const QString& theParam)
639 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
640 QString aVal = aResMgr->stringValue(theSection, theParam);
641 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(),
642 theParam.toStdString());
643 std::string aValue = aVal.toStdString();
644 if (aValue.empty()) {
645 aValue = aProp->defaultValue();
646 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
648 LightApp_Preferences* pref = preferences();
652 aProp->setValue(aValue);
654 myWorkshop->displayer()->redisplayObjects();
657 void SHAPERGUI::putInfo(const QString& theInfo, const int theMSecs)
659 application()->putInfo(theInfo, theMSecs);
662 void SHAPERGUI::inspectSalomeModules()
664 QStringList aModuleNames;
665 getApp()->modules(aModuleNames, false);
666 foreach(QString eachModule, aModuleNames) {
667 Config_ModuleReader::addDependencyModule(eachModule.toStdString());
671 bool SHAPERGUI::abortAllOperations()
673 return workshop()->operationMgr()->abortAllOperations();
676 void SHAPERGUI::createFeatureActions()
678 myWorkshop->menuMgr()->createFeatureActions();