Salome HOME
0ab84bf8260d8d9f8683abe29d1977b6beb48259
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include "PartSet_Module.h"
2 //#include <PartSet_OperationSketch.h>
3 //#include <PartSet_OperationFeatureCreate.h>
4 //#include <PartSet_OperationFeatureEdit.h>
5 //#include <PartSet_Listener.h>
6 //#include <PartSet_TestOCC.h>
7 #include "PartSet_WidgetSketchLabel.h"
8 #include "PartSet_Validators.h"
9 #include "PartSet_Tools.h"
10 #include "PartSet_WidgetPoint2D.h"
11 #include "PartSet_WidgetPoint2dDistance.h"
12 #include "PartSet_WidgetShapeSelector.h"
13
14 #include <ModuleBase_Operation.h>
15 #include <ModuleBase_IViewer.h>
16 #include <ModuleBase_IViewWindow.h>
17 #include <ModuleBase_IPropertyPanel.h>
18 //#include <ModuleBase_OperationDescription.h>
19 //#include <ModuleBase_WidgetFactory.h>
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_MainWindow.h>
32 #include <XGUI_Displayer.h>
33 #include <XGUI_Viewer.h>
34 #include <XGUI_Workshop.h>
35 #include <XGUI_OperationMgr.h>
36 #include <XGUI_ViewPort.h>
37 #include <XGUI_ActionsMgr.h>
38 #include <XGUI_ViewerProxy.h>
39 #include <XGUI_ContextMenuMgr.h>
40 #include <XGUI_PropertyPanel.h>
41 #include <XGUI_ModuleConnector.h>
42 #include <XGUI_Tools.h>
43
44 #include <SketchPlugin_Line.h>
45 #include <SketchPlugin_Sketch.h>
46 #include <SketchPlugin_Point.h>
47 #include <SketchPlugin_Arc.h>
48 #include <SketchPlugin_Circle.h>
49 #include <SketchPlugin_ConstraintLength.h>
50 #include <SketchPlugin_ConstraintDistance.h>
51 #include <SketchPlugin_ConstraintParallel.h>
52 #include <SketchPlugin_ConstraintPerpendicular.h>
53 #include <SketchPlugin_ConstraintRadius.h>
54 #include <SketchPlugin_ConstraintRigid.h>
55
56 //#include <Config_PointerMessage.h>
57 //#include <Config_ModuleReader.h>
58 //#include <Config_WidgetReader.h>
59 //#include <Events_Loop.h>
60 //#include <Events_Message.h>
61 //#include <Events_Error.h>
62
63 //#include <GeomAPI_Shape.h>
64 //#include <GeomAPI_AISObject.h>
65 //#include <AIS_Shape.hxx>
66 //#include <AIS_DimensionSelectionMode.hxx>
67
68 #include <StdSelect_TypeOfFace.hxx>
69
70 #include <QObject>
71 #include <QMouseEvent>
72 #include <QString>
73 #include <QTimer>
74 #include <QApplication>
75
76 #include <GeomAlgoAPI_FaceBuilder.h>
77 #include <GeomDataAPI_Dir.h>
78
79 #ifdef _DEBUG
80 #include <QDebug>
81 #endif
82
83
84 /*!Create and return new instance of XGUI_Module*/
85 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
86 {
87   return new PartSet_Module(theWshop);
88 }
89
90 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
91   : ModuleBase_IModule(theWshop), 
92   myIsDragging(false), myRestartingMode(true), myDragDone(false)
93 {
94   //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
95   ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
96   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
97           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
98
99   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
100           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
101
102   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
103           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
104
105   //myListener = new PartSet_Listener(this);
106
107 //  connect(myWorkshop, SIGNAL(operationStarted(ModuleBase_Operation*)), 
108 //    this, SLOT(onOperationStarted(ModuleBase_Operation*)));
109
110 //  connect(myWorkshop, SIGNAL(operationStopped(ModuleBase_Operation*)), this,
111 //          SLOT(onOperationStopped(ModuleBase_Operation*)));
112
113   //XGUI_Workshop* aXWshop = xWorkshop();
114   //XGUI_ContextMenuMgr* aContextMenuMgr = aXWshop->contextMenuMgr();
115   //connect(aContextMenuMgr, SIGNAL(actionTriggered(const QString&, bool)), this,
116   //        SLOT(onContextMenuCommand(const QString&, bool)));
117
118 }
119
120 PartSet_Module::~PartSet_Module()
121 {
122 }
123
124 void PartSet_Module::registerValidators()
125 {
126   //Registering of validators
127   SessionPtr aMgr = ModelAPI_Session::get();
128   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
129   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
130   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
131   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
132   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
133   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
134 }
135
136
137 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation) 
138 {
139   if (theOperation->isEditOperation())
140     return;
141   /// Restart sketcher operations automatically
142   FeaturePtr aFeature = theOperation->feature();
143   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
144             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
145   if (aSPFeature && myRestartingMode) {
146     myLastOperationId = theOperation->id();
147     myLastFeature = theOperation->feature();
148     launchOperation(myLastOperationId);
149   } else {
150     breakOperationSequence();
151   }
152 }
153
154 void PartSet_Module::breakOperationSequence()
155 {
156   myLastOperationId = "";
157   myLastFeature = FeaturePtr();
158   myRestartingMode = false;
159
160 }
161
162 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
163 {
164   breakOperationSequence();
165 }
166
167 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
168 {
169   myRestartingMode = true;
170   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
171     // Display all sketcher sub-Objects
172     myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
173     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
174     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
175
176     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
177       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
178       std::list<ResultPtr> aResults = aFeature->results();
179       std::list<ResultPtr>::const_iterator aIt;
180       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
181         aDisplayer->display((*aIt), false);
182       }
183       aDisplayer->display(aFeature);
184     }
185     // Hide sketcher result
186     std::list<ResultPtr> aResults = myCurrentSketch->results();
187     std::list<ResultPtr>::const_iterator aIt;
188     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
189       aDisplayer->erase((*aIt), false);
190     }
191     aDisplayer->erase(myCurrentSketch);
192   }
193 }
194
195 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
196 {
197   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
198     // Hide all sketcher sub-Objects
199     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
200     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
201
202     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
203       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
204       std::list<ResultPtr> aResults = aFeature->results();
205       std::list<ResultPtr>::const_iterator aIt;
206       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
207         aDisplayer->erase((*aIt), false);
208       }
209       aDisplayer->erase(aFeature, false);
210     }
211     // Display sketcher result
212     std::list<ResultPtr> aResults = myCurrentSketch->results();
213     std::list<ResultPtr>::const_iterator aIt;
214     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
215       aDisplayer->display((*aIt), false);
216     }
217     aDisplayer->display(myCurrentSketch);
218     
219     myCurrentSketch = CompositeFeaturePtr();
220   }
221 }
222
223
224
225 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
226 {
227   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
228     ModuleBase_ModelWidget* aWgt = theOperation->propertyPanel()->activeWidget();
229     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
230       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
231       if (aPnt2dWgt) {
232         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
233         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
234           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
235         if (aPoint) {
236           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
237           PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(), 
238             SketchPlugin_Line::START_ID(), aPoint->x(), aPoint->y());
239           theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
240         }
241       }
242     }
243   }
244 }
245
246
247 void PartSet_Module::onSelectionChanged()
248 {
249   // Activate edit operation for sketcher objects
250   //ModuleBase_ISelection* aSelect = myWorkshop->selection();
251   //QObjectPtrList aObjects = aSelect->selectedPresentations();
252   //if (aObjects.size() == 1) {
253   //  FeaturePtr aFeature = ModelAPI_Feature::feature(aObjects.first());
254   //  if (aFeature) {
255   //    std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
256   //              std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
257   //    if (aSPFeature) {
258   //      editFeature(aSPFeature);
259   //    }
260   //  }
261   //}
262 }
263
264 //ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
265 //{
266 //  ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
267 //  if (aOperation) {
268 //    // If the current operation is sketcher sub-operation then we have to create 
269 //    // Costomized operation
270 //    if (aOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
271 //      PartSet_Operation* aPSOp = new PartSet_Operation(theFeatureId.c_str(), this);
272 //      aPSOp->setWorkshop(myWorkshop);
273 //      connect(myWorkshop, SIGNAL(selectionChanged()), aPSOp, SLOT(onSelectionChanged()));
274 //      return aPSOp;
275 //    }
276 //  }
277 //  return ModuleBase_IModule::getNewOperation(theFeatureId);
278 //}
279
280
281 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
282 {
283   if (!(theEvent->buttons() & Qt::LeftButton))
284     return;
285   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
286   // Use only for sketch operations
287   if (aOperation && myCurrentSketch) {
288     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
289       return;
290
291     bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
292     bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
293
294     // Avoid non-sketch operations
295     if ((!isSketchOpe) && (!isSketcher))
296       return;
297
298     bool isEditing = aOperation->isEditOperation();
299
300     // Ignore creation sketch operation
301     if ((!isSketcher) && (!isEditing))
302       return;
303
304     // Remember highlighted objects for editing
305     ModuleBase_ISelection* aSelect = myWorkshop->selection();
306     QList<ModuleBase_ViewerPrs> aObjects = aSelect->getHighlighted();
307      myEditingFeatures.clear();
308     if (aObjects.size() > 0) {
309       foreach(ModuleBase_ViewerPrs aPrs, aObjects) {
310         FeaturePtr aFeature = ModelAPI_Feature::feature(aObjects.first().object());
311         if (aFeature)
312           myEditingFeatures.append(aFeature);
313       }
314     } 
315     // If nothing highlighted - return
316     if (myEditingFeatures.size() == 0)
317       return;
318
319     if (isSketcher) {
320       CompositeFeaturePtr aSketch = 
321         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aOperation->feature());
322       if (!PartSet_Tools::sketchPlane(aSketch))
323         return;
324       
325       //myCurrentSketch = aOperation->feature();
326       myIsDragging = true;
327       get2dPoint(theWnd, theEvent, myCurX, myCurY);
328       myDragDone = false;
329       myWorkshop->viewer()->enableSelection(false);
330
331       launchEditing();
332
333     } else if (isSketchOpe && isEditing) {
334       aOperation->abort();
335
336       myIsDragging = true;
337       get2dPoint(theWnd, theEvent, myCurX, myCurY);
338       myDragDone = false;
339       myWorkshop->viewer()->enableSelection(false);
340
341       // This is necessary in order to finalize previous operation
342       QApplication::processEvents();
343       launchEditing();
344       //QTimer::singleShot(10, this, SLOT(launchEditing()));
345     }
346   }
347 }
348
349
350 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
351                                 double& theX, double& theY)
352 {
353   Handle(V3d_View) aView = theWnd->v3dView();
354   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
355   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
356 }
357
358
359 void PartSet_Module::launchEditing()
360 {
361   if (myEditingFeatures.size() == 1) {
362     FeaturePtr aFeature = myEditingFeatures.first();
363     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
364               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
365     if (aSPFeature) {
366       editFeature(aSPFeature);
367     }
368   }
369 }
370
371 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
372 {
373   if (myIsDragging) {
374     myIsDragging = false;
375     myWorkshop->viewer()->enableSelection(true);
376     if (myDragDone)
377       myWorkshop->currentOperation()->commit();
378   }
379 }
380
381
382 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
383 {
384   if (myIsDragging) {
385     Handle(V3d_View) aView = theWnd->v3dView();
386     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
387     double aX, aY;
388     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
389     double dX = myCurX - aX;
390     double dY = myCurY - aY;
391
392     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
393     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
394     QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
395     foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
396       PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
397       if (aWgt2d) {
398         aWgt2d->setPoint(aWgt2d->x() - dX, aWgt2d->y() - dY);
399       }
400     }
401     myDragDone = true;
402     myCurX = aX;
403     myCurY = aY;
404   }
405 }
406
407
408 QStringList PartSet_Module::sketchOperationIdList() const
409 {
410   QStringList aIds;
411   aIds << SketchPlugin_Line::ID().c_str();
412   aIds << SketchPlugin_Point::ID().c_str();
413   aIds << SketchPlugin_Arc::ID().c_str();
414   aIds << SketchPlugin_Circle::ID().c_str();
415   aIds << SketchPlugin_ConstraintLength::ID().c_str();
416   aIds << SketchPlugin_ConstraintDistance::ID().c_str();
417   aIds << SketchPlugin_ConstraintRigid::ID().c_str();
418   aIds << SketchPlugin_ConstraintRadius::ID().c_str();
419   aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
420   aIds << SketchPlugin_ConstraintParallel::ID().c_str();
421   return aIds;
422 }
423
424 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
425 {
426   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
427   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
428     /// If last line finished on vertex the lines creation sequence has to be break
429     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
430     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
431     if (aWidgets.last() == aPanel->activeWidget()) {
432       breakOperationSequence();
433       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
434       PartSet_Tools::setConstraints(myCurrentSketch, aOperation->feature(), 
435         SketchPlugin_Line::END_ID(), aPnt2dWgt->x(), aPnt2dWgt->y());
436     }
437   }
438 }
439
440
441 //void PartSet_Module::featureCreated(QAction* theFeature)
442 //{
443 //  connect(theFeature, SIGNAL(triggered(bool)), this, SLOT(onFeatureTriggered()));
444 //}
445
446 //std::string PartSet_Module::featureFile(const std::string& theFeatureId)
447 //{
448 //  return myFeaturesInFiles[theFeatureId];
449 //}
450
451 /*
452  *
453  */
454 //void PartSet_Module::onFeatureTriggered()
455 //{
456 //  QAction* aCmd = dynamic_cast<QAction*>(sender());
457 //  //Do nothing on uncheck
458 //  if (aCmd->isCheckable() && !aCmd->isChecked())
459 //    return;
460 //  launchOperation(aCmd->data().toString());
461 //}
462
463
464 //void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
465 //{
466 //  XGUI_Workshop* aXWshp = xWorkshop();
467 //  XGUI_Displayer* aDisplayer = aXWshp->displayer();
468 //  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
469 //  if (aPreviewOp) {
470 //    XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
471 //    connect(aPropPanel, SIGNAL(storedPoint2D(ObjectPtr, const std::string&)), this,
472 //            SLOT(onStorePoint2D(ObjectPtr, const std::string&)), Qt::UniqueConnection);
473 //
474 //    //aDisplayer->deactivateObjectsOutOfContext();
475 //    PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
476 //    if (aSketchOp) {
477 //      if (aSketchOp->isEditOperation()) {
478 //        setSketchingMode(getSketchPlane(aSketchOp->feature()));
479 //      } else {
480 //        aDisplayer->openLocalContext();
481 //        aDisplayer->activateObjectsOutOfContext(QIntList());
482 //        myPlaneFilter = new StdSelect_FaceFilter(StdSelect_Plane);
483 //        aDisplayer->addSelectionFilter(myPlaneFilter);
484 //        QIntList aModes = sketchSelectionModes(aPreviewOp->feature());
485 //        aDisplayer->setSelectionModes(aModes);
486 //      } 
487 //    }
488 //  }
489 //}
490
491 //void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
492 //{
493 //  if (!theOperation)
494 //    return;
495 //  XGUI_Workshop* aXWshp = xWorkshop();
496 //  XGUI_Displayer* aDisplayer = aXWshp->displayer();
497 //  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(theOperation);
498 //  if (aPreviewOp) {
499 //    XGUI_PropertyPanel* aPropPanel = aXWshp->propertyPanel();
500 //
501 //    PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
502 //    if (aSketchOp) {
503 //      aDisplayer->closeLocalContexts();
504 //    } else {
505 //      PartSet_OperationFeatureCreate* aCreationOp = 
506 //        dynamic_cast<PartSet_OperationFeatureCreate*>(aPreviewOp);
507 //      if (aCreationOp) {
508 //        // Activate just created object for selection
509 //        FeaturePtr aFeature = aCreationOp->feature();
510 //        QIntList aModes = sketchSelectionModes(aFeature);
511 //        const std::list<ResultPtr>& aResults = aFeature->results();
512 //        std::list<ResultPtr>::const_iterator anIt, aLast = aResults.end();
513 //        for (anIt = aResults.begin(); anIt != aLast; anIt++) {
514 //          aDisplayer->activate(*anIt, aModes);
515 //        }
516 //        aDisplayer->activate(aFeature, aModes);
517 //        aDisplayer->clearSelected();
518 //      }
519 //    }
520 //  }// else {
521 //    // Activate results of current feature for selection
522 //    //FeaturePtr aFeature = theOperation->feature();
523 //    //XGUI_Displayer* aDisplayer = aXWshp->displayer();
524 //    //std::list<ResultPtr> aResults = aFeature->results();
525 //    //std::list<ResultPtr>::const_iterator aIt;
526 //    //for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
527 //    //  aDisplayer->activate(*aIt);
528 //    //}    
529 //  //}
530 //}
531
532 //void PartSet_Module::onContextMenuCommand(const QString& theId, bool isChecked)
533 //{
534 //  QList<ObjectPtr> aFeatures = workshop()->selection()->selectedObjects();
535 //  if (theId == "EDIT_CMD" && (aFeatures.size() > 0)) {
536 //    FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(aFeatures.first());
537 //    if (aFeature)
538 //      editFeature(aFeature);
539 //  }
540 //}
541
542 //void PartSet_Module::onMousePressed(QMouseEvent* theEvent)
543 //{
544 //  XGUI_Workshop* aXWshp = xWorkshop();
545 //  PartSet_OperationSketchBase* aPreviewOp = 
546 //    dynamic_cast<PartSet_OperationSketchBase*>(workshop()->currentOperation());
547 //  if (aPreviewOp) {
548 //    ModuleBase_ISelection* aSelection = workshop()->selection();
549 //    aPreviewOp->mousePressed(theEvent, myWorkshop->viewer(), aSelection);
550 //  }
551 //}
552
553 //void PartSet_Module::onMouseReleased(QMouseEvent* theEvent)
554 //{
555 //  PartSet_OperationSketchBase* aPreviewOp = 
556 //    dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
557 //  if (aPreviewOp) {
558 //    ModuleBase_ISelection* aSelection = workshop()->selection();
559 //    // Initialise operation with preliminary selection
560 //    aPreviewOp->mouseReleased(theEvent, myWorkshop->viewer(), aSelection);
561 //  }
562 //}
563
564 //void PartSet_Module::onMouseMoved(QMouseEvent* theEvent)
565 //{
566 //  PartSet_OperationSketchBase* aPreviewOp = 
567 //    dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
568 //  if (aPreviewOp)
569 //    aPreviewOp->mouseMoved(theEvent, myWorkshop->viewer());
570 //}
571
572 //void PartSet_Module::onKeyRelease(QKeyEvent* theEvent)
573 //{
574 //  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
575 //  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
576 //  if (aPreviewOp) {
577 //    aPreviewOp->keyReleased(theEvent->key());
578 //  }
579 //}
580
581 //void PartSet_Module::onMouseDoubleClick(QMouseEvent* theEvent)
582 //{
583 //  PartSet_OperationSketchBase* aPreviewOp = 
584 //    dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
585 //  Handle(V3d_View) aView = myWorkshop->viewer()->activeView();
586 //  if (aPreviewOp && (!aView.IsNull())) {
587 //    ModuleBase_ISelection* aSelection = workshop()->selection();
588 //    // Initialise operation with preliminary selection
589 //    aPreviewOp->mouseDoubleClick(theEvent, aView, aSelection);
590 //  }
591 //}
592
593 //void PartSet_Module::onPlaneSelected(double theX, double theY, double theZ)
594 //{
595 //  myWorkshop->viewer()->setViewProjection(theX, theY, theZ);
596 //}
597
598 //void PartSet_Module::onSketchLaunched()
599 //{
600 //  xWorkshop()->actionsMgr()->update();
601 //  // Set working plane
602 //  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
603 //  FeaturePtr aSketch = anOperation->feature();
604 //  setSketchingMode(getSketchPlane(aSketch));
605 //}
606
607 //void PartSet_Module::onFitAllView()
608 //{
609 //  myWorkshop->viewer()->fitAll();
610 //}
611
612 //void PartSet_Module::onRestartOperation(std::string theName, ObjectPtr theObject)
613 //{
614 //  FeaturePtr aFeature = ModelAPI_Feature::feature(theObject);
615 //
616 //  std::string aKind = aFeature ? aFeature->getKind() : "";
617 //  ModuleBase_Operation* anOperation = createOperation(theName, aKind);
618 //
619 //  PartSet_OperationSketchBase* aSketchOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
620 //  if (aSketchOp) {
621 //    PartSet_OperationFeatureCreate* aCreateOp = dynamic_cast<PartSet_OperationFeatureCreate*>(anOperation);
622 //    if (aCreateOp)
623 //      aCreateOp->initFeature(aFeature);
624 //    else {
625 //      anOperation->setFeature(aFeature);
626 //    }
627 //    ModuleBase_ISelection* aSelection = workshop()->selection();
628 //    // Initialise operation with preliminary selection
629 //    aSketchOp->initSelection(aSelection, myWorkshop->viewer());
630 //  } else if (aFeature) { // In case of edit operation: set the previously created feature to the operation
631 //    anOperation->setFeature(aFeature);
632 //    ////Deactivate result of current feature in order to avoid its selection
633 //    XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
634 //    std::list<ResultPtr> aResults = aFeature->results();
635 //    std::list<ResultPtr>::const_iterator aIt;
636 //    for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt) {
637 //      aDisplayer->deactivate(*aIt);
638 //    }
639 //  }
640 //  sendOperation(anOperation);
641 //  xWorkshop()->actionsMgr()->updateCheckState();
642 //}
643
644 //void PartSet_Module::onMultiSelectionEnabled(bool theEnabled)
645 //{
646 //  ModuleBase_IViewer* aViewer = myWorkshop->viewer();
647 //  aViewer->enableMultiselection(theEnabled);
648 //}
649
650 //void PartSet_Module::onStopSelection(const QList<ObjectPtr>& theFeatures, const bool isStop)
651 //{
652 //  XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
653 //  //if (!isStop) {
654 //  //  foreach(ObjectPtr aObject, theFeatures) {
655 //  //    activateFeature(aObject);
656 //  //  }
657 //  //}
658 //  aDisplayer->stopSelection(theFeatures, isStop, false);
659 //
660 //  ModuleBase_IViewer* aViewer = myWorkshop->viewer();
661 //  aViewer->enableSelection(!isStop);
662 //
663 //  aDisplayer->updateViewer();
664 //}
665
666 //void PartSet_Module::onSetSelection(const QList<ObjectPtr>& theFeatures)
667 //{
668 //  XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
669 //  aDisplayer->setSelected(theFeatures, false);
670 //  aDisplayer->updateViewer();
671 //}
672
673 //void PartSet_Module::setSketchingMode(const gp_Pln& thePln)
674 //{
675 //  XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
676 //  if (!myPlaneFilter.IsNull()) {
677 //    aDisplayer->removeSelectionFilter(myPlaneFilter);
678 //    myPlaneFilter.Nullify();
679 //  }
680 //  QIntList aModes;
681 //  // Clear standard selection modes
682 //  aDisplayer->setSelectionModes(aModes);
683 //  aDisplayer->openLocalContext();
684 //
685 //  // Set filter
686 //  mySketchFilter = new ModuleBase_ShapeInPlaneFilter(thePln);
687 //  aDisplayer->addSelectionFilter(mySketchFilter);
688 //
689 //  // Get default selection modes
690 //  aModes = sketchSelectionModes(ObjectPtr());
691 //  aDisplayer->activateObjectsOutOfContext(aModes);
692 //}
693
694 //void PartSet_Module::onFeatureConstructed(ObjectPtr theFeature, int theMode)
695 //{
696 //  bool isDisplay = theMode != PartSet_OperationSketchBase::FM_Hide;
697 //  ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
698 //  PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
699 //  if (aPrevOp) {
700 //    std::list<FeaturePtr> aList = aPrevOp->subFeatures();
701 //    XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
702 //    QIntList aModes = sketchSelectionModes(aPrevOp->feature());
703 //    std::list<FeaturePtr>::iterator aSFIt;
704 //    for (aSFIt = aList.begin(); aSFIt != aList.end(); ++aSFIt) {
705 //      std::list<ResultPtr> aResults = (*aSFIt)->results();
706 //      std::list<ResultPtr>::iterator aIt;
707 //      for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
708 //        if (!isDisplay)
709 //          aDisplayer->erase((*aIt), false);
710 //      }
711 //      if (!isDisplay)
712 //        aDisplayer->erase((*aSFIt), false);
713 //    }
714 //    //aDisplayer->deactivateObjectsOutOfContext();
715 //  }
716 //  if (isDisplay)
717 //    ModelAPI_EventCreator::get()->sendUpdated(
718 //        theFeature, Events_Loop::loop()->eventByName(EVENT_OBJECT_TO_REDISPLAY));
719 //}
720
721 //ModuleBase_Operation* PartSet_Module::createOperation(const std::string& theCmdId,
722 //                                                      const std::string& theFeatureKind)
723 //{
724 //  // create the operation
725 //  ModuleBase_Operation* anOperation = 0;
726 //  if (theCmdId == PartSet_OperationSketch::Type()) {
727 //    anOperation = new PartSet_OperationSketch(theCmdId.c_str(), this);
728 //  } else {
729 //    ModuleBase_Operation* aCurOperation = myWorkshop->currentOperation();
730 //    CompositeFeaturePtr aSketch;
731 //    PartSet_OperationSketchBase* aPrevOp = dynamic_cast<PartSet_OperationSketchBase*>(aCurOperation);
732 //    if (aPrevOp) {
733 //      aSketch = aPrevOp->sketch();
734 //    }
735 //    if (PartSet_OperationFeatureCreate::canProcessKind(theCmdId)) {
736 //      anOperation = new PartSet_OperationFeatureCreate(theCmdId.c_str(), this, aSketch);
737 //    } else if (theCmdId == PartSet_OperationFeatureEdit::Type()) {
738 //      anOperation = new PartSet_OperationFeatureEdit(theCmdId.c_str(), this, aSketch);
739 //    }
740 //  }
741 //
742 //  if (!anOperation) {
743 //    anOperation = new ModuleBase_Operation(theCmdId.c_str(), this);
744 //  }
745 //
746 //  // set operation description and list of widgets corresponding to the feature xml definition
747 //  std::string aFeatureKind = theFeatureKind.empty() ? theCmdId : theFeatureKind;
748 //
749 //  std::string aPluginFileName = featureFile(aFeatureKind);
750 //  Config_WidgetReader aWdgReader = Config_WidgetReader(aPluginFileName);
751 //  aWdgReader.readAll();
752 //  std::string aXmlCfg = aWdgReader.featureWidgetCfg(aFeatureKind);
753 //  std::string aDescription = aWdgReader.featureDescription(aFeatureKind);
754 //
755 //  anOperation->getDescription()->setDescription(QString::fromStdString(aDescription));
756 //  anOperation->getDescription()->setXmlRepresentation(QString::fromStdString(aXmlCfg));
757 //
758 //  // connect the operation
759 //  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
760 //  if (aPreviewOp) {
761 //    connect(aPreviewOp, SIGNAL(featureConstructed(ObjectPtr, int)), this,
762 //            SLOT(onFeatureConstructed(ObjectPtr, int)));
763 //    connect(aPreviewOp, SIGNAL(restartRequired(std::string, ObjectPtr)), this,
764 //            SLOT(onRestartOperation(std::string, ObjectPtr)));
765 //    // If manage multi selection the it will be impossible to select more then one
766 //    // object under operation Edit
767 ////    connect(aPreviewOp, SIGNAL(multiSelectionEnabled(bool)), this,
768 ////            SLOT(onMultiSelectionEnabled(bool)));
769 //
770 //    connect(aPreviewOp, SIGNAL(stopSelection(const QList<ObjectPtr>&, const bool)), this,
771 //            SLOT(onStopSelection(const QList<ObjectPtr>&, const bool)));
772 //    connect(aPreviewOp, SIGNAL(setSelection(const QList<ObjectPtr>&)), this,
773 //            SLOT(onSetSelection(const QList<ObjectPtr>&)));
774 //
775 //    PartSet_OperationSketch* aSketchOp = dynamic_cast<PartSet_OperationSketch*>(aPreviewOp);
776 //    if (aSketchOp) {
777 //      connect(aSketchOp, SIGNAL(planeSelected(double, double, double)), this,
778 //              SLOT(onPlaneSelected(double, double, double)));
779 //      connect(aSketchOp, SIGNAL(fitAllView()), this, SLOT(onFitAllView()));
780 //      connect(aSketchOp, SIGNAL(launchSketch()), this, SLOT(onSketchLaunched()));
781 //    }
782 //  }
783 //
784 //  return anOperation;
785 //}
786
787
788 //void PartSet_Module::updateCurrentPreview(const std::string& theCmdId)
789 //{
790 //  ModuleBase_Operation* anOperation = myWorkshop->currentOperation();
791 //  if (!anOperation)
792 //    return;
793 //
794 //  PartSet_OperationSketchBase* aPreviewOp = dynamic_cast<PartSet_OperationSketchBase*>(anOperation);
795 //  if (!aPreviewOp)
796 //    return;
797 //
798 //  FeaturePtr aFeature = aPreviewOp->feature();
799 //  if (!aFeature || aFeature->getKind() != theCmdId)
800 //    return;
801 //
802 //  XGUI_Displayer* aDisplayer = xWorkshop()->displayer();
803 //  // Hide result of sketch
804 //  std::list<ResultPtr> aResults = aFeature->results();
805 //  std::list<ResultPtr>::const_iterator aIt;
806 //  for (aIt = aResults.cbegin(); aIt != aResults.cend(); ++aIt)
807 //    aDisplayer->erase(*aIt, false);
808 //
809 //  std::list<FeaturePtr> aList = aPreviewOp->subFeatures();
810 //
811 //  std::list<FeaturePtr>::const_iterator anIt = aList.begin(), aLast = aList.end();
812 //  for (; anIt != aLast; anIt++) {
813 //    std::shared_ptr<SketchPlugin_Feature> aSPFeature = std::dynamic_pointer_cast<
814 //        SketchPlugin_Feature>(*anIt);
815 //    if (!aSPFeature)
816 //      continue;
817 //    std::list<ResultPtr> aResults = aSPFeature->results();
818 //    std::list<ResultPtr>::const_iterator aRIt;
819 //    for (aRIt = aResults.cbegin(); aRIt != aResults.cend(); ++aRIt) {
820 //      aDisplayer->display((*aRIt), false);
821 //      aDisplayer->activate((*aRIt), sketchSelectionModes((*aRIt)));
822 //    }
823 //    aDisplayer->display(aSPFeature, false);
824 //    aDisplayer->activate(aSPFeature, sketchSelectionModes(aSPFeature));
825 //  }
826 //  aDisplayer->updateViewer();
827 //}
828
829 //void PartSet_Module::editFeature(FeaturePtr theFeature)
830 //{
831 //  if (!theFeature)
832 //    return;
833
834 //  if (theFeature->getKind() == SKETCH_KIND) {
835   //FeaturePtr aFeature = theFeature;
836   //if (XGUI_Tools::isModelObject(aFeature)) {
837   //  ObjectPtr aObject = std::dynamic_pointer_cast<ModelAPI_Object>(aFeature);
838   //  aFeature = aObject->featureRef();
839   //}
840
841   //if (aFeature) {
842 //  onRestartOperation(theFeature->getKind(), theFeature);
843 //  updateCurrentPreview(theFeature->getKind());
844   //}
845 //  }
846 //}
847
848 //void PartSet_Module::onStorePoint2D(ObjectPtr theFeature, const std::string& theAttribute)
849 //{
850 //  FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
851 //
852 //  PartSet_OperationSketchBase* aPreviewOp = 
853 //    dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
854 //  if (!aPreviewOp)
855 //    return;
856 //
857 //  std::shared_ptr<GeomDataAPI_Point2D> aPoint = std::dynamic_pointer_cast<GeomDataAPI_Point2D>(
858 //      aFeature->data()->attribute(theAttribute));
859 //
860 //  PartSet_Tools::setConstraints(aPreviewOp->sketch(), aFeature, theAttribute, aPoint->x(),
861 //                                aPoint->y());
862 //}
863
864 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
865                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
866                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
867 {
868   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
869   XGUI_Workshop* aWorkshop = aConnector->workshop();
870   if (theType == "sketch-start-label") {
871     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
872     aWgt->setWorkshop(aWorkshop);
873     theModelWidgets.append(aWgt);
874     return aWgt->getControl();
875
876   } else if (theType == "sketch-2dpoint_selector") {
877     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
878     aWgt->setWorkshop(aWorkshop);
879     aWgt->setSketch(myCurrentSketch);
880
881     connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)), 
882       this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
883
884     theModelWidgets.append(aWgt);
885     return aWgt->getControl();
886
887   } if (theType == "point2ddistance") {
888     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
889     aWgt->setWorkshop(aWorkshop);
890     aWgt->setSketch(myCurrentSketch);
891
892     theModelWidgets.append(aWgt);
893     return aWgt->getControl();
894
895   } if (theType == "sketch_shape_selector") {
896     PartSet_WidgetShapeSelector* aWgt = 
897       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
898     aWgt->setSketcher(myCurrentSketch);
899
900     theModelWidgets.append(aWgt);
901     return aWgt->getControl();
902
903   }else
904     return 0;
905 }
906
907
908 //XGUI_Workshop* PartSet_Module::xWorkshop() const
909 //{
910 //  XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
911 //  if (aConnector) {
912 //    return aConnector->workshop();
913 //  }
914 //  return 0;
915 //}
916
917
918 //QIntList PartSet_Module::sketchSelectionModes(ObjectPtr theFeature)
919 //{
920 //  QIntList aModes;
921 //  FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theFeature);
922 //  if (aFeature) {
923 //    if (aFeature->getKind() == SketchPlugin_Sketch::ID()) {
924 //      aModes.append(TopAbs_FACE);
925 //      return aModes;
926 //    } else if (PartSet_Tools::isConstraintFeature(aFeature->getKind())) {
927 //      aModes.append(AIS_DSM_Text);
928 //      aModes.append(AIS_DSM_Line);
929 //      return aModes;
930 //    }
931 //  } 
932 //  aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
933 //  aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
934 //  return aModes;
935 //}
936
937
938 //gp_Pln PartSet_Module::getSketchPlane(FeaturePtr theSketch) const
939 //{
940 //  DataPtr aData = theSketch->data();
941 //  std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
942 //      aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
943 //  std::shared_ptr<GeomDataAPI_Dir> aNorm = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
944 //      aData->attribute(SketchPlugin_Sketch::NORM_ID()));
945 //  gp_Pnt aOrig(anOrigin->x(), anOrigin->y(), anOrigin->z());
946 //  gp_Dir aDir(aNorm->x(), aNorm->y(), aNorm->z());
947 //  return gp_Pln(aOrig, aDir);
948 //}
949
950
951 //void PartSet_Module::onSelectionChanged()
952 //{
953 //  ModuleBase_ISelection* aSelect = myWorkshop->selection();
954 //  QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
955 //  // We need to stop edit operation if selection is cleared
956 //  if (aSelected.size() == 0) {
957 //    // do not perform commit of the current edit operation here, because
958 //    // this functionality is realized inside this operation
959 //    /*PartSet_OperationFeatureEdit* anEditOp = 
960 //      dynamic_cast<PartSet_OperationFeatureEdit*>(myWorkshop->currentOperation());
961 //    if (!anEditOp)
962 //      return;
963 //    anEditOp->commit();*/
964 //  } else {
965 //    PartSet_OperationSketchBase* aSketchOp = 
966 //      dynamic_cast<PartSet_OperationSketchBase*>(myWorkshop->currentOperation());
967 //    if (aSketchOp) {
968 //      aSketchOp->selectionChanged(aSelect);
969 //    }
970 //  }
971 //}