Salome HOME
Fix for the issue #910: never fully remove the results, just make them disabled when...
[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     updateObject(myFeature);
121   }
122 }
123
124 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
125 {
126   // 1. hide main planes if they have been displayed
127   erasePreviewPlanes();
128   // 2. if the planes were displayed, change the view projection
129   TopoDS_Shape aShape = thePrs.shape();
130   std::shared_ptr<GeomAPI_Shape> aGShape;
131   std::shared_ptr<GeomAPI_Shape> aBaseShape;
132
133   DataPtr aData = feature()->data();
134   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
135                             (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
136
137   // selection happens in OCC viewer
138   if (!aShape.IsNull()) {
139     aGShape =  std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
140     aGShape->setImpl(new TopoDS_Shape(aShape));
141
142     if (aSelAttr && aSelAttr->context()) {
143       aBaseShape = aSelAttr->context()->shape();
144     }
145   }
146   else { // selection happens in OCC viewer(on body) of in the OB browser
147     if (aSelAttr) {
148       aGShape = aSelAttr->value();
149     }
150   }
151   if (aGShape.get() != NULL) {
152     // get plane parameters
153     std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
154     std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
155     gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
156     double aTwist = 0.0;
157
158     // orienting projection
159     if(aBaseShape.get() != NULL) {
160       std::shared_ptr<GeomAPI_Pnt> aCenterPnt = GeomAlgoAPI_ShapeTools::centreOfMass(aGShape);
161       gp_Pnt aPnt = aCenterPnt->impl<gp_Pnt>();
162       aPnt.Translate(aDir->impl<gp_Dir>().XYZ() * (10 * Precision::Confusion()));
163
164       BRepClass3d_SolidClassifier aClassifier;
165       aClassifier.Load(aBaseShape->impl<TopoDS_Shape>());
166       aClassifier.Perform(aPnt, Precision::Confusion());
167
168       if(aClassifier.State() == TopAbs_IN) {
169         aXYZ.Reverse();
170       }
171     }
172
173     myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
174   }
175   // 3. Clear text in the label
176   myLabel->setText("");
177   myLabel->setToolTip("");
178   disconnect(workshop()->selector(), SIGNAL(selectionChanged()), 
179               this, SLOT(onSelectionChanged()));
180   // 4. deactivate face selection filter
181   activateFilters(false);
182
183   // 5. Clear selection mode and define sketching mode
184   //XGUI_Displayer* aDisp = workshop()->displayer();
185   //aDisp->closeLocalContexts();
186   emit planeSelected(plane());
187   // after the plane is selected in the sketch, the sketch selection should be activated
188   // it can not be performed in the sketch label widget because, we don't need to switch off
189   // the selection by any label deactivation, but need to switch it off by stop the sketch
190   activateSelection(true);
191
192   // 6. Update sketcher actions
193   XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
194   anActMgr->update();
195   myWorkshop->viewer()->update();
196 }
197
198 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
199 {
200   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
201   return PartSet_Tools::sketchPlane(aSketch);
202 }
203
204 bool PartSet_WidgetSketchLabel::focusTo()
205 {
206   myLabel->setFocus();
207   return true;
208 }
209
210 void PartSet_WidgetSketchLabel::enableFocusProcessing()
211 {
212   myLabel->installEventFilter(this);
213 }
214
215 void PartSet_WidgetSketchLabel::storeAttributeValue()
216 {
217   ModuleBase_WidgetValidated::storeAttributeValue();
218 }
219
220 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
221 {
222   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
223
224   // it is not necessary to save the previous plane value because the plane is chosen once
225   DataPtr aData = feature()->data();
226   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
227     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
228   if (aSelAttr) {
229     ResultPtr anEmptyResult;
230     GeomShapePtr anEmptyShape;
231     aSelAttr->setValue(anEmptyResult, anEmptyShape);
232   }
233 }
234
235 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
236 {
237   bool isOwnerSet = false;
238
239   const TopoDS_Shape& aShape = thePrs.shape();
240   std::shared_ptr<GeomAPI_Dir> aDir;
241
242   if (thePrs.object() && (feature() != thePrs.object())) {
243     DataPtr aData = feature()->data();
244     AttributeSelectionPtr aSelAttr = 
245       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
246       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
247     if (aSelAttr) {
248       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
249       if (aRes) {
250         GeomShapePtr aShapePtr(new GeomAPI_Shape());
251         if (aShape.IsNull()) {  // selection happens in the OCC viewer
252           aShapePtr = ModelAPI_Tools::shape(aRes);
253         }
254         else { // selection happens in OB browser
255           aShapePtr->setImpl(new TopoDS_Shape(aShape));
256         }
257         if (aShapePtr.get() != NULL) {
258           aSelAttr->setValue(aRes, aShapePtr);
259           isOwnerSet = true;
260         }
261       }
262     }
263   }
264   else if (!aShape.IsNull()) {
265     aDir = setSketchPlane(aShape);
266     isOwnerSet = aDir.get();
267   }
268   return isOwnerSet;
269 }
270
271 void PartSet_WidgetSketchLabel::activateCustom()
272 {
273   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
274   if (aPlane.get()) {
275     activateSelection(true);
276     return;
277   }
278
279   bool aBodyIsVisualized = false;
280   XGUI_Displayer* aDisp = workshop()->displayer();
281   QObjectPtrList aDisplayed = aDisp->displayedObjects();
282   foreach (ObjectPtr anObj, aDisplayed) {
283     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
284     if (aResult.get() != NULL) {
285       aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
286       if (aBodyIsVisualized)
287         break;
288     }
289   }
290
291   if (!aBodyIsVisualized) {
292     // We have to select a plane before any operation
293     showPreviewPlanes();
294   }
295   activateSelection(true);
296
297   myLabel->setText(myText);
298   myLabel->setToolTip(myTooltip);
299
300   connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
301   activateFilters(true);
302
303   aDisp->updateViewer();
304 }
305
306 void PartSet_WidgetSketchLabel::deactivate()
307 {
308   erasePreviewPlanes();
309   activateSelection(false);
310
311   activateFilters(false);
312 }
313
314 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
315 {
316   if (toActivate) {
317     QIntList aModes;
318     std::shared_ptr<GeomAPI_Pln> aPlane = plane();
319     if (aPlane.get()) {
320       myWorkshop->module()->activeSelectionModes(aModes);
321     }
322     else {
323       aModes << TopAbs_FACE;
324     }
325     myWorkshop->activateSubShapesSelection(aModes);
326   } else {
327     myWorkshop->deactivateSubShapesSelection();
328   }
329 }
330
331 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
332 {
333   if (myPreviewDisplayed) {
334     XGUI_Displayer* aDisp = workshop()->displayer();
335     aDisp->eraseAIS(myYZPlane, false);
336     aDisp->eraseAIS(myXZPlane, false);
337     aDisp->eraseAIS(myXYPlane, false);
338     myPreviewDisplayed = false;
339   }
340 }
341
342 void PartSet_WidgetSketchLabel::showPreviewPlanes()
343 {
344   if (myPreviewDisplayed)
345     return;
346
347   if (!myYZPlane) { // If planes are not created
348     // Create Preview
349     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
350     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
351     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
352     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
353
354     std::vector<int> aYZRGB, aXZRGB, aXYRGB;
355     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
356                                                         YZ_PLANE_COLOR);
357     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
358                                                         XZ_PLANE_COLOR);
359     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
360                                                         XY_PLANE_COLOR);
361     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
362     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
363     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
364
365     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
366     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
367     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
368   }
369   XGUI_Displayer* aDisp = workshop()->displayer();
370   aDisp->displayAIS(myYZPlane, true, false);
371   aDisp->displayAIS(myXZPlane, true, false);
372   aDisp->displayAIS(myXYPlane, true, false);
373   myPreviewDisplayed = true;
374 }
375
376
377 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
378                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
379                                                            const int theRGB[3])
380 {
381   double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
382   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
383   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
384   aAIS->createShape(aFace);
385   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
386   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
387   return aAIS;
388 }
389
390
391 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
392 {
393   if (theShape.IsNull())
394     return std::shared_ptr<GeomAPI_Dir>();
395
396   // get selected shape
397   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
398   aGShape->setImpl(new TopoDS_Shape(theShape));
399
400
401
402   // get plane parameters
403   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
404
405   // set plane parameters to feature
406   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
407   double anA, aB, aC, aD;
408   aPlane->coefficients(anA, aB, aC, aD);
409
410   // calculate attributes of the sketch
411   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
412   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
413   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
414   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
415   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
416   // X axis is preferable to be dirX on the sketch
417   const double tol = Precision::Confusion();
418   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
419   std::shared_ptr<GeomAPI_Dir> aTempDir(
420       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
421   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
422   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
423
424   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
425       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
426   anOrigin->setValue(anOrigPnt);
427   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
428       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
429   aNormal->setValue(aNormDir);
430   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
431       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
432   aDirX->setValue(aXDir);
433   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
434   return aDir;
435 }
436
437 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
438 {
439   myShowConstraints->setChecked(theOn);
440   emit showConstraintToggled(theOn);
441 }
442
443 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
444 {
445   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
446   return aConnector->workshop();
447 }