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