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