Salome HOME
Preselection using in operations: setSelection of widget returns a modified list...
[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 #include <XGUI_ModuleConnector.h>
19
20 #include <ModuleBase_Operation.h>
21 #include <ModuleBase_ViewerPrs.h>
22 #include <ModuleBase_Tools.h>
23 #include <ModuleBase_IModule.h>
24
25 #include <ModelAPI_ResultBody.h>
26 #include <ModelAPI_Tools.h>
27
28 #include <GeomAlgoAPI_FaceBuilder.h>
29 #include <GeomDataAPI_Point.h>
30 #include <GeomDataAPI_Dir.h>
31 #include <GeomAPI_XYZ.h>
32
33 #include <SketchPlugin_Sketch.h>
34 #include <SketcherPrs_Tools.h>
35
36 #include <Precision.hxx>
37 #include <gp_Pln.hxx>
38 #include <gp_Pnt.hxx>
39 #include <gp_Dir.hxx>
40 #include <AIS_Shape.hxx>
41 #include <AIS_DimensionSelectionMode.hxx>
42
43 #include <Config_WidgetAPI.h>
44 #include <Config_PropManager.h>
45
46 #include <QLabel>
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   QVBoxLayout* aLayout = new QVBoxLayout(this);
68   ModuleBase_Tools::zeroMargins(aLayout);
69   aLayout->addWidget(myLabel);
70
71   myShowConstraints = new QCheckBox(tr("Show constraints"), this);
72   aLayout->addWidget(myShowConstraints);
73
74   setLayout(aLayout);
75   connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
76   myShowConstraints->setChecked(toShowConstraints);
77 }
78
79 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
80 {
81   erasePreviewPlanes();
82 }
83
84 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues)
85 {
86   // do not use the given selection if the plane of the sketch has been already set.
87   // If this check is absent, a selected plane in the viewer can be set in the sketch
88   // even if the sketch is built on another plane.
89   if (plane().get())
90     return true;
91
92   return ModuleBase_WidgetValidated::setSelection(theValues);
93 }
94
95 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
96 {
97   QList<QWidget*> aResult;
98   aResult << myLabel;
99   return aResult;
100 }
101
102 void PartSet_WidgetSketchLabel::onSelectionChanged()
103 {
104   QList<ModuleBase_ViewerPrs> aSelectedPrs = myWorkshop->selector()->selection()->getSelected(
105                                                            ModuleBase_ISelection::AllControls);
106   if (aSelectedPrs.empty())
107     return;
108   ModuleBase_ViewerPrs aPrs = aSelectedPrs.first();
109   if (aPrs.isEmpty() || !isValidSelection(aPrs))
110     return;
111
112   // 2. set the selection to sketch
113   setSelectionCustom(aPrs);
114   // 3. hide main planes if they have been displayed
115   erasePreviewPlanes();
116   // 4. if the planes were displayed, change the view projection
117   TopoDS_Shape aShape = aPrs.shape();
118   std::shared_ptr<GeomAPI_Shape> aGShape;
119   // selection happens in OCC viewer
120   if (!aShape.IsNull()) {
121     aGShape =  std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape);
122     aGShape->setImpl(new TopoDS_Shape(aShape));
123   }
124   else { // selection happens in OCC viewer(on body) of in the OB browser
125     DataPtr aData = feature()->data();
126     AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
127                               (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
128     if (aSelAttr) {
129       aGShape = aSelAttr->value();
130     }
131   }
132   if (aGShape.get() != NULL) {
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   // 5. Clear text in the label
140   myLabel->setText("");
141   myLabel->setToolTip("");
142   disconnect(myWorkshop->selector(), SIGNAL(selectionChanged()), 
143               this, SLOT(onSelectionChanged()));
144   // 6. deactivate face selection filter
145   activateFilters(myWorkshop->module()->workshop(), false);
146
147   // 7. Clear selection mode and define sketching mode
148   //XGUI_Displayer* aDisp = myWorkshop->displayer();
149   //aDisp->closeLocalContexts();
150   emit planeSelected(plane());
151   // after the plane is selected in the sketch, the sketch selection should be activated
152   // it can not be performed in the sketch label widget because, we don't need to switch off
153   // the selection by any label deactivation, but need to switch it off by stop the sketch
154   activateSelection(true);
155
156   // 8. Update sketcher actions
157   XGUI_ActionsMgr* anActMgr = myWorkshop->actionsMgr();
158   anActMgr->update();
159   myWorkshop->viewer()->update();
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()) {  // selection happens in the OCC viewer
214           aShapePtr = ModelAPI_Tools::shape(aRes);
215         }
216         else { // selection happens in OB browser
217           aShapePtr->setImpl(new TopoDS_Shape(aShape));
218         }
219         if (aShapePtr.get() != NULL) {
220           aSelAttr->setValue(aRes, aShapePtr);
221           isOwnerSet = true;
222         }
223       }
224     }
225   }
226   else if (!aShape.IsNull()) {
227     aDir = setSketchPlane(aShape);
228     isOwnerSet = aDir.get();
229   }
230   return isOwnerSet;
231 }
232
233 void PartSet_WidgetSketchLabel::activateCustom()
234 {
235   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
236   if (aPlane.get()) {
237     activateSelection(true);
238     return;
239   }
240
241   bool aBodyIsVisualized = false;
242   XGUI_Displayer* aDisp = myWorkshop->displayer();
243   QObjectPtrList aDisplayed = aDisp->displayedObjects();
244   foreach (ObjectPtr anObj, aDisplayed) {
245     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
246     if (aResult.get() != NULL) {
247       aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
248       if (aBodyIsVisualized)
249         break;
250     }
251   }
252
253   if (!aBodyIsVisualized) {
254     // We have to select a plane before any operation
255     showPreviewPlanes();
256   }
257   activateSelection(true);
258
259   myLabel->setText(myText);
260   myLabel->setToolTip(myTooltip);
261
262   connect(myWorkshop->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
263   activateFilters(myWorkshop->module()->workshop(), true);
264
265   aDisp->updateViewer();
266 }
267
268 void PartSet_WidgetSketchLabel::deactivate()
269 {
270   erasePreviewPlanes();
271   activateSelection(false);
272
273   activateFilters(myWorkshop->module()->workshop(), false);
274 }
275
276 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
277 {
278   if (toActivate) {
279     QIntList aModes;
280     std::shared_ptr<GeomAPI_Pln> aPlane = plane();
281     if (aPlane.get()) {
282       myWorkshop->moduleConnector()->module()->activeSelectionModes(aModes);
283     }
284     else {
285       aModes << TopAbs_FACE;
286     }
287     myWorkshop->moduleConnector()->activateSubShapesSelection(aModes);
288   } else {
289     myWorkshop->moduleConnector()->deactivateSubShapesSelection();
290   }
291 }
292
293 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
294 {
295   if (myPreviewDisplayed) {
296     XGUI_Displayer* aDisp = myWorkshop->displayer();
297     aDisp->eraseAIS(myYZPlane, false);
298     aDisp->eraseAIS(myXZPlane, false);
299     aDisp->eraseAIS(myXYPlane, false);
300     myPreviewDisplayed = false;
301   }
302 }
303
304 void PartSet_WidgetSketchLabel::showPreviewPlanes()
305 {
306   if (myPreviewDisplayed)
307     return;
308
309   if (!myYZPlane) { // If planes are not created
310     // Create Preview
311     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
312     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
313     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, 1, 0));
314     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
315
316     std::vector<int> aYZRGB, aXZRGB, aXYRGB;
317     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
318                                                         YZ_PLANE_COLOR);
319     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
320                                                         XZ_PLANE_COLOR);
321     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
322                                                         XY_PLANE_COLOR);
323     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
324     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
325     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
326
327     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
328     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
329     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
330   }
331   XGUI_Displayer* aDisp = myWorkshop->displayer();
332   aDisp->displayAIS(myYZPlane, false);
333   aDisp->displayAIS(myXZPlane, false);
334   aDisp->displayAIS(myXYPlane, false);
335   myPreviewDisplayed = true;
336 }
337
338
339 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
340                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
341                                                            const int theRGB[3])
342 {
343   double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
344   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
345   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
346   aAIS->createShape(aFace);
347   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
348   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
349   return aAIS;
350 }
351
352
353 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
354 {
355   if (theShape.IsNull())
356     return std::shared_ptr<GeomAPI_Dir>();
357
358   // get selected shape
359   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
360   aGShape->setImpl(new TopoDS_Shape(theShape));
361
362
363
364   // get plane parameters
365   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
366
367   // set plane parameters to feature
368   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
369   double anA, aB, aC, aD;
370   aPlane->coefficients(anA, aB, aC, aD);
371
372   // calculate attributes of the sketch
373   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
374   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
375   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
376   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
377   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
378   // X axis is preferable to be dirX on the sketch
379   const double tol = Precision::Confusion();
380   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
381   std::shared_ptr<GeomAPI_Dir> aTempDir(
382       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
383   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
384   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
385
386   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
387       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
388   anOrigin->setValue(anOrigPnt);
389   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
390       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
391   aNormal->setValue(aNormDir);
392   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
393       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
394   aDirX->setValue(aXDir);
395   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
396   return aDir;
397 }
398
399 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
400 {
401   myShowConstraints->setChecked(theOn);
402   emit showConstraintToggled(theOn);
403 }