File size: 10,290 Bytes
985c397 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | /***************************************************************************
* Copyright (c) 2011 Juergen Riegel <FreeCAD@juergen-riegel.net> *
* *
* 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 <QAction>
#include <QListWidget>
#include <QMessageBox>
#include <Base/Interpreter.h>
#include <Base/Converter.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <Gui/Selection/Selection.h>
#include <Gui/ViewProvider.h>
#include <Mod/PartDesign/App/FeatureFillet.h>
#include <Mod/Part/App/Attacher.h>
#include <Mod/Part/App/Geometry.h>
#include <Mod/Part/App/Tools.h>
#include <Mod/Part/App/GizmoHelper.h>
#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<PartDesign::Fillet>();
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<std::string> 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<double>(&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<PartDesign::Fillet>()) {
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<PartDesign::Fillet>()) {
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<PartDesign::Fillet>();
if (!fillet || fillet->isError()) {
gizmoContainer->visible = false;
return;
}
Part::TopoShape baseShape = fillet->getBaseTopoShape(true);
std::vector<Part::TopoShape> 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"
|