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