]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_SketcherMgr.cpp
Salome HOME
19dede5a37c90ed311d3e22de90cbee6a7416b7a
[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 #include "PartSet_WidgetSketchLabel.h"
13
14 #include <ModuleBase_WidgetEditor.h>
15
16 #include <XGUI_ModuleConnector.h>
17 #include <XGUI_Displayer.h>
18 #include <XGUI_Workshop.h>
19 #include <XGUI_ContextMenuMgr.h>
20 #include <XGUI_Selection.h>
21 #include <XGUI_SelectionMgr.h>
22 #include <ModuleBase_ModelWidget.h>
23 #include <XGUI_ModuleConnector.h>
24 #include <XGUI_PropertyPanel.h>
25 #include <XGUI_ViewerProxy.h>
26
27 #include <ModuleBase_IViewer.h>
28 #include <ModuleBase_IWorkshop.h>
29 #include <ModuleBase_IViewWindow.h>
30 #include <ModuleBase_Operation.h>
31 #include <ModuleBase_ISelection.h>
32 #include <ModuleBase_IPropertyPanel.h>
33 #include <ModuleBase_Operation.h>
34
35 #include <GeomDataAPI_Point2D.h>
36
37 #include <Events_Loop.h>
38
39 #include <SketchPlugin_Line.h>
40 #include <SketchPlugin_Sketch.h>
41 #include <SketchPlugin_Point.h>
42 #include <SketchPlugin_Arc.h>
43 #include <SketchPlugin_Circle.h>
44 #include <SketchPlugin_ConstraintLength.h>
45 #include <SketchPlugin_ConstraintDistance.h>
46 #include <SketchPlugin_ConstraintParallel.h>
47 #include <SketchPlugin_ConstraintPerpendicular.h>
48 #include <SketchPlugin_ConstraintRadius.h>
49 #include <SketchPlugin_ConstraintRigid.h>
50 #include <SketchPlugin_ConstraintHorizontal.h>
51 #include <SketchPlugin_ConstraintVertical.h>
52 #include <SketchPlugin_ConstraintEqual.h>
53 #include <SketchPlugin_ConstraintTangent.h>
54 #include <SketchPlugin_ConstraintCoincidence.h>
55 #include <SketchPlugin_ConstraintFillet.h>
56 #include <SketchPlugin_ConstraintMirror.h>
57 #include <SketchPlugin_MultiRotation.h>
58 #include <SketchPlugin_MultiTranslation.h>
59
60 #include <SketcherPrs_Tools.h>
61
62 #include <SelectMgr_IndexedMapOfOwner.hxx>
63 #include <StdSelect_BRepOwner.hxx>
64
65 //#include <AIS_DimensionSelectionMode.hxx>
66 #include <AIS_Shape.hxx>
67
68 #include <ModelAPI_Events.h>
69 #include <ModelAPI_Session.h>
70
71 #include <QMouseEvent>
72 #include <QApplication>
73
74 //#define DEBUG_DO_NOT_BY_ENTER
75 //#define DEBUG_MOUSE_OVER_WINDOW_FLAGS
76
77 /// Returns list of unique objects by sum of objects from List1 and List2
78 /*QList<ModuleBase_ViewerPrs> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
79                                        const QList<ModuleBase_ViewerPrs>& theList2)
80 {
81   QList<ModuleBase_ViewerPrs> aRes;
82   foreach (ModuleBase_ViewerPrs aPrs, theList1) {
83     if (!aRes.contains(aPrs))
84       aRes.append(aPrs);
85   }
86   foreach (ModuleBase_ViewerPrs aPrs, theList2) {
87     if (!aRes.contains(aPrs))
88       aRes.append(aPrs);
89   }
90   return aRes;
91 }*/
92
93 // Fills the list of features the list of selected presentations.
94 // \param theList a list of selected presentations
95 // \param theSketch a sketch to project a vertex shape of a presentation to the plane
96 // and find the corresponded attribute
97 // \param theFeatureList  an output list of features
98 void fillFeatureList(const QList<ModuleBase_ViewerPrs>& theList,
99                      const FeaturePtr theSketch,
100                      QList<FeaturePtr>& theFeatureList)
101 {
102   QList<ModuleBase_ViewerPrs> aRes;
103
104   QList<ModuleBase_ViewerPrs>::const_iterator anIt = theList.begin(),
105                                               aLast = theList.end();
106   for (; anIt != aLast; anIt++)
107   {
108     ModuleBase_ViewerPrs aPrs = *anIt;
109     FeaturePtr aFeature = ModelAPI_Feature::feature(aPrs.object());
110     if (aFeature.get()  && !theFeatureList.contains(aFeature))
111       theFeatureList.append(aFeature);
112   }
113 }
114
115 /// Fills attribute and result lists by the selected owner. In case if the attribute is found,
116 /// by the owner shape, it is put to the list. Otherwise if type of owner shape is edge, put the function
117 /// result as is to the list of results.
118 /// \param theOwner a viewer selected owner
119 /// \param theFeature a feature, where the attribute is searched
120 /// \param theSketch a current sketch
121 /// \param theSelectedAttribute an output list of attributes
122 /// \param theSelectedResults an output list of edge results
123 void getAttributesOrResults(const Handle(SelectMgr_EntityOwner)& theOwner,
124                             const FeaturePtr& theFeature, const FeaturePtr& theSketch,
125                             const ResultPtr& theResult,
126                             std::set<AttributePtr>& aSelectedAttributes,
127                             std::set<ResultPtr>& aSelectedResults)
128 {
129   Handle(StdSelect_BRepOwner) aBRepOwner = Handle(StdSelect_BRepOwner)::DownCast(theOwner);
130   if (aBRepOwner.IsNull())
131     return;
132   Handle(AIS_InteractiveObject) anIO = Handle(AIS_InteractiveObject)::DownCast(
133                                                                     aBRepOwner->Selectable());
134   if (aBRepOwner->HasShape()) {
135     const TopoDS_Shape& aShape = aBRepOwner->Shape();
136     TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
137     if (aShapeType == TopAbs_VERTEX) {
138       AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature,
139                                                                     aShape, theSketch);
140       if (aPntAttr.get() != NULL)
141         aSelectedAttributes.insert(aPntAttr);
142     }
143     else if (aShapeType == TopAbs_EDGE &&
144              aSelectedResults.find(theResult) == aSelectedResults.end()) {
145       aSelectedResults.insert(theResult);
146     }
147   }
148 }
149
150 PartSet_SketcherMgr::PartSet_SketcherMgr(PartSet_Module* theModule)
151   : QObject(theModule), myModule(theModule), myIsDragging(false), myDragDone(false),
152     myIsPropertyPanelValueChanged(false), myIsMouseOverWindow(false),
153     myIsMouseOverViewProcessed(true), myPreviousUpdateViewerEnabled(true),
154     myIsPopupMenuActive(false), myIsConstraintsShown(true)
155 {
156   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
157   ModuleBase_IViewer* aViewer = anIWorkshop->viewer();
158
159   myPreviousSelectionEnabled = true;//aViewer->isSelectionEnabled();
160
161   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
162           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
163
164   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
165           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
166
167   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
168           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
169
170   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
171           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
172
173   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
174   XGUI_Workshop* aWorkshop = aConnector->workshop();
175   connect(aWorkshop, SIGNAL(applicationStarted()), this, SLOT(onApplicationStarted()));
176 }
177
178 PartSet_SketcherMgr::~PartSet_SketcherMgr()
179 {
180   if (!myPlaneFilter.IsNull())
181     myPlaneFilter.Nullify();
182 }
183
184 void PartSet_SketcherMgr::onEnterViewPort()
185 {
186   // 1. if the mouse over window, update the next flag. Do not perform update visibility of
187   // created feature because it should be done in onMouseMove(). Some widgets watch
188   // the mouse move and use the cursor position to update own values. If the presentaion is
189   // redisplayed before this update, the feature presentation jumps from reset value to current.
190   myIsMouseOverWindow = true;
191   myIsPropertyPanelValueChanged = false;
192 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
193   qDebug(QString("onEnterViewPort: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
194 #endif
195
196   #ifdef DEBUG_DO_NOT_BY_ENTER
197   return;
198   #endif
199
200   if (!isNestedCreateOperation(getCurrentOperation()))
201     return;
202   // we need change displayed state of the current operation feature
203   // if the feature is presentable, e.g. distance construction. It has no results, so workshop does
204   // not accept a signal about the result created. Nothing is shown until mouse is moved out/in view
205   // port. If the isDisplayed flag is true, the presentable feature is displayed as soon as the
206   // presentation becomes valid and redisplay happens
207   ModuleBase_Operation* aOperation = getCurrentOperation();
208   if (aOperation) {
209     FeaturePtr aFeature = aOperation->feature();
210     if (aFeature.get() && aFeature->data()->isValid()) {
211       visualizeFeature(aOperation, canDisplayObject(aFeature), false);
212     }
213   }
214 }
215
216 void PartSet_SketcherMgr::onLeaveViewPort()
217 {
218   myIsMouseOverViewProcessed = false;
219   myIsMouseOverWindow = false;
220   myIsPropertyPanelValueChanged = false;
221 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
222   qDebug(QString("onLeaveViewPort: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
223 #endif
224
225   #ifdef DEBUG_DO_NOT_BY_ENTER
226   return;
227   #endif
228
229   if (!isNestedCreateOperation(getCurrentOperation()))
230     return;
231   // the method should be performed if the popup menu is called,
232   // the reset of the current widget should not happen
233   if (myIsPopupMenuActive)
234     return;
235
236   // 2. if the mouse IS NOT over window, reset the active widget value and hide the presentation
237   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
238   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
239   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
240   // disable the viewer update in order to avoid visualization of redisplayed feature in viewer
241   // obtained after reset value
242   bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
243   ModuleBase_Operation* aOperation = getCurrentOperation();
244   ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
245   ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
246   if (aActiveWgt) {
247     aActiveWgt->reset();
248   }
249   aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
250
251   // hides the presentation of the current operation feature
252   //myIsPropertyPanelValueChanged = false;
253   // the feature is to be erased here, but it is correct to call canDisplayObject because
254   // there can be additional check (e.g. editor widget in distance constraint)
255   FeaturePtr aFeature = getCurrentOperation()->feature();
256   visualizeFeature(aOperation, canDisplayObject(aFeature));
257 }
258
259 void PartSet_SketcherMgr::onBeforeValuesChangedInPropertyPanel()
260 {
261   if (isNestedCreateOperation(getCurrentOperation()))
262     return;
263   // it is necessary to save current selection in order to restore it after the values are modifed
264   storeSelection();
265
266   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
267   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
268   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
269   myPreviousUpdateViewerEnabled = aDisplayer->enableUpdateViewer(false);
270 }
271
272 void PartSet_SketcherMgr::onAfterValuesChangedInPropertyPanel()
273 {
274   if (isNestedCreateOperation(getCurrentOperation()))
275     return;
276   // it is necessary to restore current selection in order to restore it after the values are modified
277   restoreSelection();
278   myCurrentSelection.clear();
279
280   // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
281   // viewer happens by deselect/select the modified objects. The flag should be restored after
282   // the selection processing. The update viewer should be also called.
283   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
284   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
285   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
286   aDisplayer->enableUpdateViewer(myPreviousUpdateViewerEnabled);
287   aDisplayer->updateViewer();
288 }
289
290 void PartSet_SketcherMgr::onValuesChangedInPropertyPanel()
291 {
292   if (!isNestedCreateOperation(getCurrentOperation()))
293     return;
294
295   // visualize the current operation feature
296   myIsPropertyPanelValueChanged = true;
297   ModuleBase_Operation* aOperation = getCurrentOperation();
298   // the feature is to be erased here, but it is correct to call canDisplayObject because
299   // there can be additional check (e.g. editor widget in distance constraint)
300   FeaturePtr aFeature = getCurrentOperation()->feature();
301   visualizeFeature(aOperation, canDisplayObject(aFeature));
302 }
303
304 void PartSet_SketcherMgr::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
305 {
306   get2dPoint(theWnd, theEvent, myClickedPoint);
307
308   if (!(theEvent->buttons() & Qt::LeftButton))
309     return;
310
311   // Clear dragging mode
312   myIsDragging = false;
313
314   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
315   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
316   if (!aViewer->canDragByMouse())
317     return;
318
319   ModuleBase_Operation* aOperation = getCurrentOperation();
320   if (aOperation && aOperation->isEditOperation()) {
321     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
322     ModuleBase_ModelWidget* aActiveWgt = aPanel->activeWidget();
323     // If the current widget is a selector, do nothing, it processes the mouse press
324     if(aActiveWgt && aActiveWgt->isViewerSelector()) {
325       return;
326     }
327   }
328
329   // Use only for sketch operations
330   if (aOperation && myCurrentSketch) {
331     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
332       return;
333
334     bool isSketcher = isSketchOperation(aOperation);
335     bool isSketchOpe = isNestedSketchOperation(aOperation);
336
337     // Avoid non-sketch operations
338     if ((!isSketchOpe) && (!isSketcher))
339       return;
340
341     bool isEditing = aOperation->isEditOperation();
342
343     // Ignore creation sketch operation
344     if ((!isSketcher) && (!isEditing))
345       return;
346
347     // MoveTo in order to highlight current object
348     aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
349
350     // Remember highlighted objects for editing
351     ModuleBase_ISelection* aSelect = aWorkshop->selection();
352
353     bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
354     storeSelection(!aHasShift);
355
356     if (myCurrentSelection.empty()) {
357       if (isSketchOpe && (!isSketcher))
358         // commit previous operation
359         if (!aOperation->commit())
360           aOperation->abort();
361       return;
362     }
363     // Init flyout point for radius rotation
364     FeaturePtr aFeature = myCurrentSelection.begin().key();
365
366     if (isSketcher) {
367       myIsDragging = true;
368       get2dPoint(theWnd, theEvent, myCurrentPoint);
369       myDragDone = false;
370       launchEditing();
371       if (aFeature.get() != NULL) {
372         std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
373                   std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
374       if (aSPFeature.get() && aSPFeature->getKind() == SketchPlugin_ConstraintRadius::ID()) {
375           DataPtr aData = aSPFeature->data();
376           AttributePtr aAttr = aData->attribute(SketchPlugin_Constraint::FLYOUT_VALUE_PNT());
377           std::shared_ptr<GeomDataAPI_Point2D> aFPAttr = 
378             std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aAttr);
379           aFPAttr->setValue(myCurrentPoint.myCurX, myCurrentPoint.myCurY);
380         }
381       }
382     } else if (isSketchOpe && isEditing) {
383       // If selected another object commit current result
384       aOperation->commit();
385
386       myIsDragging = true;
387       get2dPoint(theWnd, theEvent, myCurrentPoint);
388       myDragDone = false;
389
390       // This is necessary in order to finalize previous operation
391       QApplication::processEvents();
392       launchEditing();
393     }
394   }
395 }
396
397 void PartSet_SketcherMgr::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
398 {
399   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
400   ModuleBase_IViewer* aViewer = aWorkshop->viewer();
401   if (!aViewer->canDragByMouse())
402     return;
403   ModuleBase_Operation* aOp = getCurrentOperation();
404   if (aOp) {
405     if (isNestedSketchOperation(aOp)) {
406       get2dPoint(theWnd, theEvent, myClickedPoint);
407
408       // Only for sketcher operations
409       if (myIsDragging) {
410         if (myDragDone) {
411           //aOp->commit();
412           myCurrentSelection.clear();
413           // Reselect edited object
414           /*aViewer->AISContext()->MoveTo(theEvent->x(), theEvent->y(), theWnd->v3dView());
415           if (theEvent->modifiers() & Qt::ShiftModifier)
416             aViewer->AISContext()->ShiftSelect();
417           else
418             aViewer->AISContext()->Select();
419             */
420         }
421       }
422     }
423   }
424   aWorkshop->viewer()->enableSelection(myPreviousSelectionEnabled);
425   myIsDragging = false;
426 }
427
428 void PartSet_SketcherMgr::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
429 {
430   if (isNestedCreateOperation(getCurrentOperation()) && !myIsMouseOverViewProcessed) {
431     myIsMouseOverViewProcessed = true;
432     // 1. perform the widget mouse move functionality and display the presentation
433     ModuleBase_Operation* aOperation = getCurrentOperation();
434     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
435     ModuleBase_ModelWidget* anActiveWdg = aPanel->activeWidget();
436     // the mouse move should be processed in the widget, if it can in order to visualize correct
437     // presentation. These widgets correct the feature attribute according to the mouse position
438     PartSet_WidgetPoint2D* aPoint2DWdg = dynamic_cast<PartSet_WidgetPoint2D*>(anActiveWdg);
439     if (aPoint2DWdg) {
440       aPoint2DWdg->onMouseMove(theWnd, theEvent);
441     }
442     PartSet_WidgetPoint2dDistance* aDistanceWdg = dynamic_cast<PartSet_WidgetPoint2dDistance*>
443                                                                 (anActiveWdg);
444     if (aDistanceWdg) {
445       aDistanceWdg->onMouseMove(theWnd, theEvent);
446     }
447     // the feature is to be erased here, but it is correct to call canDisplayObject because
448     // there can be additional check (e.g. editor widget in distance constraint)
449     FeaturePtr aFeature = getCurrentOperation()->feature();
450     visualizeFeature(aOperation, canDisplayObject(aFeature));
451   }
452
453   myClickedPoint.clear();
454
455   if (myIsDragging) {
456     // 1. the current selection is saved in the mouse press method in order to restore it after moving
457     // 2. the enable selection in the viewer should be temporary switched off in order to ignore
458     // mouse press signal in the viewer(it call Select for AIS context and the dragged objects are
459     // deselected). This flag should be restored in the slot, processed the mouse release signal.
460     ModuleBase_IViewer* aViewer = myModule->workshop()->viewer();
461     aViewer->enableSelection(false);
462
463     ModuleBase_Operation* aOperation = getCurrentOperation();
464     if (!aOperation)
465       return;
466     if (isSketchOperation(aOperation))
467       return; // No edit operation activated
468
469     Handle(V3d_View) aView = theWnd->v3dView();
470     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
471     double aX, aY;
472     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
473     double dX =  aX - myCurrentPoint.myCurX;
474     double dY =  aY - myCurrentPoint.myCurY;
475
476     ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
477     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
478     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
479     // 3. the flag to disable the update viewer should be set in order to avoid blinking in the 
480     // viewer happens by deselect/select the modified objects. The flag should be restored after
481     // the selection processing. The update viewer should be also called.
482     bool isEnableUpdateViewer = aDisplayer->enableUpdateViewer(false);
483
484     static Events_ID aMoveEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
485     //static Events_ID aUpdateEvent = Events_Loop::eventByName(EVENT_OBJECT_UPDATED);
486     FeatureToSelectionMap::const_iterator anIt = myCurrentSelection.begin(),
487                                           aLast = myCurrentSelection.end();
488     // 4. the features and attributes modification(move)
489     for (; anIt != aLast; anIt++) {
490       FeaturePtr aFeature = anIt.key();
491
492       std::set<AttributePtr> anAttributes = anIt.value().first;
493       // Process selection by attribute: the priority to the attribute
494       if (!anAttributes.empty()) {
495         std::set<AttributePtr>::const_iterator anAttIt = anAttributes.begin(),
496                                                anAttLast = anAttributes.end();
497         for (; anAttIt != anAttLast; anAttIt++) {
498           AttributePtr anAttr = *anAttIt;
499           if (anAttr.get() == NULL)
500             continue;
501           std::string aAttrId = anAttr->id();
502           DataPtr aData = aFeature->data();
503           if (aData->isValid()) {
504             std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
505               std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(aAttrId));
506             if (aPoint.get() != NULL) {
507               bool isImmutable = aPoint->setImmutable(true);
508               aPoint->move(dX, dY);
509               ModelAPI_EventCreator::get()->sendUpdated(aFeature, aMoveEvent);
510               aPoint->setImmutable(isImmutable);
511             }
512           }
513         }
514       } else {
515         // Process selection by feature
516         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
517           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
518         if (aSketchFeature) {
519           aSketchFeature->move(dX, dY);
520           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, aMoveEvent);
521         }
522       }
523     }
524     Events_Loop::loop()->flush(aMoveEvent); // up all move events - to be processed in the solver
525     //Events_Loop::loop()->flush(aUpdateEvent); // up update events - to redisplay presentations
526
527     // 5. it is necessary to save current selection in order to restore it after the features moving
528     restoreSelection();
529     // 6. restore the update viewer flag and call this update
530     aDisplayer->enableUpdateViewer(isEnableUpdateViewer);
531     aDisplayer->updateViewer();
532
533     myDragDone = true;
534     myCurrentPoint.setValue(aX, aY);
535   }
536 }
537
538 void PartSet_SketcherMgr::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
539 {
540   ModuleBase_Operation* aOperation = getCurrentOperation();
541   if (aOperation && aOperation->isEditOperation()) {
542     std::string aId = aOperation->id().toStdString();
543     if (isDistanceOperation(aOperation))
544     {
545       // Activate dimension value editing on double click
546       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
547       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
548       // Find corresponded widget to activate value editing
549       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
550         if (aWgt->attributeID() == "ConstraintValue") {
551           aWgt->focusTo();
552           return;
553         }
554       }
555     }
556   }
557 }
558
559 void PartSet_SketcherMgr::onApplicationStarted()
560 {
561   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
562   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
563   XGUI_Workshop* aWorkshop = aConnector->workshop();
564   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
565   if (aPropertyPanel) {
566     connect(aPropertyPanel, SIGNAL(beforeWidgetActivated(ModuleBase_ModelWidget*)),
567             this, SLOT(onBeforeWidgetActivated(ModuleBase_ModelWidget*)));
568   }
569
570   XGUI_ViewerProxy* aViewerProxy = aWorkshop->viewer();
571   connect(aViewerProxy, SIGNAL(enterViewPort()), this, SLOT(onEnterViewPort()));
572   connect(aViewerProxy, SIGNAL(leaveViewPort()), this, SLOT(onLeaveViewPort()));
573
574   XGUI_ContextMenuMgr* aContextMenuMgr = aWorkshop->contextMenuMgr();
575   connect(aContextMenuMgr, SIGNAL(beforeContextMenu()), this, SLOT(onBeforeContextMenu()));
576   connect(aContextMenuMgr, SIGNAL(afterContextMenu()), this, SLOT(onAfterContextMenu()));
577 }
578
579 void PartSet_SketcherMgr::onBeforeWidgetActivated(ModuleBase_ModelWidget* theWidget)
580 {
581   if (!myClickedPoint.myIsInitialized)
582     return;
583
584   ModuleBase_Operation* aOperation = getCurrentOperation();
585   // the distance constraint feature should not use the clickedd point
586   // this is workaround in order to don't throw down the flyout point value,
587   // set by execute() method of these type of features
588   if (isDistanceOperation(aOperation))
589     return;
590
591   PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(theWidget);
592   if (aPnt2dWgt) {
593     aPnt2dWgt->setPoint(myClickedPoint.myCurX, myClickedPoint.myCurY);
594   }
595 }
596
597 void PartSet_SketcherMgr::onBeforeContextMenu()
598 {
599   myIsPopupMenuActive = true;
600 }
601
602 void PartSet_SketcherMgr::onAfterContextMenu()
603 {
604   myIsPopupMenuActive = false;
605 }
606
607 void PartSet_SketcherMgr::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
608                                      Point& thePoint)
609 {
610   Handle(V3d_View) aView = theWnd->v3dView();
611   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
612   double aX, anY;
613   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, anY);
614   thePoint.setValue(aX, anY);
615 }
616
617 void PartSet_SketcherMgr::launchEditing()
618 {
619   if (!myCurrentSelection.empty()) {
620     FeaturePtr aFeature = myCurrentSelection.begin().key();
621     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
622               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
623     if (aSPFeature) {
624       myModule->editFeature(aSPFeature);
625     }
626   }
627 }
628
629
630 const QStringList& PartSet_SketcherMgr::sketchOperationIdList()
631 {
632   static QStringList aIds;
633   if (aIds.size() == 0) {
634     aIds << SketchPlugin_Line::ID().c_str();
635     aIds << SketchPlugin_Point::ID().c_str();
636     aIds << SketchPlugin_Arc::ID().c_str();
637     aIds << SketchPlugin_Circle::ID().c_str();
638     aIds << SketchPlugin_ConstraintFillet::ID().c_str();
639     aIds.append(constraintsIdList());
640   }
641   return aIds;
642 }
643
644 const QStringList& PartSet_SketcherMgr::constraintsIdList()
645 {
646   static QStringList aIds;
647   if (aIds.size() == 0) {
648     aIds << SketchPlugin_ConstraintLength::ID().c_str();
649     aIds << SketchPlugin_ConstraintDistance::ID().c_str();
650     aIds << SketchPlugin_ConstraintRigid::ID().c_str();
651     aIds << SketchPlugin_ConstraintRadius::ID().c_str();
652     aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
653     aIds << SketchPlugin_ConstraintParallel::ID().c_str();
654     aIds << SketchPlugin_ConstraintHorizontal::ID().c_str();
655     aIds << SketchPlugin_ConstraintVertical::ID().c_str();
656     aIds << SketchPlugin_ConstraintEqual::ID().c_str();
657     aIds << SketchPlugin_ConstraintTangent::ID().c_str();
658     aIds << SketchPlugin_ConstraintCoincidence::ID().c_str();
659     aIds << SketchPlugin_ConstraintMirror::ID().c_str();
660     aIds << SketchPlugin_MultiRotation::ID().c_str();
661     aIds << SketchPlugin_MultiTranslation::ID().c_str();
662   }
663   return aIds;
664 }
665
666 void PartSet_SketcherMgr::sketchSelectionModes(QIntList& theModes)
667 {
668   theModes.clear();
669
670   theModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
671   theModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
672   theModes.append(SketcherPrs_Tools::Sel_Constraint);
673   theModes.append(TopAbs_VERTEX);
674   theModes.append(TopAbs_EDGE);
675 }
676
677 bool PartSet_SketcherMgr::isSketchOperation(ModuleBase_Operation* theOperation)
678 {
679   return theOperation && theOperation->id().toStdString() == SketchPlugin_Sketch::ID();
680 }
681
682 bool PartSet_SketcherMgr::isNestedSketchOperation(ModuleBase_Operation* theOperation)
683 {
684   return theOperation &&
685          PartSet_SketcherMgr::sketchOperationIdList().contains(theOperation->id());
686 }
687
688 bool PartSet_SketcherMgr::isNestedCreateOperation(ModuleBase_Operation* theOperation)
689 {
690   return theOperation && !theOperation->isEditOperation() && isNestedSketchOperation(theOperation);
691 }
692
693 bool PartSet_SketcherMgr::isEntity(const std::string& theId)
694 {
695   return (theId == SketchPlugin_Line::ID()) ||
696          (theId == SketchPlugin_Point::ID()) ||
697          (theId == SketchPlugin_Arc::ID()) ||
698          (theId == SketchPlugin_Circle::ID());
699 }
700
701 bool PartSet_SketcherMgr::isDistanceOperation(ModuleBase_Operation* theOperation)
702 {
703   std::string aId = theOperation ? theOperation->id().toStdString() : "";
704
705   return (aId == SketchPlugin_ConstraintLength::ID()) ||
706          (aId == SketchPlugin_ConstraintDistance::ID()) ||
707          (aId == SketchPlugin_ConstraintRadius::ID());
708 }
709
710 void PartSet_SketcherMgr::startSketch(ModuleBase_Operation* theOperation)
711 {
712   myModule->onViewTransformed();
713
714   // Display all sketcher sub-Objects
715   myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
716   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
717
718   // Hide sketcher result
719   std::list<ResultPtr> aResults = myCurrentSketch->results();
720   std::list<ResultPtr>::const_iterator aIt;
721   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
722     (*aIt)->setDisplayed(false);
723   }
724   myCurrentSketch->setDisplayed(false);
725
726   // Display sketcher objects
727   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
728     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
729     std::list<ResultPtr> aResults = aFeature->results();
730     std::list<ResultPtr>::const_iterator aIt;
731     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
732       (*aIt)->setDisplayed(true);
733     }
734     aFeature->setDisplayed(true);
735   }
736
737   if (myPlaneFilter.IsNull()) 
738     myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
739
740   myModule->workshop()->viewer()->addSelectionFilter(myPlaneFilter);
741
742   bool aHasPlane = false;
743   if (theOperation->isEditOperation()) {
744     // If it is editing of sketch then it means that plane is already defined
745     std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
746     if (aPln.get()) {
747       myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
748       aHasPlane = true;
749     }
750   }
751   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
752   // all sketch objects should be activated in the sketch selection modes by edit operation start
753   // in case of creation operation, there is an active widget, which activates own selection mode
754   if (theOperation->isEditOperation() && aHasPlane)
755     aConnector->activateModuleSelectionModes();
756 }
757
758 void PartSet_SketcherMgr::stopSketch(ModuleBase_Operation* theOperation)
759 {
760   myIsMouseOverWindow = false;
761   myIsConstraintsShown = true;
762 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
763   qDebug(QString("stopSketch: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
764 #endif
765   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myModule->workshop());
766
767   DataPtr aData = myCurrentSketch->data();
768   if (!aData->isValid()) {
769     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
770     // The sketch was aborted
771     myCurrentSketch = CompositeFeaturePtr();
772     myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
773
774     // Erase all sketcher objects
775     QStringList aSketchIds = sketchOperationIdList();
776     QObjectPtrList aObjects = aDisplayer->displayedObjects();
777     foreach (ObjectPtr aObj, aObjects) {
778       DataPtr aObjData = aObj->data();
779       if (!aObjData->isValid())
780         aObj->setDisplayed(false);
781     }
782   }
783   else {
784   // Hide all sketcher sub-Objects
785   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
786     FeaturePtr aFeature = myCurrentSketch->subFeature(i);
787     std::list<ResultPtr> aResults = aFeature->results();
788     std::list<ResultPtr>::const_iterator aIt;
789     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
790       (*aIt)->setDisplayed(false);
791     }
792     aFeature->setDisplayed(false);
793   }
794   // Display sketcher result
795   std::list<ResultPtr> aResults = myCurrentSketch->results();
796   std::list<ResultPtr>::const_iterator aIt;
797   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
798     (*aIt)->setDisplayed(true);
799   }
800   myCurrentSketch->setDisplayed(true);
801     
802   myCurrentSketch = CompositeFeaturePtr();
803   myModule->workshop()->viewer()->removeSelectionFilter(myPlaneFilter);
804
805   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
806   }
807   // restore the module selection modes, which were changed on startSketch
808   aConnector->activateModuleSelectionModes();
809 }
810
811 void PartSet_SketcherMgr::startNestedSketch(ModuleBase_Operation* theOperation)
812 {
813   if (constraintsIdList().contains(theOperation->id())) {
814     // Show constraints if a constraint was created
815     onShowConstraintsToggle(true);
816   }
817   connectToPropertyPanel(true);
818 }
819
820 void PartSet_SketcherMgr::stopNestedSketch(ModuleBase_Operation* theOp)
821 {
822   connectToPropertyPanel(false);
823   myIsPropertyPanelValueChanged = false;
824   myIsMouseOverViewProcessed = true;
825 }
826
827 void PartSet_SketcherMgr::commitNestedSketch(ModuleBase_Operation* theOperation)
828 {
829   if (isNestedCreateOperation(theOperation)) {
830     FeaturePtr aFeature = theOperation->feature();
831     // it is necessary to check the the feature data validity because
832     // some kind of features are removed by an operation commit(the macro state of a feature)
833     if (aFeature.get() && aFeature->data()->isValid()) {
834       visualizeFeature(theOperation, true);
835     }
836   }
837 }
838
839 bool PartSet_SketcherMgr::canUndo() const
840 {
841   return isNestedCreateOperation(getCurrentOperation());
842 }
843
844 bool PartSet_SketcherMgr::canRedo() const
845 {
846   return isNestedCreateOperation(getCurrentOperation());
847 }
848
849 bool PartSet_SketcherMgr::canDisplayObject(const ObjectPtr& theObject) const
850 {
851   bool aCanDisplay = true;
852
853   bool aHasActiveSketch = activeSketch().get() != NULL;
854   if (aHasActiveSketch) {
855     // 1. the sketch feature should not be displayed during the sketch active operation
856     // it is hidden by a sketch operation start and shown by a sketch stop, just the sketch 
857     // nested features can be visualized
858     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
859     if (aFeature.get() != NULL && aFeature == activeSketch()) {
860       aCanDisplay = false;
861     }
862   }
863   else { // there are no an active sketch
864     // 2. sketch sub-features should not be visualized if the sketch operation is not active
865     FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
866     if (aFeature.get() != NULL) {
867       std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
868                               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
869       if (aSketchFeature.get()) {
870         aCanDisplay = false;
871       }
872     }
873   }
874
875   // 3. the method should not filter the objects, which are not related to the current operation.
876   // The object is filtered just if it is a current operation feature or this feature result
877   bool isObjectFound = false;
878   ModuleBase_Operation* anOperation = getCurrentOperation();
879   if (anOperation) {
880     FeaturePtr aFeature = anOperation->feature();
881     if (aFeature.get()) {
882       std::list<ResultPtr> aResults = aFeature->results();
883       if (theObject == aFeature)
884         isObjectFound = true;
885       else {
886         std::list<ResultPtr>::const_iterator anIt = aResults.begin(), aLast = aResults.end();
887         for (; anIt != aLast; anIt++) {
888           isObjectFound = *anIt == theObject;
889         }
890       }
891     }
892   }
893   if (!isObjectFound) 
894     return aCanDisplay;
895   
896   // 4. For created nested feature operation do not display the created feature if
897   // the mouse curstor leaves the OCC window.
898   // The correction cases, which ignores this condition:
899   // a. the property panel values modification
900   // b. the popup menu activated
901   // c. widget editor control
902   #ifndef DEBUG_DO_NOT_BY_ENTER
903   if (aCanDisplay && isNestedCreateOperation(getCurrentOperation())) {
904     ModuleBase_Operation* aOperation = getCurrentOperation();
905     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
906     ModuleBase_ModelWidget* anActiveWdg = aPanel ? aPanel->activeWidget() : 0;
907     ModuleBase_WidgetEditor* anEditorWdg = anActiveWdg ? dynamic_cast<ModuleBase_WidgetEditor*>(anActiveWdg) : 0;
908     // the active widget editor should not influence here. The presentation should be visible always
909     // when this widget is active.
910     if (!anEditorWdg && !myIsPopupMenuActive) {
911       // during a nested create operation, the feature is redisplayed only if the mouse over view
912       // of there was a value modified in the property panel after the mouse left the view
913       aCanDisplay = canDisplayCurrentCreatedFeature();
914     }
915   }
916   #endif
917   return aCanDisplay;
918 }
919
920 bool PartSet_SketcherMgr::canDisplayCurrentCreatedFeature() const
921 {
922   return myIsPropertyPanelValueChanged || myIsMouseOverWindow;
923 #ifdef DEBUG_MOUSE_OVER_WINDOW_FLAGS
924   qDebug(QString("canDisplayCurrentCreatedFeature: %1").arg(mouseOverWindowFlagsInfo()).toStdString().c_str());
925 #endif
926 }
927
928 bool PartSet_SketcherMgr::isObjectOfSketch(const ObjectPtr& theObject) const
929 {
930   bool isFoundObject = false;
931
932   FeaturePtr anObjectFeature = ModelAPI_Feature::feature(theObject);
933   if (anObjectFeature.get()) {
934     int aSize = myCurrentSketch->numberOfSubs();
935     for (int i = 0; i < myCurrentSketch->numberOfSubs() && !isFoundObject; i++) {
936       FeaturePtr aCurrentFeature = myCurrentSketch->subFeature(i);
937       isFoundObject = myCurrentSketch->subFeature(i) == anObjectFeature;
938     }
939   }
940   return isFoundObject;
941 }
942
943 void PartSet_SketcherMgr::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
944 {
945   myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
946 }
947
948 void PartSet_SketcherMgr::getCurrentSelection(const FeaturePtr& theFeature,
949                                               const FeaturePtr& theSketch,
950                                               ModuleBase_IWorkshop* theWorkshop,
951                                               FeatureToSelectionMap& theSelection)
952 {
953   if (theFeature.get() == NULL)
954     return;
955
956   std::set<AttributePtr> aSelectedAttributes;
957   std::set<ResultPtr> aSelectedResults;
958
959   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
960   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
961   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
962   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
963
964   std::list<ResultPtr> aResults = theFeature->results();
965   std::list<ResultPtr>::const_iterator aIt;
966   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
967   {
968     ResultPtr aResult = *aIt;
969     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
970     if (aAISObj.get() == NULL)
971       continue;
972     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
973     for (aContext->InitSelected(); aContext->MoreSelected(); aContext->NextSelected())
974     {
975       Handle(SelectMgr_EntityOwner) anOwner = aContext->SelectedOwner();
976       if (anOwner->Selectable() != anAISIO)
977         continue;
978       getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
979                              aSelectedAttributes, aSelectedResults);
980     }
981     for (aContext->InitDetected(); aContext->MoreDetected(); aContext->NextDetected()) {
982       Handle(SelectMgr_EntityOwner) anOwner = aContext->DetectedOwner();
983       if (anOwner->Selectable() != anAISIO)
984         continue;
985       getAttributesOrResults(anOwner, theFeature, theSketch, aResult,
986                              aSelectedAttributes, aSelectedResults);
987     }
988   }
989   theSelection[theFeature] = std::make_pair(aSelectedAttributes, aSelectedResults);
990 }
991
992 void PartSet_SketcherMgr::getSelectionOwners(const FeaturePtr& theFeature,
993                                              const FeaturePtr& theSketch,
994                                              ModuleBase_IWorkshop* theWorkshop,
995                                              const FeatureToSelectionMap& theSelection,
996                                              SelectMgr_IndexedMapOfOwner& anOwnersToSelect)
997 {
998   if (theFeature.get() == NULL)
999     return;
1000
1001   FeatureToSelectionMap::const_iterator anIt = theSelection.find(theFeature);
1002   std::set<AttributePtr> aSelectedAttributes = anIt.value().first;
1003   std::set<ResultPtr> aSelectedResults = anIt.value().second;
1004
1005   ModuleBase_IViewer* aViewer = theWorkshop->viewer();
1006   Handle(AIS_InteractiveContext) aContext = aViewer->AISContext();
1007   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWorkshop);
1008   XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
1009
1010   // 1. found the feature's owners. Check the AIS objects of the constructions
1011   AISObjectPtr aAISObj = aDisplayer->getAISObject(theFeature);
1012   if (aAISObj.get() != NULL && aSelectedAttributes.empty() && aSelectedResults.empty()) {
1013     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1014
1015     SelectMgr_IndexedMapOfOwner aSelectedOwners;
1016     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1017     for  (Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++) {
1018       Handle(SelectMgr_EntityOwner) anOwner = aSelectedOwners(i);
1019       if (!anOwner.IsNull())
1020         anOwnersToSelect.Add(anOwner);
1021     }
1022   }
1023
1024   // 2. found the feature results's owners
1025   std::list<ResultPtr> aResults = theFeature->results();
1026   std::list<ResultPtr>::const_iterator aIt;
1027   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt)
1028   {
1029     ResultPtr aResult = *aIt;
1030     AISObjectPtr aAISObj = aDisplayer->getAISObject(aResult);
1031     if (aAISObj.get() == NULL)
1032       continue; 
1033     Handle(AIS_InteractiveObject) anAISIO = aAISObj->impl<Handle(AIS_InteractiveObject)>();
1034
1035     SelectMgr_IndexedMapOfOwner aSelectedOwners;  
1036     aConnector->workshop()->selector()->selection()->entityOwners(anAISIO, aSelectedOwners);
1037     for  ( Standard_Integer i = 1, n = aSelectedOwners.Extent(); i <= n; i++ ) {
1038       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(aSelectedOwners(i));
1039       if ( anOwner.IsNull() || !anOwner->HasShape() )
1040         continue;
1041       const TopoDS_Shape& aShape = anOwner->Shape();
1042       TopAbs_ShapeEnum aShapeType = aShape.ShapeType();
1043       if (aShapeType == TopAbs_VERTEX) {
1044         AttributePtr aPntAttr = PartSet_Tools::findAttributeBy2dPoint(theFeature, aShape, theSketch);
1045         if (aPntAttr.get() != NULL &&
1046             aSelectedAttributes.find(aPntAttr) != aSelectedAttributes.end()) {
1047           anOwnersToSelect.Add(anOwner);
1048         }
1049       }
1050       else if (aShapeType == TopAbs_EDGE) {
1051         bool aFound = aSelectedResults.find(aResult) != aSelectedResults.end();
1052         if (aSelectedResults.find(aResult) != aSelectedResults.end() &&
1053             anOwnersToSelect.FindIndex(anOwner) <= 0)
1054           anOwnersToSelect.Add(anOwner);
1055       }
1056     }
1057   }
1058 }
1059
1060 void PartSet_SketcherMgr::connectToPropertyPanel(const bool isToConnect)
1061 {
1062   ModuleBase_IWorkshop* anIWorkshop = myModule->workshop();
1063   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(anIWorkshop);
1064   XGUI_Workshop* aWorkshop = aConnector->workshop();
1065   XGUI_PropertyPanel* aPropertyPanel = aWorkshop->propertyPanel();
1066   if (aPropertyPanel) {
1067     const QList<ModuleBase_ModelWidget*>& aWidgets = aPropertyPanel->modelWidgets();
1068     foreach (ModuleBase_ModelWidget* aWidget, aWidgets) {
1069       if (isToConnect) {
1070         connect(aWidget, SIGNAL(beforeValuesChanged()),
1071                 this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1072         connect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1073         connect(aWidget, SIGNAL(afterValuesChanged()),
1074                 this, SLOT(onAfterValuesChangedInPropertyPanel()));
1075       }
1076       else {
1077         disconnect(aWidget, SIGNAL(beforeValuesChanged()),
1078                    this, SLOT(onBeforeValuesChangedInPropertyPanel()));
1079         disconnect(aWidget, SIGNAL(valuesChanged()), this, SLOT(onValuesChangedInPropertyPanel()));
1080         disconnect(aWidget, SIGNAL(afterValuesChanged()),
1081                    this, SLOT(onAfterValuesChangedInPropertyPanel()));
1082       }
1083     }
1084   }
1085 }
1086
1087 ModuleBase_Operation* PartSet_SketcherMgr::getCurrentOperation() const
1088 {
1089   return myModule->workshop()->currentOperation();
1090 }
1091
1092 void PartSet_SketcherMgr::visualizeFeature(ModuleBase_Operation* theOperation,
1093                                            const bool isToDisplay,
1094                                            const bool isFlushRedisplay)
1095 {
1096   #ifdef DEBUG_DO_NOT_BY_ENTER
1097   return;
1098   #endif
1099
1100   if (!theOperation || theOperation->isEditOperation())
1101     return;
1102
1103   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1104   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1105
1106   // 1. change visibility of the object itself, here the presentable object is processed,
1107   // e.g. constraints features
1108   FeaturePtr aFeature = theOperation->feature();
1109   std::list<ResultPtr> aResults = aFeature->results();
1110   if (isToDisplay)
1111     aFeature->setDisplayed(true);
1112   else
1113     aFeature->setDisplayed(false);
1114
1115   // change visibility of the object results, e.g. non-constraint features
1116   std::list<ResultPtr>::const_iterator aIt;
1117   for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
1118     if (isToDisplay) {
1119       (*aIt)->setDisplayed(true);
1120     }
1121     else {
1122       (*aIt)->setDisplayed(false);
1123     }
1124   }
1125   if (isFlushRedisplay)
1126     Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1127 }
1128
1129 void PartSet_SketcherMgr::storeSelection(const bool theHighlightedOnly)
1130 {
1131   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1132   ModuleBase_ISelection* aSelect = aWorkshop->selection();
1133   QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
1134
1135   QList<FeaturePtr> aFeatureList;
1136   if (theHighlightedOnly) {
1137     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1138   }
1139   else {
1140     fillFeatureList(aHighlighted, myCurrentSketch, aFeatureList);
1141
1142     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
1143     fillFeatureList(aSelected, myCurrentSketch, aFeatureList);
1144   }
1145
1146   // 1. it is necessary to save current selection in order to restore it after the features moving
1147   myCurrentSelection.clear();
1148   QList<FeaturePtr>::const_iterator anIt = aFeatureList.begin(), aLast = aFeatureList.end();
1149   for (; anIt != aLast; anIt++) {
1150     getCurrentSelection(*anIt, myCurrentSketch, aWorkshop, myCurrentSelection);
1151   }
1152   //qDebug(QString("  storeSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1153 }
1154
1155 void PartSet_SketcherMgr::restoreSelection()
1156 {
1157   //qDebug(QString("restoreSelection: %1").arg(myCurrentSelection.size()).toStdString().c_str());
1158   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1159   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1160   FeatureToSelectionMap::const_iterator aSIt = myCurrentSelection.begin(),
1161                                         aSLast = myCurrentSelection.end();
1162   SelectMgr_IndexedMapOfOwner anOwnersToSelect;
1163   for (; aSIt != aSLast; aSIt++) {
1164     anOwnersToSelect.Clear();
1165     getSelectionOwners(aSIt.key(), myCurrentSketch, aWorkshop, myCurrentSelection,
1166                         anOwnersToSelect);
1167     aConnector->workshop()->selector()->setSelectedOwners(anOwnersToSelect, false);
1168   }
1169 }
1170
1171 void PartSet_SketcherMgr::onShowConstraintsToggle(bool theOn)
1172 {
1173   if (myIsConstraintsShown == theOn)
1174     return;
1175   if (myCurrentSketch.get() == NULL)
1176     return;
1177
1178   myIsConstraintsShown = theOn;
1179
1180   ModuleBase_IWorkshop* aWorkshop = myModule->workshop();
1181   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(aWorkshop);
1182
1183   const QStringList& aConstrIds = constraintsIdList();
1184   for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
1185     FeaturePtr aSubFeature = myCurrentSketch->subFeature(i);
1186     if (aConstrIds.contains(QString(aSubFeature->getKind().c_str()))) {
1187       if (myIsConstraintsShown) 
1188         aSubFeature->setDisplayed(true);
1189       else
1190         aSubFeature->setDisplayed(false);
1191     }
1192   }
1193   Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_TO_REDISPLAY));
1194 }
1195
1196 QString PartSet_SketcherMgr::mouseOverWindowFlagsInfo() const
1197 {
1198   return QString("myIsPropertyPanelValueChanged = %1,    myIsMouseOverWindow = %2")
1199      .arg(myIsPropertyPanelValueChanged).arg(myIsMouseOverWindow);
1200 }