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