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