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