]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_Module.cpp
Salome HOME
e2b7ccc052e4b5beecc3e6754d1ba57001b8ba3b
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 #include "PartSet_Module.h"
2 #include <PartSet_OperationSketch.h>
3 #include <PartSet_WidgetSketchLabel.h>
4 #include <PartSet_Validators.h>
5 #include <PartSet_Tools.h>
6 #include <PartSet_WidgetPoint2D.h>
7 #include <PartSet_WidgetPoint2dDistance.h>
8 #include <PartSet_WidgetShapeSelector.h>
9
10 #include <ModuleBase_Operation.h>
11 #include <ModuleBase_IViewer.h>
12 #include <ModuleBase_IViewWindow.h>
13 #include <ModuleBase_IPropertyPanel.h>
14
15 #include <ModelAPI_Object.h>
16 #include <ModelAPI_Events.h>
17 #include <ModelAPI_Validator.h>
18 #include <ModelAPI_Data.h>
19 #include <ModelAPI_Session.h>
20
21 #include <GeomDataAPI_Point2D.h>
22 #include <GeomDataAPI_Point.h>
23 #include <GeomDataAPI_Dir.h>
24
25 #include <XGUI_MainWindow.h>
26 #include <XGUI_Displayer.h>
27 #include <XGUI_Viewer.h>
28 #include <XGUI_Workshop.h>
29 #include <XGUI_OperationMgr.h>
30 #include <XGUI_ViewPort.h>
31 #include <XGUI_ActionsMgr.h>
32 #include <XGUI_ViewerProxy.h>
33 #include <XGUI_ContextMenuMgr.h>
34 #include <XGUI_PropertyPanel.h>
35 #include <XGUI_ModuleConnector.h>
36 #include <XGUI_Tools.h>
37
38 #include <SketchPlugin_Line.h>
39 #include <SketchPlugin_Sketch.h>
40 #include <SketchPlugin_Point.h>
41 #include <SketchPlugin_Arc.h>
42 #include <SketchPlugin_Circle.h>
43 #include <SketchPlugin_ConstraintLength.h>
44 #include <SketchPlugin_ConstraintDistance.h>
45 #include <SketchPlugin_ConstraintParallel.h>
46 #include <SketchPlugin_ConstraintPerpendicular.h>
47 #include <SketchPlugin_ConstraintRadius.h>
48 #include <SketchPlugin_ConstraintRigid.h>
49
50 #include <StdSelect_TypeOfFace.hxx>
51
52 #include <QObject>
53 #include <QMouseEvent>
54 #include <QString>
55 #include <QTimer>
56 #include <QApplication>
57
58 #include <GeomAlgoAPI_FaceBuilder.h>
59 #include <GeomDataAPI_Dir.h>
60
61 #ifdef _DEBUG
62 #include <QDebug>
63 #endif
64
65
66 /*!Create and return new instance of XGUI_Module*/
67 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
68 {
69   return new PartSet_Module(theWshop);
70 }
71
72 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
73   : ModuleBase_IModule(theWshop), 
74   myIsDragging(false), myRestartingMode(true), myDragDone(false)
75 {
76   //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
77   ModuleBase_IViewer* aViewer = aViewer = theWshop->viewer();
78   connect(aViewer, SIGNAL(mousePress(ModuleBase_IViewWindow*, QMouseEvent*)),
79           this, SLOT(onMousePressed(ModuleBase_IViewWindow*, QMouseEvent*)));
80
81   connect(aViewer, SIGNAL(mouseRelease(ModuleBase_IViewWindow*, QMouseEvent*)),
82           this, SLOT(onMouseReleased(ModuleBase_IViewWindow*, QMouseEvent*)));
83
84   connect(aViewer, SIGNAL(mouseMove(ModuleBase_IViewWindow*, QMouseEvent*)),
85           this, SLOT(onMouseMoved(ModuleBase_IViewWindow*, QMouseEvent*)));
86
87 }
88
89 PartSet_Module::~PartSet_Module()
90 {
91 }
92
93 void PartSet_Module::registerValidators()
94 {
95   //Registering of validators
96   SessionPtr aMgr = ModelAPI_Session::get();
97   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
98   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
99   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
100   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
101   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
102   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
103 }
104
105
106 void PartSet_Module::onOperationComitted(ModuleBase_Operation* theOperation) 
107 {
108   if (theOperation->isEditOperation())
109     return;
110   /// Restart sketcher operations automatically
111   FeaturePtr aFeature = theOperation->feature();
112   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
113             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
114   if (aSPFeature && myRestartingMode) {
115     myLastOperationId = theOperation->id();
116     myLastFeature = theOperation->feature();
117     launchOperation(myLastOperationId);
118   } else {
119     breakOperationSequence();
120   }
121 }
122
123 void PartSet_Module::breakOperationSequence()
124 {
125   myLastOperationId = "";
126   myLastFeature = FeaturePtr();
127   myRestartingMode = false;
128
129 }
130
131 void PartSet_Module::onOperationAborted(ModuleBase_Operation* theOperation)
132 {
133   breakOperationSequence();
134 }
135
136 void PartSet_Module::onOperationStarted(ModuleBase_Operation* theOperation)
137 {
138   myRestartingMode = true;
139   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
140     // Display all sketcher sub-Objects
141     myCurrentSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(theOperation->feature());
142     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
143     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
144
145     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
146       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
147       std::list<ResultPtr> aResults = aFeature->results();
148       std::list<ResultPtr>::const_iterator aIt;
149       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
150         aDisplayer->display((*aIt), false);
151       }
152       aDisplayer->display(aFeature);
153     }
154     // Hide sketcher result
155     std::list<ResultPtr> aResults = myCurrentSketch->results();
156     std::list<ResultPtr>::const_iterator aIt;
157     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
158       aDisplayer->erase((*aIt), false);
159     }
160     aDisplayer->erase(myCurrentSketch);
161   }
162 }
163
164 void PartSet_Module::onOperationStopped(ModuleBase_Operation* theOperation)
165 {
166   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
167     // Hide all sketcher sub-Objects
168     XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
169     XGUI_Displayer* aDisplayer = aConnector->workshop()->displayer();
170
171     for (int i = 0; i < myCurrentSketch->numberOfSubs(); i++) {
172       FeaturePtr aFeature = myCurrentSketch->subFeature(i);
173       std::list<ResultPtr> aResults = aFeature->results();
174       std::list<ResultPtr>::const_iterator aIt;
175       for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
176         aDisplayer->erase((*aIt), false);
177       }
178       aDisplayer->erase(aFeature, false);
179     }
180     // Display sketcher result
181     std::list<ResultPtr> aResults = myCurrentSketch->results();
182     std::list<ResultPtr>::const_iterator aIt;
183     for (aIt = aResults.begin(); aIt != aResults.end(); ++aIt) {
184       aDisplayer->display((*aIt), false);
185     }
186     aDisplayer->display(myCurrentSketch);
187     
188     myCurrentSketch = CompositeFeaturePtr();
189   }
190 }
191
192
193
194 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
195 {
196   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
197     ModuleBase_ModelWidget* aWgt = theOperation->propertyPanel()->activeWidget();
198     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
199       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
200       if (aPnt2dWgt) {
201         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
202         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
203           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
204         if (aPoint) {
205           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
206           PartSet_Tools::setConstraints(myCurrentSketch, theOperation->feature(), 
207             SketchPlugin_Line::START_ID(), aPoint->x(), aPoint->y());
208           theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
209         }
210       }
211     }
212   }
213 }
214
215
216 void PartSet_Module::onSelectionChanged()
217 {
218 }
219
220 void PartSet_Module::onMousePressed(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
221 {
222   if (!(theEvent->buttons() & Qt::LeftButton))
223     return;
224   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
225   // Use only for sketch operations
226   if (aOperation && myCurrentSketch) {
227     if (!PartSet_Tools::sketchPlane(myCurrentSketch))
228       return;
229
230     bool isSketcher = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
231     bool isSketchOpe = sketchOperationIdList().contains(aOperation->id());
232
233     // Avoid non-sketch operations
234     if ((!isSketchOpe) && (!isSketcher))
235       return;
236
237     bool isEditing = aOperation->isEditOperation();
238
239     // Ignore creation sketch operation
240     if ((!isSketcher) && (!isEditing))
241       return;
242
243     // Remember highlighted objects for editing
244     ModuleBase_ISelection* aSelect = myWorkshop->selection();
245     QList<ModuleBase_ViewerPrs> aObjects = aSelect->getHighlighted();
246      myEditingFeatures.clear();
247     if (aObjects.size() > 0) {
248       foreach(ModuleBase_ViewerPrs aPrs, aObjects) {
249         FeaturePtr aFeature = ModelAPI_Feature::feature(aObjects.first().object());
250         if (aFeature)
251           myEditingFeatures.append(aFeature);
252       }
253     } 
254     // If nothing highlighted - return
255     if (myEditingFeatures.size() == 0)
256       return;
257
258     if (isSketcher) {
259       CompositeFeaturePtr aSketch = 
260         std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(aOperation->feature());
261       if (!PartSet_Tools::sketchPlane(aSketch))
262         return;
263       
264       //myCurrentSketch = aOperation->feature();
265       myIsDragging = true;
266       get2dPoint(theWnd, theEvent, myCurX, myCurY);
267       myDragDone = false;
268       myWorkshop->viewer()->enableSelection(false);
269
270       launchEditing();
271
272     } else if (isSketchOpe && isEditing) {
273       aOperation->abort();
274
275       myIsDragging = true;
276       get2dPoint(theWnd, theEvent, myCurX, myCurY);
277       myDragDone = false;
278       myWorkshop->viewer()->enableSelection(false);
279
280       // This is necessary in order to finalize previous operation
281       QApplication::processEvents();
282       launchEditing();
283       //QTimer::singleShot(10, this, SLOT(launchEditing()));
284     }
285   }
286 }
287
288
289 void PartSet_Module::get2dPoint(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent, 
290                                 double& theX, double& theY)
291 {
292   Handle(V3d_View) aView = theWnd->v3dView();
293   gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
294   PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, theX, theY);
295 }
296
297
298 void PartSet_Module::launchEditing()
299 {
300   if (myEditingFeatures.size() == 1) {
301     FeaturePtr aFeature = myEditingFeatures.first();
302     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
303               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
304     if (aSPFeature) {
305       editFeature(aSPFeature);
306     }
307   }
308 }
309
310 /// Returns new instance of operation object (used in createOperation for customization)
311 ModuleBase_Operation* PartSet_Module::getNewOperation(const std::string& theFeatureId)
312 {
313   if (theFeatureId == PartSet_OperationSketch::Type()) {
314     return new PartSet_OperationSketch(theFeatureId.c_str(), this);
315   }
316   return ModuleBase_IModule::getNewOperation(theFeatureId);
317 }
318
319
320 void PartSet_Module::onMouseReleased(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
321 {
322   if (myIsDragging) {
323     myIsDragging = false;
324     myWorkshop->viewer()->enableSelection(true);
325     if (myDragDone)
326       myWorkshop->currentOperation()->commit();
327   }
328 }
329
330
331 void PartSet_Module::onMouseMoved(ModuleBase_IViewWindow* theWnd, QMouseEvent* theEvent)
332 {
333   if (myIsDragging) {
334     Handle(V3d_View) aView = theWnd->v3dView();
335     gp_Pnt aPoint = PartSet_Tools::convertClickToPoint(theEvent->pos(), aView);
336     double aX, aY;
337     PartSet_Tools::convertTo2D(aPoint, myCurrentSketch, aView, aX, aY);
338     double dX = myCurX - aX;
339     double dY = myCurY - aY;
340
341     ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
342     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
343     QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
344     foreach(ModuleBase_ModelWidget* aWgt, aWidgets) {
345       PartSet_WidgetPoint2D* aWgt2d = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
346       if (aWgt2d) {
347         aWgt2d->setPoint(aWgt2d->x() - dX, aWgt2d->y() - dY);
348       }
349     }
350     myDragDone = true;
351     myCurX = aX;
352     myCurY = aY;
353   }
354 }
355
356
357 QStringList PartSet_Module::sketchOperationIdList() const
358 {
359   QStringList aIds;
360   aIds << SketchPlugin_Line::ID().c_str();
361   aIds << SketchPlugin_Point::ID().c_str();
362   aIds << SketchPlugin_Arc::ID().c_str();
363   aIds << SketchPlugin_Circle::ID().c_str();
364   aIds << SketchPlugin_ConstraintLength::ID().c_str();
365   aIds << SketchPlugin_ConstraintDistance::ID().c_str();
366   aIds << SketchPlugin_ConstraintRigid::ID().c_str();
367   aIds << SketchPlugin_ConstraintRadius::ID().c_str();
368   aIds << SketchPlugin_ConstraintPerpendicular::ID().c_str();
369   aIds << SketchPlugin_ConstraintParallel::ID().c_str();
370   return aIds;
371 }
372
373 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
374 {
375   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
376   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
377     /// If last line finished on vertex the lines creation sequence has to be break
378     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
379     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
380     if (aWidgets.last() == aPanel->activeWidget()) {
381       breakOperationSequence();
382       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aPanel->activeWidget());
383       PartSet_Tools::setConstraints(myCurrentSketch, aOperation->feature(), 
384         SketchPlugin_Line::END_ID(), aPnt2dWgt->x(), aPnt2dWgt->y());
385     }
386   }
387 }
388
389 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
390                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
391                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
392 {
393   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
394   XGUI_Workshop* aWorkshop = aConnector->workshop();
395   if (theType == "sketch-start-label") {
396     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
397     aWgt->setWorkshop(aWorkshop);
398     theModelWidgets.append(aWgt);
399     return aWgt->getControl();
400
401   } else if (theType == "sketch-2dpoint_selector") {
402     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
403     aWgt->setWorkshop(aWorkshop);
404     aWgt->setSketch(myCurrentSketch);
405
406     connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)), 
407       this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
408
409     theModelWidgets.append(aWgt);
410     return aWgt->getControl();
411
412   } if (theType == "point2ddistance") {
413     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
414     aWgt->setWorkshop(aWorkshop);
415     aWgt->setSketch(myCurrentSketch);
416
417     theModelWidgets.append(aWgt);
418     return aWgt->getControl();
419
420   } if (theType == "sketch_shape_selector") {
421     PartSet_WidgetShapeSelector* aWgt = 
422       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
423     aWgt->setSketcher(myCurrentSketch);
424
425     theModelWidgets.append(aWgt);
426     return aWgt->getControl();
427
428   }else
429     return 0;
430 }