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>
48 #include <Config_WidgetAPI.h>
49 #include <Config_PropManager.h>
52 #include <QApplication>
53 #include <QVBoxLayout>
56 #include <QPushButton>
57 #include <QStackedWidget>
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)
66 QVBoxLayout* aLayout = new QVBoxLayout(this);
67 ModuleBase_Tools::zeroMargins(aLayout);
69 myStackWidget = new QStackedWidget(this);
70 myStackWidget->setContentsMargins(0,0,0,0);
71 aLayout->addWidget(myStackWidget);
73 // Define label for plane selection
74 QWidget* aFirstWgt = new QWidget(this);
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);
83 aLayout = new QVBoxLayout(aFirstWgt);
84 ModuleBase_Tools::zeroMargins(aLayout);
85 aLayout->addWidget(aLabel);
87 myStackWidget->addWidget(aFirstWgt);
89 // Define widget for sketch manmagement
90 QWidget* aSecondWgt = new QWidget(this);
91 aLayout = new QVBoxLayout(aSecondWgt);
92 ModuleBase_Tools::zeroMargins(aLayout);
94 QGroupBox* aViewBox = new QGroupBox(tr("Sketcher plane"), this);
95 QVBoxLayout* aViewLayout = new QVBoxLayout(aViewBox);
97 myViewInverted = new QCheckBox(tr("Reversed"), aViewBox);
98 aViewLayout->addWidget(myViewInverted);
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);
105 aLayout->addWidget(aViewBox);
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");
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);
119 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
120 myShowConstraints[aState] = aShowConstraints;
122 if (toShowConstraints.contains(aState))
123 aShowConstraints->setChecked(toShowConstraints[aState]);
126 myStackWidget->addWidget(aSecondWgt);
127 //setLayout(aLayout);
129 myPreviewPlanes = new PartSet_PreviewPlanes();
132 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
136 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrsPtr>& theValues,
137 const bool theToValidate)
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.
145 ModuleBase_ViewerPrsPtr aPrs = theValues.first();
146 bool aDone = setSelectionInternal(theValues, theToValidate);
148 updateByPlaneSelected(aPrs);
152 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
154 QList<QWidget*> aResult;
155 aResult << myStackWidget;
159 void PartSet_WidgetSketchLabel::onSelectionChanged()
161 QList<ModuleBase_ViewerPrsPtr> aSelected = getFilteredSelected();
163 if (aSelected.empty())
165 ModuleBase_ViewerPrsPtr aPrs = aSelected.first();
166 bool aDone = setSelectionInternal(aSelected, false);
168 updateByPlaneSelected(aPrs);
169 updateObject(myFeature);
173 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
175 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
177 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
178 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
180 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
182 for (; anIt != aLast && !aFound; anIt++) {
183 aFound = anIt.value() == aSenderCheckBox;
188 emit showConstraintToggled(aState, theOn);
191 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
192 const bool& theToBlock, bool& isFlushesActived,
193 bool& isAttributeSetInitializedBlocked)
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
201 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
202 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
204 for(; anIt != aLast; anIt++) {
205 AttributePtr anAttribute = *anIt;
207 anAttribute->blockSetInitialized(true);
209 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
213 bool PartSet_WidgetSketchLabel::setSelectionInternal(
214 const QList<ModuleBase_ViewerPrsPtr>& theValues,
215 const bool theToValidate)
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)));
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);
234 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrsPtr& thePrs)
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;
243 DataPtr aData = feature()->data();
244 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
245 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
247 // selection happens in OCC viewer
248 if (aShape.get() && !aShape->isNull()) {
251 if (aSelAttr && aSelAttr->context()) {
252 aBaseShape = aSelAttr->context()->shape();
255 else { // selection happens in OCC viewer(on body) of in the OB browser
257 aGShape = aSelAttr->value();
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();
268 // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
269 /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
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");
277 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
278 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
280 aModule->onViewTransformed();
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);
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);
302 // 6. Update sketcher actions
303 XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
304 myWorkshop->updateCommandStatus();
305 myWorkshop->viewer()->update();
308 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
310 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
311 return PartSet_Tools::sketchPlane(aSketch);
314 bool PartSet_WidgetSketchLabel::focusTo()
316 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
320 void PartSet_WidgetSketchLabel::enableFocusProcessing()
322 myStackWidget->installEventFilter(this);
325 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
327 ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
330 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
333 ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
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()));
340 ResultPtr anEmptyResult;
341 GeomShapePtr anEmptyShape;
342 aSelAttr->setValue(anEmptyResult, anEmptyShape);
346 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrsPtr& thePrs)
348 return fillSketchPlaneBySelection(feature(), thePrs);
351 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrsPtr& thePrs)
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);
358 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
359 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
360 aCanFillSketch = false;
362 // check plane or planar face of any non-sketch object
363 if (aCanFillSketch) {
364 std::shared_ptr<GeomAPI_Face> aGeomFace;
366 GeomShapePtr aGeomShape = thePrs->shape();
367 if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
368 aGeomShape = aResult->shape();
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();
376 aCanFillSketch = false;
378 return aCanFillSketch;
381 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
382 const ModuleBase_ViewerPrsPtr& thePrs)
384 bool isOwnerSet = false;
386 const GeomShapePtr& aShape = thePrs->shape();
387 std::shared_ptr<GeomAPI_Dir> aDir;
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()));
395 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs->object());
397 GeomShapePtr aShapePtr(new GeomAPI_Shape());
398 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
399 aShapePtr = ModelAPI_Tools::shape(aRes);
401 else { // selection happens in OB browser
404 if (aShapePtr.get() != NULL) {
405 aSelAttr->setValue(aRes, aShapePtr);
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();
419 void PartSet_WidgetSketchLabel::activateCustom()
421 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
423 myStackWidget->setCurrentIndex(1);
424 activateSelection(true);
428 myStackWidget->setCurrentIndex(0);
429 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
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);
436 if (!aBodyIsVisualized) {
437 // We have to select a plane before any operation
438 myPreviewPlanes->showPreviewPlanes(myWorkshop);
440 activateSelection(true);
442 //myLabel->setText(myText);
443 //myLabel->setToolTip(myTooltip);
445 connect(XGUI_Tools::workshop(myWorkshop)->selector(), SIGNAL(selectionChanged()),
446 this, SLOT(onSelectionChanged()));
447 activateFilters(true);
450 void PartSet_WidgetSketchLabel::deactivate()
452 ModuleBase_ModelWidget::deactivate();
453 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
454 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
455 activateSelection(false);
457 activateFilters(false);
459 myWorkshop->viewer()->update();
462 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
466 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
468 myWorkshop->module()->activeSelectionModes(aModes);
471 aModes << TopAbs_FACE;
473 myWorkshop->activateSubShapesSelection(aModes);
475 myWorkshop->deactivateSubShapesSelection();
480 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
481 const TopoDS_Shape& theShape)
483 if (theShape.IsNull())
484 return std::shared_ptr<GeomAPI_Dir>();
486 // get selected shape
487 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
488 aGShape->setImpl(new TopoDS_Shape(theShape));
492 // get plane parameters
493 std::shared_ptr<GeomAPI_Face> aFace(new GeomAPI_Face(aGShape));
494 std::shared_ptr<GeomAPI_Pln> aPlane = aFace->getPlane();
496 return std::shared_ptr<GeomAPI_Dir>();
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);
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)));
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();
530 void PartSet_WidgetSketchLabel::onSetPlaneView()
532 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
534 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
535 gp_Dir aDir = aDirection->impl<gp_Dir>();
536 if (myViewInverted->isChecked())
538 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
539 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
541 aModule->onViewTransformed();