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