Salome HOME
The sketch solver error should be checked after nested sketch operation check.
[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 #include <ModuleBase_PageWidget.h>
16 #include <ModuleBase_PageBase.h>
17 #include <ModuleBase_WidgetFactory.h>
18 #include <ModuleBase_OperationDescription.h>
19
20 #include <SketchPlugin_Feature.h>
21 #include <SketchPlugin_Line.h>
22
23 #include <XGUI_Workshop.h>
24 #include <XGUI_ModuleConnector.h>
25 #include <XGUI_OperationMgr.h>
26 #include <XGUI_PropertyPanel.h>
27
28 PartSet_SketcherReetntrantMgr::PartSet_SketcherReetntrantMgr(ModuleBase_IWorkshop* theWorkshop)
29 : QObject(theWorkshop),
30   myWorkshop(theWorkshop),
31   myRestartingMode(RM_None),
32   myIsFlagsBlocked(false),
33   myIsInternalEditOperation(false)
34 {
35 }
36
37 PartSet_SketcherReetntrantMgr::~PartSet_SketcherReetntrantMgr()
38 {
39 }
40
41 ModuleBase_ModelWidget* PartSet_SketcherReetntrantMgr::internalActiveWidget() const
42 {
43   ModuleBase_ModelWidget* aWidget = 0;
44   if (!isActiveMgr())
45     return aWidget;
46
47   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
48   if (aOperation) {
49     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
50     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
51     if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
52       aWidget = myInternalActiveWidget;
53   }
54   return aWidget;
55 }
56
57 bool PartSet_SketcherReetntrantMgr::isInternalEditActive() const
58 {
59   return myIsInternalEditOperation;
60 }
61
62 bool PartSet_SketcherReetntrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
63 {
64   bool aProcessed = false;
65   if (!isActiveMgr())
66     return aProcessed;
67
68   aProcessed = myIsInternalEditOperation;
69   resetFlags();
70
71   return aProcessed;
72 }
73
74 void PartSet_SketcherReetntrantMgr::operationStarted(ModuleBase_Operation* theOperation)
75 {
76   if (!isActiveMgr())
77     return;
78
79   resetFlags();
80 }
81
82 void PartSet_SketcherReetntrantMgr::operationAborted(ModuleBase_Operation* theOperation)
83 {
84   if (!isActiveMgr())
85     return;
86
87   resetFlags();
88 }
89
90 bool PartSet_SketcherReetntrantMgr::processMouseMoved(ModuleBase_IViewWindow* /* theWnd*/,
91                                                       QMouseEvent* /* theEvent*/)
92 {
93   bool aProcessed = false;
94   if (!isActiveMgr())
95     return aProcessed;
96
97   if  (myIsInternalEditOperation) {
98     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
99     if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
100       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
101                                                          (myWorkshop->currentOperation());
102       FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature() : FeaturePtr();
103       restartOperation();
104       aProcessed = true;
105
106       if (aLastFeature) {
107         ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
108         PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
109         if (aPoint2DWdg && aPoint2DWdg->canBeActivatedByMove()) {
110           QList<ModuleBase_ViewerPrs> aSelection;
111           aSelection.append(ModuleBase_ViewerPrs(aLastFeature, TopoDS_Shape(), NULL));
112           if (aPoint2DWdg->setSelection(aSelection, true))
113             aPanel->activateNextWidget(aPoint2DWdg);
114         }
115       }
116     }
117   }
118   return aProcessed;
119 }
120
121 bool PartSet_SketcherReetntrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
122                                                         QMouseEvent* /* theEvent*/)
123 {
124   return isActiveMgr() && myIsInternalEditOperation;
125 }
126
127 bool PartSet_SketcherReetntrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
128                                                          QMouseEvent* theEvent)
129 {
130   bool aProcessed = false;
131   if (!isActiveMgr())
132     return aProcessed;
133
134   if (myIsInternalEditOperation) {
135     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
136     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
137
138     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
139     if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
140       restartOperation();
141       aProcessed = true;
142
143       // fill the first widget by the mouse event point
144       // if the active widget is not the first, it means that the restarted operation is filled by
145       // the current preselection.
146       PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
147       ModuleBase_ModelWidget* aFirstWidget = aPanel->findFirstAcceptingValueWidget();
148       if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
149         aPoint2DWdg->onMouseRelease(theWnd, theEvent);
150       }
151     }
152   }
153
154   return aProcessed;
155 }
156
157 void PartSet_SketcherReetntrantMgr::onWidgetActivated()
158 {
159   if (!isActiveMgr())
160     return;
161   if (!myIsInternalEditOperation)
162     return;
163
164   PartSet_Module* aModule = module();
165   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
166   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
167   if (aFirstWidget != aPanel->activeWidget()) {
168     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
169     if (aWSelector)
170       aWSelector->activateSelectionAndFilters(true);
171   }
172 }
173
174 void PartSet_SketcherReetntrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
175 {
176   if (!isActiveMgr())
177     return;
178
179   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
180                                                        (myWorkshop->currentOperation());
181   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
182     return;
183
184   if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
185     bool isStarted = false;
186     if (!module()->sketchMgr()->sketchSolverError()) {
187       if (myRestartingMode != RM_Forbided) {
188         myRestartingMode = RM_LastFeatureUsed;
189         isStarted = startInternalEdit(thePreviousAttributeID);
190       }
191     }
192     if (!isStarted)
193       aFOperation->commit();
194   }
195 }
196
197 bool PartSet_SketcherReetntrantMgr::processEnter(const std::string& thePreviousAttributeID)
198 {
199   bool isDone = false;
200
201   if (!isActiveMgr())
202     return isDone;
203
204   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
205                                                        (myWorkshop->currentOperation());
206   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
207     return isDone;
208
209   bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
210   if (!isSketchSolverError) {
211     myRestartingMode = RM_EmptyFeatureUsed;
212     isDone = startInternalEdit(thePreviousAttributeID);
213   }
214
215   return isDone;
216 }
217
218 void PartSet_SketcherReetntrantMgr::onVertexSelected()
219 {
220   if (!isActiveMgr())
221     return;
222
223   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
224   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
225     /// If last line finished on vertex the lines creation sequence has to be break
226     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
227     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
228     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
229     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
230     bool aFoundWidget = false;
231     bool aFoundObligatory = false;
232     for (; anIt != aLast && !aFoundObligatory; anIt++) {
233       if (!aFoundWidget)
234         aFoundWidget = *anIt == anActiveWidget;
235       else
236         aFoundObligatory = (*anIt)->isObligatory();
237     }
238     if (!aFoundObligatory)
239       myRestartingMode = RM_Forbided;
240   }
241 }
242
243 void PartSet_SketcherReetntrantMgr::onBeforeStopped()
244 {
245   if (!isActiveMgr() || !myIsInternalEditOperation)
246     return;
247
248   beforeStopInternalEdit();
249 }
250
251 bool PartSet_SketcherReetntrantMgr::canBeCommittedByPreselection()
252 {
253   return !isActiveMgr() || myRestartingMode == RM_None;
254 }
255
256 bool PartSet_SketcherReetntrantMgr::isActiveMgr() const
257 {
258   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
259
260   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
261   if (!anActive) {
262     anActive = PartSet_SketcherMgr::isNestedSketchOperation(aCurrentOperation);
263     if (anActive) { // the manager is not active when the current operation is a usual Edit
264       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
265                                                        (myWorkshop->currentOperation());
266       if (aFOperation->isEditOperation())
267         anActive = myIsInternalEditOperation;
268     }
269   }
270   return anActive;
271 }
272
273 bool PartSet_SketcherReetntrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
274 {
275   bool isDone = false;
276   /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
277   /// event comes two times, so we should not start another internal edit operation
278   /// the Apply button becomes disabled becase the second additional internal feature is created
279   if (myIsInternalEditOperation)
280     return true;
281
282   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
283                                                      (myWorkshop->currentOperation());
284
285   if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
286     aFOperation->setEditOperation(false);
287     workshop()->operationMgr()->updateApplyOfOperations();
288
289     createInternalFeature();
290
291     myIsInternalEditOperation = true;
292     isDone = true;
293     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
294     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
295
296     // activate selection filters of the first widget in the viewer
297     onWidgetActivated();
298
299     // activate the last active widget in the Property Panel
300     if (!thePreviousAttributeID.empty()) {
301       ModuleBase_Operation* anEditOperation = module()->currentOperation();
302       if (anEditOperation) {
303         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
304         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
305         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
306         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
307           if (aWidgets[i]->attributeID() == thePreviousAttributeID)
308             aPreviousAttributeWidget = aWidgets[i];
309         }
310         // If the current widget is a selector, do nothing, it processes the mouse press
311         if (aPreviousAttributeWidget && !aPreviousAttributeWidget->isViewerSelector()) {
312           aPreviousAttributeWidget->focusTo();
313           aPreviousAttributeWidget->selectContent();
314         }
315       }
316     }
317   }
318   if (isDone)
319     module()->sketchMgr()->clearClickedFlags();
320
321   return isDone;
322 }
323
324 void PartSet_SketcherReetntrantMgr::beforeStopInternalEdit()
325 {
326   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
327                                                       (myWorkshop->currentOperation());
328   if (aFOperation) {
329     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
330     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
331   }
332
333   deleteInternalFeature();
334 }
335
336 void PartSet_SketcherReetntrantMgr::restartOperation()
337 {
338   if (myIsInternalEditOperation) {
339     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
340                                                                   myWorkshop->currentOperation());
341     if (aFOperation) {
342       myIsFlagsBlocked = true;
343       aFOperation->commit();
344       module()->launchOperation(aFOperation->id());
345       myIsFlagsBlocked = false;
346       resetFlags();
347     }
348   }
349 }
350
351 void PartSet_SketcherReetntrantMgr::createInternalFeature()
352 {
353   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
354                                                      (myWorkshop->currentOperation());
355
356   if (aFOperation && PartSet_SketcherMgr::isNestedSketchOperation(aFOperation)) {
357     FeaturePtr anOperationFeature = aFOperation->feature();
358
359     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
360     myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
361     XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
362                                                   (aFOperation->propertyPanel());
363
364     myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
365     myInternalWidget->setVisible(false);
366
367     ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
368
369     QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
370     ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
371
372     aFactory.createWidget(anInternalPage);
373     QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
374
375     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
376       aWidget->setFeature(myInternalFeature, true);
377     }
378     ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
379                                                                                         (aWidgets);
380     if (aFirstWidget)
381       myInternalActiveWidget = aFirstWidget;
382   }
383 }
384
385 void PartSet_SketcherReetntrantMgr::deleteInternalFeature()
386 {
387   if (myInternalActiveWidget) {
388     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
389     if (aWSelector)
390       aWSelector->activateSelectionAndFilters(false);
391     myInternalActiveWidget = 0;
392   }
393   delete myInternalWidget;
394   myInternalWidget = 0;
395
396   QObjectPtrList anObjects;
397   anObjects.append(myInternalFeature);
398   workshop()->deleteFeatures(anObjects);
399 }
400
401 void PartSet_SketcherReetntrantMgr::resetFlags()
402 {
403   if (!myIsFlagsBlocked) {
404     myIsInternalEditOperation = false;
405     myRestartingMode = RM_None;
406   }
407 }
408
409 XGUI_Workshop* PartSet_SketcherReetntrantMgr::workshop() const
410 {
411   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
412   return aConnector->workshop();
413 }
414
415 PartSet_Module* PartSet_SketcherReetntrantMgr::module() const
416 {
417   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
418 }
419