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