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