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   aFactory->registerValidator("PartSet_SketchValidator", new PartSet_SketchValidator);
117 }
118
119 void PartSet_Module::registerFilters()
120 {
121   //Registering of selection filters
122   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
123   ModuleBase_FilterFactory* aFactory = aConnector->selectionFilters();
124
125   aFactory->registerFilter("EdgeFilter", new ModuleBase_FilterLinearEdge);
126   aFactory->registerFilter("FaceFilter", new ModuleBase_FilterFace);
127 }
128
129 void PartSet_Module::operationCommitted(ModuleBase_Operation* theOperation) 
130 {
131   if (theOperation->isEditOperation())
132     return;
133   /// Restart sketcher operations automatically
134   FeaturePtr aFeature = theOperation->feature();
135   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
136             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
137   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
138                      myRestartingMode == RM_EmptyFeatureUsed)) {
139     myLastOperationId = theOperation->id();
140     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
141     launchOperation(myLastOperationId);
142   }
143   breakOperationSequence();
144 }
145
146 void PartSet_Module::breakOperationSequence()
147 {
148   myLastOperationId = "";
149   myLastFeature = FeaturePtr();
150   myRestartingMode = RM_None;
151 }
152
153 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
154 {
155   breakOperationSequence();
156 }
157
158 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
159 {
160   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
161     mySketchMgr->startSketch(theOperation);
162   }
163   if (myDocumentShapeFilter.IsNull())
164     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
165   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
166 }
167
168 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
169 {
170   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
171     mySketchMgr->stopSketch(theOperation);
172   }
173   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
174 }
175
176
177 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
178 {
179   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
180   if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) && 
181     (theOperation->isEditOperation())) {
182     // we have to manually activate the sketch label in edit mode
183       aPanel->activateWidget(aPanel->modelWidgets().first());
184       return;
185   }
186
187   // Restart last operation type 
188   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
189     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
190     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
191       // Initialise new line with first point equal to end of previous
192       PartSet_WidgetPoint2D* aPnt2dWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
193       if (aPnt2dWgt) {
194         std::shared_ptr<ModelAPI_Data> aData = myLastFeature->data();
195         std::shared_ptr<GeomDataAPI_Point2D> aPoint = 
196           std::dynamic_pointer_cast<GeomDataAPI_Point2D>(aData->attribute(SketchPlugin_Line::END_ID()));
197         if (aPoint) {
198           aPnt2dWgt->setPoint(aPoint->x(), aPoint->y());
199           PartSet_Tools::setConstraints(mySketchMgr->activeSketch(), theOperation->feature(), 
200             aWgt->attributeID(), aPoint->x(), aPoint->y());
201           theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
202         }
203       }
204     }
205   } else {
206     // Start editing constraint
207     if (theOperation->isEditOperation()) {
208       std::string aId = theOperation->id().toStdString();
209       if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
210         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
211             (aId == SketchPlugin_ConstraintLength::ID()) || 
212             (aId == SketchPlugin_ConstraintDistance::ID())) {
213           // Find and activate widget for management of point for dimension line position
214           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
215           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
216             PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
217             if (aPntWgt) {
218               aPanel->activateWidget(aPntWgt);
219               return;
220             }
221           }
222         } 
223       }
224     }
225   }
226 }
227
228
229 void PartSet_Module::onSelectionChanged()
230 {
231   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
232   if (!aOperation)
233     return;
234
235   bool isSketcherOp = false;
236   // An edit operation is enable only if the current opeation is the sketch operation
237   if (mySketchMgr->activeSketch()) {
238     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
239       isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
240   }
241   if (isSketcherOp) {
242     // Editing of constraints can be done on selection
243     ModuleBase_ISelection* aSelect = myWorkshop->selection();
244     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
245     if (aSelected.size() == 1) {
246       ModuleBase_ViewerPrs aPrs = aSelected.first();
247       ObjectPtr aObject = aPrs.object();
248       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
249       if (aFeature) {
250         std::string aId = aFeature->getKind();
251         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
252             (aId == SketchPlugin_ConstraintLength::ID()) || 
253             (aId == SketchPlugin_ConstraintDistance::ID())) {
254           editFeature(aFeature);
255         }
256       }
257     }
258   } 
259 }
260
261 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
262 {
263   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
264   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
265   anOpMgr->onKeyReleased(theEvent);
266 }
267
268 void PartSet_Module::onEnterReleased()
269 {
270   myRestartingMode = RM_EmptyFeatureUsed;
271 }
272
273 void PartSet_Module::onOperationActivatedByPreselection()
274 {
275   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
276   if (!aOperation)
277     return;
278
279   // Set final definitions if they are necessary
280   //propertyPanelDefined(aOperation);
281
282   /// Commit sketcher operations automatically
283   FeaturePtr aFeature = aOperation->feature();
284   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
285             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
286   if (aSPFeature) {
287     aOperation->commit();
288   }
289 }
290
291 void PartSet_Module::onNoMoreWidgets()
292 {
293   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
294   if (aOperation) {
295     /// Restart sketcher operations automatically
296     FeaturePtr aFeature = aOperation->feature();
297     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
298               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
299     if (aSPFeature) {
300       if (myRestartingMode != RM_Forbided)
301         myRestartingMode = RM_LastFeatureUsed;
302       aOperation->commit();
303     }
304   }
305 }
306
307 void PartSet_Module::onVertexSelected()
308 {
309   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
310   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
311     /// If last line finished on vertex the lines creation sequence has to be break
312     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
313     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
314     if (aWidgets.last() == aPanel->activeWidget()) {
315       myRestartingMode = RM_Forbided;
316     }
317   }
318 }
319
320 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
321                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
322                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
323 {
324   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
325   XGUI_Workshop* aWorkshop = aConnector->workshop();
326   if (theType == "sketch-start-label") {
327     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
328     aWgt->setWorkshop(aWorkshop);
329     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
330       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
331     theModelWidgets.append(aWgt);
332     return aWgt->getControl();
333
334   } else if (theType == "sketch-2dpoint_selector") {
335     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
336     aWgt->setWorkshop(aWorkshop);
337     aWgt->setSketch(mySketchMgr->activeSketch());
338
339     connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
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