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