Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[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_OperationSketch.h>
5 #include <PartSet_WidgetSketchLabel.h>
6 #include <PartSet_Validators.h>
7 #include <PartSet_Tools.h>
8 #include <PartSet_WidgetPoint2d.h>
9 #include <PartSet_WidgetPoint2dDistance.h>
10 #include <PartSet_WidgetShapeSelector.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_IViewWindow.h>
15 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModuleBase_WidgetEditor.h>
17 #include <ModuleBase_FilterFactory.h>
18 #include <ModuleBase_FilterLinearEdge.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_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::onOperationComitted(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::onOperationAborted(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::onOperationStopped(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 /// Returns new instance of operation object (used in createOperation for customization)
468 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
469 {
470   if (theFeatureId == PartSet_OperationSketch::Type()) {
471     return new PartSet_OperationSketch(theFeatureId.c_str(), this);
472   }
473   return ModuleBase_IModule::getNewOperation(theFeatureId);
474 }
475
476
477 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
478 {
479   myWorkshop->viewer()->enableSelection(true);
480   if (myIsDragging) {
481     myIsDragging = false;
482     if (myDragDone) {
483       myWorkshop->currentOperation()->commit();
484       myEditingFeatures.clear();
485       myEditingAttr.clear();
486     }
487   }
488 }
489
490
491 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
492 {
493   if (myIsDragging) {
494     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
495     if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
496       return; // No edit operation activated
497
498     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
499     Handle(V3d_View) aView = theWnd->v3dView();
500     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
501     double aX, aY;
502     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
503     double dX =  aX - myCurX;
504     double dY =  aY - myCurY;
505
506     if ((aOperation->id().toStdString() == SketchPlugin_Line::ID()) &&
507         (myEditingAttr.size() == 1) && 
508         myEditingAttr.first()) {
509       // probably we have prehighlighted point
510       AttributePtr aAttr = myEditingAttr.first();
511       std::string aAttrId = aAttr->id();
512       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
513       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
514       // Find corresponded widget to provide dragging
515       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
516         if (aWgt->attributeID() == aAttrId) {
517           PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
518           if (aWgt2d) {
519             aWgt2d->setPoint(aWgt2d->x() + dX, aWgt2d->y() + dY);
520             break;
521           }
522         }
523       }
524     } else {
525       foreach(FeaturePtr aFeature, myEditingFeatures) {
526         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
527           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
528         if (aSketchFeature) { 
529           aSketchFeature->move(dX, dY);
530           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, anEvent);
531         }
532       }
533       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_MOVED));
534       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
535     }
536     myDragDone = true;
537     myCurX = aX;
538     myCurY = aY;
539   }
540 }
541
542 void PartSet_Module::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
543 {
544   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
545   if (aOperation->isEditOperation()) {
546     std::string aId = aOperation->id().toStdString();
547     if ((aId == SketchPlugin_ConstraintLength::ID()) ||
548       (aId == SketchPlugin_ConstraintDistance::ID()) ||
549       (aId == SketchPlugin_ConstraintRadius::ID())) 
550     {
551       // Activate dimension value editing on double click
552       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
553       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
554       // Find corresponded widget to activate value editing
555       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
556         if (aWgt->attributeID() == "ConstraintValue") {
557           aWgt->focusTo();
558           return;
559         }
560       }
561     }
562   }
563 }
564
565 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
566 {
567   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
568   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
569   anOpMgr->onKeyReleased(theEvent);
570 }
571
572 void PartSet_Module::onEnterReleased()
573 {
574   myRestartingMode = RM_EmptyFeatureUsed;
575 }
576
577 void PartSet_Module::onOperationActivatedByPreselection()
578 {
579   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
580   if (!aOperation)
581     return;
582
583   // Set final definitions if they are necessary
584   //propertyPanelDefined(aOperation);
585
586   /// Commit sketcher operations automatically
587   FeaturePtr aFeature = aOperation->feature();
588   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
589             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
590   if (aSPFeature) {
591     aOperation->commit();
592   }
593 }
594
595 void PartSet_Module::onNoMoreWidgets()
596 {
597   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
598   if (aOperation) {
599     /// Restart sketcher operations automatically
600     FeaturePtr aFeature = aOperation->feature();
601     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
602               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
603     if (aSPFeature) {
604       if (myRestartingMode != RM_Forbided)
605         myRestartingMode = RM_LastFeatureUsed;
606       aOperation->commit();
607     }
608   }
609 }
610
611 QStringList PartSet_Module::sketchOperationIdList() const
612 {
613   QStringList aIds;
614   aIds << SketchPlugin_Line::ID().c_str();
615   aIds << SketchPlugin_Point::ID().c_str();
616   aIds << SketchPlugin_Arc::ID().c_str();
617   aIds << SketchPlugin_Circle::ID().c_str();
618   aIds << SketchPlugin_ConstraintLength::ID().c_str();
619   aIds << SketchPlugin_ConstraintDistance::ID().c_str();
620   aIds << SketchPlugin_ConstraintRigid::ID().c_str();
621   aIds << SketchPlugin_ConstraintRadius::ID().c_str();
622   aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
623   aIds << SketchPlugin_ConstraintParallel::ID().c_str();
624   return aIds;
625 }
626
627 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
628 {
629   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
630   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
631     /// If last line finished on vertex the lines creation sequence has to be break
632     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
633     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
634     if (aWidgets.last() == aPanel->activeWidget()) {
635       myRestartingMode = RM_Forbided;
636     }
637   }
638 }
639
640 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
641                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
642                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
643 {
644   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
645   XGUI_Workshop* aWorkshop = aConnector->workshop();
646   if (theType == "sketch-start-label") {
647     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
648     aWgt->setWorkshop(aWorkshop);
649     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
650       this, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
651     theModelWidgets.append(aWgt);
652     return aWgt->getControl();
653
654   } else if (theType == "sketch-2dpoint_selector") {
655     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
656     aWgt->setWorkshop(aWorkshop);
657     aWgt->setSketch(myCurrentSketch);
658
659     connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)), 
660       this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
661
662     theModelWidgets.append(aWgt);
663     return aWgt->getControl();
664
665   } if (theType == "point2ddistance") {
666     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
667     aWgt->setWorkshop(aWorkshop);
668     aWgt->setSketch(myCurrentSketch);
669
670     theModelWidgets.append(aWgt);
671     return aWgt->getControl();
672
673   } if (theType == "sketch_shape_selector") {
674     PartSet_WidgetShapeSelector* aWgt = 
675       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
676     aWgt->setSketcher(myCurrentSketch);
677
678     theModelWidgets.append(aWgt);
679     return aWgt->getControl();
680
681   } if (theType == "sketch_constraint_shape_selector") {
682     PartSet_WidgetConstraintShapeSelector* aWgt = 
683       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
684     aWgt->setSketcher(myCurrentSketch);
685
686     theModelWidgets.append(aWgt);
687     return aWgt->getControl();
688
689   }else
690     return 0;
691 }
692