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