]> SALOME platform Git repositories - modules/shaper.git/blob - src/PartSet/PartSet_WidgetSketchLabel.cpp
Salome HOME
Issue #1343 Improvement of Extrusion and Revolution operations: preview planes visual...
[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
37 #include <SketchPlugin_Sketch.h>
38 #include <SketcherPrs_Tools.h>
39
40 #include <Precision.hxx>
41 #include <gp_Pln.hxx>
42 #include <gp_Pnt.hxx>
43 #include <gp_Dir.hxx>
44 #include <AIS_Shape.hxx>
45 #include <AIS_DimensionSelectionMode.hxx>
46
47 #include <Config_WidgetAPI.h>
48 #include <Config_PropManager.h>
49
50 #include <QLabel>
51 #include <QApplication>
52 #include <QVBoxLayout>
53 #include <QCheckBox>
54 #include <QGroupBox>
55 #include <QPushButton>
56 #include <QStackedWidget>
57
58
59 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
60                         ModuleBase_IWorkshop* theWorkshop,
61                         const Config_WidgetAPI* theData,
62                         const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
63 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
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   QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
106   aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
107   aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
108   aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
109
110   QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
111                                                         aLast = aStates.end();
112   for (; anIt != aLast; anIt++) {
113     QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
114     connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
115     aLayout->addWidget(aShowConstraints);
116
117     PartSet_Tools::ConstraintVisibleState aState = anIt.key();
118     myShowConstraints[aState] = aShowConstraints;
119
120     if (toShowConstraints.contains(aState))
121       aShowConstraints->setChecked(toShowConstraints[aState]);
122   }
123
124   myStackWidget->addWidget(aSecondWgt);
125   //setLayout(aLayout);
126
127   myPreviewPlanes = new PartSet_PreviewPlanes();
128 }
129
130 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
131 {
132 }
133
134 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
135                                              const bool theToValidate)
136 {
137   // do not use the given selection if the plane of the sketch has been already set.
138   // If this check is absent, a selected plane in the viewer can be set in the sketch
139   // even if the sketch is built on another plane.
140   if (plane().get())
141     return true;
142
143   ModuleBase_ViewerPrs aPrs = theValues.first();
144   bool aDone = ModuleBase_WidgetValidated::setSelection(theValues, theToValidate);
145   if (aDone)
146     updateByPlaneSelected(aPrs);
147
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_ViewerPrs> aSelected = getFilteredSelected();
161
162   if (aSelected.empty())
163     return;
164   ModuleBase_ViewerPrs aPrs = aSelected.first();
165
166   bool aDone = ModuleBase_WidgetValidated::setSelection(aSelected, false);
167   if (aDone) {
168     updateByPlaneSelected(aPrs);
169     updateObject(myFeature);
170   }
171 }
172
173 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
174 {
175   QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
176
177   QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
178                           anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
179
180   PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
181   bool aFound = false;
182   for (; anIt != aLast && !aFound; anIt++) {
183     aFound = anIt.value() == aSenderCheckBox;
184     if (aFound)
185       aState = anIt.key();
186   }
187   if (aFound)
188     emit showConstraintToggled(aState, theOn);
189 }
190
191 void PartSet_WidgetSketchLabel::blockAttribute(const bool& theToBlock, bool& isFlushesActived,
192                                                bool& isAttributeSetInitializedBlocked)
193 {
194   ModuleBase_WidgetValidated::blockAttribute(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 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
213 {
214   // 1. hide main planes if they have been displayed
215   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
216   // 2. if the planes were displayed, change the view projection
217   const GeomShapePtr& aShape = thePrs.shape();
218   std::shared_ptr<GeomAPI_Shape> aGShape;
219   std::shared_ptr<GeomAPI_Shape> aBaseShape;
220
221   DataPtr aData = feature()->data();
222   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
223                             (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
224
225   // selection happens in OCC viewer
226   if (aShape.get() && !aShape->isNull()) {
227     aGShape = aShape;
228
229     if (aSelAttr && aSelAttr->context()) {
230       aBaseShape = aSelAttr->context()->shape();
231     }
232   }
233   else { // selection happens in OCC viewer(on body) of in the OB browser
234     if (aSelAttr) {
235       aGShape = aSelAttr->value();
236     }
237   }
238   if (aGShape.get() != NULL) {
239     // get plane parameters
240     std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
241     std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
242     gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
243     double aTwist = 0.0;
244
245     // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
246     /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
247       aXYZ.Reverse();
248     }*/
249
250     // Rotate view if the sketcher plane is selected only from preview planes
251     // Preview planes are created only if there is no any shape
252     bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
253     if (aRotate) {
254       myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
255       PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
256       if (aModule)
257         aModule->onViewTransformed();
258     }
259   }
260   // 3. Clear text in the label
261   myStackWidget->setCurrentIndex(1);
262   //myLabel->setText("");
263   //myLabel->setToolTip("");
264   XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
265   disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()), 
266               this, SLOT(onSelectionChanged()));
267   // 4. deactivate face selection filter
268   activateFilters(false);
269
270   // 5. Clear selection mode and define sketching mode
271   //XGUI_Displayer* aDisp = aWorkshop->displayer();
272   //aDisp->closeLocalContexts();
273   emit planeSelected(plane());
274   // after the plane is selected in the sketch, the sketch selection should be activated
275   // it can not be performed in the sketch label widget because, we don't need to switch off
276   // the selection by any label deactivation, but need to switch it off by stop the sketch
277   activateSelection(true);
278
279   // 6. Update sketcher actions
280   XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
281   myWorkshop->updateCommandStatus();
282   myWorkshop->viewer()->update();
283 }
284
285 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
286 {
287   CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
288   return PartSet_Tools::sketchPlane(aSketch);
289 }
290
291 bool PartSet_WidgetSketchLabel::focusTo()
292 {
293   ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
294   return true;
295 }
296
297 void PartSet_WidgetSketchLabel::enableFocusProcessing()
298 {
299   myStackWidget->installEventFilter(this);
300 }
301
302 void PartSet_WidgetSketchLabel::storeAttributeValue()
303 {
304   ModuleBase_WidgetValidated::storeAttributeValue();
305 }
306
307 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
308 {
309   ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
310
311   // it is not necessary to save the previous plane value because the plane is chosen once
312   DataPtr aData = feature()->data();
313   AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
314     (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
315   if (aSelAttr) {
316     ResultPtr anEmptyResult;
317     GeomShapePtr anEmptyShape;
318     aSelAttr->setValue(anEmptyResult, anEmptyShape);
319   }
320 }
321
322 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
323 {
324   return fillSketchPlaneBySelection(feature(), thePrs);
325 }
326
327 #include <GeomAPI_Face.h>
328 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrs& thePrs)
329 {
330   bool aCanFillSketch = true;
331   // avoid any selection on sketch object
332   ObjectPtr anObject = thePrs.object();
333   ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
334   if (aResult.get()) {
335     FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
336     if (aFeature->getKind() == SketchPlugin_Sketch::ID())
337       aCanFillSketch = false;
338   }
339   // check plane or planar face of any non-sketch object
340   if (aCanFillSketch) {
341     std::shared_ptr<GeomAPI_Face> aGeomFace;
342     const TopoDS_Shape aShape = thePrs.shape();
343     if (aShape.IsNull()) {
344       GeomShapePtr aGeomShape = aResult->shape();
345       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
346       aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
347     }
348     else if (aShape.ShapeType() == TopAbs_FACE) {
349       std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face());
350       aGeomFace->setImpl(new TopoDS_Shape(aShape));
351       aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
352     }
353     else
354       aCanFillSketch = false;
355   }
356   return aCanFillSketch;
357 }
358
359 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
360                                                            const ModuleBase_ViewerPrs& thePrs)
361 {
362   bool isOwnerSet = false;
363
364   const GeomShapePtr& aShape = thePrs.shape();
365   std::shared_ptr<GeomAPI_Dir> aDir;
366
367   if (thePrs.object() && (theFeature != thePrs.object())) {
368     DataPtr aData = theFeature->data();
369     AttributeSelectionPtr aSelAttr = 
370       std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
371       (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
372     if (aSelAttr) {
373       ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
374       if (aRes) {
375         GeomShapePtr aShapePtr(new GeomAPI_Shape());
376         if (!aShape.get() || aShape->isNull()) {  // selection happens in the OCC viewer
377           aShapePtr = ModelAPI_Tools::shape(aRes);
378         }
379         else { // selection happens in OB browser
380           aShapePtr = aShape;
381         }
382         if (aShapePtr.get() != NULL) {
383           aSelAttr->setValue(aRes, aShapePtr);
384           isOwnerSet = true;
385         }
386       }
387     }
388   }
389   else if (aShape.get() && !aShape->isNull()) {
390     const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
391     aDir = setSketchPlane(aTDShape);
392     isOwnerSet = aDir.get();
393   }
394   return isOwnerSet;
395 }
396
397 void PartSet_WidgetSketchLabel::activateCustom()
398 {
399   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
400   if (aPlane.get()) {
401     myStackWidget->setCurrentIndex(1);
402     activateSelection(true);
403     return;
404   }
405
406   myStackWidget->setCurrentIndex(0);
407   bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
408   if (!aBodyIsVisualized) {
409     // We have to select a plane before any operation
410     myPreviewPlanes->showPreviewPlanes(myWorkshop);
411   }
412   activateSelection(true);
413
414   //myLabel->setText(myText);
415   //myLabel->setToolTip(myTooltip);
416
417   connect(XGUI_Tools::workshop(myWorkshop)->selector(), SIGNAL(selectionChanged()),
418           this, SLOT(onSelectionChanged()));
419   activateFilters(true);
420 }
421
422 void PartSet_WidgetSketchLabel::deactivate()
423 {
424   ModuleBase_ModelWidget::deactivate();
425   bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
426   myPreviewPlanes->erasePreviewPlanes(myWorkshop);
427   activateSelection(false);
428
429   activateFilters(false);
430   if (aHidePreview)
431     myWorkshop->viewer()->update();
432 }
433
434 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
435 {
436   if (toActivate) {
437     QIntList aModes;
438     std::shared_ptr<GeomAPI_Pln> aPlane = plane();
439     if (aPlane.get()) {
440       myWorkshop->module()->activeSelectionModes(aModes);
441     }
442     else {
443       aModes << TopAbs_FACE;
444     }
445     myWorkshop->activateSubShapesSelection(aModes);
446   } else {
447     myWorkshop->deactivateSubShapesSelection();
448   }
449 }
450
451
452 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
453                                                                        const TopoDS_Shape& theShape)
454 {
455   if (theShape.IsNull())
456     return std::shared_ptr<GeomAPI_Dir>();
457
458   // get selected shape
459   std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
460   aGShape->setImpl(new TopoDS_Shape(theShape));
461
462
463
464   // get plane parameters
465   std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
466   if (!aPlane.get())
467     return std::shared_ptr<GeomAPI_Dir>();
468
469   // set plane parameters to feature
470   std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
471   double anA, aB, aC, aD;
472   aPlane->coefficients(anA, aB, aC, aD);
473
474   // calculate attributes of the sketch
475   std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
476   std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
477   std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
478   aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
479   std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
480   // X axis is preferable to be dirX on the sketch
481   const double tol = Precision::Confusion();
482   bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
483   std::shared_ptr<GeomAPI_Dir> aTempDir(
484       isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
485   std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
486   std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
487
488   std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
489       aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
490   anOrigin->setValue(anOrigPnt);
491   std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
492       aData->attribute(SketchPlugin_Sketch::NORM_ID()));
493   aNormal->setValue(aNormDir);
494   std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
495       aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
496   aDirX->setValue(aXDir);
497   std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
498   return aDir;
499 }
500
501 void PartSet_WidgetSketchLabel::onSetPlaneView()
502 {
503   std::shared_ptr<GeomAPI_Pln> aPlane = plane();
504   if (aPlane.get()) {
505     std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
506     gp_Dir aDir = aDirection->impl<gp_Dir>();
507     if (myViewInverted->isChecked())
508       aDir.Reverse();
509     myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
510     PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
511     if (aModule)
512       aModule->onViewTransformed();
513   }
514 }