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 bool& theToBlock, bool& isFlushesActived,
191 bool& isAttributeSetInitializedBlocked)
193 ModuleBase_WidgetValidated::blockAttribute(theToBlock, isFlushesActived,
194 isAttributeSetInitializedBlocked);
195 // We do not restore the previous state of isAttributeSetInitializedBlocked for each of
196 // attributes. It it is necessary, these states should be append to the method attributes
197 // or stored in the widget
199 std::list<AttributePtr> anAttributes = myFeature->data()->attributes("");
200 std::list<AttributePtr>::const_iterator anIt = anAttributes.begin(), aLast = anAttributes.end();
202 for(; anIt != aLast; anIt++) {
203 AttributePtr anAttribute = *anIt;
205 anAttribute->blockSetInitialized(true);
207 anAttribute->blockSetInitialized(isAttributeSetInitializedBlocked);
211 bool PartSet_WidgetSketchLabel::setSelectionInternal(const QList<ModuleBase_ViewerPrs>& theValues,
212 const bool theToValidate)
215 ModuleBase_ViewerPrs aValue;
216 if (theValues.empty()) {
217 // In order to make reselection possible, set empty object and shape should be done
218 setSelectionCustom(ModuleBase_ViewerPrs());
222 // it removes the processed value from the parameters list
223 aValue = theValues.first();//.takeFirst();
224 if (!theToValidate || isValidInFilters(aValue))
225 aDone = setSelectionCustom(aValue);
231 void PartSet_WidgetSketchLabel::updateByPlaneSelected(const ModuleBase_ViewerPrs& thePrs)
233 // 1. hide main planes if they have been displayed
234 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
235 // 2. if the planes were displayed, change the view projection
236 const GeomShapePtr& aShape = thePrs.shape();
237 std::shared_ptr<GeomAPI_Shape> aGShape;
238 std::shared_ptr<GeomAPI_Shape> aBaseShape;
240 DataPtr aData = feature()->data();
241 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
242 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
244 // selection happens in OCC viewer
245 if (aShape.get() && !aShape->isNull()) {
248 if (aSelAttr && aSelAttr->context()) {
249 aBaseShape = aSelAttr->context()->shape();
252 else { // selection happens in OCC viewer(on body) of in the OB browser
254 aGShape = aSelAttr->value();
257 if (aGShape.get() != NULL) {
258 // get plane parameters
259 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
260 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
261 gp_XYZ aXYZ = aDir->impl<gp_Dir>().XYZ();
264 // orienting projection is not needed: it is done in GeomAlgoAPI_FaceBuilder::plane
265 /*if (aGShape->impl<TopoDS_Shape>().Orientation() == TopAbs_REVERSED) {
269 // Rotate view if the sketcher plane is selected only from preview planes
270 // Preview planes are created only if there is no any shape
271 bool aRotate = Config_PropManager::boolean("Sketch planes", "rotate_to_plane", "false");
273 myWorkshop->viewer()->setViewProjection(aXYZ.X(), aXYZ.Y(), aXYZ.Z(), aTwist);
274 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
276 aModule->onViewTransformed();
279 // 3. Clear text in the label
280 myStackWidget->setCurrentIndex(1);
281 //myLabel->setText("");
282 //myLabel->setToolTip("");
283 XGUI_Workshop* aWorkshop = XGUI_Tools::workshop(myWorkshop);
284 disconnect(aWorkshop->selector(), SIGNAL(selectionChanged()),
285 this, SLOT(onSelectionChanged()));
286 // 4. deactivate face selection filter
287 activateFilters(false);
289 // 5. Clear selection mode and define sketching mode
290 //XGUI_Displayer* aDisp = aWorkshop->displayer();
291 //aDisp->closeLocalContexts();
292 emit planeSelected(plane());
293 // after the plane is selected in the sketch, the sketch selection should be activated
294 // it can not be performed in the sketch label widget because, we don't need to switch off
295 // the selection by any label deactivation, but need to switch it off by stop the sketch
296 activateSelection(true);
298 // 6. Update sketcher actions
299 XGUI_ActionsMgr* anActMgr = aWorkshop->actionsMgr();
300 myWorkshop->updateCommandStatus();
301 myWorkshop->viewer()->update();
304 std::shared_ptr<GeomAPI_Pln> PartSet_WidgetSketchLabel::plane() const
306 CompositeFeaturePtr aSketch = std::dynamic_pointer_cast<ModelAPI_CompositeFeature>(myFeature);
307 return PartSet_Tools::sketchPlane(aSketch);
310 bool PartSet_WidgetSketchLabel::focusTo()
312 ModuleBase_Tools::setFocus(myStackWidget, "PartSet_WidgetSketchLabel::focusTo()");
316 void PartSet_WidgetSketchLabel::enableFocusProcessing()
318 myStackWidget->installEventFilter(this);
321 void PartSet_WidgetSketchLabel::storeAttributeValue()
323 ModuleBase_WidgetValidated::storeAttributeValue();
326 void PartSet_WidgetSketchLabel::restoreAttributeValue(const bool theValid)
328 ModuleBase_WidgetValidated::restoreAttributeValue(theValid);
330 // it is not necessary to save the previous plane value because the plane is chosen once
331 DataPtr aData = feature()->data();
332 AttributeSelectionPtr aSelAttr = std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
333 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
335 ResultPtr anEmptyResult;
336 GeomShapePtr anEmptyShape;
337 aSelAttr->setValue(anEmptyResult, anEmptyShape);
341 bool PartSet_WidgetSketchLabel::setSelectionCustom(const ModuleBase_ViewerPrs& thePrs)
343 return fillSketchPlaneBySelection(feature(), thePrs);
346 bool PartSet_WidgetSketchLabel::canFillSketch(const ModuleBase_ViewerPrs& thePrs)
348 bool aCanFillSketch = true;
349 // avoid any selection on sketch object
350 ObjectPtr anObject = thePrs.object();
351 ResultPtr aResult = std::dynamic_pointer_cast<ModelAPI_Result>(anObject);
353 FeaturePtr aFeature = ModelAPI_Feature::feature(aResult);
354 if (aFeature->getKind() == SketchPlugin_Sketch::ID())
355 aCanFillSketch = false;
357 // check plane or planar face of any non-sketch object
358 if (aCanFillSketch) {
359 std::shared_ptr<GeomAPI_Face> aGeomFace;
360 const TopoDS_Shape aShape = thePrs.shape();
361 if (aShape.IsNull()) {
362 GeomShapePtr aGeomShape = aResult->shape();
363 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face(aGeomShape));
364 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
366 else if (aShape.ShapeType() == TopAbs_FACE) {
367 std::shared_ptr<GeomAPI_Face> aGeomFace(new GeomAPI_Face());
368 aGeomFace->setImpl(new TopoDS_Shape(aShape));
369 aCanFillSketch = aGeomFace.get() && aGeomFace->isPlanar();
372 aCanFillSketch = false;
374 return aCanFillSketch;
377 bool PartSet_WidgetSketchLabel::fillSketchPlaneBySelection(const FeaturePtr& theFeature,
378 const ModuleBase_ViewerPrs& thePrs)
380 bool isOwnerSet = false;
382 const GeomShapePtr& aShape = thePrs.shape();
383 std::shared_ptr<GeomAPI_Dir> aDir;
385 if (thePrs.object() && (theFeature != thePrs.object())) {
386 DataPtr aData = theFeature->data();
387 AttributeSelectionPtr aSelAttr =
388 std::dynamic_pointer_cast<ModelAPI_AttributeSelection>
389 (aData->attribute(SketchPlugin_SketchEntity::EXTERNAL_ID()));
391 ResultPtr aRes = std::dynamic_pointer_cast<ModelAPI_Result>(thePrs.object());
393 GeomShapePtr aShapePtr(new GeomAPI_Shape());
394 if (!aShape.get() || aShape->isNull()) { // selection happens in the OCC viewer
395 aShapePtr = ModelAPI_Tools::shape(aRes);
397 else { // selection happens in OB browser
400 if (aShapePtr.get() != NULL) {
401 aSelAttr->setValue(aRes, aShapePtr);
407 else if (aShape.get() && !aShape->isNull()) {
408 const TopoDS_Shape& aTDShape = aShape->impl<TopoDS_Shape>();
409 aDir = setSketchPlane(aTDShape);
410 isOwnerSet = aDir.get();
415 void PartSet_WidgetSketchLabel::activateCustom()
417 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
419 myStackWidget->setCurrentIndex(1);
420 activateSelection(true);
424 myStackWidget->setCurrentIndex(0);
425 bool aBodyIsVisualized = myPreviewPlanes->hasVisualizedBodies(myWorkshop);
426 if (!aBodyIsVisualized) {
427 // We have to select a plane before any operation
428 myPreviewPlanes->showPreviewPlanes(myWorkshop);
430 activateSelection(true);
432 //myLabel->setText(myText);
433 //myLabel->setToolTip(myTooltip);
435 connect(XGUI_Tools::workshop(myWorkshop)->selector(), SIGNAL(selectionChanged()),
436 this, SLOT(onSelectionChanged()));
437 activateFilters(true);
440 void PartSet_WidgetSketchLabel::deactivate()
442 ModuleBase_ModelWidget::deactivate();
443 bool aHidePreview = myPreviewPlanes->isPreviewDisplayed();
444 myPreviewPlanes->erasePreviewPlanes(myWorkshop);
445 activateSelection(false);
447 activateFilters(false);
449 myWorkshop->viewer()->update();
452 void PartSet_WidgetSketchLabel::activateSelection(bool toActivate)
456 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
458 myWorkshop->module()->activeSelectionModes(aModes);
461 aModes << TopAbs_FACE;
463 myWorkshop->activateSubShapesSelection(aModes);
465 myWorkshop->deactivateSubShapesSelection();
470 std::shared_ptr<GeomAPI_Dir> PartSet_WidgetSketchLabel::setSketchPlane(const FeaturePtr& theFeature,
471 const TopoDS_Shape& theShape)
473 if (theShape.IsNull())
474 return std::shared_ptr<GeomAPI_Dir>();
476 // get selected shape
477 std::shared_ptr<GeomAPI_Shape> aGShape(new GeomAPI_Shape);
478 aGShape->setImpl(new TopoDS_Shape(theShape));
482 // get plane parameters
483 std::shared_ptr<GeomAPI_Pln> aPlane = GeomAlgoAPI_FaceBuilder::plane(aGShape);
485 return std::shared_ptr<GeomAPI_Dir>();
487 // set plane parameters to feature
488 std::shared_ptr<ModelAPI_Data> aData = theFeature->data();
489 double anA, aB, aC, aD;
490 aPlane->coefficients(anA, aB, aC, aD);
492 // calculate attributes of the sketch
493 std::shared_ptr<GeomAPI_Dir> aNormDir(new GeomAPI_Dir(anA, aB, aC));
494 std::shared_ptr<GeomAPI_XYZ> aCoords = aNormDir->xyz();
495 std::shared_ptr<GeomAPI_XYZ> aZero(new GeomAPI_XYZ(0, 0, 0));
496 aCoords = aCoords->multiplied(-aD * aCoords->distance(aZero));
497 std::shared_ptr<GeomAPI_Pnt> anOrigPnt(new GeomAPI_Pnt(aCoords));
498 // X axis is preferable to be dirX on the sketch
499 const double tol = Precision::Confusion();
500 bool isX = fabs(anA - 1.0) < tol && fabs(aB) < tol && fabs(aC) < tol;
501 std::shared_ptr<GeomAPI_Dir> aTempDir(
502 isX ? new GeomAPI_Dir(0, 1, 0) : new GeomAPI_Dir(1, 0, 0));
503 std::shared_ptr<GeomAPI_Dir> aYDir(new GeomAPI_Dir(aNormDir->cross(aTempDir)));
504 std::shared_ptr<GeomAPI_Dir> aXDir(new GeomAPI_Dir(aYDir->cross(aNormDir)));
506 std::shared_ptr<GeomDataAPI_Point> anOrigin = std::dynamic_pointer_cast<GeomDataAPI_Point>(
507 aData->attribute(SketchPlugin_Sketch::ORIGIN_ID()));
508 anOrigin->setValue(anOrigPnt);
509 std::shared_ptr<GeomDataAPI_Dir> aNormal = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
510 aData->attribute(SketchPlugin_Sketch::NORM_ID()));
511 aNormal->setValue(aNormDir);
512 std::shared_ptr<GeomDataAPI_Dir> aDirX = std::dynamic_pointer_cast<GeomDataAPI_Dir>(
513 aData->attribute(SketchPlugin_Sketch::DIRX_ID()));
514 aDirX->setValue(aXDir);
515 std::shared_ptr<GeomAPI_Dir> aDir = aPlane->direction();
519 void PartSet_WidgetSketchLabel::onSetPlaneView()
521 std::shared_ptr<GeomAPI_Pln> aPlane = plane();
523 std::shared_ptr<GeomAPI_Dir> aDirection = aPlane->direction();
524 gp_Dir aDir = aDirection->impl<gp_Dir>();
525 if (myViewInverted->isChecked())
527 myWorkshop->viewer()->setViewProjection(aDir.X(), aDir.Y(), aDir.Z(), 0.);
528 PartSet_Module* aModule = dynamic_cast<PartSet_Module*>(myWorkshop->module());
530 aModule->onViewTransformed();