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