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