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