Salome HOME
Issue #905 Update of invalid feature representation in property panel
[modules/shaper.git] / src / PartSet / PartSet_WidgetSketchLabel.cpp
1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
2
3 // File:        PartSet_WidgetSketchLabel.cpp
4 // Created:     07 July 2014
5 // Author:      Vitaly SMETANNIKOV
6
7 #include "PartSet_WidgetSketchLabel.h"
8 #include "PartSet_Tools.h"
9
10 #include "SketchPlugin_SketchEntity.h"
11
12 #include <XGUI_Workshop.h>
13 #include <XGUI_Displayer.h>
14 #include <XGUI_SelectionMgr.h>
15 #include <XGUI_Selection.h>
16 #include <XGUI_ViewerProxy.h>
17 #include <XGUI_ActionsMgr.h>
18 #include <XGUI_ModuleConnector.h>
19
20 #include <ModuleBase_Operation.h>
21 #include <ModuleBase_ViewerPrs.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_IModule.h>
24
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_FaceBuilder.h>
29 #include <GeomAlgoAPI_ShapeTools.h>
30 #include <GeomDataAPI_Point.h>
31 #include <GeomDataAPI_Dir.h>
32 #include <GeomAPI_XYZ.h>
33
34 #include <SketchPlugin_Sketch.h>
35 #include <SketcherPrs_Tools.h>
36
37 #include <BRepClass3d_SolidClassifier.hxx>
38 #include <Precision.hxx>
39 #include <gp_Pln.hxx>
40 #include <gp_Pnt.hxx>
41 #include <gp_Dir.hxx>
42 #include <AIS_Shape.hxx>
43 #include <AIS_DimensionSelectionMode.hxx>
44
45 #include <Config_WidgetAPI.h>
46 #include <Config_PropManager.h>
47
48 #include <QLabel>
49 #include <QApplication>
50 #include <QVBoxLayout>
51 #include <QCheckBox>
52
53
54 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
55                                                      ModuleBase_IWorkshop* theWorkshop,
56                                                      const Config_WidgetAPI* theData,
57                                                      const std::string& theParentId,
58                                                      bool toShowConstraints)
59 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId),
60   myPreviewDisplayed(false)
61 {
62   myText = QString::fromStdString(theData->getProperty("title"));
63   myLabel = new QLabel("", theParent);
64   myLabel->setWordWrap(true);
65   myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
66   myLabel->setToolTip("");
67   myLabel->setIndent(5);
68
69   QVBoxLayout* aLayout = new QVBoxLayout(this);
70   ModuleBase_Tools::zeroMargins(aLayout);
71   aLayout->addWidget(myLabel);
72
73   myShowConstraints = new QCheckBox(tr("Show constraints"), this);
74   aLayout->addWidget(myShowConstraints);
75
76   setLayout(aLayout);
77   connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
78   myShowConstraints->setChecked(toShowConstraints);
79 }
80
81 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
82 {
83 }
84
85 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
86                                              const bool theToValidate)
87 {
88   // do not use the given selection if the plane of the sketch has been already set.
89   // If this check is absent, a selected plane in the viewer can be set in the sketch
90   // even if the sketch is built on another plane.
91   if (plane().get())
92     return true;
93
94   ModuleBase_ViewerPrs aPrs = theValues.first();
95   bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
96   if (aDone)
97     updateByPlaneSelected(aPrs);
98
99   return aDone;
100 }
101
102 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
103 {
104   QList<QWidget*> aResult;
105   aResult << myLabel;
106   return aResult;
107 }
108
109 void PartSet_WidgetSketchLabel::onSelectionChanged()
110 {
111   QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
112
113   if (aSelected.empty())
114     return;
115   ModuleBase_ViewerPrs aPrs = aSelected.first();
116
117   bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
118   if (aDone)
119     updateByPlaneSelected(aPrs);
120 }
121
122 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
123 {
124   // 1. hide main planes if they have been displayed
125   erasePreviewPlanes();
126   // 2. if the planes were displayed, change the view projection
127   TopoDS_Shape aShape = thePrs.shape();
128   std::shared_ptr<GeomAPI_Shape> aGShape;
129   std::shared_ptr<GeomAPI_Shape> aBaseShape;
130
131   DataPtr aData = feature()->data();
132   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
133                             (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
134
135   // selection happens in OCC viewer
136   if (!aShape.IsNull()) {
137     aGShape =  std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
138     aGShape->setImpl(new TopoDS_Shape(aShape));
139
140     if (aSelAttr && aSelAttr->context()) {
141       aBaseShape = aSelAttr->context()->shape();
142     }
143   }
144   else { // selection happens in OCC viewer(on body) of in the OB browser
145     if (aSelAttr) {
146       aGShape = aSelAttr->value();
147     }
148   }
149   if (aGShape.get() != NULL) {
150     // get plane parameters
151     std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
152     std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
153     gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
154     double aTwist = 0.0;
155
156     // orienting projection
157     if(aBaseShape.get() != NULL) {
158       std::shared_ptr<GeomAPI_Pnt> aCenterPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aGShape);
159       gp_Pnt aPnt = aCenterPnt->impl<gp_Pnt>();
160       aPnt.Translate(aDir->impl<gp_Dir>().XYZ() * (10 * Precision::Confusion()));
161
162       BRepClass3d_SolidClassifier aClassifier;
163       aClassifier.Load(aBaseShape->impl<TopoDS_Shape>());
164       aClassifier.Perform(aPnt, Precision::Confusion());
165
166       if(aClassifier.State() == TopAbs_IN) {
167         aXYZ.Reverse();
168       }
169     }
170
171     myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
172   }
173   // 3. Clear text in the label
174   myLabel->setText("");
175   myLabel->setToolTip("");
176   disconnect(workshop()->selector(), SIGNAL(selectionChanged()), 
177               this, SLOT(onSelectionChanged()));
178   // 4. deactivate face selection filter
179   activateFilters(false);
180
181   // 5. Clear selection mode and define sketching mode
182   //XGUI_Displayer* aDisp = workshop()->displayer();
183   //aDisp->closeLocalContexts();
184   emit planeSelected(plane());
185   emit valuesChanged();
186   // after the plane is selected in the sketch, the sketch selection should be activated
187   // it can not be performed in the sketch label widget because, we don't need to switch off
188   // the selection by any label deactivation, but need to switch it off by stop the sketch
189   activateSelection(true);
190
191   // 6. Update sketcher actions
192   XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
193   anActMgr->update();
194   myWorkshop->viewer()->update();
195 }
196
197 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
198 {
199   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
200   return PartSet_Tools::sketchPlane(aSketch);
201 }
202
203 bool PartSet_WidgetSketchLabel::focusTo()
204 {
205   myLabel->setFocus();
206   return true;
207 }
208
209 void PartSet_WidgetSketchLabel::enableFocusProcessing()
210 {
211   myLabel->installEventFilter(this);
212 }
213
214 void PartSet_WidgetSketchLabel::storeAttributeValue()
215 {
216   ModuleBase_WidgetValidated::storeAttributeValue();
217 }
218
219 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
220 {
221   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
222
223   // it is not necessary to save the previous plane value because the plane is chosen once
224   DataPtr aData = feature()->data();
225   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
226     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
227   if (aSelAttr) {
228     ResultPtr anEmptyResult;
229     GeomShapePtr anEmptyShape;
230     aSelAttr->setValue(anEmptyResult, anEmptyShape);
231   }
232 }
233
234 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
235 {
236   bool isOwnerSet = false;
237
238   const TopoDS_Shape& aShape = thePrs.shape();
239   std::shared_ptr<GeomAPI_Dir> aDir;
240
241   if (thePrs.object() && (feature() != thePrs.object())) {
242     DataPtr aData = feature()->data();
243     AttributeSelectionPtr aSelAttr = 
244       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
245       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
246     if (aSelAttr) {
247       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
248       if (aRes) {
249         GeomShapePtr aShapePtr(new GeomAPI_Shape());
250         if (aShape.IsNull()) {  // selection happens in the OCC viewer
251           aShapePtr = ModelAPI_Tools::shape(aRes);
252         }
253         else { // selection happens in OB browser
254           aShapePtr->setImpl(new TopoDS_Shape(aShape));
255         }
256         if (aShapePtr.get() != NULL) {
257           aSelAttr->setValue(aRes, aShapePtr);
258           isOwnerSet = true;
259         }
260       }
261     }
262   }
263   else if (!aShape.IsNull()) {
264     aDir = setSketchPlane(aShape);
265     isOwnerSet = aDir.get();
266   }
267   return isOwnerSet;
268 }
269
270 void PartSet_WidgetSketchLabel::activateCustom()
271 {
272   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
273   if (aPlane.get()) {
274     activateSelection(true);
275     return;
276   }
277
278   bool aBodyIsVisualized = false;
279   XGUI_Displayer* aDisp = workshop()->displayer();
280   QObjectPtrList aDisplayed = aDisp->displayedObjects();
281   foreach (ObjectPtr anObj, aDisplayed) {
282     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
283     if (aResult.get() != NULL) {
284       aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
285       if (aBodyIsVisualized)
286         break;
287     }
288   }
289
290   if (!aBodyIsVisualized) {
291     // We have to select a plane before any operation
292     showPreviewPlanes();
293   }
294   activateSelection(true);
295
296   myLabel->setText(myText);
297   myLabel->setToolTip(myTooltip);
298
299   connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
300   activateFilters(true);
301
302   aDisp->updateViewer();
303 }
304
305 void PartSet_WidgetSketchLabel::deactivate()
306 {
307   erasePreviewPlanes();
308   activateSelection(false);
309
310   activateFilters(false);
311 }
312
313 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
314 {
315   if (toActivate) {
316     QIntList aModes;
317     std::shared_ptr<GeomAPI_Pln> aPlane = plane();
318     if (aPlane.get()) {
319       myWorkshop->module()->activeSelectionModes(aModes);
320     }
321     else {
322       aModes << TopAbs_FACE;
323     }
324     myWorkshop->activateSubShapesSelection(aModes);
325   } else {
326     myWorkshop->deactivateSubShapesSelection();
327   }
328 }
329
330 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
331 {
332   if (myPreviewDisplayed) {
333     XGUI_Displayer* aDisp = workshop()->displayer();
334     aDisp->eraseAIS(myYZPlane, false);
335     aDisp->eraseAIS(myXZPlane, false);
336     aDisp->eraseAIS(myXYPlane, false);
337     myPreviewDisplayed = false;
338   }
339 }
340
341 void PartSet_WidgetSketchLabel::showPreviewPlanes()
342 {
343   if (myPreviewDisplayed)
344     return;
345
346   if (!myYZPlane) { // If planes are not created
347     // Create Preview
348     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
349     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
350     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
351     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
352
353     std::vector<int> aYZRGB, aXZRGB, aXYRGB;
354     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
355                                                         YZ_PLANE_COLOR);
356     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
357                                                         XZ_PLANE_COLOR);
358     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
359                                                         XY_PLANE_COLOR);
360     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
361     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
362     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
363
364     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
365     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
366     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
367   }
368   XGUI_Displayer* aDisp = workshop()->displayer();
369   aDisp->displayAIS(myYZPlane, true, false);
370   aDisp->displayAIS(myXZPlane, true, false);
371   aDisp->displayAIS(myXYPlane, true, false);
372   myPreviewDisplayed = true;
373 }
374
375
376 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
377                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
378                                                            const int theRGB[3])
379 {
380   double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
381   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
382   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
383   aAIS->createShape(aFace);
384   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
385   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
386   return aAIS;
387 }
388
389
390 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
391 {
392   if (theShape.IsNull())
393     return std::shared_ptr<GeomAPI_Dir>();
394
395   // get selected shape
396   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
397   aGShape->setImpl(new TopoDS_Shape(theShape));
398
399
400
401   // get plane parameters
402   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
403
404   // set plane parameters to feature
405   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
406   double anA, aB, aC, aD;
407   aPlane->coefficients(anA, aB, aC, aD);
408
409   // calculate attributes of the sketch
410   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
411   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
412   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
413   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
414   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
415   // X axis is preferable to be dirX on the sketch
416   const double tol = Precision::Confusion();
417   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
418   std::shared_ptr<GeomAPI_Dir> aTempDir(
419       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
420   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
421   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
422
423   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
424       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
425   anOrigin->setValue(anOrigPnt);
426   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
427       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
428   aNormal->setValue(aNormDir);
429   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
430       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
431   aDirX->setValue(aXDir);
432   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
433   return aDir;
434 }
435
436 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
437 {
438   myShowConstraints->setChecked(theOn);
439   emit showConstraintToggled(theOn);
440 }
441
442 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
443 {
444   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
445   return aConnector->workshop();
446 }