Salome HOME
There is a problem with lost selection on an edge. Create a sketch, create two lines...
[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
19 #include <ModuleBase_Operation.h>
20 #include <ModuleBase_ViewerPrs.h>
21 #include <ModuleBase_Tools.h>
22 #include <ModuleBase_IModule.h>
23
24 #include <GeomAlgoAPI_FaceBuilder.h>
25 #include <GeomDataAPI_Point.h>
26 #include <GeomDataAPI_Dir.h>
27 #include <GeomAPI_XYZ.h>
28
29 #include <SketchPlugin_Sketch.h>
30 #include <SketcherPrs_Tools.h>
31
32 #include <Precision.hxx>
33 #include <gp_Pln.hxx>
34 #include <gp_Pnt.hxx>
35 #include <gp_Dir.hxx>
36 #include <AIS_Shape.hxx>
37 #include <AIS_DimensionSelectionMode.hxx>
38
39 #include <Config_WidgetAPI.h>
40 #include <Config_PropManager.h>
41
42 #include <QLabel>
43 //#include <QTimer>
44 #include <QApplication>
45 #include <QVBoxLayout>
46 #include <QCheckBox>
47
48
49 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
50                                                      const Config_WidgetAPI* theData,
51                                                      const std::string& theParentId,
52                                                      bool toShowConstraints)
53     : ModuleBase_WidgetValidated(theParent, theData, theParentId),
54       myPreviewDisplayed(false),
55       myWorkshop(NULL)
56 {
57   myText = QString::fromStdString(theData->getProperty("title"));
58   myLabel = new QLabel("", theParent);
59   myLabel->setWordWrap(true);
60   myTooltip = QString::fromStdString(theData->getProperty("tooltip"));
61   myLabel->setToolTip("");
62   myLabel->setIndent(5);
63
64   //mySelectionTimer = new QTimer(this);
65   //connect(mySelectionTimer, SIGNAL(timeout()), SLOT(setSketchingMode()));
66   //mySelectionTimer->setSingleShot(true);
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   erasePreviewPlanes();
83 }
84
85 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
86 {
87   QList<QWidget*> aResult;
88   aResult << myLabel;
89   return aResult;
90 }
91
92 void PartSet_WidgetSketchLabel::onPlaneSelected()
93 {
94
95   XGUI_Selection* aSelection = myWorkshop->selector()->selection();
96   QList<ModuleBase_ViewerPrs> aSelected = aSelection->getSelected();
97   if (!aSelected.empty()) {
98     ModuleBase_ViewerPrs aPrs = aSelected.first();
99     Handle(SelectMgr_EntityOwner) anOwner = aSelected.first().owner();
100     if (isValid(anOwner)) {
101       setSelection(anOwner);
102
103       TopoDS_Shape aShape = aPrs.shape();
104       if (!aShape.IsNull()) {
105         erasePreviewPlanes();
106         DataPtr aData = feature()->data();
107         AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
108                                   (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
109         if (aSelAttr) {
110           GeomShapePtr aShapePtr = aSelAttr->value();
111           if (aShapePtr.get() == NULL || aShapePtr->isNull()) {
112             std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
113             aGShape->setImpl(new TopoDS_Shape(aShape));
114             // get plane parameters
115             std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
116             std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
117
118             myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
119           }
120         }
121
122         // Clear text in the label
123         myLabel->setText("");
124         myLabel->setToolTip("");
125         disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
126                    this, SLOT(onPlaneSelected()));
127         activateFilters(myWorkshop->module()->workshop(), false);
128
129         // Clear selection mode and define sketching mode
130         //XGUI_Displayer* aDisp = myWorkshop->displayer();
131         //aDisp->closeLocalContexts();
132         emit planeSelected(plane());
133         //setSketchingMode();
134
135         // Update sketcher actions
136         XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
137         anActMgr->update();
138         myWorkshop->viewer()->update();
139       }
140     }
141   }
142 }
143
144 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
145 {
146   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
147   return PartSet_Tools::sketchPlane(aSketch);
148
149 }
150
151 bool PartSet_WidgetSketchLabel::focusTo()
152 {
153   myLabel->setFocus();
154   return true;
155 }
156
157 void PartSet_WidgetSketchLabel::enableFocusProcessing()
158 {
159   myLabel->installEventFilter(this);
160 }
161
162 void PartSet_WidgetSketchLabel::storeAttributeValue()
163 {
164 }
165
166 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
167 {
168   // it is not necessary to save the previous plane value because the plane is chosen once
169   DataPtr aData = feature()->data();
170   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
171     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
172   if (aSelAttr) {
173     ResultPtr anEmptyResult;
174     GeomShapePtr anEmptyShape;
175     aSelAttr->setValue(anEmptyResult, anEmptyShape);
176   }
177 }
178
179 bool PartSet_WidgetSketchLabel::setSelection(const Handle_SelectMgr_EntityOwner& theOwner)
180 {
181   bool isOwnerSet = false;
182
183   ModuleBase_ViewerPrs aPrs;
184   myWorkshop->selector()->selection()->fillPresentation(aPrs, theOwner);
185
186   const TopoDS_Shape& aShape = aPrs.shape();
187   std::shared_ptr<GeomAPI_Dir> aDir;
188
189   if (aPrs.object() && (feature() != aPrs.object())) {
190     DataPtr aData = feature()->data();
191     AttributeSelectionPtr aSelAttr = 
192       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
193       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
194     if (aSelAttr) {
195       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(aPrs.object());
196       if (aRes) {
197         GeomShapePtr aShapePtr(new GeomAPI_Shape());
198         aShapePtr->setImpl(new TopoDS_Shape(aShape));
199         aSelAttr->setValue(aRes, aShapePtr);
200         isOwnerSet = true;
201       }
202     }
203   }
204   else if (!aShape.IsNull()) {
205     aDir = setSketchPlane(aShape);
206     isOwnerSet = aDir;
207   }
208   return isOwnerSet;
209 }
210
211 void PartSet_WidgetSketchLabel::activateCustom()
212 {
213   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
214   if (aPlane) {
215     //setSketchingMode();
216     // In order to avoid Opening/Closing of context too often
217     // it can be useful for a delay on the property panel filling
218     // it is possible that it is not necessary anymore, but it requires a check
219     //mySelectionTimer->start(20);
220   } else {
221     // We have to select a plane before any operation
222     showPreviewPlanes();
223
224     XGUI_Displayer* aDisp = myWorkshop->displayer();
225     //aDisp->openLocalContext();
226     //aDisp->activateObjects(QIntList());
227     QIntList aModes;
228     aModes << TopAbs_FACE;
229     aDisp->activateObjects(aModes);
230
231     myLabel->setText(myText);
232     myLabel->setToolTip(myTooltip);
233
234     connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onPlaneSelected()));
235     activateFilters(myWorkshop->module()->workshop(), true);
236
237     aDisp->updateViewer();
238   }
239 }
240
241 void PartSet_WidgetSketchLabel::deactivate()
242 {
243   // Do not set selection mode if the widget was activated for a small moment 
244   //mySelectionTimer->stop();
245   //XGUI_Displayer* aDisp = myWorkshop->displayer();
246   //aDisp->closeLocalContexts();
247   erasePreviewPlanes();
248   activateFilters(myWorkshop->module()->workshop(), false);
249 }
250
251 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
252 {
253   if (myPreviewDisplayed) {
254     XGUI_Displayer* aDisp = myWorkshop->displayer();
255     aDisp->eraseAIS(myYZPlane, false);
256     aDisp->eraseAIS(myXZPlane, false);
257     aDisp->eraseAIS(myXYPlane, false);
258     myPreviewDisplayed = false;
259   }
260 }
261
262 void PartSet_WidgetSketchLabel::showPreviewPlanes()
263 {
264   if (myPreviewDisplayed)
265     return;
266
267   if (!myYZPlane) { // If planes are not created
268     // Create Preview
269     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
270     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
271     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
272     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
273
274     std::vector<int> aYZRGB, aXZRGB, aXYRGB;
275     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
276                                                         YZ_PLANE_COLOR);
277     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
278                                                         XZ_PLANE_COLOR);
279     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
280                                                         XY_PLANE_COLOR);
281     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
282     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
283     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
284
285     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
286     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
287     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
288   }
289   XGUI_Displayer* aDisp = myWorkshop->displayer();
290   aDisp->displayAIS(myYZPlane, false);
291   aDisp->displayAIS(myXZPlane, false);
292   aDisp->displayAIS(myXYPlane, false);
293   myPreviewDisplayed = true;
294 }
295
296
297 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
298                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
299                                                            const int theRGB[3])
300 {
301   double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
302   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
303   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
304   aAIS->createShape(aFace);
305   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
306   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
307   return aAIS;
308 }
309
310
311 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
312 {
313   if (theShape.IsNull())
314     return std::shared_ptr<GeomAPI_Dir>();
315
316   // get selected shape
317   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
318   aGShape->setImpl(new TopoDS_Shape(theShape));
319
320   // get plane parameters
321   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
322
323   // set plane parameters to feature
324   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
325   double anA, aB, aC, aD;
326   aPlane->coefficients(anA, aB, aC, aD);
327
328   // calculate attributes of the sketch
329   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
330   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
331   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
332   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
333   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
334   // X axis is preferable to be dirX on the sketch
335   const double tol = Precision::Confusion();
336   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
337   std::shared_ptr<GeomAPI_Dir> aTempDir(
338       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
339   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
340   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
341
342   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
343       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
344   anOrigin->setValue(anOrigPnt);
345   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
346       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
347   aNormal->setValue(aNormDir);
348   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
349       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
350   aDirX->setValue(aXDir);
351   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
352   return aDir;
353 }
354
355
356 /*void PartSet_WidgetSketchLabel::setSketchingMode()
357 {
358   XGUI_Displayer* aDisp = myWorkshop->displayer();
359   // Clear standard selection modes if they are defined
360   //aDisp->activateObjects(aModes);
361   //aDisp->openLocalContext();
362
363   // Get default selection modes
364   
365   QIntList aModes;
366   aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
367   aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
368   aModes.append(SketcherPrs_Tools::Sel_Constraint);
369   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
370   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
371
372   aDisp->activateObjects(aModes);
373 }*/
374
375 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
376 {
377   myShowConstraints->setChecked(theOn);
378   emit showConstraintToggled(theOn);
379 }