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