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