Salome HOME
Issue #3232: Delete automatic constraints if they cause conflicts
[modules/shaper.git] / src / PartSet / PartSet_SketcherReentrantMgr.cpp
1 // Copyright (C) 2014-2019  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 email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_SketcherReentrantMgr.h"
21 #include "PartSet_ExternalObjectsMgr.h"
22 #include "PartSet_Module.h"
23 #include "PartSet_SketcherMgr.h"
24 #include "PartSet_WidgetPoint2d.h"
25
26 #include "ModelAPI_Session.h"
27 #include "ModelAPI_AttributeString.h"
28 #include "ModelAPI_AttributeRefAttr.h"
29 #include "ModelAPI_AttributeReference.h"
30 #include "ModelAPI_EventReentrantMessage.h"
31
32 #include "GeomDataAPI_Point2D.h"
33
34 #include "GeomAPI_Lin2d.h"
35 #include "GeomAPI_Dir2d.h"
36
37 #include <ModuleBase_IPropertyPanel.h>
38 #include <ModuleBase_ISelectionActivate.h>
39 #include <ModuleBase_OperationFeature.h>
40 #include <ModuleBase_ModelWidget.h>
41 #include <ModuleBase_ViewerPrs.h>
42 #include <ModuleBase_WidgetSelector.h>
43 #include <ModuleBase_PageWidget.h>
44 #include <ModuleBase_PageBase.h>
45 #include <ModuleBase_WidgetFactory.h>
46 #include <ModuleBase_OperationDescription.h>
47 #include "ModuleBase_ToolBox.h"
48 #include "ModuleBase_ISelection.h"
49
50 #include <SketchPlugin_Feature.h>
51 #include <SketchPlugin_Line.h>
52 #include <SketchPlugin_MacroArc.h>
53 #include <SketchPlugin_MacroCircle.h>
54 #include <SketchPlugin_Point.h>
55 #include <SketchPlugin_Trim.h>
56 #include <SketchPlugin_Split.h>
57 #include <SketchPlugin_ConstraintHorizontal.h>
58 #include <SketchPlugin_ConstraintVertical.h>
59
60 #include <XGUI_Workshop.h>
61 #include <XGUI_ModuleConnector.h>
62 #include <XGUI_OperationMgr.h>
63 #include <XGUI_PropertyPanel.h>
64 #include <XGUI_ErrorMgr.h>
65 #include <XGUI_SelectionMgr.h>
66
67 #include <QToolButton>
68
69 //#define DEBUG_RESTART
70
71 PartSet_SketcherReentrantMgr::PartSet_SketcherReentrantMgr(ModuleBase_IWorkshop* theWorkshop)
72 : QObject(theWorkshop),
73   myWorkshop(theWorkshop),
74   myRestartingMode(RM_None),
75   myIsFlagsBlocked(false),
76   myIsInternalEditOperation(false),
77   myNoMoreWidgetsAttribute(""),
78   myIsAutoConstraints(true),
79   myLastAutoConstraint(0)
80 {
81 }
82
83 PartSet_SketcherReentrantMgr::~PartSet_SketcherReentrantMgr()
84 {
85 }
86
87 bool PartSet_SketcherReentrantMgr::isInternalEditActive() const
88 {
89   return myIsInternalEditOperation;
90 }
91
92 void PartSet_SketcherReentrantMgr::updateInternalEditActiveState()
93 {
94   if  (myIsInternalEditOperation) {
95     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
96                                                          (myWorkshop->currentOperation());
97     if (aFOperation) {
98       FeaturePtr aFeature = aFOperation->feature();
99       QString anError = myWorkshop->module()->getFeatureError(aFeature);
100       // stop started internal edit operation as soon as the operation becomes invalid
101       // it is especially important for the sketch tangent arc feature
102       if (!anError.isEmpty()) {
103         aFOperation->setEditOperation(false);
104         //workshop()->operationMgr()->updateApplyOfOperations();
105         myIsInternalEditOperation = false;
106         updateAcceptAllAction();
107       }
108     }
109   }
110 }
111
112 bool PartSet_SketcherReentrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
113 {
114   bool aProcessed = false;
115   if (!isActiveMgr())
116     return aProcessed;
117
118   aProcessed = myIsInternalEditOperation;
119   resetFlags();
120
121   return aProcessed;
122 }
123
124 void PartSet_SketcherReentrantMgr::operationStarted(ModuleBase_Operation* theOperation)
125 {
126   if (!isActiveMgr())
127     return;
128
129   //if (myPreviousFeature.get() && myRestartingMode == RM_LastFeatureUsed) {
130     //ModuleBase_OperationFeature* aCurrentOperation = dynamic_cast<ModuleBase_OperationFeature*>(
131     //                                                            myWorkshop->currentOperation());
132     //CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
133     //if (myPreviousFeature.get() && myPreviousFeature->data()->isValid()) // it is not removed
134       //copyReetntrantAttributes(myPreviousFeature, aCurrentOperation->feature(), aSketch);
135   //}
136   resetFlags();
137 }
138
139 void PartSet_SketcherReentrantMgr::operationAborted(ModuleBase_Operation* theOperation)
140 {
141   if (!isActiveMgr())
142     return;
143
144   resetFlags();
145 }
146
147 bool PartSet_SketcherReentrantMgr::processMouseMoved(ModuleBase_IViewWindow* theWnd,
148                                                       QMouseEvent* theEvent)
149 {
150   bool aProcessed = false;
151   if (!isActiveMgr())
152     return aProcessed;
153
154   if  (myIsInternalEditOperation) {
155     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
156                                                          (myWorkshop->currentOperation());
157     FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature()
158                                                                      : FeaturePtr();
159     if (aLastFeature) {
160       ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
161       ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
162
163       FeaturePtr aCurrentFeature = aFOperation->feature();
164       bool isLineFeature = false, isReentrantArcFeature = false;
165       std::string anAttributeOnStart;
166       if (aCurrentFeature->getKind() == SketchPlugin_Line::ID()) {
167         anAttributeOnStart = SketchPlugin_Line::START_ID();
168         isLineFeature = anActiveWidget->attributeID() == anAttributeOnStart;
169       }
170       else if (isTangentArc(aFOperation, module()->sketchMgr()->activeSketch())) {
171         isReentrantArcFeature = true;
172       }
173       bool aCanBeActivatedByMove = isLineFeature || isReentrantArcFeature;
174       if (aCanBeActivatedByMove) {
175         /// before restarting of operation we need to clear selection, as it may take part in
176         /// new feature creation, e.g. tangent arc. But it is not necessary as it was processed
177         /// by mouse release when the operation was restarted.
178         workshop()->selector()->clearSelection();
179
180         myPreviousFeature = aFOperation->feature();
181         restartOperation();
182         myPreviousFeature = FeaturePtr();
183
184         anActiveWidget = module()->activeWidget();
185         aProcessed = true;
186         if (anActiveWidget && anActiveWidget->attributeID() == anAttributeOnStart) {
187           // it was not deactivated by preselection processing
188           aPanel->activateNextWidget(anActiveWidget);
189         }
190       } else {
191         // processing mouse move in active widget of restarted operation
192         ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
193         PartSet_MouseProcessor* aProcessor = dynamic_cast<PartSet_MouseProcessor*>(anActiveWidget);
194         if (aProcessor)
195           aProcessor->mouseMoved(theWnd, theEvent);
196       }
197     }
198   }
199   return aProcessed;
200 }
201
202 bool PartSet_SketcherReentrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
203                                                         QMouseEvent* /* theEvent*/)
204 {
205   return isActiveMgr() && myIsInternalEditOperation;
206 }
207
208 bool PartSet_SketcherReentrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWindow,
209                                                         QMouseEvent* theEvent)
210 {
211   bool aProcessed = false;
212   if (!isActiveMgr())
213     return aProcessed;
214
215   if (myIsInternalEditOperation) {
216     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
217     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
218
219     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
220     if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
221
222       // block of viewer update
223       // we need to block update content of the viewer because of Sketch Point feature
224       // in activate() the value of the point is initialized and it can be displayed
225       // but the default value is [0, 0]. So, we block update viewer contentent until
226       // onMouseRelease happens, which correct the point position
227       ModuleBase_Tools::blockUpdateViewer(true);
228
229       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
230                                                            (myWorkshop->currentOperation());
231       myPreviousFeature = aFOperation->feature();
232
233       /// selection should be obtained from workshop before ask if the operation can be started as
234       /// the canStartOperation method performs commit/abort of previous operation.
235       /// Sometimes commit/abort
236       /// may cause selection clear(Sketch operation) as a result
237       /// it will be lost and is not used for preselection.
238       ModuleBase_ISelection* aSelection = myWorkshop->selection();
239       QList<ModuleBase_ViewerPrsPtr> aPreSelected =
240         aSelection->getSelected(ModuleBase_ISelection::AllControls);
241
242       myClickedSketchPoint = PartSet_Tools::getPnt2d(theEvent, theWindow,
243                                                      module()->sketchMgr()->activeSketch());
244       FeaturePtr anExternalCreatedFeature;
245       if (!aPreSelected.empty()) {
246         ModuleBase_ViewerPrsPtr aValue = aPreSelected.first();
247         module()->getGeomSelection(aValue, mySelectedObject, mySelectedAttribute);
248
249         PartSet_WidgetPoint2D* aPointWidget = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
250         if (aPointWidget) {
251           GeomShapePtr aShape;
252           aPointWidget->getGeomSelection_(aValue, mySelectedObject, aShape);
253           ObjectPtr anExternalObject =
254             aPointWidget->getExternalObjectMgr()->getExternalObjectValidated();
255           // if external object is during reentrant operation and is used as a parameter of feature
256           // it should be removed after the operation is restarted. (Circle feature, Projection)
257           if (anExternalObject.get())
258             anExternalCreatedFeature = ModelAPI_Feature::feature(anExternalObject);
259         }
260       }
261
262       restartOperation();
263       // remove created external feature
264       if (anExternalCreatedFeature.get()) {
265         QObjectPtrList anObjects;
266         anObjects.append(anExternalCreatedFeature);
267         workshop()->deleteFeatures(anObjects);
268       }
269
270       myClickedSketchPoint = std::shared_ptr<GeomAPI_Pnt2d>();
271       mySelectedObject = ObjectPtr();
272       mySelectedAttribute = AttributePtr();
273
274       myPreviousFeature = FeaturePtr();
275       aProcessed = true;
276
277       // fill the first widget by the mouse event point
278       // if the active widget is not the first, it means that the restarted operation is filled by
279       // the current preselection.
280       PartSet_MouseProcessor* aMouseProcessor = dynamic_cast<PartSet_MouseProcessor*>(
281                                                                        module()->activeWidget());
282       //PartSet_WidgetPoint2D* aPoint2DWdg =
283       //  dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
284       PartSet_MouseProcessor* aFirstWidget = dynamic_cast<PartSet_MouseProcessor*>(
285                                                         aPanel->findFirstAcceptingValueWidget());
286       //if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
287       if (aMouseProcessor && aMouseProcessor == aFirstWidget) {
288         std::shared_ptr<ModuleBase_ViewerPrs> aSelectedPrs;
289         if (!aPreSelected.empty())
290           aSelectedPrs = aPreSelected.front();
291         if (aSelectedPrs.get() && aSelectedPrs->object().get()
292             && !aSelectedPrs->object()->data()->isValid()) {
293           // the selected object was removed diring restart, e.g. presentable macro feature
294           // there are created objects to replace the object depending on created feature kind
295           aSelectedPrs = std::shared_ptr<ModuleBase_ViewerPrs>();
296         }
297         aMouseProcessor->setPreSelection(aSelectedPrs, theWindow, theEvent);
298         //aPoint2DWdg->mouseReleased(theWindow, theEvent);
299         //if (!aPreSelected.empty())
300         //  aPoint2DWdg->setPreSelection(ModuleBase_ViewerPrsPtr());
301       }
302       // unblock viewer update
303       ModuleBase_Tools::blockUpdateViewer(false);
304     }
305   }
306   return aProcessed;
307 }
308
309 //******************************************************
310 void PartSet_SketcherReentrantMgr::setReentrantPreSelection(
311                                        const std::shared_ptr<Events_Message>& theMessage)
312 {
313   ReentrantMessagePtr aReentrantMessage =
314                       std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
315   if (!aReentrantMessage.get())
316     return;
317
318   // if feature has already filled the selected object, we should not overwrite it
319   if (!aReentrantMessage->selectedObject().get())
320     aReentrantMessage->setSelectedObject(mySelectedObject);
321
322   aReentrantMessage->setSelectedAttribute(mySelectedAttribute);
323   aReentrantMessage->setClickedPoint(myClickedSketchPoint);
324 }
325
326 //void PartSet_SketcherReentrantMgr::onWidgetActivated()
327 //{
328 //  if (!isActiveMgr())
329 //    return;
330 //  if (!myIsInternalEditOperation)
331 //    return;
332 //
333 //  PartSet_Module* aModule = module();
334 //  ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
335 //  ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
336 //  if (aFirstWidget != aPanel->activeWidget()) {
337 //    ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>
338 //      (aFirstWidget);
339 //    if (aWSelector) {
340 //      myWorkshop->selectionActivate()->updateSelectionModesAndFilters(aWSelector);
341 //    }
342 //  }
343 //}
344
345 void PartSet_SketcherReentrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
346 {
347 #ifdef DEBUG_RESTART
348   std::cout << "PartSet_SketcherReentrantMgr::onNoMoreWidgets" << std::endl;
349 #endif
350
351   if (!isActiveMgr())
352     return;
353
354   // we should avoid processing of the signal about no more widgets attributes and
355   // do this after the restart operaion is finished if it was called
356   // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
357   // if it should be called after restart
358   if (myIsFlagsBlocked) {
359     myNoMoreWidgetsAttribute = thePreviousAttributeID;
360     return;
361   }
362
363   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
364                                                        (myWorkshop->currentOperation());
365   if (module()->sketchMgr()->isDragModeCreation()) {
366     if (aFOperation && myIsAutoConstraints)
367       addConstraints(aFOperation->feature());
368     return;
369   }
370   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
371     return;
372
373   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
374     if (aFOperation->isNeedToBeAborted()) {
375       aFOperation->abort();
376     }
377     else {
378       bool isStarted = false;
379       if (!module()->sketchMgr()->sketchSolverError()) {
380         if (myRestartingMode != RM_Forbided) {
381           myRestartingMode = RM_LastFeatureUsed;
382           isStarted = startInternalEdit(thePreviousAttributeID);
383         }
384       }
385       if (!isStarted) {
386         if (myIsAutoConstraints)
387           addConstraints(aFOperation->feature());
388         aFOperation->commit();
389       }
390     }
391   }
392 }
393
394 bool PartSet_SketcherReentrantMgr::processEnter(const std::string& thePreviousAttributeID)
395 {
396   bool isDone = false;
397
398   if (!isActiveMgr())
399     return isDone;
400
401   // empty previous attribute means that the Apply/Ok button has focus and the enter
402   // should not lead to start edition mode of the previous operation
403   if (thePreviousAttributeID.empty())
404     return isDone;
405
406   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
407                                                        (myWorkshop->currentOperation());
408   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
409     return isDone;
410
411   bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
412
413   if (!isSketchSolverError) {
414     myRestartingMode = RM_EmptyFeatureUsed;
415     // It seems that the call is obsolete for Enter key processing
416     // It prevent finalysing of the current operation by Enter key
417     //isDone = startInternalEdit(thePreviousAttributeID);
418   }
419
420   return isDone;
421 }
422
423 void PartSet_SketcherReentrantMgr::onVertexSelected()
424 {
425   if (!isActiveMgr())
426     return;
427
428   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
429   std::string anOperationId = anOperation->id().toStdString();
430   if (anOperationId == SketchPlugin_Line::ID() ||
431       isTangentArc(anOperation, module()->sketchMgr()->activeSketch())) {
432     /// If last line finished on vertex the lines creation sequence has to be break
433     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
434     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
435     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
436     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
437     bool aFoundWidget = false;
438     bool aFoundObligatory = false;
439     for (; anIt != aLast && !aFoundObligatory; anIt++) {
440       if (!aFoundWidget)
441         aFoundWidget = *anIt == anActiveWidget;
442       else
443         aFoundObligatory = (*anIt)->isObligatory();
444     }
445     if (!aFoundObligatory)
446       myRestartingMode = RM_Forbided;
447   }
448 }
449
450 void PartSet_SketcherReentrantMgr::onAfterValuesChangedInPropertyPanel()
451 {
452
453   if (isInternalEditActive()) {
454     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
455                                                        (myWorkshop->currentOperation());
456     ModuleBase_ModelWidget* aWidget = (ModuleBase_ModelWidget*)sender();
457     if (!aWidget->isModifiedInEdit().empty())
458       restartOperation();
459   }
460 }
461
462 void PartSet_SketcherReentrantMgr::onBeforeStopped()
463 {
464   if (!isActiveMgr() || !myIsInternalEditOperation)
465     return;
466
467   beforeStopInternalEdit();
468 }
469
470 bool PartSet_SketcherReentrantMgr::canBeCommittedByPreselection()
471 {
472   return !isActiveMgr() || myRestartingMode == RM_None;
473 }
474
475 bool PartSet_SketcherReentrantMgr::isActiveMgr() const
476 {
477   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
478
479   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
480   if (!anActive) {
481     anActive = module()->sketchMgr()->isNestedSketchOperation(aCurrentOperation);
482     if (anActive) { // the manager is not active when the current operation is a usual Edit
483       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
484                                                        (myWorkshop->currentOperation());
485       if (aFOperation->isEditOperation())
486         anActive = myIsInternalEditOperation;
487     }
488   }
489   return anActive;
490 }
491
492 bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
493 {
494 #ifdef DEBUG_RESTART
495   std::cout << "PartSet_SketcherReentrantMgr::startInternalEdit" << std::endl;
496 #endif
497
498   bool isDone = false;
499   /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
500   /// event comes two times, so we should not start another internal edit operation
501   /// the Apply button becomes disabled becase the second additional internal feature is created
502   if (myIsInternalEditOperation)
503     return true;
504
505   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
506                                                      (myWorkshop->currentOperation());
507
508   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
509     /// improvement to deselect automatically all eventual selected objects, when
510     // returning to the neutral point of the Sketcher or start internal edit
511     workshop()->selector()->clearSelection();
512
513     if (myIsAutoConstraints)
514       addConstraints(aFOperation->feature());
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       setInternalActiveWidget(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   setInternalActiveWidget(0);
683   delete myInternalWidget;
684   myInternalWidget = 0;
685
686   QObjectPtrList anObjects;
687   anObjects.append(myInternalFeature);
688   workshop()->deleteFeatures(anObjects);
689   myInternalFeature = FeaturePtr();
690 }
691
692 void PartSet_SketcherReentrantMgr::resetFlags()
693 {
694   if (!myIsFlagsBlocked) {
695     myIsInternalEditOperation = false;
696     updateAcceptAllAction();
697     myRestartingMode = RM_None;
698     myReentrantMessage = std::shared_ptr<Events_Message>();
699   }
700 }
701
702 bool PartSet_SketcherReentrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
703                                                              const FeaturePtr& theNewFeature,
704                                                              const CompositeFeaturePtr& theSketch,
705                                                              const bool /*isTemporary*/)
706 {
707   bool aChanged = false;
708   if (!theSourceFeature.get() || !theSourceFeature->data().get() ||
709       !theSourceFeature->data()->isValid())
710     return aChanged;
711
712 #ifdef DEBUG_RESTART
713   std::cout << "PartSet_SketcherReentrantMgr::copyReetntrantAttributes from '"
714             << theSourceFeature->data()->name() << "' to '" << theNewFeature->data()->name()
715             << "'" << std::endl;
716 #endif
717
718   std::string aFeatureKind = theSourceFeature->getKind();
719   /*if (aFeatureKind == SketchPlugin_Line::ID()) {
720     // Initialize new line with first point equal to end of previous
721     std::shared_ptr<ModelAPI_Data> aSFData = theSourceFeature->data();
722     std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
723                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
724     std::shared_ptr<ModelAPI_Data> aNFData = theNewFeature->data();
725     std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
726                                                  aNFData->attribute(SketchPlugin_Line::START_ID()));
727     aNPoint->setValue(aSPoint->x(), aSPoint->y());
728     PartSet_Tools::createConstraint(theSketch, aSPoint, aNPoint);
729
730     aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
731                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
732     aNPoint->setValue(aSPoint->x(), aSPoint->y());
733   }
734   else*/ if (aFeatureKind == SketchPlugin_MacroCircle::ID()) {
735     // set circle type
736     /*std::string aTypeAttributeId = SketchPlugin_MacroCircle::CIRCLE_TYPE();
737     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
738     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
739     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
740       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
741     //ModuleBase_Tools::flushUpdated(theNewFeature);*/
742     //aChanged = true;
743   }
744   else if (aFeatureKind == SketchPlugin_MacroArc::ID()) {
745     // set arc type
746     /*std::string aTypeAttributeId = SketchPlugin_MacroArc::ARC_TYPE();
747     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
748     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
749     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
750       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());*/
751     //// if the arc is tangent, set coincidence to end point of the previous arc
752     //std::string anArcType = aSourceFeatureTypeAttr->value();
753     //if (anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT()) {
754     //  // get the last point of the previuos arc feature(geom point 2d)
755     //  std::shared_ptr<ModelAPI_Data> aSData = theSourceFeature->data();
756     //  std::shared_ptr<GeomDataAPI_Point2D> aSPointAttr =
757     //                                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
758     //                                  aSData->attribute(SketchPlugin_Arc::END_ID()));
759     //  // get point attribute on the current feature
760     //  AttributeRefAttrPtr aTangentPointAttr = theNewFeature->data()->refattr(
761     //                                                SketchPlugin_Arc::TANGENT_POINT_ID());
762     //  aTangentPointAttr->setAttr(aSPointAttr);
763
764     //  std::shared_ptr<GeomDataAPI_Point2D> aNPointAttr =
765     //                                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
766     //                                theNewFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
767     //  aNPointAttr->setValue(aSPointAttr->x(), aSPointAttr->y());
768
769     //}
770     //ModuleBase_Tools::flushUpdated(theNewFeature);
771     //aChanged = true;
772   }
773   else if (aFeatureKind == SketchPlugin_Trim::ID() ||
774            aFeatureKind == SketchPlugin_Split::ID()) {
775     std::string aPreviewObjectAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
776                 SketchPlugin_Trim::PREVIEW_OBJECT(): SketchPlugin_Split::PREVIEW_OBJECT();
777     std::string aPreviewPointAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
778                 SketchPlugin_Trim::PREVIEW_POINT(): SketchPlugin_Split::PREVIEW_POINT();
779     std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
780                       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
781                       theSourceFeature->data()->attribute(aPreviewObjectAttribute));
782     std::shared_ptr<ModelAPI_AttributeReference> aNRefPreviewAttr =
783                         std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
784                         theNewFeature->data()->attribute(aPreviewObjectAttribute));
785     aNRefPreviewAttr->setValue(aRefPreviewAttr->value());
786     std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
787                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
788                       theSourceFeature->data()->attribute(aPreviewPointAttribute));
789     std::shared_ptr<GeomDataAPI_Point2D> aNPointPreviewAttr =
790                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
791                       theNewFeature->data()->attribute(aPreviewPointAttribute));
792     aNPointPreviewAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
793   }
794   return aChanged;
795 }
796
797 bool PartSet_SketcherReentrantMgr::isTangentArc(ModuleBase_Operation* theOperation,
798                                                  const CompositeFeaturePtr& /*theSketch*/) const
799 {
800   bool aTangentArc = false;
801   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
802                                                                         (theOperation);
803   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
804     FeaturePtr aFeature = aFOperation->feature();
805     if (aFeature.get() && aFeature->getKind() == SketchPlugin_MacroArc::ID()) {
806       AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_MacroArc::ARC_TYPE());
807       std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
808       aTangentArc = anArcType == SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE();
809     }
810   }
811   return aTangentArc;
812 }
813
814 void PartSet_SketcherReentrantMgr::updateAcceptAllAction()
815 {
816   CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
817   if (aSketch.get())
818     workshop()->errorMgr()->updateAcceptAllAction(aSketch);
819 }
820
821 XGUI_Workshop* PartSet_SketcherReentrantMgr::workshop() const
822 {
823   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
824   return aConnector->workshop();
825 }
826
827 PartSet_Module* PartSet_SketcherReentrantMgr::module() const
828 {
829   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
830 }
831
832 void PartSet_SketcherReentrantMgr::setInternalActiveWidget(ModuleBase_ModelWidget* theWidget)
833 {
834   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
835     (myWorkshop->currentOperation());
836   if (aFOperation)
837   {
838     XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
839       (aFOperation->propertyPanel());
840     if (aPropertyPanel)
841       aPropertyPanel->setInternalActiveWidget(theWidget);
842   }
843 }
844
845 void PartSet_SketcherReentrantMgr::onAutoConstraints(bool isOn)
846 {
847   myIsAutoConstraints = isOn;
848 }
849
850 void PartSet_SketcherReentrantMgr::addConstraints(const FeaturePtr& theFeature)
851 {
852   if (theFeature->getKind() != SketchPlugin_Line::ID())
853     return;
854
855   static GeomDir2dPtr myHorDir(new GeomAPI_Dir2d(1, 0));
856   static GeomDir2dPtr myVertDir(new GeomAPI_Dir2d(0, 1));
857
858   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
859   std::shared_ptr<GeomDataAPI_Point2D> aPoint1 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
860       aData->attribute(SketchPlugin_Line::START_ID()));
861   std::shared_ptr<GeomDataAPI_Point2D> aPoint2 = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
862       aData->attribute(SketchPlugin_Line::END_ID()));
863   if (aPoint1.get() && aPoint2.get()) {
864     GeomLine2dPtr aLine(new GeomAPI_Lin2d(aPoint1->pnt(), aPoint2->pnt()));
865     GeomDir2dPtr aDir = aLine->direction();
866     double aHorAngle = fabs(myHorDir->angle(aDir));
867     double aVertAngle = fabs(myVertDir->angle(aDir));
868     if (aHorAngle > M_PI/2.)
869       aHorAngle = M_PI - aHorAngle;
870     if (aVertAngle > M_PI/2.)
871       aVertAngle = M_PI - aVertAngle;
872
873     double aTolerance = Config_PropManager::real(SKETCH_TAB_NAME, "angular_tolerance");
874     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
875     FeaturePtr aFeature;
876     if (aHorAngle < aTolerance) {
877       // Add horizontal constraint
878       aFeature = aSketch->addFeature(SketchPlugin_ConstraintHorizontal::ID());
879     }
880     else if (aVertAngle < aTolerance) {
881       // Add vertical constraint
882       aFeature = aSketch->addFeature(SketchPlugin_ConstraintVertical::ID());
883     }
884     if (aFeature.get()) {
885       aFeature->refattr(SketchPlugin_Constraint::ENTITY_A())->setObject(
886           theFeature->firstResult());
887       myLastAutoConstraint = aFeature.get();
888     }
889   }
890 }
891
892
893 bool PartSet_SketcherReentrantMgr::isLastAutoConstraint(const ObjectPtr& theObj) const
894 {
895   return theObj.get() == myLastAutoConstraint;
896 }