]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReetntrantMgr.cpp
Salome HOME
a7418714813f12a3975cf8019a36e8d01e786e29
[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   myRestartingMode(RM_None),
27   myIsFlagsBlocked(false),
28   myIsInternalEditOperation(false)
29 {
30 }
31
32 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
33 {
34 }
35
36 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
37 {
38   ModuleBase_ModelWidget* aWidget = 0;
39   if (!isActiveMgr())
40     return aWidget;
41
42   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
43   if (aOperation) {
44     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
45     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
46     if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector())) {
47       // finds the first widget which can accept a value
48       ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
49       if (aFirstWidget)
50         aWidget = aFirstWidget;
51     }
52   }
53   return aWidget;
54 }
55
56 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
57 {
58   return myIsInternalEditOperation;
59 }
60
61 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
62 {
63   bool aProcessed = false;
64   if (!isActiveMgr())
65     return aProcessed;
66
67   aProcessed = myIsInternalEditOperation;
68   resetFlags();
69
70   return aProcessed;
71 }
72
73 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
74 {
75   if (!isActiveMgr())
76     return;
77
78   resetFlags();
79 }
80
81 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
82 {
83   if (!isActiveMgr())
84     return;
85
86   resetFlags();
87 }
88
89 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
90                                                       QMouseEvent* /* theEvent*/)
91 {
92   bool aProcessed = false;
93   if (!isActiveMgr())
94     return aProcessed;
95
96   if  (myIsInternalEditOperation) {
97     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
98     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
99       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
100                                                          (myWorkshop->currentOperation());
101       FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
102       restartOperation();
103       aProcessed = true;
104
105       if (aLastFeature) {
106         ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
107         PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
108         if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
109           QList<ModuleBase_ViewerPrs> aSelection;
110           aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
111           if (aPoint2DWdg->setSelection(aSelection, true))
112             aPanel->activateNextWidget(aPoint2DWdg);
113         }
114       }
115     }
116   }
117   return aProcessed;
118 }
119
120 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
121                                                         QMouseEvent* /* theEvent*/)
122 {
123   return isActiveMgr() && myIsInternalEditOperation;
124 }
125
126 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
127                                                          QMouseEvent* theEvent)
128 {
129   bool aProcessed = false;
130   if (!isActiveMgr())
131     return aProcessed;
132
133   if (myIsInternalEditOperation) {
134     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
135     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
136
137     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
138     if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
139       restartOperation();
140       aProcessed = true;
141
142       // fill the first widget by the mouse event point
143       // if the active widget is not the first, it means that the restarted operation is filled by
144       // the current preselection.
145       PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
146       ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
147       if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
148         aPoint2DWdg->onMouseRelease(theWnd, theEvent);
149       }
150     }
151   }
152
153   return aProcessed;
154 }
155
156 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
157 {
158   if (!isActiveMgr())
159     return;
160   if (!myIsInternalEditOperation)
161     return;
162
163   PartSet_Module* aModule = module();
164   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
165   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
166   if (aFirstWidget != aPanel->activeWidget()) {
167     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
168     if (aWSelector)
169       aWSelector->activateSelectionAndFilters(true);
170   }
171 }
172
173 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
174 {
175   if (!isActiveMgr())
176     return;
177
178   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
179                                                        (myWorkshop->currentOperation());
180   if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
181     return;
182
183   if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
184     bool isStarted = false;
185     if (myRestartingMode != RM_Forbided) {
186       myRestartingMode = RM_LastFeatureUsed;
187       isStarted = startInternalEdit(thePreviousAttributeID);
188     }
189     if (!isStarted)
190       aFOperation->commit();
191   }
192 }
193
194 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
195 {
196   bool isDone = false;
197
198   if (!isActiveMgr())
199     return isDone;
200
201   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
202                                                        (myWorkshop->currentOperation());
203   if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
204     return isDone;
205
206   myRestartingMode = RM_EmptyFeatureUsed;
207   isDone = startInternalEdit(thePreviousAttributeID);
208
209   return isDone;
210 }
211
212 void PartSet_SketcherReetntrantMgr::onVertexSelected()
213 {
214   if (!isActiveMgr())
215     return;
216
217   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
218   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
219     /// If last line finished on vertex the lines creation sequence has to be break
220     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
221     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
222     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
223     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
224     bool aFoundWidget = false;
225     bool aFoundObligatory = false;
226     for (; anIt != aLast && !aFoundObligatory; anIt++) {
227       if (!aFoundWidget)
228         aFoundWidget = *anIt == anActiveWidget;
229       else
230         aFoundObligatory = (*anIt)->isObligatory();
231     }
232     if (!aFoundObligatory)
233       myRestartingMode = RM_Forbided;
234   }
235 }
236
237 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
238 {
239   if (!isActiveMgr() || !myIsInternalEditOperation)
240     return;
241
242   beforeStopInternalEdit();
243 }
244
245 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
246 {
247   return !isActiveMgr() || myRestartingMode == RM_None;
248 }
249
250 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
251 {
252   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
253
254   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
255   if (!anActive) {
256     anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
257     if (anActive) { // the manager is not active when the current operation is a usual Edit
258       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
259                                                        (myWorkshop->currentOperation());
260       if (aFOperation->isEditOperation())
261         anActive = myIsInternalEditOperation;
262     }
263   }
264   return anActive;
265 }
266
267 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
268 {
269   bool isDone = false;
270   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
271                                                      (myWorkshop->currentOperation());
272
273   if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
274     aFOperation->setEditOperation(true);
275     FeaturePtr anOperationFeature = aFOperation->feature();
276
277     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
278     myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
279
280     myIsInternalEditOperation = true;
281     isDone = true;
282     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
283     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
284
285     // activate selection filters of the first widget in the viewer
286     onWidgetActivated();
287
288     // activate the last active widget in the Property Panel
289     if (!thePreviousAttributeID.empty()) {
290       ModuleBase_Operation* anEditOperation = module()->currentOperation();
291       if (anEditOperation) {
292         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
293         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
294         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
295         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
296           if (aWidgets[i]->attributeID() == thePreviousAttributeID)
297             aPreviousAttributeWidget = aWidgets[i];
298         }
299         // If the current widget is a selector, do nothing, it processes the mouse press
300         if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
301           aPreviousAttributeWidget->focusTo();
302       }
303     }
304   }
305   return isDone;
306 }
307
308 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
309 {
310   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
311                                                       (myWorkshop->currentOperation());
312   if (aFOperation) {
313     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
314     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
315   }
316
317   QObjectPtrList anObjects;
318   anObjects.append(myInternalFeature);
319   workshop()->deleteFeatures(anObjects);
320
321   PartSet_Module* aModule = module();
322   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
323   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
324   if (aFirstWidget != aPanel->activeWidget()) {
325     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
326     if (aWSelector)
327       aWSelector->activateSelectionAndFilters(false);
328   }
329 }
330
331 void PartSet_SketcherReetntrantMgr::restartOperation()
332 {
333   if (myIsInternalEditOperation) {
334     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
335                                                                   myWorkshop->currentOperation());
336     if (aFOperation) {
337       myIsFlagsBlocked = true;
338       aFOperation->commit();
339       module()->launchOperation(aFOperation->id());
340       myIsFlagsBlocked = false;
341       resetFlags();
342     }
343   }
344 }
345
346 void PartSet_SketcherReetntrantMgr::resetFlags()
347 {
348   if (!myIsFlagsBlocked) {
349     myIsInternalEditOperation = false;
350     myRestartingMode = RM_None;
351   }
352 }
353
354 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
355 {
356   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
357   return aConnector->workshop();
358 }
359
360 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
361 {
362   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
363 }
364