Salome HOME
1. Circle creation - when 'internal' edit operation is active, the first click should...
[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           onWidgetActivated();
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       workshop()->operationMgr()->updateApplyOfOperations();
139       workshop()->updateCommandStatus();
140     }
141   }
142   myIsInternalEditOperation = false;
143   breakOperationSequence();
144 }
145
146 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow*/* theWnd*/,
147                                                       QMouseEvent*/* theEvent*/)
148 {
149   bool aProcessed = false;
150   if (!isActiveMgr())
151     return aProcessed;
152
153   if  (myIsInternalEditOperation) {
154     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
155     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
156     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
157       anOperation->commit();
158       aProcessed = true;
159     }
160   }
161   return aProcessed;
162 }
163
164 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow*/* theWnd*/,
165                                                         QMouseEvent*/* theEvent*/)
166 {
167   return isActiveMgr() && myIsInternalEditOperation;
168 }
169
170 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
171                                                          QMouseEvent* theEvent)
172 {
173   bool aProcessed = false;
174   if (!isActiveMgr())
175     return aProcessed;
176
177   if (myIsInternalEditOperation) {
178     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
179
180     ModuleBase_ModelWidget* anActiveWidget = anOperation->propertyPanel()->activeWidget();
181     if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
182       anOperation->commit();
183       aProcessed = true;
184
185       // fill the widget by the mouse event point
186       PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
187       if (aPoint2DWdg) {
188         aPoint2DWdg->onMouseRelease(theWnd, theEvent);
189       }
190     }
191   }
192
193   return aProcessed;
194 }
195
196 void PartSet_SketcherReetntrantMgr::propertyPanelDefined(ModuleBase_Operation* theOperation)
197 {
198   if (!isActiveMgr())
199     return;
200   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(theOperation);
201   if (!aFOperation)
202     return;
203
204   ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
205   if (PartSet_SketcherMgr::isSketchOperation(aFOperation) &&  (aFOperation->isEditOperation())) {
206     // we have to manually activate the sketch label in edit mode
207     aPanel->activateWidget(aPanel->modelWidgets().first());
208   }
209   else if ((aFOperation->id() == myLastOperationId.c_str()) && myLastFeature) {
210     // Restart last operation type 
211     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
212     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
213     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
214       QList<ModuleBase_ViewerPrs> aSelection;
215       aSelection.append(ModuleBase_ViewerPrs(myLastFeature, TopoDS_Shape(), NULL));
216       if (aPoint2DWdg->setSelection(aSelection, true))
217         aPanel->activateNextWidget(aPoint2DWdg);
218     }
219   }
220 }
221
222 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
223 {
224   if (!isActiveMgr())
225     return;
226
227   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
228   if (anOperation) {
229     if (PartSet_SketcherMgr::isNestedSketchOperation(anOperation)) {
230       if (myRestartingMode != RM_Forbided) {
231         myRestartingMode = RM_LastFeatureUsed;
232         myPreviousAttributeID = thePreviousAttributeID;
233       }
234       XGUI_Workshop* aWorkshop = workshop();
235       XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
236       // do nothing if the feature can not be applyed
237       if (anOpMgr->isApplyEnabled())
238         anOperation->commit();
239     }
240   }
241 }
242
243 void PartSet_SketcherReetntrantMgr::onVertexSelected()
244 {
245   if (!isActiveMgr())
246     return;
247
248   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
249   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
250     /// If last line finished on vertex the lines creation sequence has to be break
251     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
252     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
253     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
254     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
255     bool aFoundWidget = false;
256     bool aFoundObligatory = false;
257     for (; anIt != aLast && !aFoundObligatory; anIt++) {
258       if (!aFoundWidget)
259         aFoundWidget = *anIt == anActiveWidget;
260       else
261         aFoundObligatory = (*anIt)->isObligatory();
262     }
263     if (!aFoundObligatory)
264       myRestartingMode = RM_Forbided;
265   }
266 }
267
268 void PartSet_SketcherReetntrantMgr::enterReleased()
269 {
270   if (!isActiveMgr())
271     return;
272
273   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
274                                                       (myWorkshop->currentOperation());
275   if (/*!aFOperation->isEditOperation() || */myIsInternalEditOperation)
276     myRestartingMode = RM_EmptyFeatureUsed;
277 }
278
279 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
280 {
281   return !isActiveMgr() || myRestartingMode == RM_None;
282 }
283
284 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
285 {
286   if (!isActiveMgr())
287     return;
288
289   if (!myIsInternalEditOperation)
290     return;
291
292   PartSet_Module* aModule = module();
293   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
294   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
295   if (aFirstWidget != aPanel->activeWidget()) {
296     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
297     if (aWSelector)
298       aWSelector->activateSelectionAndFilters(true);
299   }
300 }
301
302 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
303 {
304   PartSet_SketcherMgr* aSketcherMgr = module()->sketchMgr();
305   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
306   return PartSet_SketcherMgr::isSketchOperation(aCurrentOperation) ||
307          PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
308 }
309
310 void PartSet_SketcherReetntrantMgr::breakOperationSequence()
311 {
312   myLastOperationId = "";
313   myLastFeature = FeaturePtr();
314   myRestartingMode = RM_None;
315 }
316
317 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
318 {
319   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
320   return aConnector->workshop();
321 }
322
323 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
324 {
325   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
326 }