Salome HOME
Merge branch 'Dev_0.6.1' of newgeom:newgeom into Dev_0.6.1
[modules/shaper.git] / src / PartSet / PartSet_Module.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 #include "PartSet_Module.h"
4 #include <PartSet_WidgetSketchLabel.h>
5 #include <PartSet_Validators.h>
6 #include <PartSet_Tools.h>
7 #include <PartSet_WidgetPoint2d.h>
8 #include <PartSet_WidgetPoint2dDistance.h>
9 #include <PartSet_WidgetShapeSelector.h>
10 #include <PartSet_SketcherMgr.h>
11
12 #include <ModuleBase_Operation.h>
13 #include <ModuleBase_IViewer.h>
14 #include <ModuleBase_IViewWindow.h>
15 #include <ModuleBase_IPropertyPanel.h>
16 #include <ModuleBase_WidgetEditor.h>
17 #include <ModuleBase_FilterFactory.h>
18 #include <ModuleBase_FilterLinearEdge.h>
19 #include <ModuleBase_FilterFace.h>
20
21
22 #include <ModelAPI_Object.h>
23 #include <ModelAPI_Events.h>
24 #include <ModelAPI_Validator.h>
25 #include <ModelAPI_Data.h>
26 #include <ModelAPI_Session.h>
27
28 #include <GeomDataAPI_Point2D.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Dir.h>
31
32 #include <XGUI_Displayer.h>
33 #include <XGUI_Workshop.h>
34 #include <XGUI_OperationMgr.h>
35 #include <XGUI_PropertyPanel.h>
36 #include <XGUI_ModuleConnector.h>
37 #include <XGUI_Tools.h>
38
39 #include <SketchPlugin_Feature.h>
40 #include <SketchPlugin_Sketch.h>
41 #include <SketchPlugin_Line.h>
42 //#include <SketchPlugin_Arc.h>
43 //#include <SketchPlugin_Circle.h>
44 #include <SketchPlugin_ConstraintLength.h>
45 #include <SketchPlugin_ConstraintDistance.h>
46 #include <SketchPlugin_ConstraintParallel.h>
47 #include <SketchPlugin_ConstraintPerpendicular.h>
48 #include <SketchPlugin_ConstraintRadius.h>
49 //#include <SketchPlugin_ConstraintRigid.h>
50
51 #include <Events_Loop.h>
52
53 #include <StdSelect_TypeOfFace.hxx>
54 #include <TopoDS_Vertex.hxx>
55 #include <TopoDS.hxx>
56 #include <TopoDS_Shape.hxx>
57 #include <BRep_Tool.hxx>
58
59 #include <QObject>
60 #include <QMouseEvent>
61 #include <QString>
62 #include <QTimer>
63 #include <QApplication>
64
65 #include <GeomAlgoAPI_FaceBuilder.h>
66 #include <GeomDataAPI_Dir.h>
67
68 #ifdef _DEBUG
69 #include <QDebug>
70 #endif
71
72 /*!Create and return new instance of XGUI_Module*/
73 extern "C" PARTSET_EXPORT ModuleBase_IModule* createModule(ModuleBase_IWorkshop* theWshop)
74 {
75   return new PartSet_Module(theWshop);
76 }
77
78 PartSet_Module::PartSet_Module(ModuleBase_IWorkshop* theWshop)
79   : ModuleBase_IModule(theWshop), 
80   myRestartingMode(RM_None)
81 {
82   //myWorkshop = dynamic_cast<XGUI_Workshop*>(theWshop);
83   mySketchMgr = new PartSet_SketcherMgr(this);
84
85   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(theWshop);
86   XGUI_Workshop* aWorkshop = aConnector->workshop();
87
88   XGUI_OperationMgr* anOpMgr = aWorkshop->operationMgr();
89   connect(anOpMgr, SIGNAL(keyEnterReleased()), this, SLOT(onEnterReleased()));
90   connect(anOpMgr, SIGNAL(operationActivatedByPreselection()),
91           this, SLOT(onOperationActivatedByPreselection()));
92
93   ModuleBase_IViewer* aViewer = theWshop->viewer();
94   connect(aViewer, SIGNAL(keyRelease(ModuleBase_IViewWindow*, QKeyEvent*)),
95           this, SLOT(onKeyRelease(ModuleBase_IViewWindow*, QKeyEvent*)));
96 }
97
98 PartSet_Module::~PartSet_Module()
99 {
100   if (!myDocumentShapeFilter.IsNull())
101     myDocumentShapeFilter.Nullify();
102 }
103
104 void PartSet_Module::registerValidators()
105 {
106   //Registering of validators
107   SessionPtr aMgr = ModelAPI_Session::get();
108   ModelAPI_ValidatorsFactory* aFactory = aMgr->validators();
109   aFactory->registerValidator("PartSet_DistanceValidator", new PartSet_DistanceValidator);
110   aFactory->registerValidator("PartSet_LengthValidator", new PartSet_LengthValidator);
111   aFactory->registerValidator("PartSet_PerpendicularValidator", new PartSet_PerpendicularValidator);
112   aFactory->registerValidator("PartSet_ParallelValidator", new PartSet_ParallelValidator);
113   aFactory->registerValidator("PartSet_RadiusValidator", new PartSet_RadiusValidator);
114   aFactory->registerValidator("PartSet_RigidValidator", new PartSet_RigidValidator);
115   aFactory->registerValidator("PartSet_DifferentObjects", new PartSet_DifferentObjectsValidator);
116 }
117
118 void PartSet_Module::registerFilters()
119 {
120   //Registering of selection filters
121   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
122   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
123
124   aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
125   aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
126 }
127
128 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation) 
129 {
130   if (theOperation->isEditOperation())
131     return;
132   /// Restart sketcher operations automatically
133   FeaturePtr aFeature = theOperation->feature();
134   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
135             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
136   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
137                      myRestartingMode == RM_EmptyFeatureUsed)) {
138     myLastOperationId = theOperation->id();
139     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
140     launchOperation(myLastOperationId);
141   }
142   breakOperationSequence();
143 }
144
145 void PartSet_Module::breakOperationSequence()
146 {
147   myLastOperationId = "";
148   myLastFeature = FeaturePtr();
149   myRestartingMode = RM_None;
150 }
151
152 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
153 {
154   breakOperationSequence();
155 }
156
157 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
158 {
159   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
160     mySketchMgr->startSketch(theOperation);
161   }
162   if (myDocumentShapeFilter.IsNull())
163     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
164   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
165 }
166
167 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
168 {
169   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
170     mySketchMgr->stopSketch(theOperation);
171   }
172   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
173 }
174
175
176 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
177 {
178   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
179   if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) && 
180     (theOperation->isEditOperation())) {
181     // we have to manually activate the sketch label in edit mode
182       aPanel->activateWidget(aPanel->modelWidgets().first());
183       return;
184   }
185
186   // Restart last operation type 
187   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
188     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
189     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
190       // Initialise new line with first point equal to end of previous
191       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
192       if (aPnt2dWgt) {
193         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
194         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
195           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
196         if (aPoint) {
197           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
198           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(), 
199             aWgt->attributeID(), aPoint->x(), aPoint->y());
200           theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
201         }
202       }
203     }
204   } else {
205     // Start editing constraint
206     if (theOperation->isEditOperation()) {
207       std::string aId = theOperation->id().toStdString();
208       if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
209         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
210             (aId == SketchPlugin_ConstraintLength::ID()) || 
211             (aId == SketchPlugin_ConstraintDistance::ID())) {
212           // Find and activate widget for management of point for dimension line position
213           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
214           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
215             PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
216             if (aPntWgt) {
217               aPanel->activateWidget(aPntWgt);
218               return;
219             }
220           }
221         } 
222       }
223     }
224   }
225 }
226
227
228 void PartSet_Module::onSelectionChanged()
229 {
230   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
231   if (!aOperation)
232     return;
233
234   bool isSketcherOp = false;
235   // An edit operation is enable only if the current opeation is the sketch operation
236   if (mySketchMgr->activeSketch()) {
237     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
238       isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
239   }
240   if (isSketcherOp) {
241     // Editing of constraints can be done on selection
242     ModuleBase_ISelection* aSelect = myWorkshop->selection();
243     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
244     if (aSelected.size() == 1) {
245       ModuleBase_ViewerPrs aPrs = aSelected.first();
246       ObjectPtr aObject = aPrs.object();
247       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
248       if (aFeature) {
249         std::string aId = aFeature->getKind();
250         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
251             (aId == SketchPlugin_ConstraintLength::ID()) || 
252             (aId == SketchPlugin_ConstraintDistance::ID())) {
253           editFeature(aFeature);
254         }
255       }
256     }
257   } 
258 }
259
260 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
261 {
262   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
263   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
264   anOpMgr->onKeyReleased(theEvent);
265 }
266
267 void PartSet_Module::onEnterReleased()
268 {
269   myRestartingMode = RM_EmptyFeatureUsed;
270 }
271
272 void PartSet_Module::onOperationActivatedByPreselection()
273 {
274   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
275   if (!aOperation)
276     return;
277
278   // Set final definitions if they are necessary
279   //propertyPanelDefined(aOperation);
280
281   /// Commit sketcher operations automatically
282   FeaturePtr aFeature = aOperation->feature();
283   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
284             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
285   if (aSPFeature) {
286     aOperation->commit();
287   }
288 }
289
290 void PartSet_Module::onNoMoreWidgets()
291 {
292   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
293   if (aOperation) {
294     /// Restart sketcher operations automatically
295     FeaturePtr aFeature = aOperation->feature();
296     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
297               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
298     if (aSPFeature) {
299       if (myRestartingMode != RM_Forbided)
300         myRestartingMode = RM_LastFeatureUsed;
301       aOperation->commit();
302     }
303   }
304 }
305
306 void PartSet_Module::onVertexSelected(ObjectPtr theObject, const TopoDS_Shape& theShape)
307 {
308   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
309   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
310     /// If last line finished on vertex the lines creation sequence has to be break
311     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
312     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
313     if (aWidgets.last() == aPanel->activeWidget()) {
314       myRestartingMode = RM_Forbided;
315     }
316   }
317 }
318
319 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
320                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
321                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
322 {
323   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
324   XGUI_Workshop* aWorkshop = aConnector->workshop();
325   if (theType == "sketch-start-label") {
326     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
327     aWgt->setWorkshop(aWorkshop);
328     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
329       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
330     theModelWidgets.append(aWgt);
331     return aWgt->getControl();
332
333   } else if (theType == "sketch-2dpoint_selector") {
334     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
335     aWgt->setWorkshop(aWorkshop);
336     aWgt->setSketch(mySketchMgr->activeSketch());
337
338     connect(aWgt, SIGNAL(vertexSelected(ObjectPtr, const TopoDS_Shape&)), 
339       this, SLOT(onVertexSelected(ObjectPtr, const TopoDS_Shape&)));
340
341     theModelWidgets.append(aWgt);
342     return aWgt->getControl();
343
344   } if (theType == "point2ddistance") {
345     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
346     aWgt->setWorkshop(aWorkshop);
347     aWgt->setSketch(mySketchMgr->activeSketch());
348
349     theModelWidgets.append(aWgt);
350     return aWgt->getControl();
351
352   } if (theType == "sketch_shape_selector") {
353     PartSet_WidgetShapeSelector* aWgt = 
354       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
355     aWgt->setSketcher(mySketchMgr->activeSketch());
356
357     theModelWidgets.append(aWgt);
358     return aWgt->getControl();
359
360   } if (theType == "sketch_constraint_shape_selector") {
361     PartSet_WidgetConstraintShapeSelector* aWgt = 
362       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
363     aWgt->setSketcher(mySketchMgr->activeSketch());
364
365     theModelWidgets.append(aWgt);
366     return aWgt->getControl();
367
368   } else
369     return 0;
370 }
371