]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherMgr.cpp
Salome HOME
44bac35b68d9cc90c691274021ac09fb32bae839
[modules/shaper.git] / src / PartSet / PartSet_SketcherMgr.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_SketcherMgr.cpp
4 // Created:     19 Dec 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_SketcherMgr.h"
8 #include "PartSet_Module.h"
9 #include "PartSet_WidgetPoint2d.h"
10 #include "PartSet_WidgetPoint2dDistance.h"
11 #include "PartSet_Tools.h"
12
13 #include <XGUI_ModuleConnector.h>
14 #include <XGUI_Displayer.h>
15 #include <XGUI_Workshop.h>
16 #include <XGUI_Selection.h>
17 #include <XGUI_SelectionMgr.h>
18 #include <ModuleBase_ModelWidget.h>
19 #include <XGUI_ModuleConnector.h>
20 #include <XGUI_PropertyPanel.h>
21 #include <XGUI_ViewerProxy.h>
22
23 #include <AppElements_MainWindow.h>
24
25 #include <ModuleBase_IViewer.h>
26 #include <ModuleBase_IWorkshop.h>
27 #include <ModuleBase_IViewWindow.h>
28 #include <ModuleBase_Operation.h>
29 #include <ModuleBase_ISelection.h>
30 #include <ModuleBase_IPropertyPanel.h>
31 #include <ModuleBase_Operation.h>
32
33 #include <GeomDataAPI_Point2D.h>
34
35 #include <Events_Loop.h>
36
37 #include <SketchPlugin_Line.h>
38 #include <SketchPlugin_Sketch.h>
39 #include <SketchPlugin_Point.h>
40 #include <SketchPlugin_Arc.h>
41 #include <SketchPlugin_Circle.h>
42 #include <SketchPlugin_ConstraintLength.h>
43 #include <SketchPlugin_ConstraintDistance.h>
44 #include <SketchPlugin_ConstraintParallel.h>
45 #include <SketchPlugin_ConstraintPerpendicular.h>
46 #include <SketchPlugin_ConstraintRadius.h>
47 #include <SketchPlugin_ConstraintRigid.h>
48
49 #include <SelectMgr_IndexedMapOfOwner.hxx>
50 #include <StdSelect_BRepOwner.hxx>
51
52 //#include <AIS_DimensionSelectionMode.hxx>
53 //#include <AIS_Shape.hxx>
54
55 #include <ModelAPI_Events.h>
56
57 #include <QMouseEvent>
58 #include <QApplication>
59
60
61 /// Returns list of unique objects by sum of objects from List1 and List2
62 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
63                                        const QList<ModuleBase_ViewerPrs>& theList2)
64 {
65   QList<ModuleBase_ViewerPrs> aRes;
66   foreach (ModuleBase_ViewerPrs aPrs, theList1) {
67     if (!aRes.contains(aPrs))
68       aRes.append(aPrs);
69   }
70   foreach (ModuleBase_ViewerPrs aPrs, theList2) {
71     if (!aRes.contains(aPrs))
72       aRes.append(aPrs);
73   }
74   return aRes;
75 }*/
76
77 void fillFeature2Attribute(const QList<ModuleBase_ViewerPrs>& theList,
78                            QMap<FeaturePtr, QList<AttributePtr> >& theFeature2AttributeMap,
79                            const FeaturePtr theSketch)
80 {
81   QList<ModuleBase_ViewerPrs> aRes;
82
83   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
84                                               aLast = theList.end();
85   for (; anIt != aLast; anIt++)
86   {
87     ModuleBase_ViewerPrs aPrs = *anIt;
88     FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
89     if (aFeature.get() == NULL)
90       continue;
91
92     QList<AttributePtr> anAttributes;
93     if (theFeature2AttributeMap.contains(aFeature)) {
94       anAttributes = theFeature2AttributeMap[aFeature];
95     }
96     AttributePtr anAttr;
97     TopoDS_Shape aShape = aPrs.shape();
98     if (!aShape.IsNull()) {
99       if (aShape.ShapeType() == TopAbs_VERTEX) {
100         anAttr = PartSet_Tools::findAttributeBy2dPoint(aFeature, aShape, theSketch);
101         if (anAttr.get() != NULL && !anAttributes.contains(anAttr))
102           anAttributes.push_back(anAttr);
103       }
104     }
105     theFeature2AttributeMap[aFeature] = anAttributes;
106   }
107 }
108
109
110
111
112 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
113   : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
114     myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
115     myIsMouseOverViewProcessed(true)
116 {
117   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
118   ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
119
120   myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
121
122   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
123           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
124
125   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
126           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
127
128   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
129           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
130
131   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
132           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
133
134   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
135   XGUI_Workshop* aWorkshop = aConnector->workshop();
136   connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
137 }
138
139 PartSet_SketcherMgr::~PartSet_SketcherMgr()
140 {
141   if (!myPlaneFilter.IsNull())
142     myPlaneFilter.Nullify();
143 }
144
145 void PartSet_SketcherMgr::onEnterViewPort()
146 {
147   if (!isNestedCreateOperation())
148     return;
149   // 1. if the mouse over window, update the next flag. Do not perform update visibility of
150   // created feature because it should be done in onMouseMove(). Some widgets watch
151   // the mouse move and use the cursor position to update own values. If the presentaion is
152   // redisplayed before this update, the feature presentation jumps from reset value to current.
153   myIsMouseOverWindow = true;
154   myIsPropertyPanelValueChanged = false;
155 }
156
157 void PartSet_SketcherMgr::onLeaveViewPort()
158 {
159   if (!isNestedCreateOperation())
160     return;
161
162   myIsMouseOverViewProcessed = false;
163   myIsMouseOverWindow = false;
164
165   // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
166   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
167   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
168   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
169   // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
170   // obtained after reset value
171   bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
172   ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
173   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
174   ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
175   if (aActiveWgt) {
176     aActiveWgt->reset();
177   }
178   aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
179
180   // hides the presentation of the current operation feature
181   myIsPropertyPanelValueChanged = false;
182   visualizeFeature(aOperation, false);
183 }
184
185 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
186 {
187   if (!isNestedCreateOperation())
188     return;
189
190   // visualize the current operation feature
191   myIsPropertyPanelValueChanged = true;
192   ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
193   visualizeFeature(aOperation, true);
194 }
195
196 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
197 {
198   get2dPoint(theWnd, theEvent, myClickedPoint);
199
200   if (!(theEvent->buttons() & Qt::LeftButton))
201     return;
202
203   // Clear dragging mode
204   myIsDragging = false;
205
206   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
207   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
208   if (!aViewer->canDragByMouse())
209     return;
210
211   ModuleBase_Operation* aOperation = aWorkshop->currentOperation();
212   if (aOperation && aOperation->isEditOperation()) {
213     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
214     ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
215     // If the current widget is a selector, do do nothing, it processes the mouse press
216     if(aActiveWgt && aActiveWgt->isViewerSelector()) {
217       return;
218     }
219   }
220
221   // Use only for sketch operations
222   if (aOperation && myCurrentSketch) {
223     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
224       return;
225
226     bool isSketcher = isSketchOperation(aOperation);
227     bool isSketchOpe = isNestedSketchOperation(aOperation);
228
229     // Avoid non-sketch operations
230     if ((!isSketchOpe) && (!isSketcher))
231       return;
232
233     bool isEditing = aOperation->isEditOperation();
234
235     // Ignore creation sketch operation
236     if ((!isSketcher) && (!isEditing))
237       return;
238
239     // MoveTo in order to highlight current object
240     aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
241
242     // Remember highlighted objects for editing
243     ModuleBase_ISelection* aSelect = aWorkshop->selection();
244     QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
245     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
246     myFeature2AttributeMap.clear();
247
248     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
249     if (aHasShift) {
250       fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
251       fillFeature2Attribute(aSelected, myFeature2AttributeMap, myCurrentSketch);
252     }
253     else {
254       fillFeature2Attribute(aHighlighted, myFeature2AttributeMap, myCurrentSketch);
255     }
256
257     if (myFeature2AttributeMap.empty()) {
258       if (isSketchOpe && (!isSketcher))
259         // commit previous operation
260         if (!aOperation->commit())
261           aOperation->abort();
262       return;
263     }
264
265     if (isSketcher) {
266       myIsDragging = true;
267       get2dPoint(theWnd, theEvent, myCurrentPoint);
268       myDragDone = false;
269       launchEditing();
270
271     } else if (isSketchOpe && isEditing) {
272       // If selected another object commit current result
273       aOperation->commit();
274
275       myIsDragging = true;
276       get2dPoint(theWnd, theEvent, myCurrentPoint);
277       myDragDone = false;
278
279       // This is necessary in order to finalize previous operation
280       QApplication::processEvents();
281       launchEditing();
282     }
283   }
284 }
285
286 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
287 {
288   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
289   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
290   if (!aViewer->canDragByMouse())
291     return;
292   ModuleBase_Operation* aOp = aWorkshop->currentOperation();
293   if (aOp) {
294     if (isNestedSketchOperation(aOp)) {
295       get2dPoint(theWnd, theEvent, myClickedPoint);
296
297       // Only for sketcher operations
298       if (myIsDragging) {
299         if (myDragDone) {
300           //aOp->commit();
301           myFeature2AttributeMap.clear();
302
303           // Reselect edited object
304           /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
305           if (theEvent->modifiers() & Qt::ShiftModifier)
306             aViewer->AISContext()->ShiftSelect();
307           else
308             aViewer->AISContext()->Select();
309             */
310         }
311       }
312     }
313   }
314   aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
315   myIsDragging = false;
316 }
317
318 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
319 {
320   if (isNestedCreateOperation() && !myIsMouseOverViewProcessed) {
321     myIsMouseOverViewProcessed = true;
322     // 1. perform the widget mouse move functionality and display the presentation
323     ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
324     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
325     ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
326     // the mouse move should be processed in the widget, if it can in order to visualize correct
327     // presentation. These widgets correct the feature attribute according to the mouse position
328     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
329     if (aPoint2DWdg) {
330       aPoint2DWdg->onMouseMove(theWnd, theEvent);
331     }
332     PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
333                                                                 (anActiveWdg);
334     if (aDistanceWdg) {
335       aDistanceWdg->onMouseMove(theWnd, theEvent);
336     }
337     visualizeFeature(aOperation, true);
338   }
339
340   myClickedPoint.clear();
341
342   if (myIsDragging) {
343     ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
344     // 1. it is necessary to save current selection in order to restore it after the features moving
345     FeatureToSelectionMap aCurrentSelection;
346     getCurrentSelection(myFeature2AttributeMap, myCurrentSketch, aWorkshop, aCurrentSelection);
347
348     // 2. the enable selection in the viewer should be temporary switched off in order to ignore
349     // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
350     // deselected). This flag should be restored in the slot, processed the mouse release signal.
351     ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
352     aViewer->enableSelection(false);
353
354     ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
355     if (!aOperation)
356       return;
357     if (isSketchOperation(aOperation))
358       return; // No edit operation activated
359
360     Handle(V3d_View) aView = theWnd->v3dView();
361     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
362     double aX, aY;
363     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
364     double dX =  aX - myCurrentPoint.myCurX;
365     double dY =  aY - myCurrentPoint.myCurY;
366
367     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
368     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
369     // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
370     // viewer happens by deselect/select the modified objects. The flag should be restored after
371     // the selection processing. The update viewer should be also called.
372     bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
373
374     static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
375     //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
376
377     FeatureToAttributesMap::const_iterator anIt = myFeature2AttributeMap.begin(),
378                                            aLast = myFeature2AttributeMap.end();
379     // 4. the features and attributes modification(move)
380     for (; anIt != aLast; anIt++) {
381       FeaturePtr aFeature = anIt.key();
382
383       AttributeList anAttributes = anIt.value();
384       // Process selection by attribute: the priority to the attribute
385       if (!anAttributes.empty()) {
386         AttributeList::const_iterator anAttIt = anAttributes.begin(), anAttLast = anAttributes.end();
387         for (; anAttIt != anAttLast; anAttIt++) {
388           AttributePtr anAttr = *anAttIt;
389           if (anAttr.get() == NULL)
390             continue;
391           std::string aAttrId = anAttr->id();
392           DataPtr aData = aFeature->data();
393           if (aData.get() != NULL) {
394             std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
395               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
396             if (aPoint.get() != NULL) {
397               bool isImmutable = aPoint->setImmutable(true);
398               aPoint->move(dX, dY);
399               ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
400               aPoint->setImmutable(isImmutable);
401             }
402           }
403         }
404       } else {
405         // Process selection by feature
406         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
407           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
408         if (aSketchFeature) {
409           aSketchFeature->move(dX, dY);
410           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
411         }
412       }
413     }
414     Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
415     //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
416
417     // 5. it is necessary to save current selection in order to restore it after the features moving
418     FeatureToSelectionMap::const_iterator aSIt = aCurrentSelection.begin(),
419                                           aSLast = aCurrentSelection.end();
420     SelectMgr_IndexedMapOfOwner anOwnersToSelect;
421     for (; aSIt != aSLast; aSIt++) {
422       anOwnersToSelect.Clear();
423       getSelectionOwners(aSIt->first, myCurrentSketch, aWorkshop, aCurrentSelection,
424                          anOwnersToSelect);
425       aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
426     }
427
428     // 6. restore the update viewer flag and call this update
429     aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
430     aDisplayer->updateViewer();
431     myDragDone = true;
432     myCurrentPoint.setValue(aX, aY);
433   }
434 }
435
436 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
437 {
438   ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
439   if (aOperation && aOperation->isEditOperation()) {
440     std::string aId = aOperation->id().toStdString();
441     if (isDistanceOperation(aOperation))
442     {
443       // Activate dimension value editing on double click
444       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
445       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
446       // Find corresponded widget to activate value editing
447       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
448         if (aWgt->attributeID() == "ConstraintValue") {
449           aWgt->focusTo();
450           return;
451         }
452       }
453     }
454   }
455 }
456
457 void PartSet_SketcherMgr::onApplicationStarted()
458 {
459   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
460   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
461   XGUI_Workshop* aWorkshop = aConnector->workshop();
462   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
463   if (aPropertyPanel) {
464     connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
465             this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
466   }
467
468   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
469   connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
470   connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
471 }
472
473 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
474 {
475   if (!myClickedPoint.myIsInitialized)
476     return;
477
478   ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
479   // the distance constraint feature should not use the clickedd point
480   // this is workaround in order to don't throw down the flyout point value,
481   // set by execute() method of these type of features
482   if (isDistanceOperation(aOperation))
483     return;
484
485   PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
486   if (aPnt2dWgt) {
487     aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
488   }
489 }
490
491 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
492                                      Point& thePoint)
493 {
494   Handle(V3d_View) aView = theWnd->v3dView();
495   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
496   double aX, anY;
497   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
498   thePoint.setValue(aX, anY);
499 }
500
501 void PartSet_SketcherMgr::launchEditing()
502 {
503   // there should be activate the vertex selection mode because the edit can happens by the selected
504   // point
505   QIntList aModes;
506   aModes << TopAbs_VERTEX << TopAbs_EDGE;
507   // TODO: #391 - to be uncommented
508   /*aModes.append(AIS_DSM_Text);
509   aModes.append(AIS_DSM_Line);
510   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
511   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));*/
512
513   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
514   aConnector->activateSubShapesSelection(aModes);
515
516   if (!myFeature2AttributeMap.empty()) {
517     FeaturePtr aFeature = myFeature2AttributeMap.begin().key();
518     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
519               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
520     if (aSPFeature) {
521       myModule->editFeature(aSPFeature);
522     }
523   }
524  
525 }
526
527
528 QStringList PartSet_SketcherMgr::sketchOperationIdList()
529 {
530   static QStringList aIds;
531   if (aIds.size() == 0) {
532     aIds << SketchPlugin_Line::ID().c_str();
533     aIds << SketchPlugin_Point::ID().c_str();
534     aIds << SketchPlugin_Arc::ID().c_str();
535     aIds << SketchPlugin_Circle::ID().c_str();
536     aIds << SketchPlugin_ConstraintLength::ID().c_str();
537     aIds << SketchPlugin_ConstraintDistance::ID().c_str();
538     aIds << SketchPlugin_ConstraintRigid::ID().c_str();
539     aIds << SketchPlugin_ConstraintRadius::ID().c_str();
540     aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
541     aIds << SketchPlugin_ConstraintParallel::ID().c_str();
542   }
543   return aIds;
544 }
545
546 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
547 {
548   return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
549 }
550
551 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
552 {
553   return theOperation &&
554          PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
555 }
556
557 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
558 {
559   std::string aId = theOperation ? theOperation->id().toStdString() : "";
560
561   return (aId == SketchPlugin_ConstraintLength::ID()) ||
562          (aId == SketchPlugin_ConstraintDistance::ID()) ||
563          (aId == SketchPlugin_ConstraintRadius::ID());
564 }
565
566 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
567 {
568   // Display all sketcher sub-Objects
569   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
570   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
571   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
572
573   // Hide sketcher result
574   std::list<ResultPtr> aResults = myCurrentSketch->results();
575   std::list<ResultPtr>::const_iterator aIt;
576   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
577     aDisplayer->erase((*aIt), false);
578   }
579   aDisplayer->erase(myCurrentSketch, false);
580
581   // Display sketcher objects
582   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
583     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
584     std::list<ResultPtr> aResults = aFeature->results();
585     std::list<ResultPtr>::const_iterator aIt;
586     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
587       aDisplayer->display((*aIt), false);
588     }
589     aDisplayer->display(aFeature, false);
590   }
591
592   if (myPlaneFilter.IsNull()) 
593     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
594
595   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
596   if (theOperation->isEditOperation()) {
597     // If it is editing of sketch then it means that plane is already defined
598     std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
599     myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
600   }
601   aDisplayer->updateViewer();
602 }
603
604 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
605 {
606   myIsMouseOverWindow = false;
607
608   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
609   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
610
611   DataPtr aData = myCurrentSketch->data();
612   if ((!aData) || (!aData->isValid())) {
613     // The sketch was aborted
614     myCurrentSketch = CompositeFeaturePtr();
615     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
616
617     // Erase all sketcher objects
618     QStringList aSketchIds = sketchOperationIdList();
619     QObjectPtrList aObjects = aDisplayer->displayedObjects();
620     foreach (ObjectPtr aObj, aObjects) {
621       DataPtr aObjData = aObj->data();
622       if ((!aObjData) || (!aObjData->isValid()))
623         aDisplayer->erase(aObj);
624     }
625     return; 
626   }
627   // Hide all sketcher sub-Objects
628   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
629     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
630     std::list<ResultPtr> aResults = aFeature->results();
631     std::list<ResultPtr>::const_iterator aIt;
632     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
633       aDisplayer->erase((*aIt), false);
634     }
635     aDisplayer->erase(aFeature, false);
636   }
637   // Display sketcher result
638   std::list<ResultPtr> aResults = myCurrentSketch->results();
639   std::list<ResultPtr>::const_iterator aIt;
640   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
641     aDisplayer->display((*aIt), false);
642   }
643   aDisplayer->display(myCurrentSketch);
644     
645   myCurrentSketch = CompositeFeaturePtr();
646   myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
647   aDisplayer->updateViewer();
648 }
649
650 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* )
651 {
652   connectToPropertyPanel(true);
653 }
654
655 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOperation)
656 {
657   connectToPropertyPanel(false);
658   myIsPropertyPanelValueChanged = false;
659   myIsMouseOverViewProcessed = true;
660   visualizeFeature(theOperation, true);
661 }
662
663 bool PartSet_SketcherMgr::canUndo() const
664 {
665   return isNestedCreateOperation();
666 }
667
668 bool PartSet_SketcherMgr::canRedo() const
669 {
670   return isNestedCreateOperation();
671 }
672
673 bool PartSet_SketcherMgr::canDisplayObject() const
674 {
675   bool aCanDisplay = true;
676   if (!isNestedCreateOperation())
677     return aCanDisplay;
678
679   // during a nested create operation, the feature is redisplayed only if the mouse over view
680   // of there was a value modified in the property panel after the mouse left the view
681   aCanDisplay = myIsPropertyPanelValueChanged || myIsMouseOverWindow;
682   return aCanDisplay;
683 }
684
685 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
686 {
687   myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
688 }
689
690 void PartSet_SketcherMgr::getCurrentSelection(const FeatureToAttributesMap& theFeatureToAttributes,
691                                               const FeaturePtr& theSketch,
692                                               ModuleBase_IWorkshop* theWorkshop,
693                                               FeatureToSelectionMap& theSelection)
694 {
695   FeatureToAttributesMap::const_iterator anIt = theFeatureToAttributes.begin(),
696                                          aLast = theFeatureToAttributes.end();
697   for (; anIt != aLast; anIt++) {
698     FeaturePtr aFeature = anIt.key();
699     getCurrentSelection(aFeature, theSketch, theWorkshop, theSelection);
700   }
701 }
702
703 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
704                                               const FeaturePtr& theSketch,
705                                               ModuleBase_IWorkshop* theWorkshop,
706                                               FeatureToSelectionMap& theSelection)
707 {
708   if (theFeature.get() == NULL)
709     return;
710
711   std::set<AttributePtr> aSelectedAttributes;
712   std::set<ResultPtr> aSelectedResults;
713
714   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
715   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
716   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
717   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
718
719   std::list<ResultPtr> aResults = theFeature->results();
720   std::list<ResultPtr>::const_iterator aIt;
721   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
722   {
723     ResultPtr aResult = *aIt;
724     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
725     if (aAISObj.get() == NULL)
726       continue;
727     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
728     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
729     {
730       Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(
731                                                                       aContext->SelectedOwner());
732       if (aBRepOwner.IsNull())
733         continue;
734       Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
735                                                                         aBRepOwner->Selectable());
736       if (anIO != anAISIO)
737         continue;
738
739       if (aBRepOwner->HasShape()) {
740         const TopoDS_Shape& aShape = aBRepOwner->Shape();
741         TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
742         if (aShapeType == TopAbs_VERTEX) {
743           AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
744                                                                         aShape, theSketch);
745           if (aPntAttr.get() != NULL)
746             aSelectedAttributes.insert(aPntAttr);
747         }
748         else if (aShapeType == TopAbs_EDGE &&
749                  aSelectedResults.find(aResult) == aSelectedResults.end()) {
750           aSelectedResults.insert(aResult);
751         }
752       }
753     }
754   }
755   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
756 }
757
758 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
759                                              const FeaturePtr& theSketch,
760                                              ModuleBase_IWorkshop* theWorkshop,
761                                              const FeatureToSelectionMap& theSelection,
762                                              SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
763 {
764   if (theFeature.get() == NULL)
765     return;
766
767   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
768   std::set<AttributePtr> aSelectedAttributes = anIt->second.first;
769   std::set<ResultPtr> aSelectedResults = anIt->second.second;
770
771   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
772   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
773   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
774   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
775
776   std::list<ResultPtr> aResults = theFeature->results();
777   std::list<ResultPtr>::const_iterator aIt;
778   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
779   {
780     ResultPtr aResult = *aIt;
781     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
782     if (aAISObj.get() == NULL)
783       continue; 
784     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
785
786     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
787     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
788     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
789       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
790       if ( anOwner.IsNull() || !anOwner->HasShape() )
791         continue;
792       const TopoDS_Shape& aShape = anOwner->Shape();
793       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
794       if (aShapeType == TopAbs_VERTEX) {
795         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
796         if (aPntAttr.get() != NULL &&
797             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
798           anOwnersToSelect.Add(anOwner);
799         }
800       }
801       else if (aShapeType == TopAbs_EDGE) {
802         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
803         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
804             anOwnersToSelect.FindIndex(anOwner) <= 0)
805           anOwnersToSelect.Add(anOwner);
806       }
807     }
808   }
809 }
810
811 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
812 {
813   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
814   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
815   XGUI_Workshop* aWorkshop = aConnector->workshop();
816   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
817   if (aPropertyPanel) {
818     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
819     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
820       if (isToConnect)
821         connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
822       else
823         disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
824     }
825   }
826 }
827
828 bool PartSet_SketcherMgr::isNestedCreateOperation() const
829 {
830   ModuleBase_Operation* aOperation = myModule->workshop()->currentOperation();
831   return aOperation && !aOperation->isEditOperation() && isNestedSketchOperation(aOperation);
832 }
833
834 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
835                                                   const bool isToDisplay)
836 {
837   if (!theOperation || theOperation->isEditOperation())
838     return;
839
840   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
841   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
842   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
843
844   // 1. change visibility of the object itself, here the presentable object is processed,
845   // e.g. constraints features
846   FeaturePtr aFeature = theOperation->feature();
847   std::list<ResultPtr> aResults = aFeature->results();
848   if (isToDisplay)
849     aDisplayer->display(aFeature, false);
850   else
851     aDisplayer->erase(aFeature, false);
852
853   // change visibility of the object results, e.g. non-constraint features
854   std::list<ResultPtr>::const_iterator aIt;
855   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
856     if (isToDisplay) {
857       aDisplayer->display(*aIt, false);
858     }
859     else {
860       aDisplayer->erase(*aIt, false);
861     }
862   }
863   aDisplayer->updateViewer();
864 }