]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReetntrantMgr.cpp
Salome HOME
Cross cursor processing in the 'internal' edit operation.
[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) {
184     if (PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
185       if (myRestartingMode != RM_Forbided) {
186         myRestartingMode = RM_LastFeatureUsed;
187         startInternalEdit(thePreviousAttributeID);
188       }
189       else {
190         aFOperation->commit();
191       }
192     }
193   }
194 }
195
196 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
197 {
198   bool isDone = false;
199
200   if (!isActiveMgr())
201     return isDone;
202
203   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
204                                                        (myWorkshop->currentOperation());
205   if (!myWorkshop->module()->getFeatureError(aFOperation->feature(), false).isEmpty())
206     return isDone;
207
208   myRestartingMode = RM_EmptyFeatureUsed;
209   startInternalEdit(thePreviousAttributeID);
210   isDone = true;
211
212   return isDone;
213 }
214
215 void PartSet_SketcherReetntrantMgr::onVertexSelected()
216 {
217   if (!isActiveMgr())
218     return;
219
220   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
221   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
222     /// If last line finished on vertex the lines creation sequence has to be break
223     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
224     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
225     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
226     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
227     bool aFoundWidget = false;
228     bool aFoundObligatory = false;
229     for (; anIt != aLast && !aFoundObligatory; anIt++) {
230       if (!aFoundWidget)
231         aFoundWidget = *anIt == anActiveWidget;
232       else
233         aFoundObligatory = (*anIt)->isObligatory();
234     }
235     if (!aFoundObligatory)
236       myRestartingMode = RM_Forbided;
237   }
238 }
239
240 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
241 {
242   if (!isActiveMgr() || !myIsInternalEditOperation)
243     return;
244
245   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
246                                                       (myWorkshop->currentOperation());
247   if (aFOperation) {
248     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
249     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
250   }
251
252   PartSet_Module* aModule = module();
253   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
254   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
255   if (aFirstWidget != aPanel->activeWidget()) {
256     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
257     if (aWSelector)
258       aWSelector->activateSelectionAndFilters(false);
259   }
260 }
261
262 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
263 {
264   return !isActiveMgr() || myRestartingMode == RM_None;
265 }
266
267 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
268 {
269   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
270
271   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
272   if (!anActive) {
273     anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
274     if (anActive) { // the manager is not active when the current operation is a usual Edit
275       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
276                                                        (myWorkshop->currentOperation());
277       if (aFOperation->isEditOperation())
278         anActive = myIsInternalEditOperation;
279     }
280   }
281   return anActive;
282 }
283
284 void PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
285 {
286   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
287                                                      (myWorkshop->currentOperation());
288
289   aFOperation->setEditOperation(true);
290   FeaturePtr anOperationFeature = aFOperation->feature();
291   if (anOperationFeature.get() != NULL) {
292
293     myIsInternalEditOperation = true;
294     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
295     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
296
297     // activate selection filters of the first widget in the viewer
298     onWidgetActivated();
299
300     // activate the last active widget in the Property Panel
301     if (!thePreviousAttributeID.empty()) {
302       ModuleBase_Operation* anEditOperation = module()->currentOperation();
303       if (anEditOperation) {
304         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
305         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
306         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
307         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
308           if (aWidgets[i]->attributeID() == thePreviousAttributeID)
309             aPreviousAttributeWidget = aWidgets[i];
310         }
311         // If the current widget is a selector, do nothing, it processes the mouse press
312         if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector())
313           aPreviousAttributeWidget->focusTo();
314       }
315     }
316   }
317 }
318
319 void PartSet_SketcherReetntrantMgr::restartOperation()
320 {
321   if (myIsInternalEditOperation) {
322     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
323                                                                   myWorkshop->currentOperation());
324     if (aFOperation) {
325       myIsFlagsBlocked = true;
326       aFOperation->commit();
327       module()->launchOperation(aFOperation->id());
328       myIsFlagsBlocked = false;
329       resetFlags();
330     }
331   }
332 }
333
334 void PartSet_SketcherReetntrantMgr::resetFlags()
335 {
336   if (!myIsFlagsBlocked) {
337     myIsInternalEditOperation = false;
338     myRestartingMode = RM_None;
339   }
340 }
341
342 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
343 {
344   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
345   return aConnector->workshop();
346 }
347
348 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
349 {
350   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
351 }
352