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