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