]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherReentrantMgr.cpp
Salome HOME
Update copyrights
[modules/shaper.git] / src / PartSet / PartSet_SketcherReentrantMgr.cpp
1 // Copyright (C) 2014-2019  CEA/DEN, EDF R&D
2 //
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
7 //
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19
20 #include "PartSet_SketcherReentrantMgr.h"
21 #include "PartSet_ExternalObjectsMgr.h"
22 #include "PartSet_Module.h"
23 #include "PartSet_SketcherMgr.h"
24 #include "PartSet_WidgetPoint2d.h"
25
26 #include "ModelAPI_Session.h"
27 #include "ModelAPI_AttributeString.h"
28 #include "ModelAPI_AttributeRefAttr.h"
29 #include "ModelAPI_AttributeReference.h"
30 #include "ModelAPI_EventReentrantMessage.h"
31
32 #include "GeomDataAPI_Point2D.h"
33
34 #include <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
47 #include <SketchPlugin_Feature.h>
48 #include <SketchPlugin_Line.h>
49 #include <SketchPlugin_MacroArc.h>
50 #include <SketchPlugin_MacroCircle.h>
51 #include <SketchPlugin_Point.h>
52 #include <SketchPlugin_Trim.h>
53 #include <SketchPlugin_Split.h>
54
55 #include <XGUI_Workshop.h>
56 #include <XGUI_ModuleConnector.h>
57 #include <XGUI_OperationMgr.h>
58 #include <XGUI_PropertyPanel.h>
59 #include <XGUI_ErrorMgr.h>
60 #include <XGUI_SelectionMgr.h>
61
62 #include <QToolButton>
63
64 //#define DEBUG_RESTART
65
66 PartSet_SketcherReentrantMgr::PartSet_SketcherReentrantMgr(ModuleBase_IWorkshop* theWorkshop)
67 : QObject(theWorkshop),
68   myWorkshop(theWorkshop),
69   myRestartingMode(RM_None),
70   myIsFlagsBlocked(false),
71   myIsInternalEditOperation(false),
72   myNoMoreWidgetsAttribute("")
73 {
74 }
75
76 PartSet_SketcherReentrantMgr::~PartSet_SketcherReentrantMgr()
77 {
78 }
79
80 bool PartSet_SketcherReentrantMgr::isInternalEditActive() const
81 {
82   return myIsInternalEditOperation;
83 }
84
85 void PartSet_SketcherReentrantMgr::updateInternalEditActiveState()
86 {
87   if  (myIsInternalEditOperation) {
88     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
89                                                          (myWorkshop->currentOperation());
90     if (aFOperation) {
91       FeaturePtr aFeature = aFOperation->feature();
92       QString anError = myWorkshop->module()->getFeatureError(aFeature);
93       // stop started internal edit operation as soon as the operation becomes invalid
94       // it is especially important for the sketch tangent arc feature
95       if (!anError.isEmpty()) {
96         aFOperation->setEditOperation(false);
97         //workshop()->operationMgr()->updateApplyOfOperations();
98         myIsInternalEditOperation = false;
99         updateAcceptAllAction();
100       }
101     }
102   }
103 }
104
105 bool PartSet_SketcherReentrantMgr::operationCommitted(ModuleBase_Operation* theOperation)
106 {
107   bool aProcessed = false;
108   if (!isActiveMgr())
109     return aProcessed;
110
111   aProcessed = myIsInternalEditOperation;
112   resetFlags();
113
114   return aProcessed;
115 }
116
117 void PartSet_SketcherReentrantMgr::operationStarted(ModuleBase_Operation* theOperation)
118 {
119   if (!isActiveMgr())
120     return;
121
122   //if (myPreviousFeature.get() && myRestartingMode == RM_LastFeatureUsed) {
123     //ModuleBase_OperationFeature* aCurrentOperation = dynamic_cast<ModuleBase_OperationFeature*>(
124     //                                                            myWorkshop->currentOperation());
125     //CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
126     //if (myPreviousFeature.get() && myPreviousFeature->data()->isValid()) // it is not removed
127       //copyReetntrantAttributes(myPreviousFeature, aCurrentOperation->feature(), aSketch);
128   //}
129   resetFlags();
130 }
131
132 void PartSet_SketcherReentrantMgr::operationAborted(ModuleBase_Operation* theOperation)
133 {
134   if (!isActiveMgr())
135     return;
136
137   resetFlags();
138 }
139
140 bool PartSet_SketcherReentrantMgr::processMouseMoved(ModuleBase_IViewWindow* theWnd,
141                                                       QMouseEvent* theEvent)
142 {
143   bool aProcessed = false;
144   if (!isActiveMgr())
145     return aProcessed;
146
147   if  (myIsInternalEditOperation) {
148     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
149                                                          (myWorkshop->currentOperation());
150     FeaturePtr aLastFeature = myRestartingMode == RM_LastFeatureUsed ? aFOperation->feature()
151                                                                      : FeaturePtr();
152     if (aLastFeature) {
153       ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
154       ModuleBase_IPropertyPanel* aPanel = myWorkshop->currentOperation()->propertyPanel();
155
156       FeaturePtr aCurrentFeature = aFOperation->feature();
157       bool isLineFeature = false, isReentrantArcFeature = false;
158       std::string anAttributeOnStart;
159       if (aCurrentFeature->getKind() == SketchPlugin_Line::ID()) {
160         anAttributeOnStart = SketchPlugin_Line::START_ID();
161         isLineFeature = anActiveWidget->attributeID() == anAttributeOnStart;
162       }
163       else if (isTangentArc(aFOperation, module()->sketchMgr()->activeSketch())) {
164         isReentrantArcFeature = true;
165       }
166       bool aCanBeActivatedByMove = isLineFeature || isReentrantArcFeature;
167       if (aCanBeActivatedByMove) {
168         /// before restarting of operation we need to clear selection, as it may take part in
169         /// new feature creation, e.g. tangent arc. But it is not necessary as it was processed
170         /// by mouse release when the operation was restarted.
171         workshop()->selector()->clearSelection();
172
173         myPreviousFeature = aFOperation->feature();
174         restartOperation();
175         myPreviousFeature = FeaturePtr();
176
177         anActiveWidget = module()->activeWidget();
178         aProcessed = true;
179         if (anActiveWidget && anActiveWidget->attributeID() == anAttributeOnStart) {
180           // it was not deactivated by preselection processing
181           aPanel->activateNextWidget(anActiveWidget);
182         }
183       } else {
184         // processing mouse move in active widget of restarted operation
185         ModuleBase_ModelWidget* anActiveWidget = module()->activeWidget();
186         PartSet_MouseProcessor* aProcessor = dynamic_cast<PartSet_MouseProcessor*>(anActiveWidget);
187         if (aProcessor)
188           aProcessor->mouseMoved(theWnd, theEvent);
189       }
190     }
191   }
192   return aProcessed;
193 }
194
195 bool PartSet_SketcherReentrantMgr::processMousePressed(ModuleBase_IViewWindow* /* theWnd*/,
196                                                         QMouseEvent* /* theEvent*/)
197 {
198   return isActiveMgr() && myIsInternalEditOperation;
199 }
200
201 bool PartSet_SketcherReentrantMgr::processMouseReleased(ModuleBase_IViewWindow* theWindow,
202                                                         QMouseEvent* theEvent)
203 {
204   bool aProcessed = false;
205   if (!isActiveMgr())
206     return aProcessed;
207
208   if (myIsInternalEditOperation) {
209     ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
210     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
211
212     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
213     if (!anActiveWidget || !anActiveWidget->isViewerSelector()) {
214
215       // block of viewer update
216       // we need to block update content of the viewer because of Sketch Point feature
217       // in activate() the value of the point is initialized and it can be displayed
218       // but the default value is [0, 0]. So, we block update viewer contentent until
219       // onMouseRelease happens, which correct the point position
220       ModuleBase_Tools::blockUpdateViewer(true);
221
222       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
223                                                            (myWorkshop->currentOperation());
224       myPreviousFeature = aFOperation->feature();
225
226       /// selection should be obtained from workshop before ask if the operation can be started as
227       /// the canStartOperation method performs commit/abort of previous operation.
228       /// Sometimes commit/abort
229       /// may cause selection clear(Sketch operation) as a result
230       /// it will be lost and is not used for preselection.
231       ModuleBase_ISelection* aSelection = myWorkshop->selection();
232       QList<ModuleBase_ViewerPrsPtr> aPreSelected =
233         aSelection->getSelected(ModuleBase_ISelection::AllControls);
234
235       myClickedSketchPoint = PartSet_Tools::getPnt2d(theEvent, theWindow,
236                                                      module()->sketchMgr()->activeSketch());
237       FeaturePtr anExternalCreatedFeature;
238       if (!aPreSelected.empty()) {
239         ModuleBase_ViewerPrsPtr aValue = aPreSelected.first();
240         module()->getGeomSelection(aValue, mySelectedObject, mySelectedAttribute);
241
242         PartSet_WidgetPoint2D* aPointWidget = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
243         if (aPointWidget) {
244           GeomShapePtr aShape;
245           aPointWidget->getGeomSelection_(aValue, mySelectedObject, aShape);
246           ObjectPtr anExternalObject =
247             aPointWidget->getExternalObjectMgr()->getExternalObjectValidated();
248           // if external object is during reentrant operation and is used as a parameter of feature
249           // it should be removed after the operation is restarted. (Circle feature, Projection)
250           if (anExternalObject.get())
251             anExternalCreatedFeature = ModelAPI_Feature::feature(anExternalObject);
252         }
253       }
254
255       restartOperation();
256       // remove created external feature
257       if (anExternalCreatedFeature.get()) {
258         QObjectPtrList anObjects;
259         anObjects.append(anExternalCreatedFeature);
260         workshop()->deleteFeatures(anObjects);
261       }
262
263       myClickedSketchPoint = std::shared_ptr<GeomAPI_Pnt2d>();
264       mySelectedObject = ObjectPtr();
265       mySelectedAttribute = AttributePtr();
266
267       myPreviousFeature = FeaturePtr();
268       aProcessed = true;
269
270       // fill the first widget by the mouse event point
271       // if the active widget is not the first, it means that the restarted operation is filled by
272       // the current preselection.
273       PartSet_MouseProcessor* aMouseProcessor = dynamic_cast<PartSet_MouseProcessor*>(
274                                                                        module()->activeWidget());
275       //PartSet_WidgetPoint2D* aPoint2DWdg =
276       //  dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
277       PartSet_MouseProcessor* aFirstWidget = dynamic_cast<PartSet_MouseProcessor*>(
278                                                         aPanel->findFirstAcceptingValueWidget());
279       //if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
280       if (aMouseProcessor && aMouseProcessor == aFirstWidget) {
281         std::shared_ptr<ModuleBase_ViewerPrs> aSelectedPrs;
282         if (!aPreSelected.empty())
283           aSelectedPrs = aPreSelected.front();
284         if (aSelectedPrs.get() && aSelectedPrs->object().get()
285             && !aSelectedPrs->object()->data()->isValid()) {
286           // the selected object was removed diring restart, e.g. presentable macro feature
287           // there are created objects to replace the object depending on created feature kind
288           aSelectedPrs = std::shared_ptr<ModuleBase_ViewerPrs>();
289         }
290         aMouseProcessor->setPreSelection(aSelectedPrs, theWindow, theEvent);
291         //aPoint2DWdg->mouseReleased(theWindow, theEvent);
292         //if (!aPreSelected.empty())
293         //  aPoint2DWdg->setPreSelection(ModuleBase_ViewerPrsPtr());
294       }
295       // unblock viewer update
296       ModuleBase_Tools::blockUpdateViewer(false);
297     }
298   }
299   return aProcessed;
300 }
301
302 //******************************************************
303 void PartSet_SketcherReentrantMgr::setReentrantPreSelection(
304                                        const std::shared_ptr<Events_Message>& theMessage)
305 {
306   ReentrantMessagePtr aReentrantMessage =
307                       std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
308   if (!aReentrantMessage.get())
309     return;
310
311   // if feature has already filled the selected object, we should not overwrite it
312   if (!aReentrantMessage->selectedObject().get())
313     aReentrantMessage->setSelectedObject(mySelectedObject);
314
315   aReentrantMessage->setSelectedAttribute(mySelectedAttribute);
316   aReentrantMessage->setClickedPoint(myClickedSketchPoint);
317 }
318
319 //void PartSet_SketcherReentrantMgr::onWidgetActivated()
320 //{
321 //  if (!isActiveMgr())
322 //    return;
323 //  if (!myIsInternalEditOperation)
324 //    return;
325 //
326 //  PartSet_Module* aModule = module();
327 //  ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
328 //  ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
329 //  if (aFirstWidget != aPanel->activeWidget()) {
330 //    ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>
331 //      (aFirstWidget);
332 //    if (aWSelector) {
333 //      myWorkshop->selectionActivate()->updateSelectionModesAndFilters(aWSelector);
334 //    }
335 //  }
336 //}
337
338 void PartSet_SketcherReentrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
339 {
340 #ifdef DEBUG_RESTART
341   std::cout << "PartSet_SketcherReentrantMgr::onNoMoreWidgets" << std::endl;
342 #endif
343
344   if (!isActiveMgr())
345     return;
346
347   // we should avoid processing of the signal about no more widgets attributes and
348   // do this after the restart operaion is finished if it was called
349   // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
350   // if it should be called after restart
351   if (myIsFlagsBlocked) {
352     myNoMoreWidgetsAttribute = thePreviousAttributeID;
353     return;
354   }
355
356   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
357                                                        (myWorkshop->currentOperation());
358   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
359     return;
360
361   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
362     if (aFOperation->isNeedToBeAborted()) {
363       aFOperation->abort();
364     }
365     else {
366       bool isStarted = false;
367       if (!module()->sketchMgr()->sketchSolverError()) {
368         if (myRestartingMode != RM_Forbided) {
369           myRestartingMode = RM_LastFeatureUsed;
370           isStarted = startInternalEdit(thePreviousAttributeID);
371         }
372       }
373       if (!isStarted)
374         aFOperation->commit();
375     }
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       setInternalActiveWidget(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   setInternalActiveWidget(0);
663   delete myInternalWidget;
664   myInternalWidget = 0;
665
666   QObjectPtrList anObjects;
667   anObjects.append(myInternalFeature);
668   workshop()->deleteFeatures(anObjects);
669   myInternalFeature = FeaturePtr();
670 }
671
672 void PartSet_SketcherReentrantMgr::resetFlags()
673 {
674   if (!myIsFlagsBlocked) {
675     myIsInternalEditOperation = false;
676     updateAcceptAllAction();
677     myRestartingMode = RM_None;
678     myReentrantMessage = std::shared_ptr<Events_Message>();
679   }
680 }
681
682 bool PartSet_SketcherReentrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
683                                                              const FeaturePtr& theNewFeature,
684                                                              const CompositeFeaturePtr& theSketch,
685                                                              const bool /*isTemporary*/)
686 {
687   bool aChanged = false;
688   if (!theSourceFeature.get() || !theSourceFeature->data().get() ||
689       !theSourceFeature->data()->isValid())
690     return aChanged;
691
692 #ifdef DEBUG_RESTART
693   std::cout << "PartSet_SketcherReentrantMgr::copyReetntrantAttributes from '"
694             << theSourceFeature->data()->name() << "' to '" << theNewFeature->data()->name()
695             << "'" << std::endl;
696 #endif
697
698   std::string aFeatureKind = theSourceFeature->getKind();
699   /*if (aFeatureKind == SketchPlugin_Line::ID()) {
700     // Initialize new line with first point equal to end of previous
701     std::shared_ptr<ModelAPI_Data> aSFData = theSourceFeature->data();
702     std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
703                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
704     std::shared_ptr<ModelAPI_Data> aNFData = theNewFeature->data();
705     std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
706                                                  aNFData->attribute(SketchPlugin_Line::START_ID()));
707     aNPoint->setValue(aSPoint->x(), aSPoint->y());
708     PartSet_Tools::createConstraint(theSketch, aSPoint, aNPoint);
709
710     aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
711                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
712     aNPoint->setValue(aSPoint->x(), aSPoint->y());
713   }
714   else*/ if (aFeatureKind == SketchPlugin_MacroCircle::ID()) {
715     // set circle type
716     /*std::string aTypeAttributeId = SketchPlugin_MacroCircle::CIRCLE_TYPE();
717     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
718     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
719     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
720       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
721     //ModuleBase_Tools::flushUpdated(theNewFeature);*/
722     //aChanged = true;
723   }
724   else if (aFeatureKind == SketchPlugin_MacroArc::ID()) {
725     // set arc type
726     /*std::string aTypeAttributeId = SketchPlugin_MacroArc::ARC_TYPE();
727     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
728     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
729     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
730       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());*/
731     //// if the arc is tangent, set coincidence to end point of the previous arc
732     //std::string anArcType = aSourceFeatureTypeAttr->value();
733     //if (anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT()) {
734     //  // get the last point of the previuos arc feature(geom point 2d)
735     //  std::shared_ptr<ModelAPI_Data> aSData = theSourceFeature->data();
736     //  std::shared_ptr<GeomDataAPI_Point2D> aSPointAttr =
737     //                                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
738     //                                  aSData->attribute(SketchPlugin_Arc::END_ID()));
739     //  // get point attribute on the current feature
740     //  AttributeRefAttrPtr aTangentPointAttr = theNewFeature->data()->refattr(
741     //                                                SketchPlugin_Arc::TANGENT_POINT_ID());
742     //  aTangentPointAttr->setAttr(aSPointAttr);
743
744     //  std::shared_ptr<GeomDataAPI_Point2D> aNPointAttr =
745     //                                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
746     //                                theNewFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
747     //  aNPointAttr->setValue(aSPointAttr->x(), aSPointAttr->y());
748
749     //}
750     //ModuleBase_Tools::flushUpdated(theNewFeature);
751     //aChanged = true;
752   }
753   else if (aFeatureKind == SketchPlugin_Trim::ID() ||
754            aFeatureKind == SketchPlugin_Split::ID()) {
755     std::string aPreviewObjectAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
756                 SketchPlugin_Trim::PREVIEW_OBJECT(): SketchPlugin_Split::PREVIEW_OBJECT();
757     std::string aPreviewPointAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
758                 SketchPlugin_Trim::PREVIEW_POINT(): SketchPlugin_Split::PREVIEW_POINT();
759     std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
760                       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
761                       theSourceFeature->data()->attribute(aPreviewObjectAttribute));
762     std::shared_ptr<ModelAPI_AttributeReference> aNRefPreviewAttr =
763                         std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
764                         theNewFeature->data()->attribute(aPreviewObjectAttribute));
765     aNRefPreviewAttr->setValue(aRefPreviewAttr->value());
766     std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
767                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
768                       theSourceFeature->data()->attribute(aPreviewPointAttribute));
769     std::shared_ptr<GeomDataAPI_Point2D> aNPointPreviewAttr =
770                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
771                       theNewFeature->data()->attribute(aPreviewPointAttribute));
772     aNPointPreviewAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
773   }
774   return aChanged;
775 }
776
777 bool PartSet_SketcherReentrantMgr::isTangentArc(ModuleBase_Operation* theOperation,
778                                                  const CompositeFeaturePtr& /*theSketch*/) const
779 {
780   bool aTangentArc = false;
781   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
782                                                                         (theOperation);
783   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
784     FeaturePtr aFeature = aFOperation->feature();
785     if (aFeature.get() && aFeature->getKind() == SketchPlugin_MacroArc::ID()) {
786       AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_MacroArc::ARC_TYPE());
787       std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
788       aTangentArc = anArcType == SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE();
789     }
790   }
791   return aTangentArc;
792 }
793
794 void PartSet_SketcherReentrantMgr::updateAcceptAllAction()
795 {
796   CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
797   if (aSketch.get())
798     workshop()->errorMgr()->updateAcceptAllAction(aSketch);
799 }
800
801 XGUI_Workshop* PartSet_SketcherReentrantMgr::workshop() const
802 {
803   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
804   return aConnector->workshop();
805 }
806
807 PartSet_Module* PartSet_SketcherReentrantMgr::module() const
808 {
809   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
810 }
811
812 void PartSet_SketcherReentrantMgr::setInternalActiveWidget(ModuleBase_ModelWidget* theWidget)
813 {
814   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
815     (myWorkshop->currentOperation());
816   if (aFOperation)
817   {
818     XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
819       (aFOperation->propertyPanel());
820     if (aPropertyPanel)
821       aPropertyPanel->setInternalActiveWidget(theWidget);
822   }
823 }