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