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