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