File size: 17,516 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 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 | /***************************************************************************
* 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 <QApplication>
#include <QKeyEvent>
#include <QListWidgetItem>
#include <QTimer>
#include <App/Application.h>
#include <App/Document.h>
#include <App/DocumentObject.h>
#include <Gui/BitmapFactory.h>
#include <Gui/Command.h>
#include <Gui/Selection/Selection.h>
#include <Gui/Tools.h>
#include <Gui/WaitCursor.h>
#include <Mod/PartDesign/App/Body.h>
#include <Mod/PartDesign/Gui/ReferenceSelection.h>
#include "TaskDressUpParameters.h"
FC_LOG_LEVEL_INIT("PartDesign", true, true)
using namespace PartDesignGui;
using namespace Gui;
/* TRANSLATOR PartDesignGui::TaskDressUpParameters */
TaskDressUpParameters::TaskDressUpParameters(
ViewProviderDressUp* DressUpView,
bool selectEdges,
bool selectFaces,
QWidget* parent
)
: TaskFeatureParameters(DressUpView, parent, DressUpView->featureIcon(), DressUpView->menuName)
, proxy(nullptr)
, deleteAction(nullptr)
, addAllEdgesAction(nullptr)
, allowFaces(selectFaces)
, allowEdges(selectEdges)
, DressUpView(DressUpView)
{
// remember initial transaction ID
App::GetApplication().getActiveTransaction(&transactionID);
selectionMode = none;
}
TaskDressUpParameters::~TaskDressUpParameters()
{
// make sure to remove selection gate in all cases
Gui::Selection().rmvSelectionGate();
}
void TaskDressUpParameters::setupTransaction()
{
if (DressUpView.expired()) {
return;
}
int tid = 0;
App::GetApplication().getActiveTransaction(&tid);
if (tid && tid == transactionID) {
return;
}
// open a transaction if none is active
std::string n("Edit ");
n += DressUpView->getObject()->Label.getValue();
transactionID = App::GetApplication().setActiveTransaction(n.c_str());
}
void TaskDressUpParameters::referenceSelected(const Gui::SelectionChanges& msg, QListWidget* widget)
{
if (strcmp(msg.pDocName, DressUpView->getObject()->getDocument()->getName()) != 0) {
return;
}
Gui::Selection().clearSelection();
PartDesign::DressUp* pcDressUp = DressUpView->getObject<PartDesign::DressUp>();
App::DocumentObject* base = this->getBase();
// TODO: Must we make a copy here instead of assigning to const char* ?
const char* fname = base->getNameInDocument();
if (strcmp(msg.pObjectName, fname) != 0) {
return;
}
const std::string subName(msg.pSubName);
std::vector<std::string> refs = pcDressUp->Base.getSubValues();
if (const auto f = std::ranges::find(refs, subName); f != refs.end()) {
refs.erase(f); // it's in the list. Remove it
removeItemFromListWidget(widget, msg.pSubName);
}
else {
refs.push_back(subName); // not yet in the list so we add it
widget->addItem(QString::fromStdString(msg.pSubName));
}
updateFeature(pcDressUp, refs);
}
void TaskDressUpParameters::addAllEdges(QListWidget* widget)
{
Q_UNUSED(widget)
if (DressUpView.expired()) {
return;
}
PartDesign::DressUp* pcDressUp = DressUpView->getObject<PartDesign::DressUp>();
App::DocumentObject* base = pcDressUp->Base.getValue();
if (!base) {
return;
}
int count = Part::Feature::getTopoShape(
base,
Part::ShapeOption::ResolveLink | Part::ShapeOption::Transform
)
.countSubShapes(TopAbs_EDGE);
auto subValues = pcDressUp->Base.getSubValues(false);
std::size_t len = subValues.size();
for (int i = 0; i < count; ++i) {
std::string name = "Edge" + std::to_string(i + 1);
if (std::find(subValues.begin(), subValues.begin() + len, name) == subValues.begin() + len) {
subValues.push_back(name);
}
}
if (subValues.size() == len) {
return;
}
try {
setupTransaction();
pcDressUp->Base.setValue(base, subValues);
}
catch (Base::Exception& e) {
e.reportException();
}
}
void TaskDressUpParameters::deleteRef(QListWidget* widget)
{
// delete any selections since the reference(s) being deleted might be highlighted
Gui::Selection().clearSelection();
// get the list of items to be deleted
QList<QListWidgetItem*> selectedList = widget->selectedItems();
PartDesign::DressUp* pcDressUp = DressUpView->getObject<PartDesign::DressUp>();
std::vector<std::string> refs = pcDressUp->Base.getSubValues();
// delete the selection backwards to assure the list index keeps valid for the deletion
QSignalBlocker block(widget);
for (int i = selectedList.count() - 1; i > -1; i--) {
// the ref index is the same as the listWidgetReferences index
// so we can erase using the row number of the element to be deleted
int rowNumber = widget->row(selectedList.at(i));
refs.erase(refs.begin() + rowNumber);
widget->model()->removeRow(rowNumber);
}
updateFeature(pcDressUp, refs);
}
void TaskDressUpParameters::updateFeature(
PartDesign::DressUp* pcDressUp,
const std::vector<std::string>& refs
)
{
if (selectionMode == refSel) {
DressUpView->highlightReferences(false);
}
setupTransaction();
pcDressUp->Base.setValue(pcDressUp->Base.getValue(), refs);
pcDressUp->recomputeFeature();
if (selectionMode == refSel) {
DressUpView->highlightReferences(true);
}
else {
hideOnError();
}
}
void TaskDressUpParameters::onButtonRefSel(bool checked)
{
setSelectionMode(checked ? refSel : none);
}
void TaskDressUpParameters::doubleClicked(QListWidgetItem* item)
{
// executed when the user double-clicks on any item in the list
// shows the fillets as they are -> useful to switch out of selection mode
Q_UNUSED(item)
wasDoubleClicked = true;
// assure we are not in selection mode
setSelectionMode(none);
// enable next possible single-click event after double-click time passed
QTimer::singleShot(
QApplication::doubleClickInterval(),
this,
&TaskDressUpParameters::itemClickedTimeout
);
}
void TaskDressUpParameters::setSelection(QListWidgetItem* current)
{
// executed when the user selected an item in the list (but double-clicked it)
// highlights the currently selected item
if (current == nullptr) {
setSelectionMode(none);
return;
}
if (!wasDoubleClicked) {
// we treat it as single-click event once the QApplication double-click time is passed
QTimer::singleShot(
QApplication::doubleClickInterval(),
this,
&TaskDressUpParameters::itemClickedTimeout
);
// name of the item
std::string subName = current->text().toStdString();
// get the document name
std::string docName = DressUpView->getObject()->getDocument()->getName();
// get the name of the body we are in
Part::BodyBase* body = PartDesign::Body::findBodyOf(DressUpView->getObject());
if (body) {
std::string objName = body->getNameInDocument();
// Enter selection mode
if (selectionMode == none) {
setSelectionMode(refSel);
}
else {
Gui::Selection().clearSelection();
}
// highlight the selected item
bool block = this->blockSelection(true);
tryAddSelection(docName, objName, subName);
this->blockSelection(block);
}
}
}
void TaskDressUpParameters::tryAddSelection(
const std::string& doc,
const std::string& obj,
const std::string& sub
)
{
try {
Gui::Selection().addSelection(doc.c_str(), obj.c_str(), sub.c_str(), 0, 0, 0);
}
catch (const Base::Exception& e) {
e.reportException();
}
catch (const Standard_Failure& e) {
Base::Console().error("OCC error: %s\n", e.GetMessageString());
}
}
QString TaskDressUpParameters::startSelectionLabel()
{
return tr("Select");
}
QString TaskDressUpParameters::stopSelectionLabel()
{
return tr("Confirm Selection");
}
void TaskDressUpParameters::itemClickedTimeout()
{
// executed after double-click time passed
wasDoubleClicked = false;
}
void TaskDressUpParameters::createAddAllEdgesAction(QListWidget* parentList)
{
// creates a context menu, a shortcut for it and connects it to a slot function
addAllEdgesAction = new QAction(tr("Add All Edges"), this);
addAllEdgesAction->setShortcut(QKeySequence(QStringLiteral("Ctrl+Shift+A")));
// display shortcut behind the context menu entry
addAllEdgesAction->setShortcutVisibleInContextMenu(true);
parentList->addAction(addAllEdgesAction);
addAllEdgesAction->setStatusTip(
tr("Adds all edges to the list box (only when in add selection mode)")
);
parentList->setContextMenuPolicy(Qt::ActionsContextMenu);
}
void TaskDressUpParameters::createDeleteAction(QListWidget* parentList)
{
// creates a context menu, a shortcut for it and connects it to a slot function
deleteAction = new QAction(tr("Remove"), this);
deleteAction->setShortcut(Gui::QtTools::deleteKeySequence());
// display shortcut behind the context menu entry
deleteAction->setShortcutVisibleInContextMenu(true);
parentList->addAction(deleteAction);
parentList->setContextMenuPolicy(Qt::ActionsContextMenu);
}
bool TaskDressUpParameters::event(QEvent* event)
{
if (event->type() == QEvent::ShortcutOverride) {
QKeyEvent* kevent = static_cast<QKeyEvent*>(event); // NOLINT
if (deleteAction && Gui::QtTools::matches(kevent, deleteAction->shortcut())) {
kevent->accept();
return true;
}
if (addAllEdgesAction && Gui::QtTools::matches(kevent, addAllEdgesAction->shortcut())) {
kevent->accept();
return true;
}
}
return TaskBox::event(event);
}
void TaskDressUpParameters::keyPressEvent(QKeyEvent* ke)
{
if (deleteAction && deleteAction->isEnabled()
&& Gui::QtTools::matches(ke, deleteAction->shortcut())) {
deleteAction->trigger();
return;
}
if (addAllEdgesAction && addAllEdgesAction->isEnabled()
&& Gui::QtTools::matches(ke, addAllEdgesAction->shortcut())) {
addAllEdgesAction->trigger();
return;
}
TaskBox::keyPressEvent(ke);
}
const std::vector<std::string> TaskDressUpParameters::getReferences() const
{
PartDesign::DressUp* pcDressUp = DressUpView->getObject<PartDesign::DressUp>();
std::vector<std::string> result = pcDressUp->Base.getSubValues();
return result;
}
// TODO: This code is identical with TaskTransformedParameters::removeItemFromListWidget()
void TaskDressUpParameters::removeItemFromListWidget(QListWidget* widget, const char* itemstr)
{
QList<QListWidgetItem*> items = widget->findItems(QString::fromLatin1(itemstr), Qt::MatchExactly);
if (!items.empty()) {
for (auto item : items) {
QListWidgetItem* it = widget->takeItem(widget->row(item));
delete it;
}
}
}
void TaskDressUpParameters::hideOnError()
{
App::DocumentObject* dressup = DressUpView->getObject();
DressUpView->setErrorState(dressup->isError());
}
ViewProviderDressUp* TaskDressUpParameters::getDressUpView() const
{
return DressUpView.expired() ? nullptr : DressUpView.get();
}
Part::Feature* TaskDressUpParameters::getBase() const
{
if (ViewProviderDressUp* vp = getDressUpView()) {
auto dressUp = vp->getObject<PartDesign::DressUp>();
// Unlikely but this may throw an exception in case we are started to edit an object which
// base feature was deleted. This exception will be likely unhandled inside the dialog and
// pass upper. But an error message inside the report view is better than a SEGFAULT.
// Generally this situation should be prevented in ViewProviderDressUp::setEdit()
return dressUp->getBaseObject();
}
return nullptr;
}
void TaskDressUpParameters::setSelectionMode(selectionModes mode)
{
if (DressUpView.expired()) {
return;
}
selectionMode = mode;
setButtons(mode);
if (mode == none) {
Gui::Selection().rmvSelectionGate();
// remove any highlights and selections
DressUpView->highlightReferences(false);
if (previouslyShownViewProvider != nullptr) {
// restore the previously shown view provider
previouslyShownViewProvider->show();
previouslyShownViewProvider = nullptr;
}
}
else {
AllowSelectionFlags allow;
allow.setFlag(AllowSelection::EDGE, allowEdges);
allow.setFlag(AllowSelection::FACE, allowFaces);
Gui::Selection().addSelectionGate(new ReferenceSelection(this->getBase(), allow));
DressUpView->highlightReferences(true);
// selection must come from the previous feature, we also need to remember the currently
// shown so we can restore it later
previouslyShownViewProvider = DressUpView->getBodyViewProvider()->getShownViewProvider();
DressUpView->showPreviousFeature(true);
}
Gui::Selection().clearSelection();
}
//**************************************************************************
//**************************************************************************
// TaskDialog
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
TaskDlgDressUpParameters::TaskDlgDressUpParameters(ViewProviderDressUp* DressUpView)
: TaskDlgFeatureParameters(DressUpView)
, parameter(nullptr)
{
assert(DressUpView);
auto pcDressUp = DressUpView->getObject<PartDesign::DressUp>();
auto base = pcDressUp->Base.getValue();
std::vector<std::string> newSubList;
bool changed = false;
auto& shadowSubs = pcDressUp->Base.getShadowSubs();
for (auto& shadowSub : shadowSubs) {
auto displayName = shadowSub.oldName;
// If there is a missing tag on the shadow sub, take a guess at a new name.
if (boost::starts_with(shadowSub.oldName, Data::MISSING_PREFIX)) {
Part::Feature::guessNewLink(displayName, base, shadowSub.newName.c_str());
newSubList.emplace_back(displayName);
changed = true;
}
}
if (changed) {
pcDressUp->Base.setValue(base, newSubList);
}
}
TaskDlgDressUpParameters::~TaskDlgDressUpParameters() = default;
//==== calls from the TaskView ===============================================================
bool TaskDlgDressUpParameters::accept()
{
getViewObject<ViewProviderDressUp>()->highlightReferences(false);
std::vector<std::string> refs = parameter->getReferences();
std::stringstream str;
str << Gui::Command::getObjectCmd(getObject()) << ".Base = ("
<< Gui::Command::getObjectCmd(parameter->getBase()) << ",[";
for (const auto& ref : refs) {
str << "\"" << ref << "\",";
}
str << "])";
Gui::Command::runCommand(Gui::Command::Doc, str.str().c_str());
return TaskDlgFeatureParameters::accept();
}
bool TaskDlgDressUpParameters::reject()
{
getViewObject<ViewProviderDressUp>()->highlightReferences(false);
return TaskDlgFeatureParameters::reject();
}
#include "moc_TaskDressUpParameters.cpp"
|