]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReentrantMgr.cpp
Salome HOME
Issue #2024: Redesign of circle and arc of circle
[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 bool PartSet_SketcherReentrantMgr::isInternalEditStarted() const
393 {
394   return myIsInternalEditOperation;
395 }
396
397 bool PartSet_SketcherReentrantMgr::isActiveMgr() const
398 {
399   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
400
401   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
402   if (!anActive) {
403     anActive = module()->sketchMgr()->isNestedSketchOperation(aCurrentOperation);
404     if (anActive) { // the manager is not active when the current operation is a usual Edit
405       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
406                                                        (myWorkshop->currentOperation());
407       if (aFOperation->isEditOperation())
408         anActive = myIsInternalEditOperation;
409     }
410   }
411   return anActive;
412 }
413
414 bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
415 {
416   bool isDone = false;
417   /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
418   /// event comes two times, so we should not start another internal edit operation
419   /// the Apply button becomes disabled becase the second additional internal feature is created
420   if (myIsInternalEditOperation)
421     return true;
422
423   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
424                                                      (myWorkshop->currentOperation());
425
426   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
427     aFOperation->setEditOperation(true/*, false*/);
428     createInternalFeature();
429
430     myIsInternalEditOperation = true;
431     updateAcceptAllAction();
432
433     isDone = true;
434     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
435     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
436
437     // activate selection filters of the first widget in the viewer
438     onWidgetActivated();
439
440     // activate the last active widget in the Property Panel
441     if (!thePreviousAttributeID.empty()) {
442       ModuleBase_Operation* anEditOperation = module()->currentOperation();
443       if (anEditOperation) {
444         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
445         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
446         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
447         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
448           if (aWidgets[i]->attributeID() == thePreviousAttributeID) {
449           /// workaround for the same attributes used in different stacked widgets(attribute types)
450           if (ModuleBase_ToolBox::isOffToolBoxParent(aWidgets[i]))
451             continue;
452             aPreviousAttributeWidget = aWidgets[i];
453           }
454         }
455         // If the current widget is a selector, do nothing, it processes the mouse press
456         if (aPreviousAttributeWidget) {
457           if (!aPreviousAttributeWidget->isViewerSelector()) {
458             aPreviousAttributeWidget->focusTo();
459             aPreviousAttributeWidget->selectContent();
460           }
461           else {
462             // in case of shape multi selector, the widget does not lose focus by filling
463             // like it is in shape selector. So, if enter is pressed, the multi shape selector
464             // control should be deactivated. The focus is moved to Apply button and there
465             // should not be active control visualized in property panel
466             if (aPreviousAttributeWidget == aPanel->activeWidget()) {
467               aPanel->activateWidget(NULL, false);
468             }
469             // if there is no the next widget to be automatically activated,
470             // the Ok button in property
471             // panel should accept the focus(example is parallel constraint on sketch lines)
472             QToolButton* anOkBtn =
473               dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_OK);
474             if (anOkBtn)
475               anOkBtn->setFocus(Qt::TabFocusReason);
476           }
477         }
478       }
479     }
480   }
481   if (isDone)
482     module()->sketchMgr()->clearClickedFlags();
483
484   return isDone;
485 }
486
487 void PartSet_SketcherReentrantMgr::beforeStopInternalEdit()
488 {
489   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
490                                                       (myWorkshop->currentOperation());
491   if (aFOperation) {
492     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
493     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
494   }
495
496   deleteInternalFeature();
497 }
498
499 void PartSet_SketcherReentrantMgr::restartOperation()
500 {
501   if (myIsInternalEditOperation) {
502     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
503                                                                   myWorkshop->currentOperation());
504     if (aFOperation) {
505       // obtain widgets(attributes) which content should be applied to attributes of new feature
506       ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
507       ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
508       const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
509       QList<ModuleBase_ModelWidget*> aValueWidgets;
510       for (int i = 0, aSize = aWidgets.size(); i < aSize; i++) {
511         ModuleBase_ModelWidget* aWidget = aWidgets[i];
512         if (!aWidget->isModifiedInEdit()) {
513           aValueWidgets.append(aWidget);
514           // the widget is cashed to fill feature of new operation by the current widget value
515           // we set empty parent to the widget in order to remove it ourselves. Reason: restart
516           // operation will clear property panel and delete all widgets. This widget should be
517           // removed only after applying value of the widget to new created feature.
518           aWidget->setParent(0);
519         }
520       }
521
522       myNoMoreWidgetsAttribute = "";
523       myIsFlagsBlocked = true;
524       module()->launchOperation(aFOperation->id());
525       myIsFlagsBlocked = false;
526       resetFlags();
527       // we should avoid processing of the signal about no more widgets attributes and
528       // do this after the restart operaion is finished if it was called
529       // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
530       // if it should be called after restart
531       if (!myNoMoreWidgetsAttribute.empty()) {
532         onNoMoreWidgets(myNoMoreWidgetsAttribute);
533         myNoMoreWidgetsAttribute = "";
534       }
535
536       // filling new feature by the previous value of active widget
537       // (e.g. circle_type in macro Circle)
538       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
539                                                                 myWorkshop->currentOperation());
540       myIsValueChangedBlocked = true; // flag to avoid onAfterValuesChangedInPropertyPanel slot
541       for (int i = 0, aSize = aValueWidgets.size(); i < aSize; i++) {
542         ModuleBase_ModelWidget* aWidget = aValueWidgets[i];
543         aWidget->setEditingMode(false);
544         aWidget->setFeature(aFOperation->feature());
545         aWidget->storeValue();
546         // we must delete this widget
547         delete aWidget;
548       }
549       myIsValueChangedBlocked = false;
550     }
551   }
552 }
553
554 void PartSet_SketcherReentrantMgr::createInternalFeature()
555 {
556   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
557                                                      (myWorkshop->currentOperation());
558
559   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
560     FeaturePtr anOperationFeature = aFOperation->feature();
561
562     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
563     myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
564
565     bool isFeatureChanged = copyReetntrantAttributes(anOperationFeature, myInternalFeature,
566                                                      aSketch, false);
567     XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
568                                                   (aFOperation->propertyPanel());
569
570     myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
571     myInternalWidget->setVisible(false);
572
573     ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
574
575     QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
576     ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
577
578     aFactory.createWidget(anInternalPage);
579     QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
580
581     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
582       bool isStoreValue = !aFOperation->isEditOperation() &&
583                           !aWidget->getDefaultValue().empty() &&
584                           !aWidget->isComputedDefault();
585       aWidget->setFeature(myInternalFeature, isStoreValue);
586       if (!isStoreValue && isFeatureChanged)
587         aWidget->restoreValue();
588     }
589
590     ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
591                                                                                         (aWidgets);
592     if (aFirstWidget)
593       myInternalActiveWidget = aFirstWidget;
594   }
595 }
596
597 void PartSet_SketcherReentrantMgr::deleteInternalFeature()
598 {
599   if (myInternalActiveWidget) {
600     ModuleBase_WidgetSelector* aWSelector =
601       dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
602     if (aWSelector)
603       aWSelector->activateSelectionAndFilters(false);
604     myInternalActiveWidget = 0;
605   }
606   delete myInternalWidget;
607   myInternalWidget = 0;
608
609   QObjectPtrList anObjects;
610   anObjects.append(myInternalFeature);
611   workshop()->deleteFeatures(anObjects);
612   myInternalFeature = FeaturePtr();
613 }
614
615 void PartSet_SketcherReentrantMgr::resetFlags()
616 {
617   if (!myIsFlagsBlocked) {
618     myIsInternalEditOperation = false;
619     updateAcceptAllAction();
620     myRestartingMode = RM_None;
621   }
622 }
623
624 bool PartSet_SketcherReentrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
625                                                              const FeaturePtr& theNewFeature,
626                                                              const CompositeFeaturePtr& theSketch,
627                                                              const bool isTemporary)
628 {
629   bool aChanged = false;
630   if (!theSourceFeature.get() || !theSourceFeature->data().get() ||
631       !theSourceFeature->data()->isValid())
632     return aChanged;
633
634   std::string aFeatureKind = theSourceFeature->getKind();
635   if (aFeatureKind == SketchPlugin_Line::ID()) {
636     // Initialize new line with first point equal to end of previous
637     std::shared_ptr<ModelAPI_Data> aSFData = theSourceFeature->data();
638     std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
639                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
640     std::shared_ptr<ModelAPI_Data> aNFData = theNewFeature->data();
641     std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
642                                                  aNFData->attribute(SketchPlugin_Line::START_ID()));
643     aNPoint->setValue(aSPoint->x(), aSPoint->y());
644     PartSet_Tools::createConstraint(theSketch, aSPoint, aNPoint);
645
646     aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
647                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
648     aNPoint->setValue(aSPoint->x(), aSPoint->y());
649   }
650   else if (aFeatureKind == SketchPlugin_MacroCircle::ID()) {
651     // set circle type
652     std::string aTypeAttributeId = SketchPlugin_MacroCircle::CIRCLE_TYPE();
653     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
654     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
655     aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
656     //ModuleBase_Tools::flushUpdated(theNewFeature);
657     aChanged = true;
658   }
659   else if (aFeatureKind == SketchPlugin_MacroArc::ID()) {
660     // set arc type
661     std::string aTypeAttributeId = SketchPlugin_MacroArc::ARC_TYPE();
662     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
663     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
664     aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
665
666     //// if the arc is tangent, set coincidence to end point of the previous arc
667     //std::string anArcType = aSourceFeatureTypeAttr->value();
668     //if (anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT()) {
669     //  // get the last point of the previuos arc feature(geom point 2d)
670     //  std::shared_ptr<ModelAPI_Data> aSData = theSourceFeature->data();
671     //  std::shared_ptr<GeomDataAPI_Point2D> aSPointAttr =
672     //                                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
673     //                                  aSData->attribute(SketchPlugin_Arc::END_ID()));
674     //  // get point attribute on the current feature
675     //  AttributeRefAttrPtr aTangentPointAttr = theNewFeature->data()->refattr(
676     //                                                SketchPlugin_Arc::TANGENT_POINT_ID());
677     //  aTangentPointAttr->setAttr(aSPointAttr);
678
679     //  std::shared_ptr<GeomDataAPI_Point2D> aNPointAttr =
680     //                                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
681     //                                theNewFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
682     //  aNPointAttr->setValue(aSPointAttr->x(), aSPointAttr->y());
683
684     //}
685     //ModuleBase_Tools::flushUpdated(theNewFeature);
686     aChanged = true;
687   }
688   return aChanged;
689 }
690
691 bool PartSet_SketcherReentrantMgr::isTangentArc(ModuleBase_Operation* theOperation,
692                                                  const CompositeFeaturePtr& /*theSketch*/) const
693 {
694   bool aTangentArc = false;
695   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
696                                                                         (theOperation);
697   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
698     FeaturePtr aFeature = aFOperation->feature();
699     if (aFeature.get() && aFeature->getKind() == SketchPlugin_MacroArc::ID()) {
700       AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_MacroArc::ARC_TYPE());
701       std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
702       aTangentArc = anArcType == SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE();
703     }
704   }
705   return aTangentArc;
706 }
707
708 void PartSet_SketcherReentrantMgr::updateAcceptAllAction()
709 {
710   CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
711   if (aSketch.get())
712     workshop()->errorMgr()->updateAcceptAllAction(aSketch);
713 }
714
715 XGUI_Workshop* PartSet_SketcherReentrantMgr::workshop() const
716 {
717   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
718   return aConnector->workshop();
719 }
720
721 PartSet_Module* PartSet_SketcherReentrantMgr::module() const
722 {
723   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
724 }