Salome HOME
Issue #2071 Fatal error when Create box
[modules/shaper.git] / src / PartSet / PartSet_SketcherReentrantMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "PartSet_SketcherReentrantMgr.h"
4 #include "PartSet_Module.h"
5 #include "PartSet_SketcherMgr.h"
6 #include "PartSet_WidgetPoint2d.h"
7
8 #include "ModelAPI_Session.h"
9 #include "ModelAPI_AttributeString.h"
10 #include "ModelAPI_AttributeRefAttr.h"
11
12 #include "GeomDataAPI_Point2D.h"
13
14 #include <ModuleBase_IPropertyPanel.h>
15 #include <ModuleBase_OperationFeature.h>
16 #include <ModuleBase_ModelWidget.h>
17 #include <ModuleBase_ViewerPrs.h>
18 #include <ModuleBase_WidgetSelector.h>
19 #include <ModuleBase_PageWidget.h>
20 #include <ModuleBase_PageBase.h>
21 #include <ModuleBase_WidgetFactory.h>
22 #include <ModuleBase_OperationDescription.h>
23 #include "ModuleBase_ToolBox.h"
24 #include "ModuleBase_ISelection.h"
25
26 #include <SketchPlugin_Feature.h>
27 #include <SketchPlugin_Line.h>
28 #include <SketchPlugin_MacroArc.h>
29 #include <SketchPlugin_MacroCircle.h>
30 #include <SketchPlugin_Point.h>
31
32 #include <XGUI_Workshop.h>
33 #include <XGUI_ModuleConnector.h>
34 #include <XGUI_OperationMgr.h>
35 #include <XGUI_PropertyPanel.h>
36 #include <XGUI_ErrorMgr.h>
37 #include <XGUI_SelectionMgr.h>
38
39 #include <QToolButton>
40
41 PartSet_SketcherReentrantMgr::PartSet_SketcherReentrantMgr(ModuleBase_IWorkshop* theWorkshop)
42 : QObject(theWorkshop),
43   myWorkshop(theWorkshop),
44   myRestartingMode(RM_None),
45   myIsFlagsBlocked(false),
46   myIsInternalEditOperation(false),
47   myIsValueChangedBlocked(false),
48   myInternalActiveWidget(0),
49   myNoMoreWidgetsAttribute("")
50 {
51 }
52
53 PartSet_SketcherReentrantMgr::~PartSet_SketcherReentrantMgr()
54 {
55 }
56
57 ModuleBase_ModelWidget* PartSet_SketcherReentrantMgr::internalActiveWidget() const
58 {
59   ModuleBase_ModelWidget* aWidget = 0;
60   if (!isActiveMgr())
61     return aWidget;
62
63   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
64   if (anOperation) {
65     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
66     if (aPanel) { // check for case when the operation is started but property panel is not filled
67       ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
68       if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
69         aWidget = myInternalActiveWidget;
70     }
71   }
72   return aWidget;
73 }
74
75 bool PartSet_SketcherReentrantMgr::isInternalEditActive() const
76 {
77   return myIsInternalEditOperation;
78 }
79
80 void PartSet_SketcherReentrantMgr::updateInternalEditActiveState()
81 {
82   if  (myIsInternalEditOperation) {
83     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
84                                                          (myWorkshop->currentOperation());
85     if (aFOperation) {
86       FeaturePtr aFeature = aFOperation->feature();
87       QString anError = myWorkshop->module()->getFeatureError(aFeature);
88       // stop started internal edit operation as soon as the operation becomes invalid
89       // it is especially important for the sketch tangent arc feature
90       if (!anError.isEmpty()) {
91         aFOperation->setEditOperation(false);
92         //workshop()->operationMgr()->updateApplyOfOperations();
93         myIsInternalEditOperation = false;
94         updateAcceptAllAction();
95       }
96     }
97   }
98 }
99
100 bool PartSet_SketcherReentrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
101 {
102   bool aProcessed = false;
103   if (!isActiveMgr())
104     return aProcessed;
105
106   aProcessed = myIsInternalEditOperation;
107   resetFlags();
108
109   return aProcessed;
110 }
111
112 void PartSet_SketcherReentrantMgr::operationStarted(ModuleBase_Operation* theOperation)
113 {
114   if (!isActiveMgr())
115     return;
116
117   if (myPreviousFeature.get() && myRestartingMode == RM_LastFeatureUsed) {
118     ModuleBase_OperationFeature* aCurrentOperation = dynamic_cast<ModuleBase_OperationFeature*>(
119                                                                 myWorkshop->currentOperation());
120     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
121     copyReetntrantAttributes(myPreviousFeature, aCurrentOperation->feature(), aSketch);
122   }
123   resetFlags();
124 }
125
126 void PartSet_SketcherReentrantMgr::operationAborted(ModuleBase_Operation* theOperation)
127 {
128   if (!isActiveMgr())
129     return;
130
131   resetFlags();
132 }
133
134 bool PartSet_SketcherReentrantMgr::processMouseMoved(ModuleBase_IViewWindow* theWnd,
135                                                       QMouseEvent* theEvent)
136 {
137   bool aProcessed = false;
138   if (!isActiveMgr())
139     return aProcessed;
140
141   if  (myIsInternalEditOperation) {
142     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
143                                                          (myWorkshop->currentOperation());
144     FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature()
145                                                                      : FeaturePtr();
146     if (aLastFeature) {
147       ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
148       ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
149
150       FeaturePtr aCurrentFeature = aFOperation->feature();
151       bool isLineFeature = false, isArcFeature = false;
152       std::string anAttributeOnStart;
153       if (aCurrentFeature->getKind() == SketchPlugin_Line::ID()) {
154         anAttributeOnStart = SketchPlugin_Line::START_ID();
155         isLineFeature = anActiveWidget->attributeID() == anAttributeOnStart;
156       }
157       else if (isTangentArc(aFOperation, module()->sketchMgr()->activeSketch())) {
158         anAttributeOnStart = SketchPlugin_MacroArc::TANGENT_POINT_ID();
159         isArcFeature = anActiveWidget->attributeID() == anAttributeOnStart;
160       }
161       bool aCanBeActivatedByMove = isLineFeature || isArcFeature;
162       if (aCanBeActivatedByMove) {
163         /// before restarting of operation we need to clear selection, as it may take part in
164         /// new feature creation, e.g. tangent arc. But it is not necessary as it was processed
165         /// by mouse release when the operation was restarted.
166         workshop()->selector()->clearSelection();
167
168         myPreviousFeature = aFOperation->feature();
169         restartOperation();
170         myPreviousFeature = FeaturePtr();
171
172         anActiveWidget = module()->activeWidget();
173         aCurrentFeature = anActiveWidget->feature();
174         aProcessed = true;
175         if (anActiveWidget->attributeID() == anAttributeOnStart) {
176           // it was not deactivated by preselection processing
177           aPanel->activateNextWidget(anActiveWidget);
178         }
179       } else {
180         // processing mouse move in active widget of restarted operation
181         ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
182         PartSet_MouseProcessor* aProcessor = dynamic_cast<PartSet_MouseProcessor*>(anActiveWidget);
183         if (aProcessor)
184           aProcessor->mouseMoved(theWnd, theEvent);
185       }
186     }
187   }
188   return aProcessed;
189 }
190
191 bool PartSet_SketcherReentrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
192                                                         QMouseEvent* /* theEvent*/)
193 {
194   return isActiveMgr() && myIsInternalEditOperation;
195 }
196
197 bool PartSet_SketcherReentrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWnd,
198                                                          QMouseEvent* theEvent)
199 {
200   bool aProcessed = false;
201   if (!isActiveMgr())
202     return aProcessed;
203
204   if (myIsInternalEditOperation) {
205     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
206     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
207
208     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
209     if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
210
211       // block of viewer update
212       // we need to block update content of the viewer because of Sketch Point feature
213       // in activate() the value of the point is initialized and it can be displayed
214       // but the default value is [0, 0]. So, we block update viewer contentent until
215       // onMouseRelease happens, which correct the point position
216       ModuleBase_Tools::blockUpdateViewer(true);
217
218       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
219                                                            (myWorkshop->currentOperation());
220       myPreviousFeature = aFOperation->feature();
221
222       /// selection should be obtained from workshop before ask if the operation can be started as
223       /// the canStartOperation method performs commit/abort of previous operation.
224       /// Sometimes commit/abort
225       /// may cause selection clear(Sketch operation) as a result
226       /// it will be lost and is not used for preselection.
227       ModuleBase_ISelection* aSelection = myWorkshop->selection();
228       QList<ModuleBase_ViewerPrsPtr> aPreSelected =
229         aSelection->getSelected(ModuleBase_ISelection::AllControls);
230
231       restartOperation();
232       myPreviousFeature = FeaturePtr();
233       aProcessed = true;
234
235       // fill the first widget by the mouse event point
236       // if the active widget is not the first, it means that the restarted operation is filled by
237       // the current preselection.
238       PartSet_MouseProcessor* aMouseProcessor = dynamic_cast<PartSet_MouseProcessor*>(
239                                                                        module()->activeWidget());
240       //PartSet_WidgetPoint2D* aPoint2DWdg =
241       //  dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
242       PartSet_MouseProcessor* aFirstWidget = dynamic_cast<PartSet_MouseProcessor*>(
243                                                         aPanel->findFirstAcceptingValueWidget());
244       //if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
245       if (aMouseProcessor && aMouseProcessor == aFirstWidget) {
246         std::shared_ptr<ModuleBase_ViewerPrs> aSelectedPrs;
247         if (!aPreSelected.empty())
248           aSelectedPrs = aPreSelected.front();
249         aMouseProcessor->setPreSelection(aSelectedPrs, theWnd, theEvent);
250         //aPoint2DWdg->mouseReleased(theWnd, theEvent);
251         //if (!aPreSelected.empty())
252         //  aPoint2DWdg->setPreSelection(ModuleBase_ViewerPrsPtr());
253       }
254       // unblock viewer update
255       ModuleBase_Tools::blockUpdateViewer(false);
256     }
257   }
258
259   return aProcessed;
260 }
261
262 void PartSet_SketcherReentrantMgr::onWidgetActivated()
263 {
264   if (!isActiveMgr())
265     return;
266   if (!myIsInternalEditOperation)
267     return;
268
269   PartSet_Module* aModule = module();
270   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
271   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
272   if (aFirstWidget != aPanel->activeWidget()) {
273     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
274     if (aWSelector)
275       aWSelector->activateSelectionAndFilters(true);
276   }
277 }
278
279 void PartSet_SketcherReentrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
280 {
281   if (!isActiveMgr())
282     return;
283
284   // we should avoid processing of the signal about no more widgets attributes and
285   // do this after the restart operaion is finished if it was called
286   // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
287   // if it should be called after restart
288   if (myIsFlagsBlocked) {
289     myNoMoreWidgetsAttribute = thePreviousAttributeID;
290     return;
291   }
292
293   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
294                                                        (myWorkshop->currentOperation());
295   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
296     return;
297
298   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
299     bool isStarted = false;
300     if (!module()->sketchMgr()->sketchSolverError()) {
301       if (myRestartingMode != RM_Forbided) {
302         myRestartingMode = RM_LastFeatureUsed;
303         isStarted = startInternalEdit(thePreviousAttributeID);
304       }
305     }
306     if (!isStarted)
307       aFOperation->commit();
308   }
309 }
310
311 bool PartSet_SketcherReentrantMgr::processEnter(const std::string& thePreviousAttributeID)
312 {
313   bool isDone = false;
314
315   if (!isActiveMgr())
316     return isDone;
317
318   // empty previous attribute means that the Apply/Ok button has focus and the enter
319   // should not lead to start edition mode of the previous operation
320   if (thePreviousAttributeID.empty())
321     return isDone;
322
323   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
324                                                        (myWorkshop->currentOperation());
325   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
326     return isDone;
327
328   bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
329
330   if (!isSketchSolverError) {
331     myRestartingMode = RM_EmptyFeatureUsed;
332     isDone = startInternalEdit(thePreviousAttributeID);
333   }
334
335   return isDone;
336 }
337
338 void PartSet_SketcherReentrantMgr::onVertexSelected()
339 {
340   if (!isActiveMgr())
341     return;
342
343   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
344   std::string anOperationId = anOperation->id().toStdString();
345   if (anOperationId == SketchPlugin_Line::ID() ||
346       isTangentArc(anOperation, module()->sketchMgr()->activeSketch())) {
347     /// If last line finished on vertex the lines creation sequence has to be break
348     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
349     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
350     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
351     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
352     bool aFoundWidget = false;
353     bool aFoundObligatory = false;
354     for (; anIt != aLast && !aFoundObligatory; anIt++) {
355       if (!aFoundWidget)
356         aFoundWidget = *anIt == anActiveWidget;
357       else
358         aFoundObligatory = (*anIt)->isObligatory();
359     }
360     if (!aFoundObligatory)
361       myRestartingMode = RM_Forbided;
362   }
363 }
364
365 void PartSet_SketcherReentrantMgr::onAfterValuesChangedInPropertyPanel()
366 {
367   // blocked flag in order to avoid circling when storeValue will be applied in
368   // this method to cached widget
369   if (myIsValueChangedBlocked)
370     return;
371
372   if (isInternalEditActive()) {
373     ModuleBase_ModelWidget* aWidget = (ModuleBase_ModelWidget*)sender();
374     if (!aWidget->isModifiedInEdit())
375       restartOperation();
376   }
377 }
378
379 void PartSet_SketcherReentrantMgr::onBeforeStopped()
380 {
381   if (!isActiveMgr() || !myIsInternalEditOperation)
382     return;
383
384   beforeStopInternalEdit();
385 }
386
387 bool PartSet_SketcherReentrantMgr::canBeCommittedByPreselection()
388 {
389   return !isActiveMgr() || myRestartingMode == RM_None;
390 }
391
392 void PartSet_SketcherReentrantMgr::appendCreatedObjects(const std::set<ObjectPtr>& theObjects)
393 {
394   if (!myIsFlagsBlocked) // we need to collect objects only when launch operation is called
395     return;
396
397   for (std::set<ObjectPtr>::const_iterator anIt = theObjects.begin();
398        anIt != theObjects.end(); ++anIt) {
399     FeaturePtr aFeature = ModelAPI_Feature::feature(*anIt);
400     if (myCreatedFeatures.find(aFeature) != myCreatedFeatures.end())
401       myCreatedFeatures.insert(aFeature);
402   }
403 }
404
405 bool PartSet_SketcherReentrantMgr::isActiveMgr() const
406 {
407   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
408
409   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
410   if (!anActive) {
411     anActive = module()->sketchMgr()->isNestedSketchOperation(aCurrentOperation);
412     if (anActive) { // the manager is not active when the current operation is a usual Edit
413       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
414                                                        (myWorkshop->currentOperation());
415       if (aFOperation->isEditOperation())
416         anActive = myIsInternalEditOperation;
417     }
418   }
419   return anActive;
420 }
421
422 bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
423 {
424   bool isDone = false;
425   /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
426   /// event comes two times, so we should not start another internal edit operation
427   /// the Apply button becomes disabled becase the second additional internal feature is created
428   if (myIsInternalEditOperation)
429     return true;
430
431   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
432                                                      (myWorkshop->currentOperation());
433
434   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
435     aFOperation->setEditOperation(true/*, false*/);
436     createInternalFeature();
437
438     myIsInternalEditOperation = true;
439     updateAcceptAllAction();
440
441     isDone = true;
442     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
443     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
444
445     // activate selection filters of the first widget in the viewer
446     onWidgetActivated();
447
448     // activate the last active widget in the Property Panel
449     if (!thePreviousAttributeID.empty()) {
450       ModuleBase_Operation* anEditOperation = module()->currentOperation();
451       if (anEditOperation) {
452         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
453         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
454         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
455         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
456           if (aWidgets[i]->attributeID() == thePreviousAttributeID) {
457           /// workaround for the same attributes used in different stacked widgets(attribute types)
458           if (ModuleBase_ToolBox::isOffToolBoxParent(aWidgets[i]))
459             continue;
460             aPreviousAttributeWidget = aWidgets[i];
461           }
462         }
463         // If the current widget is a selector, do nothing, it processes the mouse press
464         if (aPreviousAttributeWidget) {
465           if (!aPreviousAttributeWidget->isViewerSelector()) {
466             aPreviousAttributeWidget->focusTo();
467             aPreviousAttributeWidget->selectContent();
468           }
469           else {
470             // in case of shape multi selector, the widget does not lose focus by filling
471             // like it is in shape selector. So, if enter is pressed, the multi shape selector
472             // control should be deactivated. The focus is moved to Apply button and there
473             // should not be active control visualized in property panel
474             if (aPreviousAttributeWidget == aPanel->activeWidget()) {
475               aPanel->activateWidget(NULL, false);
476             }
477             // if there is no the next widget to be automatically activated,
478             // the Ok button in property
479             // panel should accept the focus(example is parallel constraint on sketch lines)
480             QToolButton* anOkBtn =
481               dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_OK);
482             if (anOkBtn)
483               anOkBtn->setFocus(Qt::TabFocusReason);
484           }
485         }
486       }
487     }
488   }
489   if (isDone)
490     module()->sketchMgr()->clearClickedFlags();
491
492   return isDone;
493 }
494
495 void PartSet_SketcherReentrantMgr::beforeStopInternalEdit()
496 {
497   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
498                                                       (myWorkshop->currentOperation());
499   if (aFOperation) {
500     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
501     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
502   }
503
504   deleteInternalFeature();
505 }
506
507 void PartSet_SketcherReentrantMgr::restartOperation()
508 {
509   if (myIsInternalEditOperation) {
510     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
511                                                                   myWorkshop->currentOperation());
512     if (aFOperation) {
513       // obtain widgets(attributes) which content should be applied to attributes of new feature
514       ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
515       ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
516       const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
517       QList<ModuleBase_ModelWidget*> aValueWidgets;
518       for (int i = 0, aSize = aWidgets.size(); i < aSize; i++) {
519         ModuleBase_ModelWidget* aWidget = aWidgets[i];
520         if (!aWidget->isModifiedInEdit()) {
521           aValueWidgets.append(aWidget);
522           // the widget is cashed to fill feature of new operation by the current widget value
523           // we set empty parent to the widget in order to remove it ourselves. Reason: restart
524           // operation will clear property panel and delete all widgets. This widget should be
525           // removed only after applying value of the widget to new created feature.
526           aWidget->setParent(0);
527         }
528       }
529
530       myNoMoreWidgetsAttribute = "";
531       myIsFlagsBlocked = true;
532       module()->launchOperation(aFOperation->id());
533       myIsFlagsBlocked = false;
534       resetFlags();
535       // we should avoid processing of the signal about no more widgets attributes and
536       // do this after the restart operaion is finished if it was called
537       // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
538       // if it should be called after restart
539       if (!myNoMoreWidgetsAttribute.empty()) {
540         onNoMoreWidgets(myNoMoreWidgetsAttribute);
541         myNoMoreWidgetsAttribute = "";
542       }
543
544       // filling new feature by the previous value of active widget
545       // (e.g. circle_type in macro Circle)
546       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
547                                                                 myWorkshop->currentOperation());
548       myIsValueChangedBlocked = true; // flag to avoid onAfterValuesChangedInPropertyPanel slot
549       for (int i = 0, aSize = aValueWidgets.size(); i < aSize; i++) {
550         ModuleBase_ModelWidget* aWidget = aValueWidgets[i];
551         aWidget->setEditingMode(false);
552         aWidget->setFeature(aFOperation->feature());
553         aWidget->storeValue();
554         // we must delete this widget
555         delete aWidget;
556       }
557       myIsValueChangedBlocked = false;
558     }
559   }
560 }
561
562 void PartSet_SketcherReentrantMgr::createInternalFeature()
563 {
564   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
565                                                      (myWorkshop->currentOperation());
566
567   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
568     FeaturePtr anOperationFeature = aFOperation->feature();
569
570     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
571     myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
572
573     bool isFeatureChanged = copyReetntrantAttributes(anOperationFeature, myInternalFeature,
574                                                      aSketch, false);
575     XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
576                                                   (aFOperation->propertyPanel());
577
578     myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
579     myInternalWidget->setVisible(false);
580
581     ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
582
583     QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
584     ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
585
586     aFactory.createWidget(anInternalPage);
587     QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
588
589     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
590       bool isStoreValue = !aFOperation->isEditOperation() &&
591                           !aWidget->getDefaultValue().empty() &&
592                           !aWidget->isComputedDefault();
593       aWidget->setFeature(myInternalFeature, isStoreValue);
594       if (!isStoreValue && isFeatureChanged)
595         aWidget->restoreValue();
596     }
597
598     ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
599                                                                                         (aWidgets);
600     if (aFirstWidget)
601       myInternalActiveWidget = aFirstWidget;
602   }
603 }
604
605 void PartSet_SketcherReentrantMgr::deleteInternalFeature()
606 {
607   if (myInternalActiveWidget) {
608     ModuleBase_WidgetSelector* aWSelector =
609       dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
610     if (aWSelector)
611       aWSelector->activateSelectionAndFilters(false);
612     myInternalActiveWidget = 0;
613   }
614   delete myInternalWidget;
615   myInternalWidget = 0;
616
617   QObjectPtrList anObjects;
618   anObjects.append(myInternalFeature);
619   workshop()->deleteFeatures(anObjects);
620   myInternalFeature = FeaturePtr();
621 }
622
623 void PartSet_SketcherReentrantMgr::resetFlags()
624 {
625   if (!myIsFlagsBlocked) {
626     myIsInternalEditOperation = false;
627     updateAcceptAllAction();
628     myRestartingMode = RM_None;
629     myCreatedFeatures.clear();
630   }
631 }
632
633 bool PartSet_SketcherReentrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
634                                                              const FeaturePtr& theNewFeature,
635                                                              const CompositeFeaturePtr& theSketch,
636                                                              const bool isTemporary)
637 {
638   bool aChanged = false;
639   if (!theSourceFeature.get() || !theSourceFeature->data().get() ||
640       !theSourceFeature->data()->isValid())
641     return aChanged;
642
643   std::string aFeatureKind = theSourceFeature->getKind();
644   if (aFeatureKind == SketchPlugin_Line::ID()) {
645     // Initialize new line with first point equal to end of previous
646     std::shared_ptr<ModelAPI_Data> aSFData = theSourceFeature->data();
647     std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
648                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
649     std::shared_ptr<ModelAPI_Data> aNFData = theNewFeature->data();
650     std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
651                                                  aNFData->attribute(SketchPlugin_Line::START_ID()));
652     aNPoint->setValue(aSPoint->x(), aSPoint->y());
653     PartSet_Tools::createConstraint(theSketch, aSPoint, aNPoint);
654
655     aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
656                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
657     aNPoint->setValue(aSPoint->x(), aSPoint->y());
658   }
659   else if (aFeatureKind == SketchPlugin_MacroCircle::ID()) {
660     // set circle type
661     std::string aTypeAttributeId = SketchPlugin_MacroCircle::CIRCLE_TYPE();
662     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
663     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
664     aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
665     //ModuleBase_Tools::flushUpdated(theNewFeature);
666     aChanged = true;
667   }
668   else if (aFeatureKind == SketchPlugin_MacroArc::ID()) {
669     // set arc type
670     std::string aTypeAttributeId = SketchPlugin_MacroArc::ARC_TYPE();
671     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
672     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
673     aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
674
675     //// if the arc is tangent, set coincidence to end point of the previous arc
676     //std::string anArcType = aSourceFeatureTypeAttr->value();
677     //if (anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT()) {
678     //  // get the last point of the previuos arc feature(geom point 2d)
679     //  std::shared_ptr<ModelAPI_Data> aSData = theSourceFeature->data();
680     //  std::shared_ptr<GeomDataAPI_Point2D> aSPointAttr =
681     //                                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
682     //                                  aSData->attribute(SketchPlugin_Arc::END_ID()));
683     //  // get point attribute on the current feature
684     //  AttributeRefAttrPtr aTangentPointAttr = theNewFeature->data()->refattr(
685     //                                                SketchPlugin_Arc::TANGENT_POINT_ID());
686     //  aTangentPointAttr->setAttr(aSPointAttr);
687
688     //  std::shared_ptr<GeomDataAPI_Point2D> aNPointAttr =
689     //                                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
690     //                                theNewFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
691     //  aNPointAttr->setValue(aSPointAttr->x(), aSPointAttr->y());
692
693     //}
694     //ModuleBase_Tools::flushUpdated(theNewFeature);
695     aChanged = true;
696   }
697   return aChanged;
698 }
699
700 bool PartSet_SketcherReentrantMgr::isTangentArc(ModuleBase_Operation* theOperation,
701                                                  const CompositeFeaturePtr& /*theSketch*/) const
702 {
703   bool aTangentArc = false;
704   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
705                                                                         (theOperation);
706   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
707     FeaturePtr aFeature = aFOperation->feature();
708     if (aFeature.get() && aFeature->getKind() == SketchPlugin_MacroArc::ID()) {
709       AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_MacroArc::ARC_TYPE());
710       std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
711       aTangentArc = anArcType == SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE();
712     }
713   }
714   return aTangentArc;
715 }
716
717 void PartSet_SketcherReentrantMgr::updateAcceptAllAction()
718 {
719   CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
720   if (aSketch.get())
721     workshop()->errorMgr()->updateAcceptAllAction(aSketch);
722 }
723
724 XGUI_Workshop* PartSet_SketcherReentrantMgr::workshop() const
725 {
726   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
727   return aConnector->workshop();
728 }
729
730 PartSet_Module* PartSet_SketcherReentrantMgr::module() const
731 {
732   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
733 }