File size: 14,362 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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | /***************************************************************************
* Copyright (c) 2015 Stefan Tröger <stefantroeger@gmx.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 <App/Application.h>
#include <App/Document.h>
#include <Gui/Application.h>
#include <Gui/CommandT.h>
#include <Gui/Document.h>
#include <Gui/Selection/Selection.h>
#include <Gui/Tools.h>
#include <Mod/PartDesign/App/FeatureLoft.h>
#include "ui_TaskLoftParameters.h"
#include "TaskLoftParameters.h"
#include "TaskSketchBasedParameters.h"
Q_DECLARE_METATYPE(App::PropertyLinkSubList::SubSet)
using namespace PartDesignGui;
using namespace Gui;
/* TRANSLATOR PartDesignGui::TaskLoftParameters */
TaskLoftParameters::TaskLoftParameters(ViewProviderLoft* LoftView, bool /*newObj*/, QWidget* parent)
: TaskSketchBasedParameters(LoftView, parent, "PartDesign_AdditiveLoft", tr("Loft Parameters"))
, ui(new Ui_TaskLoftParameters)
{
// we need a separate container widget to add all controls to
proxy = new QWidget(this);
ui->setupUi(proxy);
QMetaObject::connectSlotsByName(this);
// clang-format off
connect(ui->buttonProfileBase, &QToolButton::toggled,
this, &TaskLoftParameters::onProfileButton);
connect(ui->buttonRefAdd, &QToolButton::toggled,
this, &TaskLoftParameters::onRefButtonAdd);
connect(ui->buttonRefRemove, &QToolButton::toggled,
this, &TaskLoftParameters::onRefButtonRemove);
connect(ui->checkBoxRuled, &QCheckBox::toggled,
this, &TaskLoftParameters::onRuled);
connect(ui->checkBoxClosed, &QCheckBox::toggled,
this, &TaskLoftParameters::onClosed);
connect(ui->checkBoxUpdateView, &QCheckBox::toggled,
this, &TaskLoftParameters::onUpdateView);
// clang-format on
// Create context menu
QAction* remove = new QAction(tr("Remove"), this);
remove->setShortcut(Gui::QtTools::deleteKeySequence());
// display shortcut behind the context menu entry
remove->setShortcutVisibleInContextMenu(true);
ui->listWidgetReferences->addAction(remove);
ui->listWidgetReferences->setContextMenuPolicy(Qt::ActionsContextMenu);
connect(remove, &QAction::triggered, this, &TaskLoftParameters::onDeleteSection);
connect(
ui->listWidgetReferences->model(),
&QAbstractListModel::rowsMoved,
this,
&TaskLoftParameters::indexesMoved
);
this->groupLayout()->addWidget(proxy);
// Temporarily prevent unnecessary feature recomputes
const auto childs = proxy->findChildren<QWidget*>();
for (QWidget* child : childs) {
child->blockSignals(true);
}
// add the profiles
PartDesign::Loft* loft = LoftView->getObject<PartDesign::Loft>();
App::DocumentObject* profile = loft->Profile.getValue();
if (profile) {
Gui::Application::Instance->showViewProvider(profile);
// TODO: if it is a single vertex of a sketch, use that subshape's name
QString label = make2DLabel(profile, loft->Profile.getSubValues());
ui->profileBaseEdit->setText(label);
}
for (auto& subSet : loft->Sections.getSubListValues()) {
Gui::Application::Instance->showViewProvider(subSet.first);
// TODO: if it is a single vertex of a sketch, use that subshape's name
QString label = make2DLabel(subSet.first, subSet.second);
QListWidgetItem* item = new QListWidgetItem();
item->setText(label);
item->setData(Qt::UserRole, QVariant::fromValue(subSet));
ui->listWidgetReferences->addItem(item);
}
// get options
ui->checkBoxRuled->setChecked(loft->Ruled.getValue());
ui->checkBoxClosed->setChecked(loft->Closed.getValue());
// activate and de-activate dialog elements as appropriate
for (QWidget* child : childs) {
child->blockSignals(false);
}
updateUI();
}
TaskLoftParameters::~TaskLoftParameters() = default;
void TaskLoftParameters::updateUI()
{
// we must assure the changed loft is kept visible on section changes,
// see https://forum.freecad.org/viewtopic.php?f=3&t=63252
auto loft = getObject<PartDesign::Loft>();
if (loft) {
auto view = getViewObject();
view->makeTemporaryVisible(!loft->Sections.getValues().empty());
}
}
void TaskLoftParameters::onSelectionChanged(const Gui::SelectionChanges& msg)
{
if (selectionMode == none) {
return;
}
if (msg.Type == Gui::SelectionChanges::AddSelection) {
if (referenceSelected(msg)) {
App::Document* document = App::GetApplication().getDocument(msg.pDocName);
App::DocumentObject* object = document ? document->getObject(msg.pObjectName) : nullptr;
if (object) {
// TODO: if it is a single vertex of a sketch, use that subshape's name
QString label = make2DLabel(object, {msg.pSubName});
if (selectionMode == refProfile) {
ui->profileBaseEdit->setText(label);
}
else if (selectionMode == refAdd) {
QListWidgetItem* item = new QListWidgetItem();
item->setText(label);
item->setData(
Qt::UserRole,
QVariant::fromValue(
std::make_pair(object, std::vector<std::string>(1, msg.pSubName))
)
);
ui->listWidgetReferences->addItem(item);
}
else if (selectionMode == refRemove) {
removeFromListWidget(ui->listWidgetReferences, label);
}
}
clearButtons();
recomputeFeature();
}
clearButtons();
exitSelectionMode();
updateUI();
}
}
bool TaskLoftParameters::referenceSelected(const Gui::SelectionChanges& msg) const
{
if (msg.Type == Gui::SelectionChanges::AddSelection && selectionMode != none) {
if (strcmp(msg.pDocName, getObject()->getDocument()->getName()) != 0) {
return false;
}
// not allowed to reference ourself
const char* fname = getObject()->getNameInDocument();
if (strcmp(msg.pObjectName, fname) == 0) {
return false;
}
// every selection needs to be a profile in itself, hence currently only full objects are
// supported, not individual edges of a part
// change the references
auto loft = getObject<PartDesign::Loft>();
App::Document* doc = loft->getDocument();
App::DocumentObject* obj = doc->getObject(msg.pObjectName);
if (selectionMode == refProfile) {
auto view = getViewObject<ViewProviderLoft>();
view->highlightReferences(ViewProviderLoft::Profile, false);
loft->Profile.setValue(obj, {msg.pSubName});
return true;
}
if (selectionMode == refAdd || selectionMode == refRemove) {
// now check the sections
std::vector<App::DocumentObject*> refs = loft->Sections.getValues();
const auto f = std::ranges::find(refs, obj);
if (selectionMode == refAdd) {
if (f != refs.end()) {
return false; // duplicate selection
}
loft->Sections.addValue(obj, {msg.pSubName});
}
else if (selectionMode == refRemove) {
if (f == refs.end()) {
return false;
}
loft->Sections.removeValue(obj);
}
return true;
}
}
return false;
}
void TaskLoftParameters::removeFromListWidget(QListWidget* widget, QString name)
{
QList<QListWidgetItem*> items = widget->findItems(name, Qt::MatchExactly);
if (!items.empty()) {
for (auto it : items) {
QListWidgetItem* item = widget->takeItem(widget->row(it));
delete item;
}
}
}
void TaskLoftParameters::onDeleteSection()
{
// Delete the selected profile
int row = ui->listWidgetReferences->currentRow();
QListWidgetItem* item = ui->listWidgetReferences->takeItem(row);
if (item) {
QByteArray data(
item->data(Qt::UserRole).value<App::PropertyLinkSubList::SubSet>().first->getNameInDocument()
);
delete item;
// search inside the list of sections
if (const auto loft = getObject<PartDesign::Loft>()) {
std::vector<App::DocumentObject*> refs = loft->Sections.getValues();
App::DocumentObject* obj = loft->getDocument()->getObject(data.constData());
if (const auto f = std::ranges::find(refs, obj); f != refs.end()) {
loft->Sections.removeValue(obj);
recomputeFeature();
updateUI();
}
}
}
}
void TaskLoftParameters::indexesMoved()
{
QAbstractItemModel* model = qobject_cast<QAbstractItemModel*>(sender());
if (!model) {
return;
}
if (auto loft = getObject<PartDesign::Loft>()) {
auto originals = loft->Sections.getSubListValues();
int rows = model->rowCount();
for (int i = 0; i < rows; i++) {
QModelIndex index = model->index(i, 0);
originals[i] = index.data(Qt::UserRole).value<App::PropertyLinkSubList::SubSet>();
}
loft->Sections.setSubListValues(originals);
recomputeFeature();
updateUI();
}
}
void TaskLoftParameters::clearButtons(const selectionModes notThis)
{
if (notThis != refProfile) {
ui->buttonProfileBase->setChecked(false);
}
if (notThis != refAdd) {
ui->buttonRefAdd->setChecked(false);
}
if (notThis != refRemove) {
ui->buttonRefRemove->setChecked(false);
}
}
void TaskLoftParameters::exitSelectionMode()
{
selectionMode = none;
Gui::Selection().clearSelection();
this->blockSelection(true);
}
void TaskLoftParameters::changeEvent(QEvent* /*e*/)
{}
void TaskLoftParameters::onClosed(bool val)
{
if (auto loft = getObject<PartDesign::Loft>()) {
loft->Closed.setValue(val);
recomputeFeature();
}
}
void TaskLoftParameters::onRuled(bool val)
{
if (auto loft = getObject<PartDesign::Loft>()) {
loft->Ruled.setValue(val);
recomputeFeature();
}
}
void TaskLoftParameters::setSelectionMode(selectionModes mode, bool checked)
{
if (checked) {
clearButtons(mode);
Gui::Selection().clearSelection();
selectionMode = mode;
this->blockSelection(false);
}
else {
Gui::Selection().clearSelection();
selectionMode = none;
}
auto view = getViewObject<ViewProviderLoft>();
view->highlightReferences(ViewProviderLoft::Both, checked);
}
void TaskLoftParameters::onProfileButton(bool checked)
{
setSelectionMode(refProfile, checked);
}
void TaskLoftParameters::onRefButtonAdd(bool checked)
{
setSelectionMode(refAdd, checked);
}
void TaskLoftParameters::onRefButtonRemove(bool checked)
{
setSelectionMode(refRemove, checked);
}
//**************************************************************************
//**************************************************************************
// TaskDialog
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TaskDlgLoftParameters::TaskDlgLoftParameters(ViewProviderLoft* LoftView, bool newObj)
: TaskDlgSketchBasedParameters(LoftView)
{
assert(LoftView);
parameter = new TaskLoftParameters(LoftView, newObj);
Content.push_back(parameter);
Content.push_back(preview);
}
TaskDlgLoftParameters::~TaskDlgLoftParameters() = default;
bool TaskDlgLoftParameters::accept()
{
if (auto loft = getObject<PartDesign::Loft>()) {
getViewObject<ViewProviderLoft>()->highlightReferences(ViewProviderLoft::Both, false);
// First verify that the loft can be built and then hide the sections as otherwise
// they will remain hidden if the loft's recompute fails
if (TaskDlgSketchBasedParameters::accept()) {
for (App::DocumentObject* obj : loft->Sections.getValues()) {
Gui::cmdAppObjectHide(obj);
}
return true;
}
}
return false;
}
//==== calls from the TaskView ===============================================================
#include "moc_TaskLoftParameters.cpp"
|