Salome HOME
Remove extra includes
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include "PartSet_Module.h"
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_WidgetSketchLabel.h>
4 #include <PartSet_Validators.h>
5 #include <PartSet_Tools.h>
6 #include <PartSet_WidgetPoint2d.h>
7 #include <PartSet_WidgetPoint2dDistance.h>
8 #include <PartSet_WidgetShapeSelector.h>
9
10 #include <ModuleBase_Operation.h>
11 #include <ModuleBase_IViewer.h>
12 #include <ModuleBase_IViewWindow.h>
13 #include <ModuleBase_IPropertyPanel.h>
14 #include <ModuleBase_WidgetEditor.h>
15 #include <ModuleBase_FilterFactory.h>
16 #include <ModuleBase_FilterLinearEdge.h>
17
18
19 #include <ModelAPI_Object.h>
20 #include <ModelAPI_Events.h>
21 #include <ModelAPI_Validator.h>
22 #include <ModelAPI_Data.h>
23 #include <ModelAPI_Session.h>
24
25 #include <GeomDataAPI_Point2D.h>
26 #include <GeomDataAPI_Point.h>
27 #include <GeomDataAPI_Dir.h>
28
29 #include <XGUI_Displayer.h>
30 #include <XGUI_Workshop.h>
31 #include <XGUI_OperationMgr.h>
32 #include <XGUI_PropertyPanel.h>
33 #include <XGUI_ModuleConnector.h>
34 #include <XGUI_Tools.h>
35
36 #include <SketchPlugin_Line.h>
37 #include <SketchPlugin_Sketch.h>
38 #include <SketchPlugin_Point.h>
39 #include <SketchPlugin_Arc.h>
40 #include <SketchPlugin_Circle.h>
41 #include <SketchPlugin_ConstraintLength.h>
42 #include <SketchPlugin_ConstraintDistance.h>
43 #include <SketchPlugin_ConstraintParallel.h>
44 #include <SketchPlugin_ConstraintPerpendicular.h>
45 #include <SketchPlugin_ConstraintRadius.h>
46 #include <SketchPlugin_ConstraintRigid.h>
47
48 #include <Events_Loop.h>
49
50 #include <StdSelect_TypeOfFace.hxx>
51 #include <TopoDS_Vertex.hxx>
52 #include <TopoDS.hxx>
53 #include <TopoDS_Shape.hxx>
54 #include <BRep_Tool.hxx>
55
56 #include <QObject>
57 #include <QMouseEvent>
58 #include <QString>
59 #include <QTimer>
60 #include <QApplication>
61
62 #include <GeomAlgoAPI_FaceBuilder.h>
63 #include <GeomDataAPI_Dir.h>
64
65 #ifdef _DEBUG
66 #include <QDebug>
67 #endif
68
69
70 /// Returns list of unique objects by sum of objects from List1 and List2
71 QList<ObjectPtr> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
72                                        const QList<ModuleBase_ViewerPrs>& theList2)
73 {
74   QList<ObjectPtr> aRes;
75   foreach (ModuleBase_ViewerPrs aPrs, theList1) {
76     if (!aRes.contains(aPrs.object()))
77       aRes.append(aPrs.object());
78   }
79   foreach (ModuleBase_ViewerPrs aPrs, theList2) {
80     if (!aRes.contains(aPrs.object()))
81       aRes.append(aPrs.object());
82   }
83   return aRes;
84 }
85
86 /*!Create and return new instance of XGUI_Module*/
87 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
88 {
89   return new PartSet_Module(theWshop);
90 }
91
92 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
93   : ModuleBase_IModule(theWshop), 
94   myIsDragging(false), myRestartingMode(RM_None), myDragDone(false)
95 {
96   //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
97   ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
98   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
99           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
100
101   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
102           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
103
104   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
105           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
106
107   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
108           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
109
110   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
111   XGUI_Workshop* aWorkshop = aConnector->workshop();
112
113   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
114   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
115
116   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
117           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
118 }
119
120 PartSet_Module::~PartSet_Module()
121 {
122   if (!myDocumentShapeFilter.IsNull())
123     myDocumentShapeFilter.Nullify();
124   if (!myPlaneFilter.IsNull())
125     myPlaneFilter.Nullify();
126 }
127
128 void PartSet_Module::registerValidators()
129 {
130   //Registering of validators
131   SessionPtr aMgr = ModelAPI_Session::get();
132   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
133   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
134   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
135   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
136   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
137   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
138   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
139 }
140
141 void PartSet_Module::registerFilters()
142 {
143   //Registering of selection filters
144   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
145   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
146
147   aFactory->registerFilter("LinearEdgeFilter", new ModuleBase_FilterLinearEdge);
148 }
149
150 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation) 
151 {
152   if (theOperation->isEditOperation())
153     return;
154   /// Restart sketcher operations automatically
155   FeaturePtr aFeature = theOperation->feature();
156   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
157             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
158   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
159                      myRestartingMode == RM_EmptyFeatureUsed)) {
160     myLastOperationId = theOperation->id();
161     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
162     launchOperation(myLastOperationId);
163   }
164   breakOperationSequence();
165 }
166
167 void PartSet_Module::breakOperationSequence()
168 {
169   myLastOperationId = "";
170   myLastFeature = FeaturePtr();
171   myRestartingMode = RM_None;
172 }
173
174 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
175 {
176   breakOperationSequence();
177 }
178
179 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
180 {
181   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
182     // Display all sketcher sub-Objects
183     myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
184     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
185     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
186
187     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
188       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
189       std::list<ResultPtr> aResults = aFeature->results();
190       std::list<ResultPtr>::const_iterator aIt;
191       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
192         aDisplayer->display((*aIt), false);
193       }
194       aDisplayer->display(aFeature);
195     }
196     // Hide sketcher result
197     std::list<ResultPtr> aResults = myCurrentSketch->results();
198     std::list<ResultPtr>::const_iterator aIt;
199     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
200       aDisplayer->erase((*aIt), false);
201     }
202     aDisplayer->erase(myCurrentSketch);
203
204
205     if (myPlaneFilter.IsNull()) 
206       myPlaneFilter = new ModuleBase_ShapeInPlaneFilter();
207     myWorkshop->viewer()->addSelectionFilter(myPlaneFilter);
208     if (theOperation->isEditOperation()) {
209       // If it is editing of sketch then it means that plane is already defined
210       std::shared_ptr<GeomAPI_Pln> aPln = PartSet_Tools::sketchPlane(myCurrentSketch);
211       myPlaneFilter->setPlane(aPln->impl<gp_Pln>());
212     }
213   }
214   if (myDocumentShapeFilter.IsNull())
215     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
216   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
217 }
218
219 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
220 {
221   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
222     DataPtr aData = myCurrentSketch->data();
223     if ((!aData) || (!aData->isValid())) {
224       // The sketch was aborted
225       myCurrentSketch = CompositeFeaturePtr();
226       return; 
227     }
228     // Hide all sketcher sub-Objects
229     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
230     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
231     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
232       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
233       std::list<ResultPtr> aResults = aFeature->results();
234       std::list<ResultPtr>::const_iterator aIt;
235       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
236         aDisplayer->erase((*aIt), false);
237       }
238       aDisplayer->erase(aFeature, false);
239     }
240     // Display sketcher result
241     std::list<ResultPtr> aResults = myCurrentSketch->results();
242     std::list<ResultPtr>::const_iterator aIt;
243     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
244       aDisplayer->display((*aIt), false);
245     }
246     aDisplayer->display(myCurrentSketch);
247     
248     myCurrentSketch = CompositeFeaturePtr();
249     myWorkshop->viewer()->removeSelectionFilter(myPlaneFilter);
250   }
251   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
252 }
253
254 void PartSet_Module::onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>& thePln)
255 {
256   myPlaneFilter->setPlane(thePln->impl<gp_Pln>());
257 }
258
259
260 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
261 {
262   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
263   if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) && 
264     (theOperation->isEditOperation())) {
265     // we have to manually activate the sketch label in edit mode
266       aPanel->activateWidget(aPanel->modelWidgets().first());
267       return;
268   }
269
270   // Restart last operation type 
271   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
272     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
273     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
274       // Initialise new line with first point equal to end of previous
275       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
276       if (aPnt2dWgt) {
277         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
278         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
279           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
280         if (aPoint) {
281           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
282           PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(), 
283             aWgt->attributeID(), aPoint->x(), aPoint->y());
284           theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
285         }
286       }
287     }
288   } else {
289     // Start editing constraint
290     if (theOperation->isEditOperation()) {
291       std::string aId = theOperation->id().toStdString();
292       if (sketchOperationIdList().contains(QString(aId.c_str()))) {
293         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
294             (aId == SketchPlugin_ConstraintLength::ID()) || 
295             (aId == SketchPlugin_ConstraintDistance::ID())) {
296           // Find and activate widget for management of point for dimension line position
297           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
298           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
299             PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
300             if (aPntWgt) {
301               aPanel->activateWidget(aPntWgt);
302               return;
303             }
304           }
305         } 
306       }
307     }
308   }
309 }
310
311
312 void PartSet_Module::onSelectionChanged()
313 {
314   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
315   bool isSketcherOp = false;
316   // An edit operation is enable only if the current opeation is the sketch operation
317   if (aOperation && myCurrentSketch) {
318     if (PartSet_Tools::sketchPlane(myCurrentSketch))
319       isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
320   }
321   if (!isSketcherOp)
322     return;
323
324   // Editing of constraints can be done on selection
325   ModuleBase_ISelection* aSelect = myWorkshop->selection();
326   QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
327   if (aSelected.size() == 1) {
328     ModuleBase_ViewerPrs aPrs = aSelected.first();
329     ObjectPtr aObject = aPrs.object();
330     FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
331     if (aFeature) {
332       std::string aId = aFeature->getKind();
333       if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
334           (aId == SketchPlugin_ConstraintLength::ID()) || 
335           (aId == SketchPlugin_ConstraintDistance::ID())) {
336         editFeature(aFeature);
337       }
338     }
339   }
340 }
341
342 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent) 
343 {
344   if (!(theEvent->buttons() & Qt::LeftButton))
345     return;
346
347   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
348   // Use only for sketch operations
349   if (aOperation && myCurrentSketch) {
350     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
351       return;
352
353     bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
354     bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
355
356     // Avoid non-sketch operations
357     if ((!isSketchOpe) && (!isSketcher))
358       return;
359
360     bool isEditing = aOperation->isEditOperation();
361
362     // Ignore creation sketch operation
363     if ((!isSketcher) && (!isEditing))
364       return;
365
366     if (theEvent->modifiers()) {
367       // If user performs multiselection
368       if (isSketchOpe && (!isSketcher))
369         if (!aOperation->commit())
370           aOperation->abort();
371       return;
372     }
373     // Remember highlighted objects for editing
374     ModuleBase_ISelection* aSelect = myWorkshop->selection();
375     QList<ModuleBase_ViewerPrs> aHighlighted = aSelect->getHighlighted();
376     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
377     myEditingFeatures.clear();
378     myEditingAttr.clear();
379     if ((aHighlighted.size() == 0) && (aSelected.size() == 0)) {
380       if (isSketchOpe && (!isSketcher))
381         // commit previous operation
382         if (!aOperation->commit())
383           aOperation->abort();
384       return;
385     }
386
387     QObjectPtrList aSelObjects = getSumList(aHighlighted, aSelected);
388     if ((aHighlighted.size() == 1) && (aSelected.size() == 0)) {
389       // Move by selected shape (vertex). Can be used only for single selection
390       foreach(ModuleBase_ViewerPrs aPrs, aHighlighted) {
391         FeaturePtr aFeature = ModelAPI_Feature::feature(aHighlighted.first().object());
392         if (aFeature) {
393           myEditingFeatures.append(aFeature);
394           TopoDS_Shape aShape = aPrs.shape();
395           if (!aShape.IsNull()) {
396             if (aShape.ShapeType() == TopAbs_VERTEX) {
397               AttributePtr aAttr = PartSet_Tools::findAttributeBy2dPoint(myEditingFeatures.first(), 
398                                                                          aShape, myCurrentSketch);
399               if (aAttr)
400                 myEditingAttr.append(aAttr);
401             }
402           }
403         }
404       }
405     } else {
406       // Provide multi-selection. Can be used only for features
407       foreach (ObjectPtr aObj, aSelObjects) {
408         FeaturePtr aFeature = ModelAPI_Feature::feature(aObj);
409         if (aFeature && (!myEditingFeatures.contains(aFeature)))
410           myEditingFeatures.append(aFeature);
411       }
412
413     }
414     // If nothing highlighted - return
415     if (myEditingFeatures.size() == 0)
416       return;
417
418     if (isSketcher) {
419       myIsDragging = true;
420       get2dPoint(theWnd, theEvent, myCurX, myCurY);
421       myDragDone = false;
422       myWorkshop->viewer()->enableSelection(false);
423       launchEditing();
424
425     } else if (isSketchOpe && isEditing) {
426       // If selected another object
427       aOperation->abort();
428
429       myIsDragging = true;
430       get2dPoint(theWnd, theEvent, myCurX, myCurY);
431       myDragDone = false;
432       myWorkshop->viewer()->enableSelection(false);
433
434       // This is necessary in order to finalize previous operation
435       QApplication::processEvents();
436       launchEditing();
437     }
438   }
439 }
440
441
442 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
443                                 double& theX, double& theY)
444 {
445   Handle(V3d_View) aView = theWnd->v3dView();
446   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
447   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
448 }
449
450
451 void PartSet_Module::launchEditing()
452 {
453   if (myEditingFeatures.size() > 0) {
454     FeaturePtr aFeature = myEditingFeatures.first();
455     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
456               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
457     if (aSPFeature) {
458       editFeature(aSPFeature);
459     }
460   }
461 }
462
463 /// Returns new instance of operation object (used in createOperation for customization)
464 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
465 {
466   if (theFeatureId == PartSet_OperationSketch::Type()) {
467     return new PartSet_OperationSketch(theFeatureId.c_str(), this);
468   }
469   return ModuleBase_IModule::getNewOperation(theFeatureId);
470 }
471
472
473 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
474 {
475   myWorkshop->viewer()->enableSelection(true);
476   if (myIsDragging) {
477     myIsDragging = false;
478     if (myDragDone) {
479       myWorkshop->currentOperation()->commit();
480       myEditingFeatures.clear();
481       myEditingAttr.clear();
482     }
483   }
484 }
485
486
487 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
488 {
489   if (myIsDragging) {
490     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
491     if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
492       return; // No edit operation activated
493
494     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
495     Handle(V3d_View) aView = theWnd->v3dView();
496     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
497     double aX, aY;
498     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
499     double dX =  aX - myCurX;
500     double dY =  aY - myCurY;
501
502     if ((aOperation->id().toStdString() == SketchPlugin_Line::ID()) &&
503         (myEditingAttr.size() == 1) && 
504         myEditingAttr.first()) {
505       // probably we have prehighlighted point
506       AttributePtr aAttr = myEditingAttr.first();
507       std::string aAttrId = aAttr->id();
508       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
509       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
510       // Find corresponded widget to provide dragging
511       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
512         if (aWgt->attributeID() == aAttrId) {
513           PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
514           if (aWgt2d) {
515             aWgt2d->setPoint(aWgt2d->x() + dX, aWgt2d->y() + dY);
516             break;
517           }
518         }
519       }
520     } else {
521       foreach(FeaturePtr aFeature, myEditingFeatures) {
522         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
523           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
524         if (aSketchFeature) { 
525           aSketchFeature->move(dX, dY);
526           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, anEvent);
527         }
528       }
529       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_MOVED));
530       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
531     }
532     myDragDone = true;
533     myCurX = aX;
534     myCurY = aY;
535   }
536 }
537
538 void PartSet_Module::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
539 {
540   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
541   if (aOperation->isEditOperation()) {
542     std::string aId = aOperation->id().toStdString();
543     if ((aId == SketchPlugin_ConstraintLength::ID()) ||
544       (aId == SketchPlugin_ConstraintDistance::ID()) ||
545       (aId == SketchPlugin_ConstraintRadius::ID())) 
546     {
547       // Activate dimension value editing on double click
548       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
549       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
550       // Find corresponded widget to activate value editing
551       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
552         if (aWgt->attributeID() == "ConstraintValue") {
553           aWgt->focusTo();
554           return;
555         }
556       }
557     }
558   }
559 }
560
561 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
562 {
563   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
564   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
565   anOpMgr->onKeyReleased(theEvent);
566 }
567
568 void PartSet_Module::onEnterReleased()
569 {
570   myRestartingMode = RM_EmptyFeatureUsed;
571 }
572
573 void PartSet_Module::onNoMoreWidgets()
574 {
575   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
576   if (aOperation) {
577     /// Restart sketcher operations automatically
578     FeaturePtr aFeature = aOperation->feature();
579     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
580               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
581     if (aSPFeature) {
582       if (myRestartingMode != RM_Forbided)
583         myRestartingMode = RM_LastFeatureUsed;
584       aOperation->commit();
585     }
586   }
587 }
588
589 QStringList PartSet_Module::sketchOperationIdList() const
590 {
591   QStringList aIds;
592   aIds << SketchPlugin_Line::ID().c_str();
593   aIds << SketchPlugin_Point::ID().c_str();
594   aIds << SketchPlugin_Arc::ID().c_str();
595   aIds << SketchPlugin_Circle::ID().c_str();
596   aIds << SketchPlugin_ConstraintLength::ID().c_str();
597   aIds << SketchPlugin_ConstraintDistance::ID().c_str();
598   aIds << SketchPlugin_ConstraintRigid::ID().c_str();
599   aIds << SketchPlugin_ConstraintRadius::ID().c_str();
600   aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
601   aIds << SketchPlugin_ConstraintParallel::ID().c_str();
602   return aIds;
603 }
604
605 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
606 {
607   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
608   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
609     /// If last line finished on vertex the lines creation sequence has to be break
610     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
611     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
612     if (aWidgets.last() == aPanel->activeWidget()) {
613       myRestartingMode = RM_Forbided;
614     }
615   }
616 }
617
618 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
619                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
620                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
621 {
622   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
623   XGUI_Workshop* aWorkshop = aConnector->workshop();
624   if (theType == "sketch-start-label") {
625     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
626     aWgt->setWorkshop(aWorkshop);
627     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
628       this, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
629     theModelWidgets.append(aWgt);
630     return aWgt->getControl();
631
632   } else if (theType == "sketch-2dpoint_selector") {
633     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
634     aWgt->setWorkshop(aWorkshop);
635     aWgt->setSketch(myCurrentSketch);
636
637     connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)), 
638       this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
639
640     theModelWidgets.append(aWgt);
641     return aWgt->getControl();
642
643   } if (theType == "point2ddistance") {
644     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
645     aWgt->setWorkshop(aWorkshop);
646     aWgt->setSketch(myCurrentSketch);
647
648     theModelWidgets.append(aWgt);
649     return aWgt->getControl();
650
651   } if (theType == "sketch_shape_selector") {
652     PartSet_WidgetShapeSelector* aWgt = 
653       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
654     aWgt->setSketcher(myCurrentSketch);
655
656     theModelWidgets.append(aWgt);
657     return aWgt->getControl();
658
659   } if (theType == "sketch_constraint_shape_selector") {
660     PartSet_WidgetConstraintShapeSelector* aWgt = 
661       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
662     aWgt->setSketcher(myCurrentSketch);
663
664     theModelWidgets.append(aWgt);
665     return aWgt->getControl();
666
667   }else
668     return 0;
669 }
670