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>
33 #include <QtxPopupMgr.h>
34 #include <QtxActionMenuMgr.h>
35 #include <QtxActionToolMgr.h>
36 #include <QtxResourceMgr.h>
38 #include <Config_PropManager.h>
39 #include <Config_ModuleReader.h>
41 #include <AIS_ListOfInteractive.hxx>
42 #include <AIS_ListIteratorOfListOfInteractive.hxx>
44 #include <QDockWidget>
51 SHAPERGUI_EXPORT CAM_Module* createModule()
53 return new SHAPERGUI();
56 SHAPERGUI_EXPORT char* getModuleVersion()
63 * Class for preferences management
65 class SHAPERGUI_PrefMgr: public ModuleBase_IPrefMgr
69 /// \param theMgr preferences manager of SALOME
70 /// \param theModName name of the module
71 SHAPERGUI_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):myMgr(theMgr), myModName(theModName) {}
73 virtual int addPreference(const QString& theLbl, int pId,
74 SUIT_PreferenceMgr::PrefItemType theType,
75 const QString& theSection, const QString& theName )
77 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
80 virtual void setItemProperty(const QString& thePropName,
81 const QVariant& theValue,
84 myMgr->setItemProperty(thePropName, theValue, theId);
88 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
91 LightApp_Preferences* myMgr;
98 //******************************************************
99 SHAPERGUI::SHAPERGUI()
100 : LightApp_Module("SHAPER"),
101 mySelector(0), myIsOpened(0), myPopupMgr(0)
103 myWorkshop = new XGUI_Workshop(this);
104 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
105 this, SLOT(onUpdateCommandStatus()));
107 myProxyViewer = new SHAPERGUI_SalomeViewer(this);
109 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
110 ModuleBase_Preferences::loadCustomProps();
113 //******************************************************
114 SHAPERGUI::~SHAPERGUI()
118 //******************************************************
119 void SHAPERGUI::initialize(CAM_Application* theApp)
121 LightApp_Module::initialize(theApp);
122 inspectSalomeModules();
124 myWorkshop->startApplication();
125 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
128 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
132 //******************************************************
133 void SHAPERGUI::windows(QMap<int, int>& theWndMap) const
135 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
138 //******************************************************
139 void SHAPERGUI::viewManagers(QStringList& theList) const
141 theList.append(OCCViewer_Viewer::Type());
144 //******************************************************
145 void SHAPERGUI::connectToStudy(CAM_Study* theStudy)
147 // if there are created viewer managers, we should try to create viewer
148 // selector and initialize viewer with it. It sets interactive contect to the
149 // proxy viewer. If study is opened, CAM application calls this method before the open() of data model
150 // the SHAPER data model is specific and during open(load) redisplay signals are flushed, so
151 // we need to connect to the viewer before it. Here, it seems the most appropriate place for this
152 // according to SALOME architecture.
154 ViewManagerList OCCViewManagers;
155 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
156 if (OCCViewManagers.size() > 0) {
157 mySelector = createSelector(OCCViewManagers.first());
160 LightApp_Module::connectToStudy(theStudy);
163 //******************************************************
164 bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
166 bool isDone = LightApp_Module::activateModule(theStudy);
167 SHAPERGUI_DataModel* aDataModel = dynamic_cast<SHAPERGUI_DataModel*>(dataModel());
168 aDataModel->initRootObject();
174 QObject* aObj = myWorkshop->objectBrowser()->parent();
175 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
177 QAction* aViewAct = aObjDoc->toggleViewAction();
178 aViewAct->setEnabled(true);
179 myWorkshop->objectBrowser()->setVisible(true);
180 aObjDoc->setVisible(true);
181 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
185 ViewManagerList OCCViewManagers;
186 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
187 if (OCCViewManagers.size() > 0) {
188 mySelector = createSelector(OCCViewManagers.first());
191 // it should be pefromed after the selector creation in order to have AISContext
192 myWorkshop->activateModule();
193 //action(myEraseAll)->setEnabled(false);
196 myWorkshop->objectBrowser()->rebuildDataTree();
197 myWorkshop->updateCommandStatus();
201 myWorkshop->updateCommandStatus();
203 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
204 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
205 myIsEditEnabled = getApp()->isEditEnabled();
206 getApp()->setEditEnabled(false);
208 // this following row is caused by #187 bug.
209 // SALOME saves the dock widget positions before deactivateModule() and
210 // load it after the module activation. So, if the panel is visible before
211 // deactivate, it becomes visible after activate.
212 // In order to avoid the visible property panel, the widget position save is
213 // switch off in this module
214 aResMgr->setValue("Study", "store_positions", false);
216 // Synchronize displayed objects
217 Handle(AIS_InteractiveContext) aContext;
218 if (mySelector && mySelector->viewer())
219 aContext = mySelector->viewer()->getAISContext();
221 if (!aContext.IsNull()) {
222 XGUI_Displayer* aDisp = myWorkshop->displayer();
223 QObjectPtrList aObjList = aDisp->displayedObjects();
225 AIS_ListOfInteractive aList;
226 aContext->DisplayedObjects(aList);
227 AIS_ListIteratorOfListOfInteractive aLIt;
228 Handle(AIS_InteractiveObject) anAISIO;
229 foreach (ObjectPtr aObj, aObjList) {
230 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
231 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
233 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
234 anAISIO = aLIt.Value();
235 if (anAISIO.Access() == aAIS.Access()) {
241 aObj->setDisplayed(false);
242 //aDisp->erase(aObj, false);
245 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
247 myProxyViewer->activateViewer(true);
251 //******************************************************
252 bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
254 myProxyViewer->activateViewer(false);
258 myWorkshop->deactivateModule();
260 QObject* aObj = myWorkshop->objectBrowser()->parent();
261 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
263 aObjDoc->setVisible(false);
264 myWorkshop->objectBrowser()->setVisible(false);
265 QAction* aViewAct = aObjDoc->toggleViewAction();
266 aViewAct->setEnabled(false);
269 // the active operation should be stopped for the next activation.
270 // There should not be active operation and visualized preview.
271 // Abort operation should be performed before the selection's remove
272 // because the displayed objects should be removed from the viewer, but
273 // the AIS context is obtained from the selector.
274 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
275 while (anOperation) {
276 anOperation->abort();
277 anOperation = myWorkshop->operationMgr()->currentOperation();
279 // Delete selector because it has to be redefined on next activation
281 myProxyViewer->setSelector(0);
286 //myWorkshop->contextMenuMgr()->disconnectViewer();
288 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
289 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
290 getApp()->setEditEnabled(myIsEditEnabled);
292 return LightApp_Module::deactivateModule(theStudy);
295 //******************************************************
296 void SHAPERGUI::onViewManagerAdded(SUIT_ViewManager* theMgr)
299 mySelector = createSelector(theMgr);
300 myWorkshop->module()->activateSelectionFilters();
301 myWorkshop->synchronizeViewer();
305 //******************************************************
306 void SHAPERGUI::onViewManagerRemoved(SUIT_ViewManager* theMgr)
309 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
310 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
311 if (mySelector->viewer() == aViewer) {
312 XGUI_Displayer* aDisp = myWorkshop->displayer();
313 QObjectPtrList aObjects = aDisp->displayedObjects();
314 foreach(ObjectPtr aObj, aObjects)
315 aObj->setDisplayed(false);
316 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
317 myProxyViewer->setSelector(0);
321 myWorkshop->module()->clearViewer();
327 //******************************************************
328 QtxPopupMgr* SHAPERGUI::popupMgr()
331 myPopupMgr = new QtxPopupMgr( 0, this );
335 //******************************************************
336 void SHAPERGUI::onDefaultPreferences()
338 // reset main resources
339 ModuleBase_Preferences::resetResourcePreferences(preferences());
340 // reset plugin's resources
341 ModuleBase_Preferences::resetConfigPropPreferences(preferences());
343 myWorkshop->displayer()->redisplayObjects();
346 //******************************************************
347 void SHAPERGUI::onUpdateCommandStatus()
349 getApp()->updateActions();
352 //******************************************************
353 SHAPERGUI_OCCSelector* SHAPERGUI::createSelector(SUIT_ViewManager* theMgr)
355 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
356 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
357 SHAPERGUI_OCCSelector* aSelector = new SHAPERGUI_OCCSelector(aViewer, getApp()->selectionMgr());
358 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
359 QList<SUIT_Selector*> aList;
360 aMgr->selectors(aList);
361 foreach(SUIT_Selector* aSel, aList)
363 aSel->setEnabled(aSel == aSelector);
365 myProxyViewer->setSelector(aSelector);
371 //******************************************************
372 CAM_DataModel* SHAPERGUI::createDataModel()
374 return new SHAPERGUI_DataModel(this);
377 QAction* SHAPERGUI::addFeature(const QString& theWBName, const ActionInfo& theInfo,
378 const bool isAddSeparator)
380 return addFeature(theWBName,
383 theInfo.text, //Issue #650: in the SALOME mode the tooltip should be same as text
391 //******************************************************
392 QAction* SHAPERGUI::addFeature(const QString& theWBName, const QString& theId,
393 const QString& theTitle, const QString& theTip,
394 const QIcon& theIcon, const QKeySequence& theKeys,
395 bool isCheckable, const bool isAddSeparator,
396 const QString& theStatusTip)
398 static QString aLastTool = "";
400 if (aLastTool.isEmpty())
401 aLastTool = theWBName;
402 else if (theWBName != aLastTool) {
403 aLastTool = theWBName;
405 desktop()->addToolBarBreak();
411 int aId = myActionsList.size();
412 myActionsList.append(theId);
413 SUIT_Desktop* aDesk = application()->desktop();
415 for (unsigned int i = 0; i < theKeys.count(); i++)
417 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
419 aAction->setStatusTip(theStatusTip);
421 aAction->setData(theId);
423 int aWBMenu = createMenu(theWBName, -1, -1, 50/*10-Window, 1000 - Help*/);
424 int aItemId = createMenu(aId, aWBMenu);
426 createMenu(separator(), aWBMenu);
428 int aWBTool = createTool(theWBName, theWBName);
429 int aToolId = createTool(aId, aWBTool);
431 createTool(separator(), aWBTool);
436 bool SHAPERGUI::isFeatureOfNested(const QAction* theAction)
438 return dynamic_cast<const SHAPERGUI_NestedButton*>(theAction);
441 QAction* SHAPERGUI::addFeatureOfNested(const QString& theWBName,
442 const ActionInfo& theInfo,
443 const QList<QAction*>& theNestedActions)
445 myActionsList.append(theInfo.id);
446 SUIT_Desktop* aDesk = application()->desktop();
447 SHAPERGUI_NestedButton* anAction = new SHAPERGUI_NestedButton(aDesk, theNestedActions);
448 anAction->setData(theInfo.id);
449 anAction->setCheckable(theInfo.checkable);
450 anAction->setChecked(theInfo.checked);
451 anAction->setEnabled(theInfo.enabled);
452 anAction->setVisible(theInfo.visible);
453 anAction->setIcon(theInfo.icon);
454 anAction->setText(theInfo.text);
455 anAction->setToolTip(theInfo.toolTip);
456 anAction->setShortcut(theInfo.shortcut);
457 anAction->setFont(theInfo.font);
459 int aWBMenu = createMenu(theWBName, -1, -1, 50);
460 int aItemId = createMenu(anAction, aWBMenu);
461 createMenu(separator(), aWBMenu); /// nested action is always separated of others
463 int aWBTool = createTool(theWBName, theWBName);
464 int aToolId = createTool(anAction, aWBTool);
465 createTool(separator(), aWBTool); /// nested action is always separated of others
471 //******************************************************
472 QAction* SHAPERGUI::addDesktopCommand(const QString& theId, const QString& theTitle,
473 const QString& theTip, const QIcon& theIcon,
474 const QKeySequence& theKeys, bool isCheckable,
475 const char* theMenuSourceText, const int theMenuPosition)
477 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
479 int aId = myActionsList.size();
480 myActionsList.append(theId);
481 SUIT_Desktop* aDesk = application()->desktop();
483 for (unsigned int i = 0; i < theKeys.count(); i++)
485 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
487 aAction->setStatusTip(theTip);
488 aAction->setData(theId);
489 createMenu(aId, aMenu, theMenuPosition);
493 //******************************************************
494 void SHAPERGUI::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
496 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
497 createMenu(separator(), aMenu, -1, theMenuPosition);
500 bool SHAPERGUI::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
505 SUIT_Desktop* aDesktop = application()->desktop();
509 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
513 aToolMgr->append( theAction, theToolBarTitle );
517 //******************************************************
518 QList<QAction*> SHAPERGUI::commandList() const
520 QList<QAction*> aActions;
521 for (int i = 0; i < myActionsList.size(); i++) {
522 QAction* aCmd = action(i);
523 if (aCmd && myActionsList.contains(aCmd->data().toString()))
524 aActions.append(aCmd);
530 //******************************************************
531 QMainWindow* SHAPERGUI::desktop() const
533 return application()->desktop();
536 void SHAPERGUI::setFeatureInfo(const QString& theFeatureId,
537 const std::shared_ptr<Config_FeatureMessage>& theMessage)
539 myFeaturesInfo.insert(theFeatureId, theMessage);
542 std::shared_ptr<Config_FeatureMessage> SHAPERGUI::featureInfo(const QString& theFeatureId)
544 std::shared_ptr<Config_FeatureMessage> aMessage;
545 if (myFeaturesInfo.contains(theFeatureId))
546 aMessage = myFeaturesInfo[theFeatureId];
550 //******************************************************
551 void SHAPERGUI::selectionChanged()
553 LightApp_Module::selectionChanged();
554 myWorkshop->salomeViewerSelectionChanged();
557 //******************************************************
558 void SHAPERGUI::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
560 myWorkshop->contextMenuMgr()->updateViewerMenu();
561 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
562 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
566 //******************************************************
567 void SHAPERGUI::createPreferences()
569 LightApp_Preferences* pref = preferences();
572 ModuleBase_Preferences::updateConfigByResources();
573 QString aModName = moduleName();
575 QtxPreferenceItem* item = pref->findItem(aModName, true );
576 if ( item && (!item->isEmpty() )) {
577 item->parentItem()->removeItem(item);
581 int catId = pref->addPreference(aModName, -1 );
584 SHAPERGUI_PrefMgr aMgr(pref, aModName);
585 ModuleBase_Preferences::createEditContent(&aMgr, catId);
587 int viewTab = pref->addItem(tr("Viewer"), catId);
588 // Create other parameters group in viewer tab
589 int otherGroup = pref->addItem(tr("Default selection"), viewTab);
590 pref->setItemProperty("columns", 3, otherGroup);
591 pref->addItem(tr("Faces"), otherGroup,
592 SUIT_PreferenceMgr::Bool,
593 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
594 pref->addItem(tr("Edges"), otherGroup,
595 SUIT_PreferenceMgr::Bool,
596 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
597 pref->addItem(tr("Vertices"), otherGroup,
598 SUIT_PreferenceMgr::Bool,
599 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
601 int sensitivityGroup = pref->addItem(tr("Selection sensitivity"), viewTab);
602 pref->setItemProperty("columns", 2, sensitivityGroup);
603 pref->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::Double,
604 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
605 pref->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::Double,
606 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
610 //******************************************************
611 void SHAPERGUI::preferencesChanged(const QString& theSection, const QString& theParam)
613 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
614 QString aVal = aResMgr->stringValue(theSection, theParam);
615 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(), theParam.toStdString());
616 std::string aValue = aVal.toStdString();
617 if (aValue.empty()) {
618 aValue = aProp->defaultValue();
619 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
621 LightApp_Preferences* pref = preferences();
625 aProp->setValue(aValue);
627 myWorkshop->displayer()->redisplayObjects();
630 void SHAPERGUI::putInfo(const QString& theInfo, const int theMSecs)
632 application()->putInfo(theInfo, theMSecs);
635 void SHAPERGUI::inspectSalomeModules()
637 QStringList aModuleNames;
638 getApp()->modules(aModuleNames, false);
639 foreach(QString eachModule, aModuleNames) {
640 Config_ModuleReader::addDependencyModule(eachModule.toStdString());
644 bool SHAPERGUI::abortAllOperations()
646 return workshop()->operationMgr()->abortAllOperations();
649 void SHAPERGUI::createFeatureActions()
651 myWorkshop->menuMgr()->createFeatureActions();