Salome HOME
Issue #2420 Invalid sketch after creation second circle (reentrant operation) Jenkins...
[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 =
268             aPointWidget->getExternalObjectMgr()->getExternalObjectValidated();
269           // if external object is during reentrant operation and is used as a parameter of feature
270           // it should be removed after the operation is restarted. (Circle feature, Projection)
271           if (anExternalObject.get())
272             anExternalCreatedFeature = ModelAPI_Feature::feature(anExternalObject);
273         }
274       }
275
276       restartOperation();
277       // remove created external feature
278       if (anExternalCreatedFeature.get()) {
279         QObjectPtrList anObjects;
280         anObjects.append(anExternalCreatedFeature);
281         workshop()->deleteFeatures(anObjects);
282       }
283
284       myClickedSketchPoint = std::shared_ptr<GeomAPI_Pnt2d>();
285       mySelectedObject = ObjectPtr();
286       mySelectedAttribute = AttributePtr();
287
288       myPreviousFeature = FeaturePtr();
289       aProcessed = true;
290
291       // fill the first widget by the mouse event point
292       // if the active widget is not the first, it means that the restarted operation is filled by
293       // the current preselection.
294       PartSet_MouseProcessor* aMouseProcessor = dynamic_cast<PartSet_MouseProcessor*>(
295                                                                        module()->activeWidget());
296       //PartSet_WidgetPoint2D* aPoint2DWdg =
297       //  dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
298       PartSet_MouseProcessor* aFirstWidget = dynamic_cast<PartSet_MouseProcessor*>(
299                                                         aPanel->findFirstAcceptingValueWidget());
300       //if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
301       if (aMouseProcessor && aMouseProcessor == aFirstWidget) {
302         std::shared_ptr<ModuleBase_ViewerPrs> aSelectedPrs;
303         if (!aPreSelected.empty())
304           aSelectedPrs = aPreSelected.front();
305         if (aSelectedPrs.get() && aSelectedPrs->object().get()
306             && !aSelectedPrs->object()->data()->isValid()) {
307           // the selected object was removed diring restart, e.g. presentable macro feature
308           // there are created objects to replace the object depending on created feature kind
309           aSelectedPrs = std::shared_ptr<ModuleBase_ViewerPrs>();
310         }
311         aMouseProcessor->setPreSelection(aSelectedPrs, theWindow, theEvent);
312         //aPoint2DWdg->mouseReleased(theWindow, theEvent);
313         //if (!aPreSelected.empty())
314         //  aPoint2DWdg->setPreSelection(ModuleBase_ViewerPrsPtr());
315       }
316       // unblock viewer update
317       ModuleBase_Tools::blockUpdateViewer(false);
318     }
319   }
320   return aProcessed;
321 }
322
323 //******************************************************
324 void PartSet_SketcherReentrantMgr::setReentrantPreSelection(
325                                        const std::shared_ptr<Events_Message>& theMessage)
326 {
327   ReentrantMessagePtr aReentrantMessage =
328                       std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
329   if (!aReentrantMessage.get())
330     return;
331
332   // if feature has already filled the selected object, we should not overwrite it
333   if (!aReentrantMessage->selectedObject().get())
334     aReentrantMessage->setSelectedObject(mySelectedObject);
335
336   aReentrantMessage->setSelectedAttribute(mySelectedAttribute);
337   aReentrantMessage->setClickedPoint(myClickedSketchPoint);
338 }
339
340 //void PartSet_SketcherReentrantMgr::onWidgetActivated()
341 //{
342 //  if (!isActiveMgr())
343 //    return;
344 //  if (!myIsInternalEditOperation)
345 //    return;
346 //
347 //  PartSet_Module* aModule = module();
348 //  ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
349 //  ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
350 //  if (aFirstWidget != aPanel->activeWidget()) {
351 //    ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>
352 //      (aFirstWidget);
353 //    if (aWSelector) {
354 //      myWorkshop->selectionActivate()->updateSelectionModesAndFilters(aWSelector);
355 //    }
356 //  }
357 //}
358
359 void PartSet_SketcherReentrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
360 {
361 #ifdef DEBUG_RESTART
362   std::cout << "PartSet_SketcherReentrantMgr::onNoMoreWidgets" << std::endl;
363 #endif
364
365   if (!isActiveMgr())
366     return;
367
368   // we should avoid processing of the signal about no more widgets attributes and
369   // do this after the restart operaion is finished if it was called
370   // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
371   // if it should be called after restart
372   if (myIsFlagsBlocked) {
373     myNoMoreWidgetsAttribute = thePreviousAttributeID;
374     return;
375   }
376
377   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
378                                                        (myWorkshop->currentOperation());
379   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
380     return;
381
382   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
383     if (aFOperation->isNeedToBeAborted()) {
384       aFOperation->abort();
385     }
386     else {
387       bool isStarted = false;
388       if (!module()->sketchMgr()->sketchSolverError()) {
389         if (myRestartingMode != RM_Forbided) {
390           myRestartingMode = RM_LastFeatureUsed;
391           isStarted = startInternalEdit(thePreviousAttributeID);
392         }
393       }
394       if (!isStarted)
395         aFOperation->commit();
396     }
397   }
398 }
399
400 bool PartSet_SketcherReentrantMgr::processEnter(const std::string& thePreviousAttributeID)
401 {
402   bool isDone = false;
403
404   if (!isActiveMgr())
405     return isDone;
406
407   // empty previous attribute means that the Apply/Ok button has focus and the enter
408   // should not lead to start edition mode of the previous operation
409   if (thePreviousAttributeID.empty())
410     return isDone;
411
412   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
413                                                        (myWorkshop->currentOperation());
414   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
415     return isDone;
416
417   bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
418
419   if (!isSketchSolverError) {
420     myRestartingMode = RM_EmptyFeatureUsed;
421     isDone = startInternalEdit(thePreviousAttributeID);
422   }
423
424   return isDone;
425 }
426
427 void PartSet_SketcherReentrantMgr::onVertexSelected()
428 {
429   if (!isActiveMgr())
430     return;
431
432   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
433   std::string anOperationId = anOperation->id().toStdString();
434   if (anOperationId == SketchPlugin_Line::ID() ||
435       isTangentArc(anOperation, module()->sketchMgr()->activeSketch())) {
436     /// If last line finished on vertex the lines creation sequence has to be break
437     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
438     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
439     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
440     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
441     bool aFoundWidget = false;
442     bool aFoundObligatory = false;
443     for (; anIt != aLast && !aFoundObligatory; anIt++) {
444       if (!aFoundWidget)
445         aFoundWidget = *anIt == anActiveWidget;
446       else
447         aFoundObligatory = (*anIt)->isObligatory();
448     }
449     if (!aFoundObligatory)
450       myRestartingMode = RM_Forbided;
451   }
452 }
453
454 void PartSet_SketcherReentrantMgr::onAfterValuesChangedInPropertyPanel()
455 {
456
457   if (isInternalEditActive()) {
458     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
459                                                        (myWorkshop->currentOperation());
460     ModuleBase_ModelWidget* aWidget = (ModuleBase_ModelWidget*)sender();
461     if (!aWidget->isModifiedInEdit().empty())
462       restartOperation();
463   }
464 }
465
466 void PartSet_SketcherReentrantMgr::onBeforeStopped()
467 {
468   if (!isActiveMgr() || !myIsInternalEditOperation)
469     return;
470
471   beforeStopInternalEdit();
472 }
473
474 bool PartSet_SketcherReentrantMgr::canBeCommittedByPreselection()
475 {
476   return !isActiveMgr() || myRestartingMode == RM_None;
477 }
478
479 bool PartSet_SketcherReentrantMgr::isActiveMgr() const
480 {
481   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
482
483   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
484   if (!anActive) {
485     anActive = module()->sketchMgr()->isNestedSketchOperation(aCurrentOperation);
486     if (anActive) { // the manager is not active when the current operation is a usual Edit
487       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
488                                                        (myWorkshop->currentOperation());
489       if (aFOperation->isEditOperation())
490         anActive = myIsInternalEditOperation;
491     }
492   }
493   return anActive;
494 }
495
496 bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
497 {
498 #ifdef DEBUG_RESTART
499   std::cout << "PartSet_SketcherReentrantMgr::startInternalEdit" << std::endl;
500 #endif
501
502   bool isDone = false;
503   /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
504   /// event comes two times, so we should not start another internal edit operation
505   /// the Apply button becomes disabled becase the second additional internal feature is created
506   if (myIsInternalEditOperation)
507     return true;
508
509   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
510                                                      (myWorkshop->currentOperation());
511
512   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
513     /// improvement to deselect automatically all eventual selected objects, when
514     // returning to the neutral point of the Sketcher or start internal edit
515     workshop()->selector()->clearSelection();
516
517     aFOperation->setEditOperation(true/*, false*/);
518     createInternalFeature();
519
520     myIsInternalEditOperation = true;
521     updateAcceptAllAction();
522
523     isDone = true;
524     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
525     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
526
527     // activate selection filters of the first widget in the viewer
528     //onWidgetActivated();
529
530     // activate the last active widget in the Property Panel
531     if (!thePreviousAttributeID.empty()) {
532       ModuleBase_Operation* anEditOperation = module()->currentOperation();
533       if (anEditOperation) {
534         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
535         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
536         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
537         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
538           if (aWidgets[i]->attributeID() == thePreviousAttributeID) {
539           /// workaround for the same attributes used in different stacked widgets(attribute types)
540           if (ModuleBase_ToolBox::isOffToolBoxParent(aWidgets[i]))
541             continue;
542             aPreviousAttributeWidget = aWidgets[i];
543           }
544         }
545         // If the current widget is a selector, do nothing, it processes the mouse press
546         if (aPreviousAttributeWidget) {
547           if (!aPreviousAttributeWidget->isViewerSelector()) {
548             aPreviousAttributeWidget->focusTo();
549             aPreviousAttributeWidget->emitFocusInWidget();
550             aPreviousAttributeWidget->selectContent();
551           }
552           else {
553             // in case of shape multi selector, the widget does not lose focus by filling
554             // like it is in shape selector. So, if enter is pressed, the multi shape selector
555             // control should be deactivated. The focus is moved to Apply button and there
556             // should not be active control visualized in property panel
557             if (aPreviousAttributeWidget == aPanel->activeWidget()) {
558               aPanel->activateWidget(NULL, false);
559             }
560             // if there is no the next widget to be automatically activated,
561             // the Ok button in property
562             // panel should accept the focus(example is parallel constraint on sketch lines)
563             QToolButton* anOkBtn =
564               dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_OK);
565             if (anOkBtn)
566               ModuleBase_Tools::setFocus(anOkBtn, "XGUI_PropertyPanel::activateNextWidget");
567           }
568         }
569       }
570     }
571   }
572   if (isDone)
573     module()->sketchMgr()->clearClickedFlags();
574
575   return isDone;
576 }
577
578 void PartSet_SketcherReentrantMgr::beforeStopInternalEdit()
579 {
580   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
581                                                       (myWorkshop->currentOperation());
582   if (aFOperation) {
583     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
584     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
585   }
586
587   deleteInternalFeature();
588 }
589
590 void PartSet_SketcherReentrantMgr::restartOperation()
591 {
592 #ifdef DEBUG_RESTART
593   std::cout << "PartSet_SketcherReentrantMgr::restartOperation" << std::endl;
594 #endif
595
596   if (myIsInternalEditOperation) {
597     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
598                                                                   myWorkshop->currentOperation());
599     if (aFOperation) {
600       ModuleBase_ISelection* aSelection = myWorkshop->selection();
601       QList<ModuleBase_ViewerPrsPtr> aPreSelected =
602         aSelection->getSelected(ModuleBase_ISelection::AllControls);
603
604       if (myInternalFeature.get())
605         copyReetntrantAttributes(myInternalFeature, aFOperation->feature(),
606                                  module()->sketchMgr()->activeSketch());
607
608       myNoMoreWidgetsAttribute = "";
609       myIsFlagsBlocked = true;
610       /// launch has 'false' parameter to do not start new operation if the previous operation
611       /// is not committed. It is important for Line Sketch feature as it uses the previous
612       /// created feature parameter(to build coincidence), but by abort the previous is removed
613       module()->launchOperation(aFOperation->id(), true);
614       myIsFlagsBlocked = false;
615       resetFlags();
616
617       // we should avoid processing of the signal about no more widgets attributes and
618       // do this after the restart operaion is finished if it was called
619       // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
620       // if it should be called after restart
621       if (!myNoMoreWidgetsAttribute.empty()) {
622         onNoMoreWidgets(myNoMoreWidgetsAttribute);
623         myNoMoreWidgetsAttribute = "";
624       }
625     }
626   }
627 }
628
629 void PartSet_SketcherReentrantMgr::createInternalFeature()
630 {
631   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
632                                                      (myWorkshop->currentOperation());
633
634   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
635     FeaturePtr anOperationFeature = aFOperation->feature();
636
637     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
638     myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
639
640 #ifdef DEBUG_RESTART
641     std::cout << "PartSet_SketcherReentrantMgr::createInternalFeature: "
642               << myInternalFeature->data()->name() << std::endl;
643 #endif
644
645     bool isFeatureChanged = copyReetntrantAttributes(anOperationFeature, myInternalFeature,
646                                                      aSketch, false);
647     XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
648                                                   (aFOperation->propertyPanel());
649
650     myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
651     myInternalWidget->setVisible(false);
652
653     ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
654
655     QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
656     ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
657
658     aFactory.createWidget(anInternalPage);
659     QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
660
661     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
662       bool isStoreValue = !aFOperation->isEditOperation() &&
663                           !aWidget->getDefaultValue().empty() &&
664                           !aWidget->isComputedDefault();
665       aWidget->setFeature(myInternalFeature, isStoreValue);
666       if (!isStoreValue && isFeatureChanged)
667         aWidget->restoreValue();
668     }
669
670     ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
671                                                                                         (aWidgets);
672     if (aFirstWidget)
673       myInternalActiveWidget = aFirstWidget;
674   }
675 }
676
677 void PartSet_SketcherReentrantMgr::deleteInternalFeature()
678 {
679 #ifdef DEBUG_RESTART
680   std::cout << "PartSet_SketcherReentrantMgr::deleteInternalFeature: "
681             << myInternalFeature->data()->name() << std::endl;
682 #endif
683   if (myInternalActiveWidget)
684     myInternalActiveWidget = 0;
685   delete myInternalWidget;
686   myInternalWidget = 0;
687
688   QObjectPtrList anObjects;
689   anObjects.append(myInternalFeature);
690   workshop()->deleteFeatures(anObjects);
691   myInternalFeature = FeaturePtr();
692 }
693
694 void PartSet_SketcherReentrantMgr::resetFlags()
695 {
696   if (!myIsFlagsBlocked) {
697     myIsInternalEditOperation = false;
698     updateAcceptAllAction();
699     myRestartingMode = RM_None;
700     myReentrantMessage = std::shared_ptr<Events_Message>();
701   }
702 }
703
704 bool PartSet_SketcherReentrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
705                                                              const FeaturePtr& theNewFeature,
706                                                              const CompositeFeaturePtr& theSketch,
707                                                              const bool /*isTemporary*/)
708 {
709   bool aChanged = false;
710   if (!theSourceFeature.get() || !theSourceFeature->data().get() ||
711       !theSourceFeature->data()->isValid())
712     return aChanged;
713
714 #ifdef DEBUG_RESTART
715   std::cout << "PartSet_SketcherReentrantMgr::copyReetntrantAttributes from '"
716             << theSourceFeature->data()->name() << "' to '" << theNewFeature->data()->name()
717             << "'" << std::endl;
718 #endif
719
720   std::string aFeatureKind = theSourceFeature->getKind();
721   /*if (aFeatureKind == SketchPlugin_Line::ID()) {
722     // Initialize new line with first point equal to end of previous
723     std::shared_ptr<ModelAPI_Data> aSFData = theSourceFeature->data();
724     std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
725                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
726     std::shared_ptr<ModelAPI_Data> aNFData = theNewFeature->data();
727     std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
728                                                  aNFData->attribute(SketchPlugin_Line::START_ID()));
729     aNPoint->setValue(aSPoint->x(), aSPoint->y());
730     PartSet_Tools::createConstraint(theSketch, aSPoint, aNPoint);
731
732     aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
733                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
734     aNPoint->setValue(aSPoint->x(), aSPoint->y());
735   }
736   else*/ if (aFeatureKind == SketchPlugin_MacroCircle::ID()) {
737     // set circle type
738     /*std::string aTypeAttributeId = SketchPlugin_MacroCircle::CIRCLE_TYPE();
739     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
740     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
741     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
742       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
743     //ModuleBase_Tools::flushUpdated(theNewFeature);*/
744     //aChanged = true;
745   }
746   else if (aFeatureKind == SketchPlugin_MacroArc::ID()) {
747     // set arc type
748     /*std::string aTypeAttributeId = SketchPlugin_MacroArc::ARC_TYPE();
749     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
750     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
751     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
752       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());*/
753     //// if the arc is tangent, set coincidence to end point of the previous arc
754     //std::string anArcType = aSourceFeatureTypeAttr->value();
755     //if (anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT()) {
756     //  // get the last point of the previuos arc feature(geom point 2d)
757     //  std::shared_ptr<ModelAPI_Data> aSData = theSourceFeature->data();
758     //  std::shared_ptr<GeomDataAPI_Point2D> aSPointAttr =
759     //                                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
760     //                                  aSData->attribute(SketchPlugin_Arc::END_ID()));
761     //  // get point attribute on the current feature
762     //  AttributeRefAttrPtr aTangentPointAttr = theNewFeature->data()->refattr(
763     //                                                SketchPlugin_Arc::TANGENT_POINT_ID());
764     //  aTangentPointAttr->setAttr(aSPointAttr);
765
766     //  std::shared_ptr<GeomDataAPI_Point2D> aNPointAttr =
767     //                                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
768     //                                theNewFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
769     //  aNPointAttr->setValue(aSPointAttr->x(), aSPointAttr->y());
770
771     //}
772     //ModuleBase_Tools::flushUpdated(theNewFeature);
773     //aChanged = true;
774   }
775   else if (aFeatureKind == SketchPlugin_Trim::ID() ||
776            aFeatureKind == SketchPlugin_Split::ID()) {
777     std::string aPreviewObjectAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
778                 SketchPlugin_Trim::PREVIEW_OBJECT(): SketchPlugin_Split::PREVIEW_OBJECT();
779     std::string aPreviewPointAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
780                 SketchPlugin_Trim::PREVIEW_POINT(): SketchPlugin_Split::PREVIEW_POINT();
781     std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
782                       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
783                       theSourceFeature->data()->attribute(aPreviewObjectAttribute));
784     std::shared_ptr<ModelAPI_AttributeReference> aNRefPreviewAttr =
785                         std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
786                         theNewFeature->data()->attribute(aPreviewObjectAttribute));
787     aNRefPreviewAttr->setValue(aRefPreviewAttr->value());
788     std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
789                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
790                       theSourceFeature->data()->attribute(aPreviewPointAttribute));
791     std::shared_ptr<GeomDataAPI_Point2D> aNPointPreviewAttr =
792                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
793                       theNewFeature->data()->attribute(aPreviewPointAttribute));
794     aNPointPreviewAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
795   }
796   return aChanged;
797 }
798
799 bool PartSet_SketcherReentrantMgr::isTangentArc(ModuleBase_Operation* theOperation,
800                                                  const CompositeFeaturePtr& /*theSketch*/) const
801 {
802   bool aTangentArc = false;
803   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
804                                                                         (theOperation);
805   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
806     FeaturePtr aFeature = aFOperation->feature();
807     if (aFeature.get() && aFeature->getKind() == SketchPlugin_MacroArc::ID()) {
808       AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_MacroArc::ARC_TYPE());
809       std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
810       aTangentArc = anArcType == SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE();
811     }
812   }
813   return aTangentArc;
814 }
815
816 void PartSet_SketcherReentrantMgr::updateAcceptAllAction()
817 {
818   CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
819   if (aSketch.get())
820     workshop()->errorMgr()->updateAcceptAllAction(aSketch);
821 }
822
823 XGUI_Workshop* PartSet_SketcherReentrantMgr::workshop() const
824 {
825   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
826   return aConnector->workshop();
827 }
828
829 PartSet_Module* PartSet_SketcherReentrantMgr::module() const
830 {
831   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
832 }