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