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