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