1 // Copyright (C) 2014-2017 CEA/DEN, EDF R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
20 #include "SHAPERGUI.h"
21 #include "SHAPERGUI_DataModel.h"
22 #include "SHAPERGUI_OCCSelector.h"
23 #include <SHAPERGUI_NestedButton.h>
25 #include <XGUI_Workshop.h>
26 #include <XGUI_PropertyPanel.h>
27 #include <XGUI_ContextMenuMgr.h>
28 #include <XGUI_ObjectsBrowser.h>
29 #include <XGUI_OperationMgr.h>
30 #include <XGUI_Displayer.h>
31 #include <XGUI_MenuMgr.h>
33 #include <ModuleBase_Operation.h>
34 #include <ModuleBase_Preferences.h>
35 #include <ModuleBase_ActionInfo.h>
36 #include <ModuleBase_IModule.h>
38 #include <LightApp_Application.h>
39 #include <LightApp_SelectionMgr.h>
40 #include <LightApp_OCCSelector.h>
41 #include <LightApp_Study.h>
42 #include <OCCViewer_ViewModel.h>
44 #include <SUIT_Selector.h>
45 #include <SUIT_Desktop.h>
46 #include <SUIT_ViewManager.h>
47 #include <SUIT_ResourceMgr.h>
48 #include <SUIT_DataBrowser.h>
50 #include <QtxPopupMgr.h>
51 #include <QtxActionMenuMgr.h>
52 #include <QtxActionToolMgr.h>
53 #include <QtxResourceMgr.h>
55 #include <Config_PropManager.h>
56 #include <Config_ModuleReader.h>
58 #include <AIS_ListOfInteractive.hxx>
59 #include <AIS_ListIteratorOfListOfInteractive.hxx>
61 #include <QDockWidget>
66 //#define SALOME_PATCH_FOR_CTRL_WHEEL
69 SHAPERGUI_EXPORT CAM_Module* createModule()
71 return new SHAPERGUI();
74 SHAPERGUI_EXPORT char* getModuleVersion()
81 * Class for preferences management
83 class SHAPERGUI_PrefMgr: public ModuleBase_IPrefMgr
87 /// \param theMgr preferences manager of SALOME
88 /// \param theModName name of the module
89 SHAPERGUI_PrefMgr(LightApp_Preferences* theMgr, const QString& theModName):
90 myMgr(theMgr), myModName(theModName) {}
92 virtual int addPreference(const QString& theLbl, int pId,
93 SUIT_PreferenceMgr::PrefItemType theType,
94 const QString& theSection, const QString& theName )
96 return myMgr->addPreference(myModName, theLbl, pId, theType, theSection, theName);
99 virtual void setItemProperty(const QString& thePropName,
100 const QVariant& theValue,
101 const int theId = -1)
103 myMgr->setItemProperty(thePropName, theValue, theId);
107 virtual SUIT_PreferenceMgr* prefMgr() const { return myMgr; }
110 LightApp_Preferences* myMgr;
117 //******************************************************
118 SHAPERGUI::SHAPERGUI()
119 : LightApp_Module("SHAPER"),
120 mySelector(0), myIsOpened(0), myPopupMgr(0)
122 myWorkshop = new XGUI_Workshop(this);
123 connect(myWorkshop, SIGNAL(commandStatusUpdated()),
124 this, SLOT(onUpdateCommandStatus()));
126 myProxyViewer = new SHAPERGUI_SalomeViewer(this);
128 ModuleBase_Preferences::setResourceMgr(application()->resourceMgr());
129 ModuleBase_Preferences::loadCustomProps();
132 //******************************************************
133 SHAPERGUI::~SHAPERGUI()
137 //******************************************************
138 void SHAPERGUI::initialize(CAM_Application* theApp)
140 LightApp_Module::initialize(theApp);
142 myWorkshop->startApplication();
143 LightApp_Application* anApp = dynamic_cast<LightApp_Application*>(theApp);
146 connect(anApp, SIGNAL(preferenceResetToDefaults()), this, SLOT(onDefaultPreferences()));
150 //******************************************************
151 void SHAPERGUI::windows(QMap<int, int>& theWndMap) const
153 theWndMap.insert(LightApp_Application::WT_PyConsole, Qt::BottomDockWidgetArea);
156 //******************************************************
157 void SHAPERGUI::viewManagers(QStringList& theList) const
159 theList.append(OCCViewer_Viewer::Type());
162 //******************************************************
163 // We can not create selector in this method because it can be called when
164 // SHAPER module is not active. Take into account that creation of our selector
165 // leads to switching OFF all other selectors
166 //void SHAPERGUI::connectToStudy(CAM_Study* theStudy)
168 // // if there are created viewer managers, we should try to create viewer
169 // // selector and initialize viewer with it. It sets interactive contect to the
170 // // proxy viewer. If study is opened, CAM application calls this method before the open()
172 // // the SHAPER data model is specific and during open(load) redisplay signals are flushed, so
173 // // we need to connect to the viewer before it. Here,
174 // // it seems the most appropriate place for this
175 // // according to SALOME architecture.
176 // if (!mySelector) {
177 // ViewManagerList OCCViewManagers;
178 // application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
179 // if (OCCViewManagers.size() > 0) {
180 // mySelector = createSelector(OCCViewManagers.first());
183 // LightApp_Module::connectToStudy(theStudy);
186 //******************************************************
187 bool SHAPERGUI::activateModule(SUIT_Study* theStudy)
189 bool isDone = LightApp_Module::activateModule(theStudy);
190 SHAPERGUI_DataModel* aDataModel = dynamic_cast<SHAPERGUI_DataModel*>(dataModel());
191 aDataModel->initRootObject();
197 QObject* aObj = myWorkshop->objectBrowser()->parent();
198 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
200 QAction* aViewAct = aObjDoc->toggleViewAction();
201 aViewAct->setEnabled(true);
202 myWorkshop->objectBrowser()->setVisible(true);
203 aObjDoc->setVisible(true);
204 desktop()->tabifyDockWidget(aObjDoc, myWorkshop->propertyPanel());
208 ViewManagerList OCCViewManagers;
209 application()->viewManagers(OCCViewer_Viewer::Type(), OCCViewManagers);
210 if (OCCViewManagers.size() > 0) {
211 mySelector = createSelector(OCCViewManagers.first());
214 // it should be pefromed after the selector creation in order to have AISContext
215 myWorkshop->activateModule();
216 //action(myEraseAll)->setEnabled(false);
219 myWorkshop->objectBrowser()->rebuildDataTree();
220 myWorkshop->updateCommandStatus();
224 myWorkshop->updateCommandStatus();
226 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
227 myIsStorePositions = aResMgr->booleanValue("Study", "store_positions", true);
228 myIsEditEnabled = getApp()->isEditEnabled();
229 getApp()->setEditEnabled(false);
231 // this following row is caused by #187 bug.
232 // SALOME saves the dock widget positions before deactivateModule() and
233 // load it after the module activation. So, if the panel is visible before
234 // deactivate, it becomes visible after activate.
235 // In order to avoid the visible property panel, the widget position save is
236 // switch off in this module
237 aResMgr->setValue("Study", "store_positions", false);
239 // Synchronize displayed objects
240 Handle(AIS_InteractiveContext) aContext;
241 if (mySelector && mySelector->viewer())
242 aContext = mySelector->viewer()->getAISContext();
244 if (!aContext.IsNull()) {
245 XGUI_Displayer* aDisp = myWorkshop->displayer();
246 QObjectPtrList aObjList = aDisp->displayedObjects();
248 AIS_ListOfInteractive aList;
249 aContext->DisplayedObjects(aList);
250 AIS_ListIteratorOfListOfInteractive aLIt;
251 Handle(AIS_InteractiveObject) anAISIO;
252 foreach (ObjectPtr aObj, aObjList) {
253 AISObjectPtr aPrs = aDisp->getAISObject(aObj);
254 Handle(AIS_InteractiveObject) aAIS = aPrs->impl<Handle(AIS_InteractiveObject)>();
256 for (aLIt.Initialize(aList); aLIt.More(); aLIt.Next()) {
257 anAISIO = aLIt.Value();
258 if (anAISIO.get() == aAIS.get()) {
264 aObj->setDisplayed(false);
265 //aDisp->erase(aObj, false);
268 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
270 myProxyViewer->activateViewer(true);
272 // Postrrocessing for LoadScriptId to remove created(if it was created) SALOME Object Browser
273 connect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
274 this, SLOT(onScriptLoaded()));
278 //******************************************************
279 bool SHAPERGUI::deactivateModule(SUIT_Study* theStudy)
281 myProxyViewer->activateViewer(false);
285 myWorkshop->deactivateModule();
287 QObject* aObj = myWorkshop->objectBrowser()->parent();
288 QDockWidget* aObjDoc = dynamic_cast<QDockWidget*>(aObj);
290 aObjDoc->setVisible(false);
291 myWorkshop->objectBrowser()->setVisible(false);
292 QAction* aViewAct = aObjDoc->toggleViewAction();
293 aViewAct->setEnabled(false);
296 // the active operation should be stopped for the next activation.
297 // There should not be active operation and visualized preview.
298 // Abort operation should be performed before the selection's remove
299 // because the displayed objects should be removed from the viewer, but
300 // the AIS context is obtained from the selector.
301 ModuleBase_Operation* anOperation = myWorkshop->operationMgr()->currentOperation();
302 while (anOperation) {
303 anOperation->abort();
304 anOperation = myWorkshop->operationMgr()->currentOperation();
306 // Delete selector because it has to be redefined on next activation
308 myProxyViewer->setSelector(0);
313 //myWorkshop->contextMenuMgr()->disconnectViewer();
315 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
316 aResMgr->setValue("Study", "store_positions", myIsStorePositions);
317 getApp()->setEditEnabled(myIsEditEnabled);
319 // Postrrocessing for LoadScriptId to remove created(if it was created) SALOME Object Browser
320 disconnect(getApp()->action(LightApp_Application::UserID+1), SIGNAL(triggered(bool)),
321 this, SLOT(onScriptLoaded()));
323 return LightApp_Module::deactivateModule(theStudy);
326 //******************************************************
327 void SHAPERGUI::onViewManagerAdded(SUIT_ViewManager* theMgr)
330 mySelector = createSelector(theMgr);
331 myWorkshop->module()->activateSelectionFilters();
332 myWorkshop->synchronizeViewer();
336 //******************************************************
337 void SHAPERGUI::onViewManagerRemoved(SUIT_ViewManager* theMgr)
340 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
341 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
342 if (mySelector->viewer() == aViewer) {
343 XGUI_Displayer* aDisp = myWorkshop->displayer();
344 QObjectPtrList aObjects = aDisp->displayedObjects();
345 foreach(ObjectPtr aObj, aObjects)
346 aObj->setDisplayed(false);
347 Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
348 myProxyViewer->setSelector(0);
352 myWorkshop->module()->clearViewer();
358 //******************************************************
359 QtxPopupMgr* SHAPERGUI::popupMgr()
362 myPopupMgr = new QtxPopupMgr( 0, this );
366 //******************************************************
367 void SHAPERGUI::onDefaultPreferences()
369 // reset main resources
370 ModuleBase_Preferences::resetResourcePreferences(preferences());
371 // reset plugin's resources
372 ModuleBase_Preferences::resetConfigPropPreferences(preferences());
374 myWorkshop->displayer()->redisplayObjects();
377 //******************************************************
378 void SHAPERGUI::onScriptLoaded()
380 // this slot is called after processing of the LoadScriptId action of SalomeApp Application
381 // Each dumped script contains updateObjBrowser() that creates a new instance of Object
382 // Browser. When SHAPER module is active, this browser should not be used. It might be removed
383 // as hidden by means of updateWindows() of SalomeApp_Application or to remove
384 // it manually (because this method of application is protected)
385 SUIT_DataBrowser* aBrowser = getApp()->objectBrowser();
390 //******************************************************
391 void SHAPERGUI::onUpdateCommandStatus()
393 getApp()->updateActions();
396 //******************************************************
397 SHAPERGUI_OCCSelector* SHAPERGUI::createSelector(SUIT_ViewManager* theMgr)
399 if (theMgr->getType() == OCCViewer_Viewer::Type()) {
400 OCCViewer_Viewer* aViewer = static_cast<OCCViewer_Viewer*>(theMgr->getViewModel());
401 SHAPERGUI_OCCSelector* aSelector = new SHAPERGUI_OCCSelector(aViewer,
402 getApp()->selectionMgr());
403 #ifdef SALOME_PATCH_FOR_CTRL_WHEEL
404 aViewer->setUseLocalSelection(true);
406 LightApp_SelectionMgr* aMgr = getApp()->selectionMgr();
407 QList<SUIT_Selector*> aList;
408 aMgr->selectors(aList);
409 foreach(SUIT_Selector* aSel, aList)
411 aSel->setEnabled(aSel == aSelector);
413 myProxyViewer->setSelector(aSelector);
419 //******************************************************
420 CAM_DataModel* SHAPERGUI::createDataModel()
422 return new SHAPERGUI_DataModel(this);
425 QAction* SHAPERGUI::addFeature(const QString& theWBName, const ActionInfo& theInfo,
426 const bool isAddSeparator)
428 return addFeature(theWBName,
431 //Issue #650: in the SALOME mode the tooltip should be same as text
440 //******************************************************
441 QAction* SHAPERGUI::addFeature(const QString& theWBName, const QString& theId,
442 const QString& theTitle, const QString& theTip,
443 const QIcon& theIcon, const QKeySequence& theKeys,
444 bool isCheckable, const bool isAddSeparator,
445 const QString& theStatusTip)
447 static QString aLastTool = "";
449 if (aLastTool.isEmpty())
450 aLastTool = theWBName;
451 else if (theWBName != aLastTool) {
452 aLastTool = theWBName;
454 desktop()->addToolBarBreak();
460 int aId = myActionsList.size();
461 myActionsList.append(theId);
462 SUIT_Desktop* aDesk = application()->desktop();
464 for (int i = 0; i < theKeys.count(); i++)
466 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
468 aAction->setStatusTip(theStatusTip);
470 aAction->setData(theId);
472 int aWBMenu = createMenu(theWBName, -1, -1, 30/*10-Window, 1000 - Help*/);
473 int aItemId = createMenu(aId, aWBMenu);
475 createMenu(separator(), aWBMenu);
477 int aWBTool = createTool(theWBName, theWBName);
478 int aToolId = createTool(aId, aWBTool);
480 createTool(separator(), aWBTool);
485 bool SHAPERGUI::isFeatureOfNested(const QAction* theAction)
487 return dynamic_cast<const SHAPERGUI_NestedButton*>(theAction);
490 QAction* SHAPERGUI::addFeatureOfNested(const QString& theWBName,
491 const ActionInfo& theInfo,
492 const QList<QAction*>& theNestedActions)
494 myActionsList.append(theInfo.id);
495 SUIT_Desktop* aDesk = application()->desktop();
496 SHAPERGUI_NestedButton* anAction = new SHAPERGUI_NestedButton(aDesk, theNestedActions);
497 anAction->setData(theInfo.id);
498 anAction->setCheckable(theInfo.checkable);
499 anAction->setChecked(theInfo.checked);
500 anAction->setEnabled(theInfo.enabled);
501 anAction->setVisible(theInfo.visible);
502 anAction->setIcon(theInfo.icon);
503 anAction->setText(theInfo.text);
504 anAction->setToolTip(theInfo.toolTip);
505 anAction->setShortcut(theInfo.shortcut);
506 anAction->setFont(theInfo.font);
508 int aWBMenu = createMenu(theWBName, -1, -1, 30);
509 int aItemId = createMenu(anAction, aWBMenu);
510 createMenu(separator(), aWBMenu); /// nested action is always separated of others
512 int aWBTool = createTool(theWBName, theWBName);
513 int aToolId = createTool(anAction, aWBTool);
514 createTool(separator(), aWBTool); /// nested action is always separated of others
520 //******************************************************
521 QAction* SHAPERGUI::addDesktopCommand(const QString& theId, const QString& theTitle,
522 const QString& theTip, const QIcon& theIcon,
523 const QKeySequence& theKeys, bool isCheckable,
524 const char* theMenuSourceText, const int theMenuPosition)
526 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
528 int aId = myActionsList.size();
529 myActionsList.append(theId);
530 SUIT_Desktop* aDesk = application()->desktop();
532 for (int i = 0; i < theKeys.count(); i++)
534 QAction* aAction = createAction(aId, theTip, theIcon, theTitle, theTip, aKeys, aDesk,
536 aAction->setStatusTip(theTip);
537 aAction->setData(theId);
538 createMenu(aId, aMenu, theMenuPosition);
542 //******************************************************
543 void SHAPERGUI::addDesktopMenuSeparator(const char* theMenuSourceText, const int theMenuPosition)
545 int aMenu = createMenu(tr(theMenuSourceText), -1, -1);
546 createMenu(separator(), aMenu, -1, theMenuPosition);
549 bool SHAPERGUI::addActionInToolbar( QAction* theAction, const QString& theToolBarTitle )
554 SUIT_Desktop* aDesktop = application()->desktop();
558 QtxActionToolMgr* aToolMgr = aDesktop->toolMgr();
562 aToolMgr->append( theAction, theToolBarTitle );
566 //******************************************************
567 QList<QAction*> SHAPERGUI::commandList() const
569 QList<QAction*> aActions;
570 for (int i = 0; i < myActionsList.size(); i++) {
571 QAction* aCmd = action(i);
572 if (aCmd && myActionsList.contains(aCmd->data().toString()))
573 aActions.append(aCmd);
579 //******************************************************
580 QMainWindow* SHAPERGUI::desktop() const
582 return application()->desktop();
585 void SHAPERGUI::setFeatureInfo(const QString& theFeatureId,
586 const std::shared_ptr<Config_FeatureMessage>& theMessage)
588 myFeaturesInfo.insert(theFeatureId, theMessage);
591 std::shared_ptr<Config_FeatureMessage> SHAPERGUI::featureInfo(const QString& theFeatureId)
593 std::shared_ptr<Config_FeatureMessage> aMessage;
594 if (myFeaturesInfo.contains(theFeatureId))
595 aMessage = myFeaturesInfo[theFeatureId];
599 //******************************************************
600 void SHAPERGUI::selectionChanged()
602 LightApp_Module::selectionChanged();
603 myWorkshop->salomeViewerSelectionChanged();
606 //******************************************************
607 void SHAPERGUI::contextMenuPopup(const QString& theClient, QMenu* theMenu, QString& theTitle)
609 myWorkshop->contextMenuMgr()->updateViewerMenu();
610 myWorkshop->contextMenuMgr()->addViewerMenu(theMenu);
611 LightApp_Module::contextMenuPopup(theClient, theMenu, theTitle);
615 //******************************************************
616 void SHAPERGUI::createPreferences()
618 LightApp_Preferences* pref = preferences();
621 ModuleBase_Preferences::updateConfigByResources();
622 QString aModName = moduleName();
624 QtxPreferenceItem* item = pref->findItem(aModName, true );
625 if ( item && (!item->isEmpty() )) {
626 item->parentItem()->removeItem(item);
630 int catId = pref->addPreference(aModName, -1 );
633 SHAPERGUI_PrefMgr aMgr(pref, aModName);
634 ModuleBase_Preferences::createEditContent(&aMgr, catId);
636 int viewTab = pref->addItem(tr("Viewer"), catId);
637 // Create other parameters group in viewer tab
638 int otherGroup = pref->addItem(tr("Default selection"), viewTab);
639 pref->setItemProperty("columns", 3, otherGroup);
640 pref->addItem(tr("Faces"), otherGroup,
641 SUIT_PreferenceMgr::Bool,
642 ModuleBase_Preferences::VIEWER_SECTION, "face-selection");
643 pref->addItem(tr("Edges"), otherGroup,
644 SUIT_PreferenceMgr::Bool,
645 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection");
646 pref->addItem(tr("Vertices"), otherGroup,
647 SUIT_PreferenceMgr::Bool,
648 ModuleBase_Preferences::VIEWER_SECTION, "vertex-selection");
650 int sensitivityGroup = pref->addItem(tr("Selection sensitivity"), viewTab);
651 pref->setItemProperty("columns", 2, sensitivityGroup);
652 pref->addItem(tr("Vertex"), sensitivityGroup, SUIT_PreferenceMgr::Double,
653 ModuleBase_Preferences::VIEWER_SECTION, "point-selection-sensitivity");
654 pref->addItem(tr("Edge"), sensitivityGroup, SUIT_PreferenceMgr::Double,
655 ModuleBase_Preferences::VIEWER_SECTION, "edge-selection-sensitivity");
659 //******************************************************
660 void SHAPERGUI::preferencesChanged(const QString& theSection, const QString& theParam)
662 SUIT_ResourceMgr* aResMgr = application()->resourceMgr();
663 QString aVal = aResMgr->stringValue(theSection, theParam);
664 Config_Prop* aProp = Config_PropManager::findProp(theSection.toStdString(),
665 theParam.toStdString());
666 std::string aValue = aVal.toStdString();
667 if (aValue.empty()) {
668 aValue = aProp->defaultValue();
669 aResMgr->setValue(theSection, theParam, QString(aValue.c_str()));
671 LightApp_Preferences* pref = preferences();
675 aProp->setValue(aValue);
677 myWorkshop->displayer()->redisplayObjects();
680 void SHAPERGUI::putInfo(const QString& theInfo, const int theMSecs)
682 application()->putInfo(theInfo, theMSecs);
685 bool SHAPERGUI::abortAllOperations()
687 return workshop()->operationMgr()->abortAllOperations();
690 void SHAPERGUI::createFeatureActions()
692 myWorkshop->menuMgr()->createFeatureActions();