Salome HOME
Issues #2173, #2169: processing focus in widget by SHAPER loop (instead of Qt loop...
[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_AttributeReference.h"
12 #include "ModelAPI_EventReentrantMessage.h"
13
14 #include "GeomDataAPI_Point2D.h"
15
16 #include <ModuleBase_IPropertyPanel.h>
17 #include <ModuleBase_OperationFeature.h>
18 #include <ModuleBase_ModelWidget.h>
19 #include <ModuleBase_ViewerPrs.h>
20 #include <ModuleBase_WidgetSelector.h>
21 #include <ModuleBase_PageWidget.h>
22 #include <ModuleBase_PageBase.h>
23 #include <ModuleBase_WidgetFactory.h>
24 #include <ModuleBase_OperationDescription.h>
25 #include "ModuleBase_ToolBox.h"
26 #include "ModuleBase_ISelection.h"
27
28 #include <SketchPlugin_Feature.h>
29 #include <SketchPlugin_Line.h>
30 #include <SketchPlugin_MacroArc.h>
31 #include <SketchPlugin_MacroCircle.h>
32 #include <SketchPlugin_Point.h>
33 #include <SketchPlugin_Trim.h>
34 #include <SketchPlugin_Split.h>
35
36 #include <XGUI_Workshop.h>
37 #include <XGUI_ModuleConnector.h>
38 #include <XGUI_OperationMgr.h>
39 #include <XGUI_PropertyPanel.h>
40 #include <XGUI_ErrorMgr.h>
41 #include <XGUI_SelectionMgr.h>
42
43 #include <QToolButton>
44
45 //#define DEBUG_RESTART
46
47 PartSet_SketcherReentrantMgr::PartSet_SketcherReentrantMgr(ModuleBase_IWorkshop* theWorkshop)
48 : QObject(theWorkshop),
49   myWorkshop(theWorkshop),
50   myRestartingMode(RM_None),
51   myIsFlagsBlocked(false),
52   myIsInternalEditOperation(false),
53   myInternalActiveWidget(0),
54   myNoMoreWidgetsAttribute("")
55 {
56 }
57
58 PartSet_SketcherReentrantMgr::~PartSet_SketcherReentrantMgr()
59 {
60 }
61
62 ModuleBase_ModelWidget* PartSet_SketcherReentrantMgr::internalActiveWidget() const
63 {
64   ModuleBase_ModelWidget* aWidget = 0;
65   if (!isActiveMgr())
66     return aWidget;
67
68   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
69   if (anOperation) {
70     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
71     if (aPanel) { // check for case when the operation is started but property panel is not filled
72       ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
73       if (myIsInternalEditOperation && (!anActiveWidget || !anActiveWidget->isViewerSelector()))
74         aWidget = myInternalActiveWidget;
75     }
76   }
77   return aWidget;
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       if (!aPreSelected.empty()) {
238         ModuleBase_ViewerPrsPtr aValue = aPreSelected.first();
239         module()->getGeomSelection(aValue, mySelectedObject, mySelectedAttribute);
240
241         PartSet_WidgetPoint2D* aPointWidget = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWidget);
242         if (aPointWidget) {
243           GeomShapePtr aShape;
244           aPointWidget->getGeomSelection_(aValue, mySelectedObject, aShape);
245         }
246       }
247
248       restartOperation();
249       myClickedSketchPoint = std::shared_ptr<GeomAPI_Pnt2d>();
250       mySelectedObject = ObjectPtr();
251       mySelectedAttribute = AttributePtr();
252
253       myPreviousFeature = FeaturePtr();
254       aProcessed = true;
255
256       // fill the first widget by the mouse event point
257       // if the active widget is not the first, it means that the restarted operation is filled by
258       // the current preselection.
259       PartSet_MouseProcessor* aMouseProcessor = dynamic_cast<PartSet_MouseProcessor*>(
260                                                                        module()->activeWidget());
261       //PartSet_WidgetPoint2D* aPoint2DWdg =
262       //  dynamic_cast<PartSet_WidgetPoint2D*>(module()->activeWidget());
263       PartSet_MouseProcessor* aFirstWidget = dynamic_cast<PartSet_MouseProcessor*>(
264                                                         aPanel->findFirstAcceptingValueWidget());
265       //if (aPoint2DWdg && aPoint2DWdg == aFirstWidget) {
266       if (aMouseProcessor && aMouseProcessor == aFirstWidget) {
267         std::shared_ptr<ModuleBase_ViewerPrs> aSelectedPrs;
268         if (!aPreSelected.empty())
269           aSelectedPrs = aPreSelected.front();
270         if (aSelectedPrs.get() && aSelectedPrs->object().get()
271             && !aSelectedPrs->object()->data()->isValid()) {
272           // the selected object was removed diring restart, e.g. presentable macro feature
273           // there are created objects to replace the object depending on created feature kind
274           aSelectedPrs = std::shared_ptr<ModuleBase_ViewerPrs>();
275         }
276         aMouseProcessor->setPreSelection(aSelectedPrs, theWindow, theEvent);
277         //aPoint2DWdg->mouseReleased(theWindow, theEvent);
278         //if (!aPreSelected.empty())
279         //  aPoint2DWdg->setPreSelection(ModuleBase_ViewerPrsPtr());
280       }
281       // unblock viewer update
282       ModuleBase_Tools::blockUpdateViewer(false);
283     }
284   }
285   return aProcessed;
286 }
287
288 //******************************************************
289 void PartSet_SketcherReentrantMgr::setReentrantPreSelection(
290                                        const std::shared_ptr<Events_Message>& theMessage)
291 {
292   ReentrantMessagePtr aReentrantMessage =
293                       std::dynamic_pointer_cast<ModelAPI_EventReentrantMessage>(theMessage);
294   if (!aReentrantMessage.get())
295     return;
296
297   // if feature has already filled the selected object, we should not overwrite it
298   if (!aReentrantMessage->selectedObject().get())
299     aReentrantMessage->setSelectedObject(mySelectedObject);
300
301   aReentrantMessage->setSelectedAttribute(mySelectedAttribute);
302   aReentrantMessage->setClickedPoint(myClickedSketchPoint);
303 }
304
305 void PartSet_SketcherReentrantMgr::onWidgetActivated()
306 {
307   if (!isActiveMgr())
308     return;
309   if (!myIsInternalEditOperation)
310     return;
311
312   PartSet_Module* aModule = module();
313   ModuleBase_ModelWidget* aFirstWidget = aModule->activeWidget();
314   ModuleBase_IPropertyPanel* aPanel = aModule->currentOperation()->propertyPanel();
315   if (aFirstWidget != aPanel->activeWidget()) {
316     ModuleBase_WidgetSelector* aWSelector = dynamic_cast<ModuleBase_WidgetSelector*>(aFirstWidget);
317     if (aWSelector)
318       aWSelector->activateSelectionAndFilters(true);
319   }
320 }
321
322 void PartSet_SketcherReentrantMgr::onNoMoreWidgets(const std::string& thePreviousAttributeID)
323 {
324 #ifdef DEBUG_RESTART
325   std::cout << "PartSet_SketcherReentrantMgr::onNoMoreWidgets" << std::endl;
326 #endif
327
328   if (!isActiveMgr())
329     return;
330
331   // we should avoid processing of the signal about no more widgets attributes and
332   // do this after the restart operaion is finished if it was called
333   // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
334   // if it should be called after restart
335   if (myIsFlagsBlocked) {
336     myNoMoreWidgetsAttribute = thePreviousAttributeID;
337     return;
338   }
339
340   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
341                                                        (myWorkshop->currentOperation());
342   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
343     return;
344
345   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
346     bool isStarted = false;
347     if (!module()->sketchMgr()->sketchSolverError()) {
348       if (myRestartingMode != RM_Forbided) {
349         myRestartingMode = RM_LastFeatureUsed;
350         isStarted = startInternalEdit(thePreviousAttributeID);
351       }
352     }
353     if (!isStarted)
354       aFOperation->commit();
355   }
356 }
357
358 bool PartSet_SketcherReentrantMgr::processEnter(const std::string& thePreviousAttributeID)
359 {
360   bool isDone = false;
361
362   if (!isActiveMgr())
363     return isDone;
364
365   // empty previous attribute means that the Apply/Ok button has focus and the enter
366   // should not lead to start edition mode of the previous operation
367   if (thePreviousAttributeID.empty())
368     return isDone;
369
370   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
371                                                        (myWorkshop->currentOperation());
372   if (!myWorkshop->module()->getFeatureError(aFOperation->feature()).isEmpty())
373     return isDone;
374
375   bool isSketchSolverError = module()->sketchMgr()->sketchSolverError();
376
377   if (!isSketchSolverError) {
378     myRestartingMode = RM_EmptyFeatureUsed;
379     isDone = startInternalEdit(thePreviousAttributeID);
380   }
381
382   return isDone;
383 }
384
385 void PartSet_SketcherReentrantMgr::onVertexSelected()
386 {
387   if (!isActiveMgr())
388     return;
389
390   ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
391   std::string anOperationId = anOperation->id().toStdString();
392   if (anOperationId == SketchPlugin_Line::ID() ||
393       isTangentArc(anOperation, module()->sketchMgr()->activeSketch())) {
394     /// If last line finished on vertex the lines creation sequence has to be break
395     ModuleBase_IPropertyPanel* aPanel = anOperation->propertyPanel();
396     ModuleBase_ModelWidget* anActiveWidget = aPanel->activeWidget();
397     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
398     QList<ModuleBase_ModelWidget*>::const_iterator anIt = aWidgets.begin(), aLast = aWidgets.end();
399     bool aFoundWidget = false;
400     bool aFoundObligatory = false;
401     for (; anIt != aLast && !aFoundObligatory; anIt++) {
402       if (!aFoundWidget)
403         aFoundWidget = *anIt == anActiveWidget;
404       else
405         aFoundObligatory = (*anIt)->isObligatory();
406     }
407     if (!aFoundObligatory)
408       myRestartingMode = RM_Forbided;
409   }
410 }
411
412 void PartSet_SketcherReentrantMgr::onAfterValuesChangedInPropertyPanel()
413 {
414
415   if (isInternalEditActive()) {
416     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
417                                                        (myWorkshop->currentOperation());
418     ModuleBase_ModelWidget* aWidget = (ModuleBase_ModelWidget*)sender();
419     if (!aWidget->isModifiedInEdit().empty())
420       restartOperation();
421   }
422 }
423
424 void PartSet_SketcherReentrantMgr::onBeforeStopped()
425 {
426   if (!isActiveMgr() || !myIsInternalEditOperation)
427     return;
428
429   beforeStopInternalEdit();
430 }
431
432 bool PartSet_SketcherReentrantMgr::canBeCommittedByPreselection()
433 {
434   return !isActiveMgr() || myRestartingMode == RM_None;
435 }
436
437 bool PartSet_SketcherReentrantMgr::isActiveMgr() const
438 {
439   ModuleBase_Operation* aCurrentOperation = myWorkshop->currentOperation();
440
441   bool anActive = PartSet_SketcherMgr::isSketchOperation(aCurrentOperation);
442   if (!anActive) {
443     anActive = module()->sketchMgr()->isNestedSketchOperation(aCurrentOperation);
444     if (anActive) { // the manager is not active when the current operation is a usual Edit
445       ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
446                                                        (myWorkshop->currentOperation());
447       if (aFOperation->isEditOperation())
448         anActive = myIsInternalEditOperation;
449     }
450   }
451   return anActive;
452 }
453
454 bool PartSet_SketcherReentrantMgr::startInternalEdit(const std::string& thePreviousAttributeID)
455 {
456 #ifdef DEBUG_RESTART
457   std::cout << "PartSet_SketcherReentrantMgr::startInternalEdit" << std::endl;
458 #endif
459
460   bool isDone = false;
461   /// this is workaround for ModuleBase_WidgetEditor, used in SALOME mode. Sometimes key enter
462   /// event comes two times, so we should not start another internal edit operation
463   /// the Apply button becomes disabled becase the second additional internal feature is created
464   if (myIsInternalEditOperation)
465     return true;
466
467   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
468                                                      (myWorkshop->currentOperation());
469
470   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
471     aFOperation->setEditOperation(true/*, false*/);
472     createInternalFeature();
473
474     myIsInternalEditOperation = true;
475     updateAcceptAllAction();
476
477     isDone = true;
478     connect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
479     connect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
480
481     // activate selection filters of the first widget in the viewer
482     onWidgetActivated();
483
484     // activate the last active widget in the Property Panel
485     if (!thePreviousAttributeID.empty()) {
486       ModuleBase_Operation* anEditOperation = module()->currentOperation();
487       if (anEditOperation) {
488         ModuleBase_IPropertyPanel* aPanel = aFOperation->propertyPanel();
489         ModuleBase_ModelWidget* aPreviousAttributeWidget = 0;
490         QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
491         for (int i = 0, aNb = aWidgets.size(); i < aNb && !aPreviousAttributeWidget; i++) {
492           if (aWidgets[i]->attributeID() == thePreviousAttributeID) {
493           /// workaround for the same attributes used in different stacked widgets(attribute types)
494           if (ModuleBase_ToolBox::isOffToolBoxParent(aWidgets[i]))
495             continue;
496             aPreviousAttributeWidget = aWidgets[i];
497           }
498         }
499         // If the current widget is a selector, do nothing, it processes the mouse press
500         if (aPreviousAttributeWidget) {
501           if (!aPreviousAttributeWidget->isViewerSelector()) {
502             aPreviousAttributeWidget->focusTo();
503             aPreviousAttributeWidget->emitFocusInWidget();
504             aPreviousAttributeWidget->selectContent();
505           }
506           else {
507             // in case of shape multi selector, the widget does not lose focus by filling
508             // like it is in shape selector. So, if enter is pressed, the multi shape selector
509             // control should be deactivated. The focus is moved to Apply button and there
510             // should not be active control visualized in property panel
511             if (aPreviousAttributeWidget == aPanel->activeWidget()) {
512               aPanel->activateWidget(NULL, false);
513             }
514             // if there is no the next widget to be automatically activated,
515             // the Ok button in property
516             // panel should accept the focus(example is parallel constraint on sketch lines)
517             QToolButton* anOkBtn =
518               dynamic_cast<XGUI_PropertyPanel*>(aPanel)->findButton(PROP_PANEL_OK);
519             if (anOkBtn)
520               anOkBtn->setFocus(Qt::TabFocusReason);
521           }
522         }
523       }
524     }
525   }
526   if (isDone)
527     module()->sketchMgr()->clearClickedFlags();
528
529   return isDone;
530 }
531
532 void PartSet_SketcherReentrantMgr::beforeStopInternalEdit()
533 {
534   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
535                                                       (myWorkshop->currentOperation());
536   if (aFOperation) {
537     disconnect(aFOperation, SIGNAL(beforeCommitted()), this, SLOT(onBeforeStopped()));
538     disconnect(aFOperation, SIGNAL(beforeAborted()), this, SLOT(onBeforeStopped()));
539   }
540
541   deleteInternalFeature();
542 }
543
544 void PartSet_SketcherReentrantMgr::restartOperation()
545 {
546 #ifdef DEBUG_RESTART
547   std::cout << "PartSet_SketcherReentrantMgr::restartOperation" << std::endl;
548 #endif
549
550   if (myIsInternalEditOperation) {
551     ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>(
552                                                                   myWorkshop->currentOperation());
553     if (aFOperation) {
554       ModuleBase_ISelection* aSelection = myWorkshop->selection();
555       QList<ModuleBase_ViewerPrsPtr> aPreSelected =
556         aSelection->getSelected(ModuleBase_ISelection::AllControls);
557
558       if (myInternalFeature.get())
559         copyReetntrantAttributes(myInternalFeature, aFOperation->feature(),
560                                  module()->sketchMgr()->activeSketch());
561
562       myNoMoreWidgetsAttribute = "";
563       myIsFlagsBlocked = true;
564       /// launch has 'false' parameter to do not start new operation if the previous operation
565       /// is not committed. It is important for Line Sketch feature as it uses the previous
566       /// created feature parameter(to build coincidence), but by abort the previous is removed
567       module()->launchOperation(aFOperation->id(), true);
568       myIsFlagsBlocked = false;
569       resetFlags();
570
571       // we should avoid processing of the signal about no more widgets attributes and
572       // do this after the restart operaion is finished if it was called
573       // onNoMoreWidgets depends on myIsFlagsBlocked and fill myNoMoreWidgetsAttribute
574       // if it should be called after restart
575       if (!myNoMoreWidgetsAttribute.empty()) {
576         onNoMoreWidgets(myNoMoreWidgetsAttribute);
577         myNoMoreWidgetsAttribute = "";
578       }
579     }
580   }
581 }
582
583 void PartSet_SketcherReentrantMgr::createInternalFeature()
584 {
585   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
586                                                      (myWorkshop->currentOperation());
587
588   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
589     FeaturePtr anOperationFeature = aFOperation->feature();
590
591     CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
592     myInternalFeature = aSketch->addFeature(anOperationFeature->getKind());
593
594 #ifdef DEBUG_RESTART
595     std::cout << "PartSet_SketcherReentrantMgr::createInternalFeature: "
596               << myInternalFeature->data()->name() << std::endl;
597 #endif
598
599     bool isFeatureChanged = copyReetntrantAttributes(anOperationFeature, myInternalFeature,
600                                                      aSketch, false);
601     XGUI_PropertyPanel* aPropertyPanel = dynamic_cast<XGUI_PropertyPanel*>
602                                                   (aFOperation->propertyPanel());
603
604     myInternalWidget = new QWidget(aPropertyPanel->contentWidget()->pageWidget());
605     myInternalWidget->setVisible(false);
606
607     ModuleBase_PageWidget* anInternalPage = new ModuleBase_PageWidget(myInternalWidget);
608
609     QString aXmlRepr = aFOperation->getDescription()->xmlRepresentation();
610     ModuleBase_WidgetFactory aFactory(aXmlRepr.toStdString(), myWorkshop);
611
612     aFactory.createWidget(anInternalPage);
613     QList<ModuleBase_ModelWidget*> aWidgets = aFactory.getModelWidgets();
614
615     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
616       bool isStoreValue = !aFOperation->isEditOperation() &&
617                           !aWidget->getDefaultValue().empty() &&
618                           !aWidget->isComputedDefault();
619       aWidget->setFeature(myInternalFeature, isStoreValue);
620       if (!isStoreValue && isFeatureChanged)
621         aWidget->restoreValue();
622     }
623
624     ModuleBase_ModelWidget* aFirstWidget = ModuleBase_IPropertyPanel::findFirstAcceptingValueWidget
625                                                                                         (aWidgets);
626     if (aFirstWidget)
627       myInternalActiveWidget = aFirstWidget;
628   }
629 }
630
631 void PartSet_SketcherReentrantMgr::deleteInternalFeature()
632 {
633 #ifdef DEBUG_RESTART
634   std::cout << "PartSet_SketcherReentrantMgr::deleteInternalFeature: "
635             << myInternalFeature->data()->name() << std::endl;
636 #endif
637   if (myInternalActiveWidget) {
638     ModuleBase_WidgetSelector* aWSelector =
639       dynamic_cast<ModuleBase_WidgetSelector*>(myInternalActiveWidget);
640     if (aWSelector)
641       aWSelector->activateSelectionAndFilters(false);
642     myInternalActiveWidget = 0;
643   }
644   delete myInternalWidget;
645   myInternalWidget = 0;
646
647   QObjectPtrList anObjects;
648   anObjects.append(myInternalFeature);
649   workshop()->deleteFeatures(anObjects);
650   myInternalFeature = FeaturePtr();
651 }
652
653 void PartSet_SketcherReentrantMgr::resetFlags()
654 {
655   if (!myIsFlagsBlocked) {
656     myIsInternalEditOperation = false;
657     updateAcceptAllAction();
658     myRestartingMode = RM_None;
659     myReentrantMessage = std::shared_ptr<Events_Message>();
660   }
661 }
662
663 bool PartSet_SketcherReentrantMgr::copyReetntrantAttributes(const FeaturePtr& theSourceFeature,
664                                                              const FeaturePtr& theNewFeature,
665                                                              const CompositeFeaturePtr& theSketch,
666                                                              const bool /*isTemporary*/)
667 {
668   bool aChanged = false;
669   if (!theSourceFeature.get() || !theSourceFeature->data().get() ||
670       !theSourceFeature->data()->isValid())
671     return aChanged;
672
673 #ifdef DEBUG_RESTART
674   std::cout << "PartSet_SketcherReentrantMgr::copyReetntrantAttributes from '"
675             << theSourceFeature->data()->name() << "' to '" << theNewFeature->data()->name()
676             << "'" << std::endl;
677 #endif
678
679   std::string aFeatureKind = theSourceFeature->getKind();
680   /*if (aFeatureKind == SketchPlugin_Line::ID()) {
681     // Initialize new line with first point equal to end of previous
682     std::shared_ptr<ModelAPI_Data> aSFData = theSourceFeature->data();
683     std::shared_ptr<GeomDataAPI_Point2D> aSPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
684                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
685     std::shared_ptr<ModelAPI_Data> aNFData = theNewFeature->data();
686     std::shared_ptr<GeomDataAPI_Point2D> aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
687                                                  aNFData->attribute(SketchPlugin_Line::START_ID()));
688     aNPoint->setValue(aSPoint->x(), aSPoint->y());
689     PartSet_Tools::createConstraint(theSketch, aSPoint, aNPoint);
690
691     aNPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
692                                                  aSFData->attribute(SketchPlugin_Line::END_ID()));
693     aNPoint->setValue(aSPoint->x(), aSPoint->y());
694   }
695   else*/ if (aFeatureKind == SketchPlugin_MacroCircle::ID()) {
696     // set circle type
697     /*std::string aTypeAttributeId = SketchPlugin_MacroCircle::CIRCLE_TYPE();
698     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
699     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
700     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
701       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());
702     //ModuleBase_Tools::flushUpdated(theNewFeature);*/
703     //aChanged = true;
704   }
705   else if (aFeatureKind == SketchPlugin_MacroArc::ID()) {
706     // set arc type
707     /*std::string aTypeAttributeId = SketchPlugin_MacroArc::ARC_TYPE();
708     AttributeStringPtr aSourceFeatureTypeAttr = theSourceFeature->data()->string(aTypeAttributeId);
709     AttributeStringPtr aNewFeatureTypeAttr = theNewFeature->data()->string(aTypeAttributeId);
710     if (aNewFeatureTypeAttr->value() != aTypeAttributeId) // do nothing if there is no changes
711       aNewFeatureTypeAttr->setValue(aSourceFeatureTypeAttr->value());*/
712     //// if the arc is tangent, set coincidence to end point of the previous arc
713     //std::string anArcType = aSourceFeatureTypeAttr->value();
714     //if (anArcType == SketchPlugin_Arc::ARC_TYPE_TANGENT()) {
715     //  // get the last point of the previuos arc feature(geom point 2d)
716     //  std::shared_ptr<ModelAPI_Data> aSData = theSourceFeature->data();
717     //  std::shared_ptr<GeomDataAPI_Point2D> aSPointAttr =
718     //                                  std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
719     //                                  aSData->attribute(SketchPlugin_Arc::END_ID()));
720     //  // get point attribute on the current feature
721     //  AttributeRefAttrPtr aTangentPointAttr = theNewFeature->data()->refattr(
722     //                                                SketchPlugin_Arc::TANGENT_POINT_ID());
723     //  aTangentPointAttr->setAttr(aSPointAttr);
724
725     //  std::shared_ptr<GeomDataAPI_Point2D> aNPointAttr =
726     //                                std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
727     //                                theNewFeature->data()->attribute(SketchPlugin_Arc::END_ID()));
728     //  aNPointAttr->setValue(aSPointAttr->x(), aSPointAttr->y());
729
730     //}
731     //ModuleBase_Tools::flushUpdated(theNewFeature);
732     //aChanged = true;
733   }
734   else if (aFeatureKind == SketchPlugin_Trim::ID() ||
735            aFeatureKind == SketchPlugin_Split::ID()) {
736     std::string aPreviewObjectAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
737                 SketchPlugin_Trim::PREVIEW_OBJECT(): SketchPlugin_Split::PREVIEW_OBJECT();
738     std::string aPreviewPointAttribute = aFeatureKind == SketchPlugin_Trim::ID() ?
739                 SketchPlugin_Trim::PREVIEW_POINT(): SketchPlugin_Split::PREVIEW_POINT();
740     std::shared_ptr<ModelAPI_AttributeReference> aRefPreviewAttr =
741                       std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
742                       theSourceFeature->data()->attribute(aPreviewObjectAttribute));
743     std::shared_ptr<ModelAPI_AttributeReference> aNRefPreviewAttr =
744                         std::dynamic_pointer_cast<ModelAPI_AttributeReference>(
745                         theNewFeature->data()->attribute(aPreviewObjectAttribute));
746     aNRefPreviewAttr->setValue(aRefPreviewAttr->value());
747     std::shared_ptr<GeomDataAPI_Point2D> aPointPreviewAttr =
748                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
749                       theSourceFeature->data()->attribute(aPreviewPointAttribute));
750     std::shared_ptr<GeomDataAPI_Point2D> aNPointPreviewAttr =
751                       std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
752                       theNewFeature->data()->attribute(aPreviewPointAttribute));
753     aNPointPreviewAttr->setValue(aPointPreviewAttr->x(), aPointPreviewAttr->y());
754   }
755   return aChanged;
756 }
757
758 bool PartSet_SketcherReentrantMgr::isTangentArc(ModuleBase_Operation* theOperation,
759                                                  const CompositeFeaturePtr& /*theSketch*/) const
760 {
761   bool aTangentArc = false;
762   ModuleBase_OperationFeature* aFOperation = dynamic_cast<ModuleBase_OperationFeature*>
763                                                                         (theOperation);
764   if (aFOperation && module()->sketchMgr()->isNestedSketchOperation(aFOperation)) {
765     FeaturePtr aFeature = aFOperation->feature();
766     if (aFeature.get() && aFeature->getKind() == SketchPlugin_MacroArc::ID()) {
767       AttributeStringPtr aTypeAttr = aFeature->data()->string(SketchPlugin_MacroArc::ARC_TYPE());
768       std::string anArcType = aTypeAttr.get() ? aTypeAttr->value() : "";
769       aTangentArc = anArcType == SketchPlugin_MacroArc::ARC_TYPE_BY_TANGENT_EDGE();
770     }
771   }
772   return aTangentArc;
773 }
774
775 void PartSet_SketcherReentrantMgr::updateAcceptAllAction()
776 {
777   CompositeFeaturePtr aSketch = module()->sketchMgr()->activeSketch();
778   if (aSketch.get())
779     workshop()->errorMgr()->updateAcceptAllAction(aSketch);
780 }
781
782 XGUI_Workshop* PartSet_SketcherReentrantMgr::workshop() const
783 {
784   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
785   return aConnector->workshop();
786 }
787
788 PartSet_Module* PartSet_SketcherReentrantMgr::module() const
789 {
790   return dynamic_cast<PartSet_Module*>(myWorkshop->module());
791 }