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