1 // Copyright (C) 2014-20xx CEA/DEN, EDF R&D
3 // File: PartSet_WidgetSketchLabel.cpp
4 // Created: 07 July 2014
5 // Author: Vitaly SMETANNIKOV
7 #include "PartSet_WidgetSketchLabel.h"
8 #include "PartSet_Tools.h"
9 #include "PartSet_Module.h"
10 #include "PartSet_PreviewPlanes.h"
12 #include "SketchPlugin_SketchEntity.h"
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>
23 #include <ModuleBase_Operation.h>
24 #include <ModuleBase_ViewerPrs.h>
25 #include <ModuleBase_Tools.h>
26 #include <ModuleBase_IModule.h>
28 #include <ModelAPI_ResultBody.h>
29 #include <ModelAPI_Tools.h>
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>
38 #include <SketchPlugin_Sketch.h>
39 #include <SketcherPrs_Tools.h>
41 #include <Precision.hxx>
45 #include <AIS_Shape.hxx>
46 #include <AIS_DimensionSelectionMode.hxx>
47 #include <Bnd_Box.hxx>
49 #include <Config_WidgetAPI.h>
50 #include <Config_PropManager.h>
53 #include <QApplication>
54 #include <QVBoxLayout>
55 #include <QHBoxLayout>
58 #include <QPushButton>
59 #include <QStackedWidget>
61 #include <QDoubleValidator>
64 #define DBL_MAX 1.7976931348623158e+308
67 PartSet_WidgetSketchLabel::PartSet_WidgetSketchLabel(QWidget* theParent,
68 ModuleBase_IWorkshop* theWorkshop,
69 const Config_WidgetAPI* theData,
70 const QMap<PartSet_Tools::ConstraintVisibleState, bool>& toShowConstraints)
71 : ModuleBase_WidgetValidated(theParent, theWorkshop, theData)
73 QVBoxLayout* aLayout = new QVBoxLayout(this);
74 ModuleBase_Tools::zeroMargins(aLayout);
76 myStackWidget = new QStackedWidget(this);
77 myStackWidget->setContentsMargins(0,0,0,0);
78 aLayout->addWidget(myStackWidget);
80 // Define label for plane selection
81 QWidget* aFirstWgt = new QWidget(this);
83 // Size of the View control
84 mySizeOfViewWidget = new QWidget(aFirstWgt);
85 QHBoxLayout* aSizeLayout = new QHBoxLayout(mySizeOfViewWidget);
86 aSizeLayout->addWidget(new QLabel("Size of the view", mySizeOfViewWidget));
87 mySizeOfView = new QLineEdit(mySizeOfViewWidget);
89 QDoubleValidator* aValidator = new QDoubleValidator(0, DBL_MAX, 12, mySizeOfView);
90 aValidator->setLocale(ModuleBase_Tools::doubleLocale());
91 aValidator->setNotation(QDoubleValidator::StandardNotation);
92 mySizeOfView->setValidator(aValidator);
93 aSizeLayout->addWidget(mySizeOfView);
95 QString aText = QString::fromStdString(theData->getProperty("title"));
96 QLabel* aLabel = new QLabel(aText, aFirstWgt);
97 aLabel->setWordWrap(true);
98 QString aTooltip = QString::fromStdString(theData->getProperty("tooltip"));
99 aLabel->setToolTip(aTooltip);
100 aLabel->setIndent(5);
102 aLayout = new QVBoxLayout(aFirstWgt);
103 ModuleBase_Tools::zeroMargins(aLayout);
104 aLayout->addWidget(mySizeOfViewWidget);
105 aLayout->addWidget(aLabel);
106 aLayout->addStretch(1);
108 myStackWidget->addWidget(aFirstWgt);
110 // Define widget for sketch manmagement
111 QWidget* aSecondWgt = new QWidget(this);
112 aLayout = new QVBoxLayout(aSecondWgt);
113 ModuleBase_Tools::zeroMargins(aLayout);
115 QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
116 QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
118 myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
119 aViewLayout->addWidget(myViewInverted);
121 QPushButton* aSetViewBtn =
122 new QPushButton(QIcon(":icons/plane_view.png"), tr("Set plane view"), aViewBox);
123 connect(aSetViewBtn, SIGNAL(clicked(bool)), this, SLOT(onSetPlaneView()));
124 aViewLayout->addWidget(aSetViewBtn);
126 aLayout->addWidget(aViewBox);
128 QMap<PartSet_Tools::ConstraintVisibleState, QString> aStates;
129 aStates[PartSet_Tools::Geometrical] = tr("Show geometrical constraints");
130 aStates[PartSet_Tools::Dimensional] = tr("Show dimensional constraints");
131 aStates[PartSet_Tools::Expressions] = tr("Show existing expressions");
133 QMap<PartSet_Tools::ConstraintVisibleState, QString>::const_iterator anIt = aStates.begin(),
134 aLast = aStates.end();
135 for (; anIt != aLast; anIt++) {
136 QCheckBox* aShowConstraints = new QCheckBox(anIt.value(), this);
137 connect(aShowConstraints, SIGNAL(toggled(bool)), this, SLOT(onShowConstraint(bool)));
138 aLayout->addWidget(aShowConstraints);
140 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
141 myShowConstraints[aState] = aShowConstraints;
143 if (toShowConstraints.contains(aState))
144 aShowConstraints->setChecked(toShowConstraints[aState]);
147 myStackWidget->addWidget(aSecondWgt);
148 //setLayout(aLayout);
150 myPreviewPlanes = new PartSet_PreviewPlanes();
153 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
157 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
158 const bool theToValidate)
160 // do not use the given selection if the plane of the sketch has been already set.
161 // If this check is absent, a selected plane in the viewer can be set in the sketch
162 // even if the sketch is built on another plane.
166 ModuleBase_ViewerPrsPtr aPrs = theValues.first();
167 bool aDone = setSelectionInternal(theValues, theToValidate);
169 updateByPlaneSelected(aPrs);
173 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
175 QList<QWidget*> aResult;
176 aResult << myStackWidget;
180 void PartSet_WidgetSketchLabel::onSelectionChanged()
182 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
184 if (aSelected.empty())
186 ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
187 bool aDone = setSelectionInternal(aSelected, false);
189 updateByPlaneSelected(aPrs);
190 updateObject(myFeature);
194 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
196 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
198 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
199 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
201 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
203 for (; anIt != aLast && !aFound; anIt++) {
204 aFound = anIt.value() == aSenderCheckBox;
209 emit showConstraintToggled(aState, theOn);
212 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
213 const bool& theToBlock, bool& isFlushesActived,
214 bool& isAttributeSetInitializedBlocked)
216 ModuleBase_WidgetValidated::blockAttribute(theAttribute, theToBlock, isFlushesActived,
217 isAttributeSetInitializedBlocked);
218 // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
219 // attributes. It it is necessary, these states should be append to the method attributes
220 // or stored in the widget
222 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
223 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
225 for(; anIt != aLast; anIt++) {
226 AttributePtr anAttribute = *anIt;
228 anAttribute->blockSetInitialized(true);
230 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
234 bool PartSet_WidgetSketchLabel::setSelectionInternal(
235 const QList<ModuleBase_ViewerPrsPtr>& theValues,
236 const bool theToValidate)
239 if (theValues.empty()) {
240 // In order to make reselection possible, set empty object and shape should be done
241 setSelectionCustom(std::shared_ptr<ModuleBase_ViewerPrs>(
242 new ModuleBase_ViewerPrs(ObjectPtr(), GeomShapePtr(), NULL)));
246 // it removes the processed value from the parameters list
247 ModuleBase_ViewerPrsPtr aValue = theValues.first();//.takeFirst();
248 if (!theToValidate || isValidInFilters(aValue))
249 aDone = setSelectionCustom(aValue);
255 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
257 // 1. hide main planes if they have been displayed
258 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
259 // 2. if the planes were displayed, change the view projection
260 const GeomShapePtr& aShape = thePrs->shape();
261 std::shared_ptr<GeomAPI_Shape> aGShape;
262 std::shared_ptr<GeomAPI_Shape> aBaseShape;
264 DataPtr aData = feature()->data();
265 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
266 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
268 // selection happens in OCC viewer
269 if (aShape.get() && !aShape->isNull()) {
272 if (aSelAttr && aSelAttr->context()) {
273 aBaseShape = aSelAttr->context()->shape();
276 else { // selection happens in OCC viewer(on body) of in the OB browser
278 aGShape = aSelAttr->value();
281 if (aGShape.get() != NULL) {
282 // get plane parameters
283 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
284 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
285 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
286 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
289 // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
290 /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
294 // Rotate view if the sketcher plane is selected only from preview planes
295 // Preview planes are created only if there is no any shape
296 bool aRotate = Config_PropManager::boolean(SKETCH_TAB_NAME, "rotate_to_plane", "false");
298 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
299 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
301 aModule->onViewTransformed();
303 QString aSizeOfViewStr = mySizeOfView->text();
304 if (!aSizeOfViewStr.isEmpty()) {
306 double aSizeOfView = aSizeOfViewStr.toDouble(&isOk);
307 if (isOk && aSizeOfView > 0) {
308 Handle(V3d_View) aView3d = myWorkshop->viewer()->activeView();
309 if (!aView3d.IsNull()) {
311 double aHalfSize = aSizeOfView/2.0;
312 aBndBox.Update(-aHalfSize, -aHalfSize, -aHalfSize, aHalfSize, aHalfSize, aHalfSize);
313 aView3d->FitAll(aBndBox, 0.01, false);
318 // 3. Clear text in the label
319 myStackWidget->setCurrentIndex(1);
320 //myLabel->setText("");
321 //myLabel->setToolTip("");
322 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
323 disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()),
324 this, SLOT(onSelectionChanged()));
325 // 4. deactivate face selection filter
326 activateFilters(false);
328 // 5. Clear selection mode and define sketching mode
329 emit planeSelected(plane());
330 // after the plane is selected in the sketch, the sketch selection should be activated
331 // it can not be performed in the sketch label widget because, we don't need to switch off
332 // the selection by any label deactivation, but need to switch it off by stop the sketch
333 activateSelection(true);
335 // 6. Update sketcher actions
336 XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
337 myWorkshop->updateCommandStatus();
338 myWorkshop->viewer()->update();
341 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
343 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
344 return PartSet_Tools::sketchPlane(aSketch);
347 bool PartSet_WidgetSketchLabel::focusTo()
349 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
353 void PartSet_WidgetSketchLabel::enableFocusProcessing()
355 myStackWidget->installEventFilter(this);
358 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
360 ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
363 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
366 ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
368 // it is not necessary to save the previous plane value because the plane is chosen once
369 DataPtr aData = feature()->data();
370 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
371 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
373 ResultPtr anEmptyResult;
374 GeomShapePtr anEmptyShape;
375 aSelAttr->setValue(anEmptyResult, anEmptyShape);
379 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
381 return fillSketchPlaneBySelection(feature(), thePrs);
384 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
386 bool aCanFillSketch = true;
387 // avoid any selection on sketch object
388 ObjectPtr anObject = thePrs->object();
389 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
391 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
392 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
393 aCanFillSketch = false;
395 // check plane or planar face of any non-sketch object
396 if (aCanFillSketch) {
397 std::shared_ptr<GeomAPI_Face> aGeomFace;
399 GeomShapePtr aGeomShape = thePrs->shape();
400 if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
401 aGeomShape = aResult->shape();
404 if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
405 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
406 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
409 aCanFillSketch = false;
411 return aCanFillSketch;
414 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
415 const ModuleBase_ViewerPrsPtr& thePrs)
417 bool isOwnerSet = false;
419 const GeomShapePtr& aShape = thePrs->shape();
420 std::shared_ptr<GeomAPI_Dir> aDir;
422 if (thePrs->object() && (theFeature != thePrs->object())) {
423 DataPtr aData = theFeature->data();
424 AttributeSelectionPtr aSelAttr =
425 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
426 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
428 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
430 GeomShapePtr aShapePtr(new GeomAPI_Shape());
431 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
432 aShapePtr = ModelAPI_Tools::shape(aRes);
434 else { // selection happens in OB browser
437 if (aShapePtr.get() != NULL) {
438 aSelAttr->setValue(aRes, aShapePtr);
444 else if (aShape.get() && !aShape->isNull()) {
445 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
446 aDir = setSketchPlane(theFeature, aTDShape);
447 isOwnerSet = aDir.get();
452 void PartSet_WidgetSketchLabel::activateCustom()
454 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
456 myStackWidget->setCurrentIndex(1);
457 activateSelection(true);
461 myStackWidget->setCurrentIndex(0);
462 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
464 // Clear previous selection mode It is necessary for correct activation of preview planes
465 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
466 XGUI_Displayer* aDisp = aWorkshop->displayer();
467 aDisp->activateObjects(QIntList(), aDisp->displayedObjects(), false);
469 if (!aBodyIsVisualized) {
470 // We have to select a plane before any operation
471 myPreviewPlanes->showPreviewPlanes(myWorkshop);
472 mySizeOfViewWidget->setVisible(true);
475 mySizeOfViewWidget->setVisible(false);
477 activateSelection(true);
479 //myLabel->setText(myText);
480 //myLabel->setToolTip(myTooltip);
482 connect(XGUI_Tools::workshop(myWorkshop)->selector(), SIGNAL(selectionChanged()),
483 this, SLOT(onSelectionChanged()));
484 activateFilters(true);
487 void PartSet_WidgetSketchLabel::deactivate()
489 ModuleBase_ModelWidget::deactivate();
490 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
491 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
492 activateSelection(false);
494 activateFilters(false);
496 myWorkshop->viewer()->update();
499 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
503 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
505 myWorkshop->module()->activeSelectionModes(aModes);
508 aModes << TopAbs_FACE;
510 myWorkshop->activateSubShapesSelection(aModes);
512 myWorkshop->deactivateSubShapesSelection();
517 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
518 const TopoDS_Shape& theShape)
520 if (theShape.IsNull())
521 return std::shared_ptr<GeomAPI_Dir>();
523 // get selected shape
524 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
525 aGShape->setImpl(new TopoDS_Shape(theShape));
529 // get plane parameters
530 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
531 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
533 return std::shared_ptr<GeomAPI_Dir>();
535 // set plane parameters to feature
536 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
537 double anA, aB, aC, aD;
538 aPlane->coefficients(anA, aB, aC, aD);
540 // calculate attributes of the sketch
541 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
542 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
543 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
544 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
545 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
546 // X axis is preferable to be dirX on the sketch
547 const double tol = Precision::Confusion();
548 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
549 std::shared_ptr<GeomAPI_Dir> aTempDir(
550 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
551 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
552 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
554 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
555 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
556 anOrigin->setValue(anOrigPnt);
557 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
558 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
559 aNormal->setValue(aNormDir);
560 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
561 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
562 aDirX->setValue(aXDir);
563 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
567 void PartSet_WidgetSketchLabel::onSetPlaneView()
569 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
571 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
572 gp_Dir aDir = aDirection->impl<gp_Dir>();
573 if (myViewInverted->isChecked())
575 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
576 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
578 aModule->onViewTransformed();