]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReetntrantMgr.cpp
Salome HOME
a243e762f79bfa0cdf73035d0c0639c60edfbf0a
[modules/shaper.git] / src / PartSet / PartSet_SketcherReetntrantMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "PartSet_SketcherReetntrantMgr.h"
4 #include "PartSet_Module.h"
5 #include "PartSet_SketcherMgr.h"
6 #include "PartSet_WidgetPoint2D.h"
7
8 #include "ModelAPI_Session.h"
9
10 #include <ModuleBase_IPropertyPanel.h>
11 #include <ModuleBase_OperationFeature.h>
12 #include <ModuleBase_ModelWidget.h>
13 #include <ModuleBase_ViewerPrs.h>
14 #include <ModuleBase_WidgetSelector.h>
15
16 #include <SketchPlugin_Feature.h>
17 #include <SketchPlugin_Line.h>
18
19 #include <XGUI_Workshop.h>
20 #include <XGUI_ModuleConnector.h>
21 #include <XGUI_OperationMgr.h>
22
23 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
24 : QObject(theWorkshop),
25   myWorkshop(theWorkshop),
26   myIsInternalEditOperation(false),
27   myLastOperationId(""),
28   myPreviousAttributeID(""),
29   myRestartingMode(RM_None)
30 {
31 }
32
33 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
34 {
35 }
36
37 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::activeWidget() const
38 {
39   ModuleBase_ModelWidget* aWidget = 0;
40   if (!isActiveMgr())
41     return aWidget;
42
43   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
44   if (aOperation) {
45     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
46     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
47     if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector())) {
48       // finds the first widget which can accept a value
49       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
50       ModuleBase_ModelWidget* aFirstWidget = 0;
51       ModuleBase_ModelWidget* aWgt;
52       QList<ModuleBase_ModelWidget*>::const_iterator aWIt;
53       for (aWIt = aWidgets.begin(); aWIt != aWidgets.end() && !aFirstWidget; ++aWIt) {
54         aWgt = (*aWIt);
55         if (aWgt->canSetValue())
56           aFirstWidget = aWgt;
57       }
58       if (aFirstWidget)
59         aWidget = aFirstWidget;
60     }
61   }
62   return aWidget;
63 }
64
65 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
66 {
67   bool aProcessed = false;
68   if (!isActiveMgr())
69     return aProcessed;
70
71   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
72   if (!aFOperation)
73     return aProcessed;
74
75   FeaturePtr aFeature = aFOperation->feature();
76   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
77             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
78   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
79                      myRestartingMode == RM_EmptyFeatureUsed)) {
80     myLastOperationId = aFOperation->id().toStdString();
81     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
82     PartSet_Module* aModule = module();
83     if (!aModule->sketchMgr()->sketchSolverError()) {
84       if (!aFOperation->isEditOperation()) {
85         FeaturePtr anOperationFeature = aFOperation->feature();
86         if (anOperationFeature.get() != NULL) {
87           aModule->editFeature(anOperationFeature);
88           aProcessed = true;
89
90           myIsInternalEditOperation = true;
91           // activate selection filters of the first widget in the viewer
92           onInternalActivateFirstWidgetSelection();
93
94           // activate the last active widget in the Property Panel
95           if (!myPreviousAttributeID.empty()) {
96             ModuleBase_Operation* anEditOperation = aModule->currentOperation();
97             if (anEditOperation) {
98               ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
99               ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
100               QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
101               for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
102                 if (aWidgets[i]->attributeID() == myPreviousAttributeID)
103                   aPreviousAttributeWidget = aWidgets[i];
104               }
105               // If the current widget is a selector, do nothing, it processes the mouse press
106               if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
107                 aPreviousAttributeWidget->focusTo();
108             }
109           }
110         }
111       }
112       else {
113         // the flag should be reset before start to do not react to the widget deactivate
114         myIsInternalEditOperation = false;
115         aModule->launchOperation(myLastOperationId.c_str());
116         breakOperationSequence();
117         aProcessed = true;
118       }
119     }
120   }
121
122   if (!aProcessed)
123     breakOperationSequence();
124
125   return aProcessed;
126 }
127
128 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
129 {
130   if (!isActiveMgr())
131     return;
132
133   if (myIsInternalEditOperation) {
134     // abort the created feature, which is currently edited
135     SessionPtr aMgr = ModelAPI_Session::get();
136     if (aMgr->hasModuleDocument() && aMgr->canUndo()) {
137       aMgr->undo();
138     }
139   }
140   myIsInternalEditOperation = false;
141   breakOperationSequence();
142 }
143
144 bool PartSet_SketcherReetntrantMgr::processMouseMoved()
145 {
146   bool aProcessed = false;
147   if (!isActiveMgr())
148     return aProcessed;
149
150   if  (myIsInternalEditOperation) {
151     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
152     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
153     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
154       //if (operationMgr()->isApplyEnabled())
155       anOperation->commit();
156       aProcessed = true;
157     }
158   }
159   return aProcessed;
160 }
161
162 bool PartSet_SketcherReetntrantMgr::processMousePressed()
163 {
164   return isActiveMgr() && myIsInternalEditOperation;
165 }
166
167 bool PartSet_SketcherReetntrantMgr::processMouseReleased()
168 {
169   bool aProcessed = false;
170   if (!isActiveMgr())
171     return aProcessed;
172
173   if (myIsInternalEditOperation) {
174     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
175     //if (operationMgr()->isApplyEnabled())
176     anOperation->commit();
177     aProcessed = true;
178   }
179   return aProcessed;
180 }
181
182 void PartSet_SketcherReetntrantMgr::propertyPanelDefined(ModuleBase_Operation* theOperation)
183 {
184   if (!isActiveMgr())
185     return;
186   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
187   if (!aFOperation)
188     return;
189
190   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
191   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation())) {
192     // we have to manually activate the sketch label in edit mode
193     aPanel->activateWidget(aPanel->modelWidgets().first());
194   }
195   else if ((aFOperation->id() == myLastOperationId.c_str()) && myLastFeature) {
196     // Restart last operation type 
197     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
198     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
199     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
200       QList<ModuleBase_ViewerPrs> aSelection;
201       aSelection.append(ModuleBase_ViewerPrs(myLastFeature, TopoDS_Shape(), NULL));
202       if (aPoint2DWdg->setSelection(aSelection, true))
203         aPanel->activateNextWidget(aPoint2DWdg);
204     }
205   }
206 }
207
208 void PartSet_SketcherReetntrantMgr::noMoreWidgets(const std::string& thePreviousAttributeID)
209 {
210   if (!isActiveMgr())
211     return;
212
213   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
214   if (anOperation) {
215     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
216       if (myRestartingMode != RM_Forbided) {
217         myRestartingMode = RM_LastFeatureUsed;
218         myPreviousAttributeID = thePreviousAttributeID;
219       }
220       XGUI_Workshop* aWorkshop = workshop();
221       XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
222       // do nothing if the feature can not be applyed
223       if (anOpMgr->isApplyEnabled())
224         anOperation->commit();
225     }
226   }
227 }
228
229 void PartSet_SketcherReetntrantMgr::vertexSelected()
230 {
231   if (!isActiveMgr())
232     return;
233
234   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
235   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
236     /// If last line finished on vertex the lines creation sequence has to be break
237     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
238     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
239     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
240     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
241     bool aFoundWidget = false;
242     bool aFoundObligatory = false;
243     for (; anIt != aLast && !aFoundObligatory; anIt++) {
244       if (!aFoundWidget)
245         aFoundWidget = *anIt == anActiveWidget;
246       else
247         aFoundObligatory = (*anIt)->isObligatory();
248     }
249     if (!aFoundObligatory)
250       myRestartingMode = RM_Forbided;
251   }
252 }
253
254 void PartSet_SketcherReetntrantMgr::enterReleased()
255 {
256   if (!isActiveMgr())
257     return;
258
259   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
260                                                       (myWorkshop->currentOperation());
261   if (/*!aFOperation->isEditOperation() || */myIsInternalEditOperation)
262     myRestartingMode = RM_EmptyFeatureUsed;
263 }
264
265 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
266 {
267   return !isActiveMgr() || myRestartingMode == RM_None;
268 }
269
270 void PartSet_SketcherReetntrantMgr::onInternalActivateFirstWidgetSelection()
271 {
272   if (!isActiveMgr())
273     return;
274
275   if (!myIsInternalEditOperation)
276     return;
277
278   PartSet_Module* aModule = module();
279   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
280   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
281   if (aFirstWidget != aPanel->activeWidget()) {
282     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
283     if (aWSelector)
284       aWSelector->activateSelectionAndFilters(true);
285   }
286 }
287
288 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
289 {
290   PartSet_SketcherMgr* aSketcherMgr = module()->sketchMgr();
291   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
292   return PartSet_SketcherMgr::isSketchOperation(aCurrentOperation) ||
293          PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
294 }
295
296 void PartSet_SketcherReetntrantMgr::breakOperationSequence()
297 {
298   myLastOperationId = "";
299   myLastFeature = FeaturePtr();
300   myRestartingMode = RM_None;
301 }
302
303 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
304 {
305   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
306   return aConnector->workshop();
307 }
308
309 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
310 {
311   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
312 }