Salome HOME
Merge remote-tracking branch 'origin/Dev_0.6.1' into Dev_0.6.1
[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_MainWindow.h>
30 #include <XGUI_Displayer.h>
31 #include <XGUI_Viewer.h>
32 #include <XGUI_Workshop.h>
33 #include <XGUI_OperationMgr.h>
34 #include <XGUI_ViewPort.h>
35 #include <XGUI_ActionsMgr.h>
36 #include <XGUI_ViewerProxy.h>
37 #include <XGUI_ContextMenuMgr.h>
38 #include <XGUI_PropertyPanel.h>
39 #include <XGUI_ModuleConnector.h>
40 #include <XGUI_Tools.h>
41
42 #include <SketchPlugin_Line.h>
43 #include <SketchPlugin_Sketch.h>
44 #include <SketchPlugin_Point.h>
45 #include <SketchPlugin_Arc.h>
46 #include <SketchPlugin_Circle.h>
47 #include <SketchPlugin_ConstraintLength.h>
48 #include <SketchPlugin_ConstraintDistance.h>
49 #include <SketchPlugin_ConstraintParallel.h>
50 #include <SketchPlugin_ConstraintPerpendicular.h>
51 #include <SketchPlugin_ConstraintRadius.h>
52 #include <SketchPlugin_ConstraintRigid.h>
53
54 #include <Events_Loop.h>
55
56 #include <StdSelect_TypeOfFace.hxx>
57 #include <TopoDS_Vertex.hxx>
58 #include <TopoDS.hxx>
59 #include <TopoDS_Shape.hxx>
60 #include <BRep_Tool.hxx>
61
62 #include <QObject>
63 #include <QMouseEvent>
64 #include <QString>
65 #include <QTimer>
66 #include <QApplication>
67
68 #include <GeomAlgoAPI_FaceBuilder.h>
69 #include <GeomDataAPI_Dir.h>
70
71 #ifdef _DEBUG
72 #include <QDebug>
73 #endif
74
75
76 /// Returns list of unique objects by sum of objects from List1 and List2
77 QList<ObjectPtr> getSumList(const QList<ModuleBase_ViewerPrs>& theList1,
78                                        const QList<ModuleBase_ViewerPrs>& theList2)
79 {
80   QList<ObjectPtr> aRes;
81   foreach (ModuleBase_ViewerPrs aPrs, theList1) {
82     if (!aRes.contains(aPrs.object()))
83       aRes.append(aPrs.object());
84   }
85   foreach (ModuleBase_ViewerPrs aPrs, theList2) {
86     if (!aRes.contains(aPrs.object()))
87       aRes.append(aPrs.object());
88   }
89   return aRes;
90 }
91
92 /*!Create and return new instance of XGUI_Module*/
93 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
94 {
95   return new PartSet_Module(theWshop);
96 }
97
98 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
99   : ModuleBase_IModule(theWshop), 
100   myIsDragging(false), myRestartingMode(RM_None), myDragDone(false)
101 {
102   //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
103   ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
104   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
105           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
106
107   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
108           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
109
110   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
111           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
112
113   connect(aViewer, SIGNAL(mouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)),
114           this, SLOT(onMouseDoubleClick(ModuleBase_IViewWindow*, QMouseEvent*)));
115
116   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
117   XGUI_Workshop* aWorkshop = aConnector->workshop();
118
119   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
120   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
121
122   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
123           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
124 }
125
126 PartSet_Module::~PartSet_Module()
127 {
128   if (!myDocumentShapeFilter.IsNull())
129     myDocumentShapeFilter.Nullify();
130   if (!myPlaneFilter.IsNull())
131     myPlaneFilter.Nullify();
132 }
133
134 void PartSet_Module::registerValidators()
135 {
136   //Registering of validators
137   SessionPtr aMgr = ModelAPI_Session::get();
138   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
139   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
140   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
141   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
142   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
143   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
144   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
145 }
146
147 void PartSet_Module::registerFilters()
148 {
149   //Registering of selection filters
150   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
151   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
152
153   aFactory->registerFilter("LinearEdgeFilter", new ModuleBase_FilterLinearEdge);
154 }
155
156 void PartSet_Module::onOperationComitted(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::onOperationAborted(ModuleBase_Operation* theOperation)
181 {
182   breakOperationSequence();
183 }
184
185 void PartSet_Module::onOperationStarted(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::onOperationStopped(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 /// Returns new instance of operation object (used in createOperation for customization)
470 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
471 {
472   if (theFeatureId == PartSet_OperationSketch::Type()) {
473     return new PartSet_OperationSketch(theFeatureId.c_str(), this);
474   }
475   return ModuleBase_IModule::getNewOperation(theFeatureId);
476 }
477
478
479 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
480 {
481   myWorkshop->viewer()->enableSelection(true);
482   if (myIsDragging) {
483     myIsDragging = false;
484     if (myDragDone) {
485       myWorkshop->currentOperation()->commit();
486       myEditingFeatures.clear();
487       myEditingAttr.clear();
488     }
489   }
490 }
491
492
493 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
494 {
495   if (myIsDragging) {
496     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
497     if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID())
498       return; // No edit operation activated
499
500     static Events_ID anEvent = Events_Loop::eventByName(EVENT_OBJECT_MOVED);
501     Handle(V3d_View) aView = theWnd->v3dView();
502     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
503     double aX, aY;
504     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
505     double dX =  aX - myCurX;
506     double dY =  aY - myCurY;
507
508     if ((aOperation->id().toStdString() == SketchPlugin_Line::ID()) &&
509         (myEditingAttr.size() == 1) && 
510         myEditingAttr.first()) {
511       // probably we have prehighlighted point
512       AttributePtr aAttr = myEditingAttr.first();
513       std::string aAttrId = aAttr->id();
514       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
515       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
516       // Find corresponded widget to provide dragging
517       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
518         if (aWgt->attributeID() == aAttrId) {
519           PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
520           if (aWgt2d) {
521             aWgt2d->setPoint(aWgt2d->x() + dX, aWgt2d->y() + dY);
522             break;
523           }
524         }
525       }
526     } else {
527       foreach(FeaturePtr aFeature, myEditingFeatures) {
528         std::shared_ptr<SketchPlugin_Feature> aSketchFeature =
529           std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
530         if (aSketchFeature) { 
531           aSketchFeature->move(dX, dY);
532           ModelAPI_EventCreator::get()->sendUpdated(aSketchFeature, anEvent);
533         }
534       }
535       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_MOVED));
536       Events_Loop::loop()->flush(Events_Loop::eventByName(EVENT_OBJECT_UPDATED));
537     }
538     myDragDone = true;
539     myCurX = aX;
540     myCurY = aY;
541   }
542 }
543
544 void PartSet_Module::onMouseDoubleClick(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
545 {
546   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
547   if (aOperation->isEditOperation()) {
548     std::string aId = aOperation->id().toStdString();
549     if ((aId == SketchPlugin_ConstraintLength::ID()) ||
550       (aId == SketchPlugin_ConstraintDistance::ID()) ||
551       (aId == SketchPlugin_ConstraintRadius::ID())) 
552     {
553       // Activate dimension value editing on double click
554       ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
555       QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
556       // Find corresponded widget to activate value editing
557       foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
558         if (aWgt->attributeID() == "ConstraintValue") {
559           aWgt->focusTo();
560           return;
561         }
562       }
563     }
564   }
565 }
566
567 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
568 {
569   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
570   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
571   anOpMgr->onKeyReleased(theEvent);
572 }
573
574 void PartSet_Module::onEnterReleased()
575 {
576   myRestartingMode = RM_EmptyFeatureUsed;
577 }
578
579 void PartSet_Module::onNoMoreWidgets()
580 {
581   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
582   if (aOperation) {
583     /// Restart sketcher operations automatically
584     FeaturePtr aFeature = aOperation->feature();
585     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
586               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
587     if (aSPFeature) {
588       if (myRestartingMode != RM_Forbided)
589         myRestartingMode = RM_LastFeatureUsed;
590       aOperation->commit();
591     }
592   }
593 }
594
595 QStringList PartSet_Module::sketchOperationIdList() const
596 {
597   QStringList aIds;
598   aIds << SketchPlugin_Line::ID().c_str();
599   aIds << SketchPlugin_Point::ID().c_str();
600   aIds << SketchPlugin_Arc::ID().c_str();
601   aIds << SketchPlugin_Circle::ID().c_str();
602   aIds << SketchPlugin_ConstraintLength::ID().c_str();
603   aIds << SketchPlugin_ConstraintDistance::ID().c_str();
604   aIds << SketchPlugin_ConstraintRigid::ID().c_str();
605   aIds << SketchPlugin_ConstraintRadius::ID().c_str();
606   aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
607   aIds << SketchPlugin_ConstraintParallel::ID().c_str();
608   return aIds;
609 }
610
611 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
612 {
613   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
614   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
615     /// If last line finished on vertex the lines creation sequence has to be break
616     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
617     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
618     if (aWidgets.last() == aPanel->activeWidget()) {
619       myRestartingMode = RM_Forbided;
620     }
621   }
622 }
623
624 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
625                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
626                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
627 {
628   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
629   XGUI_Workshop* aWorkshop = aConnector->workshop();
630   if (theType == "sketch-start-label") {
631     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
632     aWgt->setWorkshop(aWorkshop);
633     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
634       this, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
635     theModelWidgets.append(aWgt);
636     return aWgt->getControl();
637
638   } else if (theType == "sketch-2dpoint_selector") {
639     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
640     aWgt->setWorkshop(aWorkshop);
641     aWgt->setSketch(myCurrentSketch);
642
643     connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)), 
644       this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
645
646     theModelWidgets.append(aWgt);
647     return aWgt->getControl();
648
649   } if (theType == "point2ddistance") {
650     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
651     aWgt->setWorkshop(aWorkshop);
652     aWgt->setSketch(myCurrentSketch);
653
654     theModelWidgets.append(aWgt);
655     return aWgt->getControl();
656
657   } if (theType == "sketch_shape_selector") {
658     PartSet_WidgetShapeSelector* aWgt = 
659       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
660     aWgt->setSketcher(myCurrentSketch);
661
662     theModelWidgets.append(aWgt);
663     return aWgt->getControl();
664
665   } if (theType == "sketch_constraint_shape_selector") {
666     PartSet_WidgetConstraintShapeSelector* aWgt = 
667       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
668     aWgt->setSketcher(myCurrentSketch);
669
670     theModelWidgets.append(aWgt);
671     return aWgt->getControl();
672
673   }else
674     return 0;
675 }
676