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