Salome HOME
Debug information for setFocus/activateWindow methods.
[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 #include "PartSet_Module.h"
10
11 #include "SketchPlugin_SketchEntity.h"
12
13 #include <XGUI_Workshop.h>
14 #include <XGUI_Displayer.h>
15 #include <XGUI_SelectionMgr.h>
16 #include <XGUI_Selection.h>
17 #include <XGUI_ViewerProxy.h>
18 #include <XGUI_ActionsMgr.h>
19 #include <XGUI_ModuleConnector.h>
20
21 #include <ModuleBase_Operation.h>
22 #include <ModuleBase_ViewerPrs.h>
23 #include <ModuleBase_Tools.h>
24 #include <ModuleBase_IModule.h>
25
26 #include <ModelAPI_ResultBody.h>
27 #include <ModelAPI_Tools.h>
28
29 #include <GeomAlgoAPI_FaceBuilder.h>
30 #include <GeomAlgoAPI_ShapeTools.h>
31 #include <GeomDataAPI_Point.h>
32 #include <GeomDataAPI_Dir.h>
33 #include <GeomAPI_XYZ.h>
34
35 #include <SketchPlugin_Sketch.h>
36 #include <SketcherPrs_Tools.h>
37
38 #include <Precision.hxx>
39 #include <gp_Pln.hxx>
40 #include <gp_Pnt.hxx>
41 #include <gp_Dir.hxx>
42 #include <AIS_Shape.hxx>
43 #include <AIS_DimensionSelectionMode.hxx>
44
45 #include <Config_WidgetAPI.h>
46 #include <Config_PropManager.h>
47
48 #include <QLabel>
49 #include <QApplication>
50 #include <QVBoxLayout>
51 #include <QCheckBox>
52 #include <QGroupBox>
53 #include <QPushButton>
54 #include <QStackedWidget>
55
56
57 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
58                                                      ModuleBase_IWorkshop* theWorkshop,
59                                                      const Config_WidgetAPI* theData,
60                                                      const std::string& theParentId,
61                                                      bool toShowConstraints)
62 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData, theParentId),
63   myPreviewDisplayed(false)
64 {
65   QVBoxLayout* aLayout = new QVBoxLayout(this);
66   ModuleBase_Tools::zeroMargins(aLayout);
67
68   myStackWidget = new QStackedWidget(this);
69   myStackWidget->setContentsMargins(0,0,0,0);
70   aLayout->addWidget(myStackWidget);
71
72   // Define label for plane selection
73   QWidget* aFirstWgt = new QWidget(this);
74
75   QString aText = QString::fromStdString(theData->getProperty("title"));
76   QLabel* aLabel = new QLabel(aText, aFirstWgt);
77   aLabel->setWordWrap(true);
78   QString aTooltip = QString::fromStdString(theData->getProperty("tooltip"));
79   aLabel->setToolTip(aTooltip);
80   aLabel->setIndent(5);
81
82   aLayout = new QVBoxLayout(aFirstWgt);
83   ModuleBase_Tools::zeroMargins(aLayout);
84   aLayout->addWidget(aLabel);
85
86   myStackWidget->addWidget(aFirstWgt);
87
88   // Define widget for sketch manmagement
89   QWidget* aSecondWgt = new QWidget(this);
90   aLayout = new QVBoxLayout(aSecondWgt);
91   ModuleBase_Tools::zeroMargins(aLayout);
92
93   QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
94   QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
95
96   myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
97   aViewLayout->addWidget(myViewInverted);
98
99   QPushButton* aSetViewBtn = new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
100   connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
101   aViewLayout->addWidget(aSetViewBtn);
102
103   aLayout->addWidget(aViewBox);
104
105   myShowConstraints = new QCheckBox(tr("Show constraints"), this);
106   connect(myShowConstraints, SIGNAL(toggled(bool)), this, SIGNAL(showConstraintToggled(bool)));
107   myShowConstraints->setChecked(toShowConstraints);
108   aLayout->addWidget(myShowConstraints);
109
110   myStackWidget->addWidget(aSecondWgt);
111   //setLayout(aLayout);
112 }
113
114 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
115 {
116 }
117
118 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
119                                              const bool theToValidate)
120 {
121   // do not use the given selection if the plane of the sketch has been already set.
122   // If this check is absent, a selected plane in the viewer can be set in the sketch
123   // even if the sketch is built on another plane.
124   if (plane().get())
125     return true;
126
127   ModuleBase_ViewerPrs aPrs = theValues.first();
128   bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
129   if (aDone)
130     updateByPlaneSelected(aPrs);
131
132   return aDone;
133 }
134
135 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
136 {
137   QList<QWidget*> aResult;
138   aResult << myStackWidget;
139   return aResult;
140 }
141
142 void PartSet_WidgetSketchLabel::onSelectionChanged()
143 {
144   QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
145
146   if (aSelected.empty())
147     return;
148   ModuleBase_ViewerPrs aPrs = aSelected.first();
149
150   bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
151   if (aDone) {
152     updateByPlaneSelected(aPrs);
153     updateObject(myFeature);
154   }
155 }
156
157 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
158 {
159   // 1. hide main planes if they have been displayed
160   erasePreviewPlanes();
161   // 2. if the planes were displayed, change the view projection
162   TopoDS_Shape aShape = thePrs.shape();
163   std::shared_ptr<GeomAPI_Shape> aGShape;
164   std::shared_ptr<GeomAPI_Shape> aBaseShape;
165
166   DataPtr aData = feature()->data();
167   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
168                             (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
169
170   // selection happens in OCC viewer
171   if (!aShape.IsNull()) {
172     aGShape =  std::shared_ptr<GeomAPI_Shape>(new GeomAPI_Shape());
173     aGShape->setImpl(new TopoDS_Shape(aShape));
174
175     if (aSelAttr && aSelAttr->context()) {
176       aBaseShape = aSelAttr->context()->shape();
177     }
178   }
179   else { // selection happens in OCC viewer(on body) of in the OB browser
180     if (aSelAttr) {
181       aGShape = aSelAttr->value();
182     }
183   }
184   if (aGShape.get() != NULL) {
185     // get plane parameters
186     std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
187     std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
188     gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
189     double aTwist = 0.0;
190
191     // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
192     /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
193       aXYZ.Reverse();
194     }*/
195
196     // Rotate view if the sketcher plane is selected only from preview planes
197     // Preview planes are created only if there is no any shape
198     bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
199     if (aRotate) {
200       myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
201       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
202       if (aModule)
203         aModule->onViewTransformed();
204     }
205   }
206   // 3. Clear text in the label
207   myStackWidget->setCurrentIndex(1);
208   //myLabel->setText("");
209   //myLabel->setToolTip("");
210   disconnect(workshop()->selector(), SIGNAL(selectionChanged()), 
211               this, SLOT(onSelectionChanged()));
212   // 4. deactivate face selection filter
213   activateFilters(false);
214
215   // 5. Clear selection mode and define sketching mode
216   //XGUI_Displayer* aDisp = workshop()->displayer();
217   //aDisp->closeLocalContexts();
218   emit planeSelected(plane());
219   // after the plane is selected in the sketch, the sketch selection should be activated
220   // it can not be performed in the sketch label widget because, we don't need to switch off
221   // the selection by any label deactivation, but need to switch it off by stop the sketch
222   activateSelection(true);
223
224   // 6. Update sketcher actions
225   XGUI_ActionsMgr* anActMgr = workshop()->actionsMgr();
226   myWorkshop->updateCommandStatus();
227   myWorkshop->viewer()->update();
228 }
229
230 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
231 {
232   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
233   return PartSet_Tools::sketchPlane(aSketch);
234 }
235
236 bool PartSet_WidgetSketchLabel::focusTo()
237 {
238   ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
239   return true;
240 }
241
242 void PartSet_WidgetSketchLabel::enableFocusProcessing()
243 {
244   myStackWidget->installEventFilter(this);
245 }
246
247 void PartSet_WidgetSketchLabel::storeAttributeValue()
248 {
249   ModuleBase_WidgetValidated::storeAttributeValue();
250 }
251
252 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
253 {
254   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
255
256   // it is not necessary to save the previous plane value because the plane is chosen once
257   DataPtr aData = feature()->data();
258   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
259     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
260   if (aSelAttr) {
261     ResultPtr anEmptyResult;
262     GeomShapePtr anEmptyShape;
263     aSelAttr->setValue(anEmptyResult, anEmptyShape);
264   }
265 }
266
267 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
268 {
269   bool isOwnerSet = false;
270
271   const TopoDS_Shape& aShape = thePrs.shape();
272   std::shared_ptr<GeomAPI_Dir> aDir;
273
274   if (thePrs.object() && (feature() != thePrs.object())) {
275     DataPtr aData = feature()->data();
276     AttributeSelectionPtr aSelAttr = 
277       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
278       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
279     if (aSelAttr) {
280       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
281       if (aRes) {
282         GeomShapePtr aShapePtr(new GeomAPI_Shape());
283         if (aShape.IsNull()) {  // selection happens in the OCC viewer
284           aShapePtr = ModelAPI_Tools::shape(aRes);
285         }
286         else { // selection happens in OB browser
287           aShapePtr->setImpl(new TopoDS_Shape(aShape));
288         }
289         if (aShapePtr.get() != NULL) {
290           aSelAttr->setValue(aRes, aShapePtr);
291           isOwnerSet = true;
292         }
293       }
294     }
295   }
296   else if (!aShape.IsNull()) {
297     aDir = setSketchPlane(aShape);
298     isOwnerSet = aDir.get();
299   }
300   return isOwnerSet;
301 }
302
303 void PartSet_WidgetSketchLabel::activateCustom()
304 {
305   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
306   if (aPlane.get()) {
307     myStackWidget->setCurrentIndex(1);
308     activateSelection(true);
309     return;
310   }
311
312   myStackWidget->setCurrentIndex(0);
313   bool aBodyIsVisualized = false;
314   XGUI_Displayer* aDisp = workshop()->displayer();
315   QObjectPtrList aDisplayed = aDisp->displayedObjects();
316   foreach (ObjectPtr anObj, aDisplayed) {
317     ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObj);
318     if (aResult.get() != NULL) {
319       aBodyIsVisualized = aResult->groupName() == ModelAPI_ResultBody::group();
320       if (aBodyIsVisualized)
321         break;
322     }
323   }
324
325   if (!aBodyIsVisualized) {
326     // We have to select a plane before any operation
327     showPreviewPlanes();
328   }
329   activateSelection(true);
330
331   //myLabel->setText(myText);
332   //myLabel->setToolTip(myTooltip);
333
334   connect(workshop()->selector(), SIGNAL(selectionChanged()), this, SLOT(onSelectionChanged()));
335   activateFilters(true);
336 }
337
338 void PartSet_WidgetSketchLabel::deactivate()
339 {
340   ModuleBase_ModelWidget::deactivate();
341   erasePreviewPlanes();
342   activateSelection(false);
343
344   activateFilters(false);
345 }
346
347 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
348 {
349   if (toActivate) {
350     QIntList aModes;
351     std::shared_ptr<GeomAPI_Pln> aPlane = plane();
352     if (aPlane.get()) {
353       myWorkshop->module()->activeSelectionModes(aModes);
354     }
355     else {
356       aModes << TopAbs_FACE;
357     }
358     myWorkshop->activateSubShapesSelection(aModes);
359   } else {
360     myWorkshop->deactivateSubShapesSelection();
361   }
362 }
363
364 void PartSet_WidgetSketchLabel::erasePreviewPlanes()
365 {
366   if (myPreviewDisplayed) {
367     XGUI_Displayer* aDisp = workshop()->displayer();
368     aDisp->eraseAIS(myYZPlane, false);
369     aDisp->eraseAIS(myXZPlane, false);
370     aDisp->eraseAIS(myXYPlane, false);
371     myPreviewDisplayed = false;
372   }
373 }
374
375 void PartSet_WidgetSketchLabel::showPreviewPlanes()
376 {
377   if (myPreviewDisplayed)
378     return;
379
380   if (!myYZPlane) { // If planes are not created
381     // Create Preview
382     std::shared_ptr<GeomAPI_Pnt> anOrigin(new GeomAPI_Pnt(0, 0, 0));
383     std::shared_ptr<GeomAPI_Dir> aYZDir(new GeomAPI_Dir(1, 0, 0));
384     // -1, not 1 for correct internal sketch coords (issue 898)
385     std::shared_ptr<GeomAPI_Dir> aXZDir(new GeomAPI_Dir(0, -1, 0));
386     std::shared_ptr<GeomAPI_Dir> aXYDir(new GeomAPI_Dir(0, 0, 1));
387
388     std::vector<int> aYZRGB, aXZRGB, aXYRGB;
389     aYZRGB = Config_PropManager::color("Visualization", "yz_plane_color",
390                                                         YZ_PLANE_COLOR);
391     aXZRGB = Config_PropManager::color("Visualization", "xz_plane_color",
392                                                         XZ_PLANE_COLOR);
393     aXYRGB = Config_PropManager::color("Visualization", "xy_plane_color",
394                                                         XY_PLANE_COLOR);
395     int aR[] = {aYZRGB[0], aYZRGB[1], aYZRGB[2]};
396     int aG[] = {aXZRGB[0], aXZRGB[1], aXZRGB[2]};
397     int aB[] = {aXYRGB[0], aXYRGB[1], aXYRGB[2]};
398
399     myYZPlane = createPreviewPlane(anOrigin, aYZDir, aR);
400     myXZPlane = createPreviewPlane(anOrigin, aXZDir, aG);
401     myXYPlane = createPreviewPlane(anOrigin, aXYDir, aB);
402   }
403   XGUI_Displayer* aDisp = workshop()->displayer();
404   aDisp->displayAIS(myYZPlane, true, false);
405   aDisp->displayAIS(myXZPlane, true, false);
406   aDisp->displayAIS(myXYPlane, true, false);
407   myPreviewDisplayed = true;
408 }
409
410
411 AISObjectPtr PartSet_WidgetSketchLabel::createPreviewPlane(std::shared_ptr<GeomAPI_Pnt> theOrigin, 
412                                                            std::shared_ptr<GeomAPI_Dir> theNorm, 
413                                                            const int theRGB[3])
414 {
415   double aSize = Config_PropManager::integer("Sketch planes", "planes_size", PLANE_SIZE);
416   std::shared_ptr<GeomAPI_Shape> aFace = GeomAlgoAPI_FaceBuilder::square(theOrigin, theNorm, aSize);
417   AISObjectPtr aAIS = AISObjectPtr(new GeomAPI_AISObject());
418   aAIS->createShape(aFace);
419   aAIS->setWidth(Config_PropManager::integer("Sketch planes", "planes_thickness", SKETCH_WIDTH));
420   aAIS->setColor(theRGB[0], theRGB[1], theRGB[2]);
421   return aAIS;
422 }
423
424
425 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const TopoDS_Shape& theShape)
426 {
427   if (theShape.IsNull())
428     return std::shared_ptr<GeomAPI_Dir>();
429
430   // get selected shape
431   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
432   aGShape->setImpl(new TopoDS_Shape(theShape));
433
434
435
436   // get plane parameters
437   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
438   if (!aPlane.get())
439     return std::shared_ptr<GeomAPI_Dir>();
440
441   // set plane parameters to feature
442   std::shared_ptr<ModelAPI_Data> aData = feature()->data();
443   double anA, aB, aC, aD;
444   aPlane->coefficients(anA, aB, aC, aD);
445
446   // calculate attributes of the sketch
447   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
448   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
449   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
450   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
451   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
452   // X axis is preferable to be dirX on the sketch
453   const double tol = Precision::Confusion();
454   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
455   std::shared_ptr<GeomAPI_Dir> aTempDir(
456       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
457   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
458   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
459
460   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
461       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
462   anOrigin->setValue(anOrigPnt);
463   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
464       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
465   aNormal->setValue(aNormDir);
466   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
467       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
468   aDirX->setValue(aXDir);
469   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
470   return aDir;
471 }
472
473 void PartSet_WidgetSketchLabel::showConstraints(bool theOn)
474 {
475   myShowConstraints->setChecked(theOn);
476   emit showConstraintToggled(theOn);
477 }
478
479 XGUI_Workshop* PartSet_WidgetSketchLabel::workshop() const
480 {
481   XGUI_ModuleConnector* aConnector = dynamic_cast<XGUI_ModuleConnector*>(myWorkshop);
482   return aConnector->workshop();
483 }
484
485
486 void PartSet_WidgetSketchLabel::onSetPlaneView()
487 {
488   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
489   if (aPlane.get()) {
490     std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
491     gp_Dir aDir = aDirection->impl<gp_Dir>();
492     if (myViewInverted->isChecked())
493       aDir.Reverse();
494     myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
495     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
496     if (aModule)
497       aModule->onViewTransformed();
498   }
499 }