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