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