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