Salome HOME
The constraint is moved by the mouse click on it(an attempt to edit value by double...
[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   // the selection is cleared after commit the create operation
134   // in order to do not use the same selected objects in the restarted operation
135   // for common behaviour, the selection is cleared even if the operation is not restarted
136   myWorkshop->viewer()->AISContext()->ClearSelected();
137
138   /// Restart sketcher operations automatically
139   FeaturePtr aFeature = theOperation->feature();
140   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
141             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
142   if (aSPFeature && (myRestartingMode == RM_LastFeatureUsed ||
143                      myRestartingMode == RM_EmptyFeatureUsed)) {
144     myLastOperationId = theOperation->id();
145     myLastFeature = myRestartingMode == RM_LastFeatureUsed ? theOperation->feature() : FeaturePtr();
146     
147     launchOperation(myLastOperationId);
148   }
149   breakOperationSequence();
150 }
151
152 void PartSet_Module::breakOperationSequence()
153 {
154   myLastOperationId = "";
155   myLastFeature = FeaturePtr();
156   myRestartingMode = RM_None;
157 }
158
159 void PartSet_Module::operationAborted(ModuleBase_Operation* theOperation)
160 {
161   breakOperationSequence();
162 }
163
164 void PartSet_Module::operationStarted(ModuleBase_Operation* theOperation)
165 {
166   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
167     mySketchMgr->startSketch(theOperation);
168   }
169   if (myDocumentShapeFilter.IsNull())
170     myDocumentShapeFilter = new PartSet_GlobalFilter(myWorkshop);
171   myWorkshop->viewer()->addSelectionFilter(myDocumentShapeFilter);
172 }
173
174 void PartSet_Module::operationStopped(ModuleBase_Operation* theOperation)
175 {
176   if (theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) {
177     mySketchMgr->stopSketch(theOperation);
178   }
179   myWorkshop->viewer()->removeSelectionFilter(myDocumentShapeFilter);
180 }
181
182
183 void PartSet_Module::propertyPanelDefined(ModuleBase_Operation* theOperation)
184 {
185   ModuleBase_IPropertyPanel* aPanel = theOperation->propertyPanel();
186   if ((theOperation->id().toStdString() == SketchPlugin_Sketch::ID()) && 
187     (theOperation->isEditOperation())) {
188     // we have to manually activate the sketch label in edit mode
189       aPanel->activateWidget(aPanel->modelWidgets().first());
190       return;
191   }
192
193   // Restart last operation type 
194   if ((theOperation->id() == myLastOperationId) && myLastFeature) {
195     ModuleBase_ModelWidget* aWgt = aPanel->activeWidget();
196     if (theOperation->id().toStdString() == SketchPlugin_Line::ID()) {
197       // Initialise new line with first point equal to end of previous
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(mySketchMgr->activeSketch(), theOperation->feature(), 
206             aWgt->attributeID(), aPoint->x(), aPoint->y());
207           theOperation->propertyPanel()->activateNextWidget(aPnt2dWgt);
208         }
209       }
210     }
211   } else {
212     // Start editing constraint
213     if (theOperation->isEditOperation()) {
214       std::string aId = theOperation->id().toStdString();
215       if (PartSet_SketcherMgr::sketchOperationIdList().contains(QString(aId.c_str()))) {
216         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
217             (aId == SketchPlugin_ConstraintLength::ID()) || 
218             (aId == SketchPlugin_ConstraintDistance::ID())) {
219           // Find and activate widget for management of point for dimension line position
220           QList<ModuleBase_ModelWidget*> aWidgets = aPanel->modelWidgets();
221           foreach (ModuleBase_ModelWidget* aWgt, aWidgets) {
222             PartSet_WidgetPoint2D* aPntWgt = dynamic_cast<PartSet_WidgetPoint2D*>(aWgt);
223             if (aPntWgt) {
224               aPanel->activateWidget(aPntWgt);
225               return;
226             }
227           }
228         } 
229       }
230     }
231   }
232 }
233
234
235 void PartSet_Module::onSelectionChanged()
236 {
237   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
238   if (!aOperation)
239     return;
240
241   bool isSketcherOp = false;
242   // An edit operation is enable only if the current opeation is the sketch operation
243   if (mySketchMgr->activeSketch()) {
244     if (PartSet_Tools::sketchPlane(mySketchMgr->activeSketch()))
245       isSketcherOp = (aOperation->id().toStdString() == SketchPlugin_Sketch::ID());
246   }
247   if (isSketcherOp) {
248     // Editing of constraints can be done on selection
249     ModuleBase_ISelection* aSelect = myWorkshop->selection();
250     QList<ModuleBase_ViewerPrs> aSelected = aSelect->getSelected();
251     if (aSelected.size() == 1) {
252       ModuleBase_ViewerPrs aPrs = aSelected.first();
253       ObjectPtr aObject = aPrs.object();
254       FeaturePtr aFeature = ModelAPI_Feature::feature(aObject);
255       if (aFeature) {
256         std::string aId = aFeature->getKind();
257         if ((aId == SketchPlugin_ConstraintRadius::ID()) ||
258             (aId == SketchPlugin_ConstraintLength::ID()) || 
259             (aId == SketchPlugin_ConstraintDistance::ID())) {
260           editFeature(aFeature);
261         }
262       }
263     }
264   } 
265 }
266
267 void PartSet_Module::onKeyRelease(ModuleBase_IViewWindow* theWnd, QKeyEvent* theEvent)
268 {
269   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
270   XGUI_OperationMgr* anOpMgr = aConnector->workshop()->operationMgr();
271   anOpMgr->onKeyReleased(theEvent);
272 }
273
274 void PartSet_Module::onEnterReleased()
275 {
276   myRestartingMode = RM_EmptyFeatureUsed;
277 }
278
279 void PartSet_Module::onOperationActivatedByPreselection()
280 {
281   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
282   if (!aOperation)
283     return;
284
285   // Set final definitions if they are necessary
286   //propertyPanelDefined(aOperation);
287
288   /// Commit sketcher operations automatically
289   FeaturePtr aFeature = aOperation->feature();
290   std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
291             std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
292   if (aSPFeature) {
293     aOperation->commit();
294   }
295 }
296
297 void PartSet_Module::onNoMoreWidgets()
298 {
299   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
300   if (aOperation) {
301     /// Restart sketcher operations automatically
302     FeaturePtr aFeature = aOperation->feature();
303     std::shared_ptr<SketchPlugin_Feature> aSPFeature = 
304               std::dynamic_pointer_cast<SketchPlugin_Feature>(aFeature);
305     if (aSPFeature) {
306       if (myRestartingMode != RM_Forbided)
307         myRestartingMode = RM_LastFeatureUsed;
308       aOperation->commit();
309     }
310   }
311 }
312
313 void PartSet_Module::onVertexSelected()
314 {
315   ModuleBase_Operation* aOperation = myWorkshop->currentOperation();
316   if (aOperation->id().toStdString() == SketchPlugin_Line::ID()) {
317     /// If last line finished on vertex the lines creation sequence has to be break
318     ModuleBase_IPropertyPanel* aPanel = aOperation->propertyPanel();
319     const QList<ModuleBase_ModelWidget*>& aWidgets = aPanel->modelWidgets();
320     if (aWidgets.last() == aPanel->activeWidget()) {
321       myRestartingMode = RM_Forbided;
322     }
323   }
324 }
325
326 QWidget* PartSet_Module::createWidgetByType(const std::string& theType, QWidget* theParent,
327                                             Config_WidgetAPI* theWidgetApi, std::string theParentId,
328                                             QList<ModuleBase_ModelWidget*>& theModelWidgets)
329 {
330   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(workshop());
331   XGUI_Workshop* aWorkshop = aConnector->workshop();
332   if (theType == "sketch-start-label") {
333     PartSet_WidgetSketchLabel* aWgt = new PartSet_WidgetSketchLabel(theParent, theWidgetApi, theParentId);
334     aWgt->setWorkshop(aWorkshop);
335     connect(aWgt, SIGNAL(planeSelected(const std::shared_ptr<GeomAPI_Pln>&)), 
336       mySketchMgr, SLOT(onPlaneSelected(const std::shared_ptr<GeomAPI_Pln>&)));
337     theModelWidgets.append(aWgt);
338     return aWgt->getControl();
339
340   } else if (theType == "sketch-2dpoint_selector") {
341     PartSet_WidgetPoint2D* aWgt = new PartSet_WidgetPoint2D(theParent, theWidgetApi, theParentId);
342     aWgt->setWorkshop(aWorkshop);
343     aWgt->setSketch(mySketchMgr->activeSketch());
344
345     connect(aWgt, SIGNAL(vertexSelected()), this, SLOT(onVertexSelected()));
346
347     theModelWidgets.append(aWgt);
348     return aWgt->getControl();
349
350   } if (theType == "point2ddistance") {
351     PartSet_WidgetPoint2dDistance* aWgt = new PartSet_WidgetPoint2dDistance(theParent, theWidgetApi, theParentId);
352     aWgt->setWorkshop(aWorkshop);
353     aWgt->setSketch(mySketchMgr->activeSketch());
354
355     theModelWidgets.append(aWgt);
356     return aWgt->getControl();
357
358   } if (theType == "sketch_shape_selector") {
359     PartSet_WidgetShapeSelector* aWgt = 
360       new PartSet_WidgetShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
361     aWgt->setSketcher(mySketchMgr->activeSketch());
362
363     theModelWidgets.append(aWgt);
364     return aWgt->getControl();
365
366   } if (theType == "sketch_constraint_shape_selector") {
367     PartSet_WidgetConstraintShapeSelector* aWgt = 
368       new PartSet_WidgetConstraintShapeSelector(theParent, workshop(), theWidgetApi, theParentId);
369     aWgt->setSketcher(mySketchMgr->activeSketch());
370
371     theModelWidgets.append(aWgt);
372     return aWgt->getControl();
373
374   } else
375     return 0;
376 }
377