File size: 12,612 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 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | /***************************************************************************
* Copyright (c) 2012 Jan Rheinländer *
* <jrheinlaender@users.sourceforge.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 <QKeyEvent>
#include <QListWidget>
#include <QMessageBox>
#include <Base/Interpreter.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <Gui/Command.h>
#include <Gui/Selection/Selection.h>
#include <Gui/ViewProvider.h>
#include <Gui/Inventor/Draggers/Gizmo.h>
#include <Mod/PartDesign/App/FeatureDraft.h>
#include <Mod/PartDesign/Gui/ReferenceSelection.h>
#include <Mod/Part/App/GizmoHelper.h>
#include "ui_TaskDraftParameters.h"
#include "TaskDraftParameters.h"
using namespace PartDesignGui;
using namespace Gui;
/* TRANSLATOR PartDesignGui::TaskDraftParameters */
TaskDraftParameters::TaskDraftParameters(ViewProviderDressUp* DressUpView, QWidget* parent)
: TaskDressUpParameters(DressUpView, false, true, parent)
, ui(new Ui_TaskDraftParameters)
{
// we need a separate container widget to add all controls to
proxy = new QWidget(this);
ui->setupUi(proxy);
this->groupLayout()->addWidget(proxy);
PartDesign::Draft* pcDraft = DressUpView->getObject<PartDesign::Draft>();
double a = pcDraft->Angle.getValue();
ui->draftAngle->setMinimum(pcDraft->Angle.getMinimum());
ui->draftAngle->setMaximum(pcDraft->Angle.getMaximum());
ui->draftAngle->setValue(a);
ui->draftAngle->selectAll();
QMetaObject::invokeMethod(ui->draftAngle, "setFocus", Qt::QueuedConnection);
// Bind input fields to properties
ui->draftAngle->bind(pcDraft->Angle);
bool r = pcDraft->Reversed.getValue();
ui->checkReverse->setChecked(r);
std::vector<std::string> strings = pcDraft->Base.getSubValues();
for (const auto& string : strings) {
ui->listWidgetReferences->addItem(QString::fromStdString(string));
}
QMetaObject::connectSlotsByName(this);
// clang-format off
connect(ui->draftAngle, qOverload<double>(&Gui::QuantitySpinBox::valueChanged),
this, &TaskDraftParameters::onAngleChanged);
connect(ui->checkReverse, &QCheckBox::toggled,
this, &TaskDraftParameters::onReversedChanged);
connect(ui->buttonRefSel, &QToolButton::toggled,
this, &TaskDraftParameters::onButtonRefSel);
connect(ui->buttonPlane, &QToolButton::toggled,
this, &TaskDraftParameters::onButtonPlane);
connect(ui->buttonLine, &QToolButton::toggled,
this, &TaskDraftParameters::onButtonLine);
// Create context menu
createDeleteAction(ui->listWidgetReferences);
connect(deleteAction, &QAction::triggered, this, &TaskDraftParameters::onRefDeleted);
connect(ui->listWidgetReferences, &QListWidget::currentItemChanged,
this, &TaskDraftParameters::setSelection);
connect(ui->listWidgetReferences, &QListWidget::itemClicked,
this, &TaskDraftParameters::setSelection);
connect(ui->listWidgetReferences, &QListWidget::itemDoubleClicked,
this, &TaskDraftParameters::doubleClicked);
// clang-format on
App::DocumentObject* ref = pcDraft->NeutralPlane.getValue();
strings = pcDraft->NeutralPlane.getSubValues();
ui->linePlane->setText(getRefStr(ref, strings));
ref = pcDraft->PullDirection.getValue();
strings = pcDraft->PullDirection.getSubValues();
ui->lineLine->setText(getRefStr(ref, strings));
if (strings.size() == 0) {
setSelectionMode(refSel);
}
else {
hideOnError();
}
}
void TaskDraftParameters::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 (selectionMode == plane) {
auto pcDraft = getObject<PartDesign::Draft>();
std::vector<std::string> planes;
App::DocumentObject* selObj {};
getReferencedSelection(pcDraft, msg, selObj, planes);
if (!selObj) {
return;
}
setupTransaction();
pcDraft->NeutralPlane.setValue(selObj, planes);
ui->linePlane->setText(getRefStr(selObj, planes));
pcDraft->getDocument()->recomputeFeature(pcDraft);
// highlight existing references for possible further selections
getDressUpView()->highlightReferences(true);
// hide the draft if there was a computation error
hideOnError();
}
else if (selectionMode == line) {
auto pcDraft = getObject<PartDesign::Draft>();
std::vector<std::string> edges;
App::DocumentObject* selObj;
getReferencedSelection(pcDraft, msg, selObj, edges);
if (!selObj) {
return;
}
setupTransaction();
pcDraft->PullDirection.setValue(selObj, edges);
ui->lineLine->setText(getRefStr(selObj, edges));
pcDraft->getDocument()->recomputeFeature(pcDraft);
// highlight existing references for possible further selections
getDressUpView()->highlightReferences(true);
// hide the draft if there was a computation error
hideOnError();
}
}
}
void TaskDraftParameters::setButtons(const selectionModes mode)
{
ui->buttonRefSel->setText(mode == refSel ? stopSelectionLabel() : startSelectionLabel());
ui->buttonRefSel->setChecked(mode == refSel);
ui->buttonLine->setChecked(mode == line);
ui->buttonPlane->setChecked(mode == plane);
}
void TaskDraftParameters::onButtonPlane(bool checked)
{
if (checked) {
setButtons(plane);
getViewObject()->showPreviousFeature(true);
selectionMode = plane;
Gui::Selection().clearSelection();
Gui::Selection().addSelectionGate(new ReferenceSelection(
this->getBase(),
AllowSelection::EDGE | AllowSelection::FACE | AllowSelection::PLANAR
));
}
}
void TaskDraftParameters::onButtonLine(bool checked)
{
if (checked) {
setButtons(line);
getViewObject()->showPreviousFeature(true);
selectionMode = line;
Gui::Selection().clearSelection();
Gui::Selection().addSelectionGate(
new ReferenceSelection(this->getBase(), AllowSelection::EDGE | AllowSelection::PLANAR)
);
}
}
void TaskDraftParameters::onRefDeleted()
{
TaskDressUpParameters::deleteRef(ui->listWidgetReferences);
}
void TaskDraftParameters::getPlane(App::DocumentObject*& obj, std::vector<std::string>& sub) const
{
sub = std::vector<std::string>(1, "");
QStringList parts = ui->linePlane->text().split(QChar::fromLatin1(':'));
obj = getObject()->getDocument()->getObject(parts[0].toStdString().c_str());
if (parts.size() > 1) {
sub[0] = parts[1].toStdString();
}
}
void TaskDraftParameters::getLine(App::DocumentObject*& obj, std::vector<std::string>& sub) const
{
sub = std::vector<std::string>(1, "");
QStringList parts = ui->lineLine->text().split(QChar::fromLatin1(':'));
obj = getObject()->getDocument()->getObject(parts[0].toStdString().c_str());
if (parts.size() > 1) {
sub[0] = parts[1].toStdString();
}
}
void TaskDraftParameters::onAngleChanged(double angle)
{
if (auto draft = getObject<PartDesign::Draft>()) {
setButtons(none);
setupTransaction();
draft->Angle.setValue(angle);
draft->recomputeFeature();
// hide the draft if there was a computation error
hideOnError();
}
}
double TaskDraftParameters::getAngle() const
{
return ui->draftAngle->value().getValue();
}
void TaskDraftParameters::onReversedChanged(const bool on)
{
if (auto draft = getObject<PartDesign::Draft>()) {
setButtons(none);
setupTransaction();
draft->Reversed.setValue(on);
draft->recomputeFeature();
// hide the draft if there was a computation error
hideOnError();
}
}
bool TaskDraftParameters::getReversed() const
{
return ui->checkReverse->isChecked();
}
TaskDraftParameters::~TaskDraftParameters()
{
try {
Gui::Selection().clearSelection();
Gui::Selection().rmvSelectionGate();
}
catch (const Py::Exception&) {
Base::PyException e; // extract the Python error text
e.reportException();
}
}
void TaskDraftParameters::changeEvent(QEvent* e)
{
TaskBox::changeEvent(e);
if (e->type() == QEvent::LanguageChange) {
ui->retranslateUi(proxy);
}
}
void TaskDraftParameters::apply()
{
// Alert user if he created an empty feature
if (ui->listWidgetReferences->count() == 0) {
Base::Console().warning(tr("Empty draft created!\n").toStdString().c_str());
}
TaskDressUpParameters::apply();
}
//**************************************************************************
//**************************************************************************
// TaskDialog
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TaskDlgDraftParameters::TaskDlgDraftParameters(ViewProviderDraft* DressUpView)
: TaskDlgDressUpParameters(DressUpView)
{
parameter = new TaskDraftParameters(DressUpView);
Content.push_back(parameter);
Content.push_back(preview);
}
TaskDlgDraftParameters::~TaskDlgDraftParameters() = default;
//==== calls from the TaskView ===============================================================
bool TaskDlgDraftParameters::accept()
{
auto tobj = getObject();
if (!tobj->isError()) {
getViewObject()->showPreviousFeature(false);
}
parameter->apply();
std::vector<std::string> strings;
App::DocumentObject* obj;
TaskDraftParameters* draftparameter = static_cast<TaskDraftParameters*>(parameter);
draftparameter->getPlane(obj, strings);
std::string neutralPlane = buildLinkSingleSubPythonStr(obj, strings);
draftparameter->getLine(obj, strings);
std::string pullDirection = buildLinkSingleSubPythonStr(obj, strings);
FCMD_OBJ_CMD(tobj, "Angle = " << draftparameter->getAngle());
FCMD_OBJ_CMD(tobj, "Reversed = " << draftparameter->getReversed());
if (neutralPlane.empty()) {
neutralPlane = "None";
}
FCMD_OBJ_CMD(tobj, "NeutralPlane = " << neutralPlane);
if (pullDirection.empty()) {
pullDirection = "None";
}
FCMD_OBJ_CMD(tobj, "PullDirection = " << pullDirection);
return TaskDlgDressUpParameters::accept();
}
#include "moc_TaskDraftParameters.cpp"
|