]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetSketchLabel.cpp
Salome HOME
Merge branch 'Dev_1.1.0' of newgeom:newgeom 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 <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     // 2. set the selection to sketch
118     setSelectionCustom(aPrs);
119     // 3. hide main planes if they have been displayed
120     erasePreviewPlanes();
121     // 4. if the planes were displayed, change the view projection
122     TopoDS_Shape aShape = aPrs.shape();
123     if (!aShape.IsNull()) {
124       DataPtr aData = feature()->data();
125       AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
126                                 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
127       if (aSelAttr) {
128         GeomShapePtr aShapePtr = aSelAttr->value();
129         if (aShapePtr.get() == NULL || aShapePtr->isNull()) {
130           //TopoDS_Shape aShape = aShapePtr->impl<TopoDS_Shape>();
131           std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
132           aGShape->setImpl(new TopoDS_Shape(aShape));
133           // get plane parameters
134           std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
135           std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
136
137           myWorkshop->viewer()->setViewProjection(aDir->x(), aDir->y(), aDir->z());
138         }
139       }
140     }
141     // 5. Clear text in the label
142     myLabel->setText("");
143     myLabel->setToolTip("");
144     disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
145                this, SLOT(onSelectionChanged()));
146     // 6. deactivate face selection filter
147     activateFilters(myWorkshop->module()->workshop(), false);
148
149     // 7. Clear selection mode and define sketching mode
150     //XGUI_Displayer* aDisp = myWorkshop->displayer();
151     //aDisp->closeLocalContexts();
152     emit planeSelected(plane());
153     //setSketchingMode();
154
155     // 8. Update sketcher actions
156     XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
157     anActMgr->update();
158     myWorkshop->viewer()->update();
159   }
160 }
161
162 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
163 {
164   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
165   return PartSet_Tools::sketchPlane(aSketch);
166
167 }
168
169 bool PartSet_WidgetSketchLabel::focusTo()
170 {
171   myLabel->setFocus();
172   return true;
173 }
174
175 void PartSet_WidgetSketchLabel::enableFocusProcessing()
176 {
177   myLabel->installEventFilter(this);
178 }
179
180 void PartSet_WidgetSketchLabel::storeAttributeValue()
181 {
182 }
183
184 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
185 {
186   // it is not necessary to save the previous plane value because the plane is chosen once
187   DataPtr aData = feature()->data();
188   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
189     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
190   if (aSelAttr) {
191     ResultPtr anEmptyResult;
192     GeomShapePtr anEmptyShape;
193     aSelAttr->setValue(anEmptyResult, anEmptyShape);
194   }
195 }
196
197 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
198 {
199   bool isOwnerSet = false;
200
201   const TopoDS_Shape& aShape = thePrs.shape();
202   std::shared_ptr<GeomAPI_Dir> aDir;
203
204   if (thePrs.object() && (feature() != thePrs.object())) {
205     DataPtr aData = feature()->data();
206     AttributeSelectionPtr aSelAttr = 
207       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
208       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
209     if (aSelAttr) {
210       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
211       if (aRes) {
212         GeomShapePtr aShapePtr(new GeomAPI_Shape());
213         if (aShape.IsNull()) {
214           aShapePtr = ModelAPI_Tools::shape(aRes);
215         }
216         else {
217           aShapePtr->setImpl(new TopoDS_Shape(aShape));
218         }
219         aSelAttr->setValue(aRes, aShapePtr);
220         isOwnerSet = true;
221       }
222     }
223   }
224   else if (!aShape.IsNull()) {
225     aDir = setSketchPlane(aShape);
226     isOwnerSet = aDir;
227   }
228   return isOwnerSet;
229 }
230
231 void PartSet_WidgetSketchLabel::activateCustom()
232 {
233   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
234   bool aBodyIsVisualized = false;
235   XGUI_Displayer* aDisp = myWorkshop->displayer();
236   QObjectPtrList aDisplayed = aDisp->displayedObjects();
237   foreach (ObjectPtr anObj, aDisplayed) {
238     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
239     if (aResult.get() != NULL) {
240       aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
241       if (aBodyIsVisualized)
242         break;
243     }
244   }
245
246   if (aPlane || aBodyIsVisualized) {
247     //setSketchingMode();
248     // In order to avoid Opening/Closing of context too often
249     // it can be useful for a delay on the property panel filling
250     // it is possible that it is not necessary anymore, but it requires a check
251     //mySelectionTimer->start(20);
252     //setSketchingMode();
253   } else {
254     // We have to select a plane before any operation
255     showPreviewPlanes();
256   }
257   QIntList aModes;
258   aModes << TopAbs_FACE;
259   aDisp->activateObjects(aModes);
260
261   myLabel->setText(myText);
262   myLabel->setToolTip(myTooltip);
263
264   connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
265   activateFilters(myWorkshop->module()->workshop(), true);
266
267   aDisp->updateViewer();
268 }
269
270 void PartSet_WidgetSketchLabel::deactivate()
271 {
272   // Do not set selection mode if the widget was activated for a small moment 
273   //mySelectionTimer->stop();
274   //XGUI_Displayer* aDisp = myWorkshop->displayer();
275   //aDisp->closeLocalContexts();
276   erasePreviewPlanes();
277   activateFilters(myWorkshop->module()->workshop(), false);
278 }
279
280 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
281 {
282   if (myPreviewDisplayed) {
283     XGUI_Displayer* aDisp = myWorkshop->displayer();
284     aDisp->eraseAIS(myYZPlane, false);
285     aDisp->eraseAIS(myXZPlane, false);
286     aDisp->eraseAIS(myXYPlane, false);
287     myPreviewDisplayed = false;
288   }
289 }
290
291 void PartSet_WidgetSketchLabel::showPreviewPlanes()
292 {
293   if (myPreviewDisplayed)
294     return;
295
296   if (!myYZPlane) { // If planes are not created
297     // Create Preview
298     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
299     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
300     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
301     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
302
303     std::vector<int> aYZRGB, aXZRGB, aXYRGB;
304     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
305                                                         YZ_PLANE_COLOR);
306     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
307                                                         XZ_PLANE_COLOR);
308     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
309                                                         XY_PLANE_COLOR);
310     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
311     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
312     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
313
314     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
315     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
316     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
317   }
318   XGUI_Displayer* aDisp = myWorkshop->displayer();
319   aDisp->displayAIS(myYZPlane, false);
320   aDisp->displayAIS(myXZPlane, false);
321   aDisp->displayAIS(myXYPlane, false);
322   myPreviewDisplayed = true;
323 }
324
325
326 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
327                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
328                                                            const int theRGB[3])
329 {
330   double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
331   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
332   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
333   aAIS->createShape(aFace);
334   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
335   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
336   return aAIS;
337 }
338
339
340 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
341 {
342   if (theShape.IsNull())
343     return std::shared_ptr<GeomAPI_Dir>();
344
345   // get selected shape
346   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
347   aGShape->setImpl(new TopoDS_Shape(theShape));
348
349   // get plane parameters
350   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
351
352   // set plane parameters to feature
353   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
354   double anA, aB, aC, aD;
355   aPlane->coefficients(anA, aB, aC, aD);
356
357   // calculate attributes of the sketch
358   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
359   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
360   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
361   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
362   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
363   // X axis is preferable to be dirX on the sketch
364   const double tol = Precision::Confusion();
365   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
366   std::shared_ptr<GeomAPI_Dir> aTempDir(
367       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
368   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
369   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
370
371   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
372       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
373   anOrigin->setValue(anOrigPnt);
374   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
375       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
376   aNormal->setValue(aNormDir);
377   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
378       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
379   aDirX->setValue(aXDir);
380   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
381   return aDir;
382 }
383
384
385 /*void PartSet_WidgetSketchLabel::setSketchingMode()
386 {
387   XGUI_Displayer* aDisp = myWorkshop->displayer();
388   // Clear standard selection modes if they are defined
389   //aDisp->activateObjects(aModes);
390   //aDisp->openLocalContext();
391
392   // Get default selection modes
393   
394   QIntList aModes;
395   aModes.append(SketcherPrs_Tools::Sel_Dimension_Text);
396   aModes.append(SketcherPrs_Tools::Sel_Dimension_Line);
397   aModes.append(SketcherPrs_Tools::Sel_Constraint);
398   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_VERTEX));
399   aModes.append(AIS_Shape::SelectionMode((TopAbs_ShapeEnum) TopAbs_EDGE));
400
401   aDisp->activateObjects(aModes);
402 }*/
403
404 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
405 {
406   myShowConstraints->setChecked(theOn);
407   emit showConstraintToggled(theOn);
408 }