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 = 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);
104 aLayout->addWidget(aViewBox);
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");
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);
118 PartSet_Tools::ConstraintVisibleState aState = anIt.key();
119 myShowConstraints[aState] = aShowConstraints;
121 if (toShowConstraints.contains(aState))
122 aShowConstraints->setChecked(toShowConstraints[aState]);
125 myStackWidget->addWidget(aSecondWgt);
126 //setLayout(aLayout);
128 myPreviewPlanes = new PartSet_PreviewPlanes();
131 PartSet_WidgetSketchLabel::~PartSet_WidgetSketchLabel()
135 bool PartSet_WidgetSketchLabel::setSelection(QList<ModuleBase_ViewerPrs>& theValues,
136 const bool theToValidate)
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.
144 ModuleBase_ViewerPrs aPrs = theValues.first();
145 bool aDone = setSelectionInternal(theValues, theToValidate);
147 updateByPlaneSelected(aPrs);
151 QList<QWidget*> PartSet_WidgetSketchLabel::getControls() const
153 QList<QWidget*> aResult;
154 aResult << myStackWidget;
158 void PartSet_WidgetSketchLabel::onSelectionChanged()
160 QList<ModuleBase_ViewerPrs> aSelected = getFilteredSelected();
162 if (aSelected.empty())
164 ModuleBase_ViewerPrs aPrs = aSelected.first();
165 bool aDone = setSelectionInternal(aSelected, false);
167 updateByPlaneSelected(aPrs);
168 updateObject(myFeature);
172 void PartSet_WidgetSketchLabel::onShowConstraint(bool theOn)
174 QCheckBox* aSenderCheckBox = qobject_cast<QCheckBox*>(sender());
176 QMap<PartSet_Tools::ConstraintVisibleState, QCheckBox*>::const_iterator
177 anIt = myShowConstraints.begin(), aLast = myShowConstraints.end();
179 PartSet_Tools::ConstraintVisibleState aState = PartSet_Tools::Geometrical;
181 for (; anIt != aLast && !aFound; anIt++) {
182 aFound = anIt.value() == aSenderCheckBox;
187 emit showConstraintToggled(aState, theOn);
190 void PartSet_WidgetSketchLabel::blockAttribute(const AttributePtr& theAttribute,
191 const bool& theToBlock, bool& isFlushesActived,
192 bool& isAttributeSetInitializedBlocked)
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
200 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
201 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
203 for(; anIt != aLast; anIt++) {
204 AttributePtr anAttribute = *anIt;
206 anAttribute->blockSetInitialized(true);
208 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
212 bool PartSet_WidgetSketchLabel::setSelectionInternal(const QList<ModuleBase_ViewerPrs>& theValues,
213 const bool theToValidate)
216 ModuleBase_ViewerPrs aValue;
217 if (theValues.empty()) {
218 // In order to make reselection possible, set empty object and shape should be done
219 setSelectionCustom(ModuleBase_ViewerPrs());
223 // it removes the processed value from the parameters list
224 aValue = theValues.first();//.takeFirst();
225 if (!theToValidate || isValidInFilters(aValue))
226 aDone = setSelectionCustom(aValue);
232 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
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;
241 DataPtr aData = feature()->data();
242 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
243 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
245 // selection happens in OCC viewer
246 if (aShape.get() && !aShape->isNull()) {
249 if (aSelAttr && aSelAttr->context()) {
250 aBaseShape = aSelAttr->context()->shape();
253 else { // selection happens in OCC viewer(on body) of in the OB browser
255 aGShape = aSelAttr->value();
258 if (aGShape.get() != NULL) {
259 // get plane parameters
260 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
261 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
262 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
265 // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
266 /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
270 // Rotate view if the sketcher plane is selected only from preview planes
271 // Preview planes are created only if there is no any shape
272 bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
274 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
275 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
277 aModule->onViewTransformed();
280 // 3. Clear text in the label
281 myStackWidget->setCurrentIndex(1);
282 //myLabel->setText("");
283 //myLabel->setToolTip("");
284 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
285 disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()),
286 this, SLOT(onSelectionChanged()));
287 // 4. deactivate face selection filter
288 activateFilters(false);
290 // 5. Clear selection mode and define sketching mode
291 //XGUI_Displayer* aDisp = aWorkshop->displayer();
292 //aDisp->closeLocalContexts();
293 emit planeSelected(plane());
294 // after the plane is selected in the sketch, the sketch selection should be activated
295 // it can not be performed in the sketch label widget because, we don't need to switch off
296 // the selection by any label deactivation, but need to switch it off by stop the sketch
297 activateSelection(true);
299 // 6. Update sketcher actions
300 XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
301 myWorkshop->updateCommandStatus();
302 myWorkshop->viewer()->update();
305 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
307 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
308 return PartSet_Tools::sketchPlane(aSketch);
311 bool PartSet_WidgetSketchLabel::focusTo()
313 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
317 void PartSet_WidgetSketchLabel::enableFocusProcessing()
319 myStackWidget->installEventFilter(this);
322 void PartSet_WidgetSketchLabel::storeAttributeValue(const AttributePtr& theAttribute)
324 ModuleBase_WidgetValidated::storeAttributeValue(theAttribute);
327 void PartSet_WidgetSketchLabel::restoreAttributeValue(const AttributePtr& theAttribute,
330 ModuleBase_WidgetValidated::restoreAttributeValue(theAttribute, theValid);
332 // it is not necessary to save the previous plane value because the plane is chosen once
333 DataPtr aData = feature()->data();
334 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
335 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
337 ResultPtr anEmptyResult;
338 GeomShapePtr anEmptyShape;
339 aSelAttr->setValue(anEmptyResult, anEmptyShape);
343 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
345 return fillSketchPlaneBySelection(feature(), thePrs);
348 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrs& thePrs)
350 bool aCanFillSketch = true;
351 // avoid any selection on sketch object
352 ObjectPtr anObject = thePrs.object();
353 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
355 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
356 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
357 aCanFillSketch = false;
359 // check plane or planar face of any non-sketch object
360 if (aCanFillSketch) {
361 std::shared_ptr<GeomAPI_Face> aGeomFace;
363 GeomShapePtr aGeomShape = thePrs.shape();
364 if ((!aGeomShape.get() || aGeomShape->isNull()) && aResult.get()) {
365 aGeomShape = aResult->shape();
368 if (aGeomShape.get() && aGeomShape->shapeType() == GeomAPI_Shape::FACE) {
369 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
370 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
373 aCanFillSketch = false;
375 return aCanFillSketch;
378 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
379 const ModuleBase_ViewerPrs& thePrs)
381 bool isOwnerSet = false;
383 const GeomShapePtr& aShape = thePrs.shape();
384 std::shared_ptr<GeomAPI_Dir> aDir;
386 if (thePrs.object() && (theFeature != thePrs.object())) {
387 DataPtr aData = theFeature->data();
388 AttributeSelectionPtr aSelAttr =
389 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
390 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
392 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
394 GeomShapePtr aShapePtr(new GeomAPI_Shape());
395 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
396 aShapePtr = ModelAPI_Tools::shape(aRes);
398 else { // selection happens in OB browser
401 if (aShapePtr.get() != NULL) {
402 aSelAttr->setValue(aRes, aShapePtr);
408 else if (aShape.get() && !aShape->isNull()) {
409 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
410 aDir = setSketchPlane(theFeature, aTDShape);
411 isOwnerSet = aDir.get();
416 void PartSet_WidgetSketchLabel::activateCustom()
418 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
420 myStackWidget->setCurrentIndex(1);
421 activateSelection(true);
425 myStackWidget->setCurrentIndex(0);
426 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
428 // Clear previous selection mode It is necessary for correct activation of preview planes
429 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
430 XGUI_Displayer* aDisp = aWorkshop->displayer();
431 aDisp->activateObjects(QIntList(), aDisp->displayedObjects(), false);
433 if (!aBodyIsVisualized) {
434 // We have to select a plane before any operation
435 myPreviewPlanes->showPreviewPlanes(myWorkshop);
437 activateSelection(true);
439 //myLabel->setText(myText);
440 //myLabel->setToolTip(myTooltip);
442 connect(XGUI_Tools::workshop(myWorkshop)->selector(), SIGNAL(selectionChanged()),
443 this, SLOT(onSelectionChanged()));
444 activateFilters(true);
447 void PartSet_WidgetSketchLabel::deactivate()
449 ModuleBase_ModelWidget::deactivate();
450 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
451 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
452 activateSelection(false);
454 activateFilters(false);
456 myWorkshop->viewer()->update();
459 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
463 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
465 myWorkshop->module()->activeSelectionModes(aModes);
468 aModes << TopAbs_FACE;
470 myWorkshop->activateSubShapesSelection(aModes);
472 myWorkshop->deactivateSubShapesSelection();
477 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
478 const TopoDS_Shape& theShape)
480 if (theShape.IsNull())
481 return std::shared_ptr<GeomAPI_Dir>();
483 // get selected shape
484 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
485 aGShape->setImpl(new TopoDS_Shape(theShape));
489 // get plane parameters
490 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
492 return std::shared_ptr<GeomAPI_Dir>();
494 // set plane parameters to feature
495 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
496 double anA, aB, aC, aD;
497 aPlane->coefficients(anA, aB, aC, aD);
499 // calculate attributes of the sketch
500 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
501 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
502 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
503 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
504 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
505 // X axis is preferable to be dirX on the sketch
506 const double tol = Precision::Confusion();
507 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
508 std::shared_ptr<GeomAPI_Dir> aTempDir(
509 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
510 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
511 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
513 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
514 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
515 anOrigin->setValue(anOrigPnt);
516 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
517 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
518 aNormal->setValue(aNormDir);
519 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
520 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
521 aDirX->setValue(aXDir);
522 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
526 void PartSet_WidgetSketchLabel::onSetPlaneView()
528 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
530 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
531 gp_Dir aDir = aDirection->impl<gp_Dir>();
532 if (myViewInverted->isChecked())
534 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
535 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
537 aModule->onViewTransformed();