/*************************************************************************** * Copyright (c) 2011 Juergen Riegel * * * * This file is part of the FreeCAD CAx development system. * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Library General Public * * License as published by the Free Software Foundation; either * * version 2 of the License, or (at your option) any later version. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Library General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this library; see the file COPYING.LIB. If not, * * write to the Free Software Foundation, Inc., 59 Temple Place, * * Suite 330, Boston, MA 02111-1307, USA * * * ***************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ui_TaskFilletParameters.h" #include "TaskFilletParameters.h" using namespace PartDesignGui; using namespace Gui; /* TRANSLATOR PartDesignGui::TaskFilletParameters */ TaskFilletParameters::TaskFilletParameters(ViewProviderDressUp* DressUpView, QWidget* parent) : TaskDressUpParameters(DressUpView, true, true, parent) , ui(new Ui_TaskFilletParameters) { // we need a separate container widget to add all controls to proxy = new QWidget(this); ui->setupUi(proxy); this->groupLayout()->addWidget(proxy); PartDesign::Fillet* pcFillet = DressUpView->getObject(); bool useAllEdges = pcFillet->UseAllEdges.getValue(); ui->checkBoxUseAllEdges->setChecked(useAllEdges); ui->buttonRefSel->setEnabled(!useAllEdges); ui->listWidgetReferences->setEnabled(!useAllEdges); double r = pcFillet->Radius.getValue(); ui->filletRadius->setUnit(Base::Unit::Length); ui->filletRadius->setValue(r); ui->filletRadius->setMinimum(0); ui->filletRadius->selectNumber(); ui->filletRadius->bind(pcFillet->Radius); QMetaObject::invokeMethod(ui->filletRadius, "setFocus", Qt::QueuedConnection); std::vector strings = pcFillet->Base.getSubValues(); for (const auto& string : strings) { ui->listWidgetReferences->addItem(QString::fromStdString(string)); } QMetaObject::connectSlotsByName(this); // clang-format off connect(ui->filletRadius, qOverload(&Gui::QuantitySpinBox::valueChanged), this, &TaskFilletParameters::onLengthChanged); connect(ui->buttonRefSel, &QToolButton::toggled, this, &TaskFilletParameters::onButtonRefSel); connect(ui->checkBoxUseAllEdges, &QToolButton::toggled, this, &TaskFilletParameters::onCheckBoxUseAllEdgesToggled); // Create context menu createDeleteAction(ui->listWidgetReferences); connect(deleteAction, &QAction::triggered, this, &TaskFilletParameters::onRefDeleted); createAddAllEdgesAction(ui->listWidgetReferences); connect(addAllEdgesAction, &QAction::triggered, this, &TaskFilletParameters::onAddAllEdges); connect(ui->listWidgetReferences, &QListWidget::currentItemChanged, this, &TaskFilletParameters::setSelection); connect(ui->listWidgetReferences, &QListWidget::itemClicked, this, &TaskFilletParameters::setSelection); connect(ui->listWidgetReferences, &QListWidget::itemDoubleClicked, this, &TaskFilletParameters::doubleClicked); // clang-format on if (strings.empty()) { setSelectionMode(refSel); } else { hideOnError(); } setupGizmos(DressUpView); } void TaskFilletParameters::onSelectionChanged(const Gui::SelectionChanges& msg) { // executed when the user selected something in the CAD object // adds/deletes the selection accordingly if (msg.Type == Gui::SelectionChanges::AddSelection) { if (selectionMode == refSel) { referenceSelected(msg, ui->listWidgetReferences); } } else if (msg.Type == Gui::SelectionChanges::ClrSelection) { // TODO: the gizmo position should be only recalculated when the feature associated // with the gizmo is removed from the list setGizmoPositions(); } } void TaskFilletParameters::onCheckBoxUseAllEdgesToggled(bool checked) { if (auto fillet = getObject()) { if (checked) { setSelectionMode(none); } ui->buttonRefSel->setEnabled(!checked); ui->listWidgetReferences->setEnabled(!checked); fillet->UseAllEdges.setValue(checked); fillet->recomputeFeature(); } } void TaskFilletParameters::setButtons(const selectionModes mode) { ui->buttonRefSel->setChecked(mode == refSel); ui->buttonRefSel->setText(mode == refSel ? stopSelectionLabel() : startSelectionLabel()); } void TaskFilletParameters::onRefDeleted() { TaskDressUpParameters::deleteRef(ui->listWidgetReferences); setGizmoPositions(); } void TaskFilletParameters::onAddAllEdges() { TaskDressUpParameters::addAllEdges(ui->listWidgetReferences); } void TaskFilletParameters::onLengthChanged(double len) { if (auto fillet = getObject()) { setSelectionMode(none); setupTransaction(); fillet->Radius.setValue(len); fillet->recomputeFeature(); // hide the fillet if there was a computation error hideOnError(); } } double TaskFilletParameters::getLength() const { return ui->filletRadius->value().getValue(); } TaskFilletParameters::~TaskFilletParameters() { try { Gui::Selection().clearSelection(); Gui::Selection().rmvSelectionGate(); } catch (const Py::Exception&) { Base::PyException e; // extract the Python error text e.reportException(); } } void TaskFilletParameters::changeEvent(QEvent* e) { TaskBox::changeEvent(e); if (e->type() == QEvent::LanguageChange) { ui->retranslateUi(proxy); } } void TaskFilletParameters::apply() { ui->filletRadius->apply(); // Alert user if he created an empty feature if (ui->listWidgetReferences->count() == 0) { std::string text = tr("Empty fillet created!").toStdString(); Base::Console().warning("%s\n", text.c_str()); } } void TaskFilletParameters::setupGizmos(ViewProviderDressUp* vp) { if (!GizmoContainer::isEnabled()) { return; } radiusGizmo = new Gui::LinearGizmo(ui->filletRadius); radiusGizmo2 = new Gui::LinearGizmo(ui->filletRadius); gizmoContainer = GizmoContainer::create({radiusGizmo, radiusGizmo2}, vp); setGizmoPositions(); } void TaskFilletParameters::setGizmoPositions() { if (!gizmoContainer) { return; } auto fillet = getObject(); if (!fillet || fillet->isError()) { gizmoContainer->visible = false; return; } Part::TopoShape baseShape = fillet->getBaseTopoShape(true); std::vector shapes = fillet->getContinuousEdges(baseShape); if (shapes.size() == 0) { gizmoContainer->visible = false; return; } gizmoContainer->visible = true; // Attach the arrow to the first edge Part::TopoShape edge = shapes[0]; auto [face1, face2] = getAdjacentFacesFromEdge(edge, baseShape); DraggerPlacementProps props1 = getDraggerPlacementFromEdgeAndFace(edge, face1); radiusGizmo->Gizmo::setDraggerPlacement(props1.position, props1.dir); DraggerPlacementProps props2 = getDraggerPlacementFromEdgeAndFace(edge, face2); radiusGizmo2->Gizmo::setDraggerPlacement(props2.position, props2.dir); // The dragger length won't be equal to the radius if the two faces // are not orthogonal so this correction is needed double angle = props1.dir.GetAngle(props2.dir); double correction = 1 / std::tan(angle / 2); radiusGizmo->setMultFactor(correction); radiusGizmo2->setMultFactor(correction); } //************************************************************************** //************************************************************************** // TaskDialog //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ TaskDlgFilletParameters::TaskDlgFilletParameters(ViewProviderFillet* DressUpView) : TaskDlgDressUpParameters(DressUpView) { parameter = new TaskFilletParameters(DressUpView); Content.push_back(parameter); Content.push_back(preview); } TaskDlgFilletParameters::~TaskDlgFilletParameters() = default; //==== calls from the TaskView =============================================================== bool TaskDlgFilletParameters::accept() { auto obj = getObject(); if (!obj->isError()) { getViewObject()->showPreviousFeature(false); } parameter->apply(); return TaskDlgDressUpParameters::accept(); } #include "moc_TaskFilletParameters.cpp"