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