Salome HOME
Issue #2154 Multiselection field is cleared when trying to add another object
[modules/shaper.git] / src / XGUI / XGUI_ModuleConnector.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D -->
2
3 // File:        XGUI_ModuleConnector.cpp
4 // Created:     3 June 2014
5 // Author:      Vitaly Smetannikov
6
7 #include "XGUI_ModuleConnector.h"
8 #include "XGUI_Workshop.h"
9 #include "XGUI_ViewerProxy.h"
10 #include "XGUI_SelectionMgr.h"
11 #include "XGUI_Selection.h"
12 #include "XGUI_OperationMgr.h"
13 #include "XGUI_Displayer.h"
14 #include "XGUI_PropertyPanel.h"
15 #include "XGUI_ActionsMgr.h"
16 #include "XGUI_ErrorMgr.h"
17
18 #include <ModuleBase_IModule.h>
19 #include <ModuleBase_ViewerPrs.h>
20 #include <ModuleBase_OperationDescription.h>
21
22 #include <AIS_Shape.hxx>
23
24 #ifndef HAVE_SALOME
25 #include "AppElements_Command.h"
26 #else
27 #include "XGUI_SalomeConnector.h"
28 #endif
29
30 XGUI_ModuleConnector::XGUI_ModuleConnector(XGUI_Workshop* theWorkshop)
31     : ModuleBase_IWorkshop(theWorkshop),
32       myWorkshop(theWorkshop)
33 {
34   XGUI_SelectionMgr* aSelector = myWorkshop->selector();
35   connect(aSelector, SIGNAL(selectionChanged()), this, SIGNAL(selectionChanged()));
36 }
37
38 XGUI_ModuleConnector::~XGUI_ModuleConnector()
39 {
40 }
41
42 ModuleBase_ISelection* XGUI_ModuleConnector::selection() const
43 {
44   return myWorkshop->selector()->selection();
45 }
46
47 ModuleBase_IModule* XGUI_ModuleConnector::module() const
48 {
49   return myWorkshop->module();
50 }
51
52 ModuleBase_IViewer* XGUI_ModuleConnector::viewer() const
53 {
54   return myWorkshop->viewer();
55 }
56
57 ModuleBase_IPropertyPanel* XGUI_ModuleConnector::propertyPanel() const
58 {
59   return myWorkshop->propertyPanel();
60 }
61
62 ModuleBase_IErrorMgr* XGUI_ModuleConnector::errorMgr() const
63 {
64   return myWorkshop->errorMgr();
65 }
66
67 ModuleBase_Operation* XGUI_ModuleConnector::currentOperation() const
68 {
69   return myWorkshop->operationMgr()->currentOperation();
70 }
71
72
73 QObjectPtrList XGUI_ModuleConnector::activeObjects(const QObjectPtrList& theObjList) const
74 {
75   QObjectPtrList aActiveOPbjects;
76   ModuleBase_IModule* aModule = myWorkshop->module();
77   // Activate objects only which can be activated
78   foreach (ObjectPtr aObj, theObjList) {
79     if (aModule->canActivateSelection(aObj))
80       aActiveOPbjects.append(aObj);
81   }
82   return aActiveOPbjects;
83 }
84
85 void XGUI_ModuleConnector::activateSubShapesSelection(const QIntList& theTypes)
86 {
87   QIntList aTypes = theTypes;
88
89   XGUI_Displayer* aDisp = myWorkshop->displayer();
90   myWorkshop->module()->customSubShapesSelectionModes(aTypes);
91   aDisp->activateObjects(aTypes, activeObjects(aDisp->displayedObjects()));
92 }
93
94 void XGUI_ModuleConnector::deactivateSubShapesSelection()
95 {
96   // Clear selection modes
97   activateModuleSelectionModes();
98 }
99
100 void XGUI_ModuleConnector::activateModuleSelectionModes()
101 {
102   XGUI_Displayer* aDisp = myWorkshop->displayer();
103   myWorkshop->activateObjectsSelection(activeObjects(aDisp->displayedObjects()));
104 }
105
106 AISObjectPtr XGUI_ModuleConnector::findPresentation(const ObjectPtr& theObject) const
107 {
108   XGUI_Displayer* aDisp = myWorkshop->displayer();
109   return aDisp->getAISObject(theObject);
110 }
111
112 ObjectPtr XGUI_ModuleConnector::findPresentedObject(const AISObjectPtr& theAIS) const
113 {
114   XGUI_Displayer* aDisp = myWorkshop->displayer();
115   ObjectPtr anObject = aDisp->getObject(theAIS);
116   return anObject;
117 }
118
119 void XGUI_ModuleConnector::setSelected(const QList<ModuleBase_ViewerPrsPtr>& theValues)
120 {
121   XGUI_Displayer* aDisp = myWorkshop->displayer();
122   if (theValues.isEmpty()) {
123     myWorkshop->selector()->clearSelection();
124   } else {
125     aDisp->setSelected(theValues);
126     // according to #2154 we need to update OB selection when selection in the viewer happens
127     myWorkshop->selector()->onViewerSelection();
128   }
129 }
130
131 void XGUI_ModuleConnector::setStatusBarMessage(const QString& theMessage)
132 {
133   myWorkshop->setStatusBarMessage(theMessage);
134 }
135
136 bool XGUI_ModuleConnector::canStartOperation(QString theId, bool& isCommitted)
137 {
138   return myWorkshop->operationMgr()->canStartOperation(theId, isCommitted);
139 }
140
141 void XGUI_ModuleConnector::processLaunchOperation(ModuleBase_Operation* theOperation)
142 {
143   XGUI_OperationMgr* anOperationMgr = workshop()->operationMgr();
144
145   if (anOperationMgr->startOperation(theOperation)) {
146     ModuleBase_OperationFeature* aFOperation =
147       dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
148     if (aFOperation) {
149       workshop()->propertyPanel()->updateContentWidget(aFOperation->feature());
150       workshop()->propertyPanel()->createContentPanel(aFOperation->feature());
151     }
152     if (!theOperation->getDescription()->hasXmlRepresentation()) {
153       if (theOperation->commit())
154         workshop()->updateCommandStatus();
155     }
156   }
157 }
158
159 ModuleBase_Operation* XGUI_ModuleConnector::findStartedOperation(const QString& theId)
160 {
161   return myWorkshop->operationMgr()->findOperation(theId);
162 }
163
164 bool XGUI_ModuleConnector::canStopOperation(ModuleBase_Operation* theOperation)
165 {
166   return myWorkshop->operationMgr()->canStopOperation(theOperation);
167 }
168
169 void XGUI_ModuleConnector::stopOperation(ModuleBase_Operation* theOperation,
170                                          bool& isCommitted)
171 {
172   myWorkshop->operationMgr()->stopOperation(theOperation, isCommitted);
173 }
174
175 void XGUI_ModuleConnector::updateCommandStatus()
176 {
177   myWorkshop->updateCommandStatus();
178 }
179
180 QMainWindow* XGUI_ModuleConnector::desktop() const
181 {
182   return myWorkshop->desktop();
183 }
184
185
186 std::shared_ptr<Config_FeatureMessage> XGUI_ModuleConnector::featureInfo(const QString& theId) const
187 {
188 #ifdef HAVE_SALOME
189   return myWorkshop->salomeConnector()->featureInfo(theId);
190 #else
191   AppElements_Command* aAction =
192     dynamic_cast<AppElements_Command*>(myWorkshop->actionsMgr()->action(theId));
193   if (aAction)
194     return aAction->featureMessage();
195   return std::shared_ptr<Config_FeatureMessage>();
196 #endif
197 }