| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2008 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
|
| 24 |
#include <QClipboard> |
| 25 |
#include <QDockWidget> |
| 26 |
#include <QKeyEvent> |
| 27 |
#include <QMessageBox> |
| 28 |
#include <QMetaObject> |
| 29 |
#include <QSignalMapper> |
| 30 |
|
| 31 |
|
| 32 |
#include <App/ComplexGeoData.h> |
| 33 |
#include <App/Document.h> |
| 34 |
#include <App/DocumentObjectPy.h> |
| 35 |
#include <App/GeoFeature.h> |
| 36 |
#include <Base/Console.h> |
| 37 |
#include <Base/PlacementPy.h> |
| 38 |
#include <Base/Tools.h> |
| 39 |
#include <Gui/Application.h> |
| 40 |
#include <Gui/Command.h> |
| 41 |
#include <Gui/DockWindowManager.h> |
| 42 |
#include <Gui/Document.h> |
| 43 |
#include <Gui/PythonWrapper.h> |
| 44 |
#include <Gui/Selection/Selection.h> |
| 45 |
#include <Gui/ViewProvider.h> |
| 46 |
#include <Gui/Window.h> |
| 47 |
|
| 48 |
#include "Placement.h" |
| 49 |
#include "ui_Placement.h" |
| 50 |
|
| 51 |
|
| 52 |
using namespace Gui::Dialog; |
| 53 |
namespace sp = std::placeholders; |
| 54 |
|
| 55 |
namespace Gui |
| 56 |
{ |
| 57 |
namespace Dialog |
| 58 |
{ |
| 59 |
class find_placement |
| 60 |
{ |
| 61 |
public: |
| 62 |
explicit find_placement(const std::string& name) |
| 63 |
: propertyname(name) |
| 64 |
{} |
| 65 |
bool operator()(const std::pair<std::string, App::Property*>& elem) const |
| 66 |
{ |
| 67 |
if (elem.first == propertyname) { |
| 68 |
// flag set that property is read-only or hidden |
| 69 |
if (elem.second->testStatus(App::Property::ReadOnly) |
| 70 |
|| elem.second->testStatus(App::Property::Hidden)) { |
| 71 |
return false; |
| 72 |
} |
| 73 |
App::PropertyContainer* parent = elem.second->getContainer(); |
| 74 |
if (parent) { |
| 75 |
// flag set that property is read-only or hidden |
| 76 |
if (parent->isReadOnly(elem.second) || parent->isHidden(elem.second)) { |
| 77 |
return false; |
| 78 |
} |
| 79 |
} |
| 80 |
return elem.second->isDerivedFrom(Base::Type::fromName("App::PropertyPlacement")); |
| 81 |
} |
| 82 |
|
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
static App::PropertyPlacement* getProperty( |
| 87 |
const App::DocumentObject* obj, |
| 88 |
const std::string& propertyName |
| 89 |
) |
| 90 |
{ |
| 91 |
std::map<std::string, App::Property*> props; |
| 92 |
obj->getPropertyMap(props); |
| 93 |
|
| 94 |
// search for the placement property |
| 95 |
std::map<std::string, App::Property*>::iterator jt; |
| 96 |
jt = std::find_if(props.begin(), props.end(), find_placement(propertyName)); |
| 97 |
if (jt != props.end()) { |
| 98 |
return dynamic_cast<App::PropertyPlacement*>(jt->second); |
| 99 |
} |
| 100 |
|
| 101 |
return nullptr; |
| 102 |
} |
| 103 |
|
| 104 |
std::string propertyname; |
| 105 |
}; |
| 106 |
|
| 107 |
} // namespace Dialog |
| 108 |
} // namespace Gui |
| 109 |
|
| 110 |
PlacementHandler::PlacementHandler() |
| 111 |
: propertyName {"Placement"} |
| 112 |
, changeProperty {false} |
| 113 |
, ignoreTransaction {false} |
| 114 |
{ |
| 115 |
setupDocument(); |
| 116 |
} |
| 117 |
|
| 118 |
void PlacementHandler::openTransactionIfNeeded() |
| 119 |
{ |
| 120 |
if (changeProperty) { |
| 121 |
openTransaction(); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
void PlacementHandler::setPropertyName(const std::string& name) |
| 126 |
{ |
| 127 |
propertyName = name; |
| 128 |
// Only with the Placement property it's possible to directly change the Inventor |
| 129 |
// representation. For other placement properties with a different name the standard property |
| 130 |
// handling must be used. |
| 131 |
changeProperty = (propertyName != "Placement"); |
| 132 |
} |
| 133 |
|
| 134 |
void PlacementHandler::setIgnoreTransactions(bool value) |
| 135 |
{ |
| 136 |
ignoreTransaction = value; |
| 137 |
} |
| 138 |
|
| 139 |
void PlacementHandler::setSelection(const std::vector<Gui::SelectionObject>& selection) |
| 140 |
{ |
| 141 |
selectionObjects = selection; |
| 142 |
} |
| 143 |
|
| 144 |
void PlacementHandler::reselectObjects() |
| 145 |
{ |
| 146 |
// we have to clear selection and reselect original object(s) |
| 147 |
// else later on the rotation is applied twice because there will |
| 148 |
// be 2 (vertex) objects in the selection, and even if both are subobjects |
| 149 |
// of the same object the rotation still gets applied twice |
| 150 |
Gui::Selection().clearSelection(); |
| 151 |
// reselect original object that was selected when placement dlg first opened |
| 152 |
for (const auto& it : selectionObjects) { |
| 153 |
Gui::Selection().addSelection(it); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
const App::DocumentObject* PlacementHandler::getFirstOfSelection() const |
| 158 |
{ |
| 159 |
if (!selectionObjects.empty()) { |
| 160 |
return selectionObjects.front().getObject(); |
| 161 |
} |
| 162 |
|
| 163 |
return nullptr; |
| 164 |
} |
| 165 |
|
| 166 |
const std::string& PlacementHandler::getPropertyName() const |
| 167 |
{ |
| 168 |
return propertyName; |
| 169 |
} |
| 170 |
|
| 171 |
void PlacementHandler::appendDocument(const std::string& name) |
| 172 |
{ |
| 173 |
documents.insert(name); |
| 174 |
} |
| 175 |
|
| 176 |
void PlacementHandler::activatedDocument(const std::string& name) |
| 177 |
{ |
| 178 |
appendDocument(name); |
| 179 |
|
| 180 |
if (changeProperty) { |
| 181 |
QMetaObject::invokeMethod(this, "openTransaction", Qt::QueuedConnection); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
void PlacementHandler::openTransaction() |
| 186 |
{ |
| 187 |
App::Document* activeDoc = App::GetApplication().getActiveDocument(); |
| 188 |
if (activeDoc) { |
| 189 |
activeDoc->openTransaction("Placement"); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
void PlacementHandler::revertTransformation() |
| 194 |
{ |
| 195 |
for (const auto& it : documents) { |
| 196 |
Gui::Document* document = Application::Instance->getDocument(it.c_str()); |
| 197 |
if (document) { |
| 198 |
if (!changeProperty) { |
| 199 |
revertTransformationOfViewProviders(document); |
| 200 |
} |
| 201 |
else { |
| 202 |
abortCommandIfActive(document); |
| 203 |
} |
| 204 |
} |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
std::vector<const App::DocumentObject*> PlacementHandler::getObjects(const Gui::Document* document) const |
| 209 |
{ |
| 210 |
auto objs = document->getDocument()->getObjectsOfType(App::DocumentObject::getClassTypeId()); |
| 211 |
std::vector<const App::DocumentObject*> list; |
| 212 |
list.insert(list.begin(), objs.begin(), objs.end()); |
| 213 |
return list; |
| 214 |
} |
| 215 |
|
| 216 |
std::vector<const App::DocumentObject*> PlacementHandler::getSelectedObjects( |
| 217 |
const Gui::Document* document |
| 218 |
) const |
| 219 |
{ |
| 220 |
App::Document* doc = document->getDocument(); |
| 221 |
std::vector<const App::DocumentObject*> list; |
| 222 |
list.reserve(selectionObjects.size()); |
| 223 |
for (const auto& it : selectionObjects) { |
| 224 |
const App::DocumentObject* obj = it.getObject(); |
| 225 |
if (obj && obj->getDocument() == doc) { |
| 226 |
list.push_back(obj); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
if (list.empty()) { |
| 231 |
auto objs = Gui::Selection().getObjectsOfType( |
| 232 |
App::DocumentObject::getClassTypeId(), |
| 233 |
doc->getName() |
| 234 |
); |
| 235 |
list.insert(list.begin(), objs.begin(), objs.end()); |
| 236 |
} |
| 237 |
|
| 238 |
return list; |
| 239 |
} |
| 240 |
|
| 241 |
void PlacementHandler::revertTransformationOfViewProviders(Gui::Document* document) |
| 242 |
{ |
| 243 |
std::vector<const App::DocumentObject*> obj = getObjects(document); |
| 244 |
for (const auto& it : obj) { |
| 245 |
auto property = find_placement::getProperty(it, this->propertyName); |
| 246 |
if (property) { |
| 247 |
Base::Placement cur = property->getValue(); |
| 248 |
Gui::ViewProvider* vp = document->getViewProvider(it); |
| 249 |
if (vp) { |
| 250 |
vp->setTransformation(cur.toMatrix()); |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
void PlacementHandler::setRefPlacement(const Base::Placement& plm) |
| 257 |
{ |
| 258 |
ref = plm; |
| 259 |
} |
| 260 |
|
| 261 |
const Base::Placement& PlacementHandler::getRefPlacement() const |
| 262 |
{ |
| 263 |
return ref; |
| 264 |
} |
| 265 |
|
| 266 |
void PlacementHandler::applyPlacement(const Base::Placement& p, bool incremental) |
| 267 |
{ |
| 268 |
Gui::Document* document = Application::Instance->activeDocument(); |
| 269 |
if (!document) { |
| 270 |
return; |
| 271 |
} |
| 272 |
|
| 273 |
std::vector<const App::DocumentObject*> sel = getSelectedObjects(document); |
| 274 |
if (!sel.empty()) { |
| 275 |
for (const auto& it : sel) { |
| 276 |
applyPlacement(document, it, p, incremental); |
| 277 |
} |
| 278 |
} |
| 279 |
else { |
| 280 |
Base::Console().warning("No object selected.\n"); |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
void PlacementHandler::applyPlacement( |
| 285 |
const Gui::Document* document, |
| 286 |
const App::DocumentObject* obj, |
| 287 |
const Base::Placement& p, |
| 288 |
bool incremental |
| 289 |
) |
| 290 |
{ |
| 291 |
auto property = find_placement::getProperty(obj, this->propertyName); |
| 292 |
if (property) { |
| 293 |
Base::Placement cur = property->getValue(); |
| 294 |
if (incremental) { |
| 295 |
cur = p * cur; |
| 296 |
} |
| 297 |
else { |
| 298 |
cur = p; |
| 299 |
} |
| 300 |
|
| 301 |
if (!changeProperty) { |
| 302 |
Gui::ViewProvider* vp = document->getViewProvider(obj); |
| 303 |
if (vp) { |
| 304 |
vp->setTransformation(cur.toMatrix()); |
| 305 |
} |
| 306 |
} |
| 307 |
else { |
| 308 |
property->setValue(cur); |
| 309 |
} |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
void PlacementHandler::applyPlacement(const QString& data, bool incremental) |
| 314 |
{ |
| 315 |
Gui::Document* document = Application::Instance->activeDocument(); |
| 316 |
if (!document) { |
| 317 |
return; |
| 318 |
} |
| 319 |
|
| 320 |
// When directly changing the property we now only have to commit the transaction, |
| 321 |
// do a recompute and open a new transaction |
| 322 |
if (changeProperty) { |
| 323 |
commitCommandIfActive(document); |
| 324 |
tryRecompute(document); |
| 325 |
openCommandIfActive(document); |
| 326 |
} |
| 327 |
else { |
| 328 |
std::vector<const App::DocumentObject*> sel = getSelectedObjects(document); |
| 329 |
if (!sel.empty()) { |
| 330 |
openCommandIfActive(document); |
| 331 |
for (const auto& it : sel) { |
| 332 |
applyPlacement(it, data, incremental); |
| 333 |
} |
| 334 |
commitCommandIfActive(document); |
| 335 |
tryRecompute(document); |
| 336 |
} |
| 337 |
else { |
| 338 |
Base::Console().warning("No object selected.\n"); |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
void PlacementHandler::applyPlacement(const App::DocumentObject* obj, const QString& data, bool incremental) |
| 344 |
{ |
| 345 |
auto property = find_placement::getProperty(obj, this->propertyName); |
| 346 |
if (property) { |
| 347 |
QString cmd; |
| 348 |
if (incremental) { |
| 349 |
cmd = getIncrementalPlacement(obj, data); |
| 350 |
} |
| 351 |
else { |
| 352 |
cmd = getSimplePlacement(obj, data); |
| 353 |
} |
| 354 |
|
| 355 |
Gui::Command::runCommand(Gui::Command::App, cmd.toLatin1()); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
QString PlacementHandler::getIncrementalPlacement(const App::DocumentObject* obj, const QString& data) const |
| 360 |
{ |
| 361 |
return QStringLiteral(R"(App.getDocument("%1").%2.%3=%4.multiply(App.getDocument("%1").%2.%3))") |
| 362 |
.arg( |
| 363 |
QString::fromLatin1(obj->getDocument()->getName()), |
| 364 |
QString::fromLatin1(obj->getNameInDocument()), |
| 365 |
QString::fromLatin1(this->propertyName.c_str()), |
| 366 |
data |
| 367 |
); |
| 368 |
} |
| 369 |
|
| 370 |
QString PlacementHandler::getSimplePlacement(const App::DocumentObject* obj, const QString& data) const |
| 371 |
{ |
| 372 |
return QStringLiteral("App.getDocument(\"%1\").%2.%3=%4") |
| 373 |
.arg( |
| 374 |
QString::fromLatin1(obj->getDocument()->getName()), |
| 375 |
QString::fromLatin1(obj->getNameInDocument()), |
| 376 |
QString::fromLatin1(this->propertyName.c_str()), |
| 377 |
data |
| 378 |
); |
| 379 |
} |
| 380 |
|
| 381 |
Base::Vector3d PlacementHandler::computeCenterOfMass() const |
| 382 |
{ |
| 383 |
Base::Vector3d centerOfMass; |
| 384 |
std::vector<App::DocumentObject*> sel = Gui::Selection().getObjectsOfType( |
| 385 |
App::GeoFeature::getClassTypeId() |
| 386 |
); |
| 387 |
if (!sel.empty()) { |
| 388 |
for (auto it : sel) { |
| 389 |
const App::PropertyComplexGeoData* propgeo |
| 390 |
= static_cast<App::GeoFeature*>(it)->getPropertyOfGeometry(); |
| 391 |
const Data::ComplexGeoData* geodata = propgeo ? propgeo->getComplexData() : nullptr; |
| 392 |
if (geodata && geodata->getCenterOfGravity(centerOfMass)) { |
| 393 |
break; |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
return centerOfMass; |
| 398 |
} |
| 399 |
|
| 400 |
void PlacementHandler::setCenterOfMass(const Base::Vector3d& pnt) |
| 401 |
{ |
| 402 |
cntOfMass = pnt; |
| 403 |
} |
| 404 |
|
| 405 |
Base::Vector3d PlacementHandler::getCenterOfMass() const |
| 406 |
{ |
| 407 |
return cntOfMass; |
| 408 |
} |
| 409 |
|
| 410 |
std::tuple<Base::Vector3d, std::vector<Base::Vector3d>> PlacementHandler::getSelectedPoints() const |
| 411 |
{ |
| 412 |
std::vector<Gui::SelectionObject> selection = Gui::Selection().getSelectionEx(); |
| 413 |
std::vector<Base::Vector3d> picked; |
| 414 |
// combine all pickedpoints into single vector |
| 415 |
// even if points are from separate objects |
| 416 |
Base::Vector3d firstSelected; // first selected will be central point when 3 points picked |
| 417 |
for (auto it = selection.begin(); it != selection.end(); ++it) { |
| 418 |
std::vector<Base::Vector3d> points = it->getPickedPoints(); |
| 419 |
if (it == selection.begin() && !points.empty()) { |
| 420 |
firstSelected = points[0]; |
| 421 |
} |
| 422 |
|
| 423 |
picked.insert(picked.begin(), points.begin(), points.end()); |
| 424 |
} |
| 425 |
|
| 426 |
return std::make_tuple(firstSelected, picked); |
| 427 |
} |
| 428 |
|
| 429 |
void PlacementHandler::tryRecompute(Gui::Document* document) |
| 430 |
{ |
| 431 |
try { |
| 432 |
document->getDocument()->recompute(); |
| 433 |
} |
| 434 |
catch (...) { |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
void PlacementHandler::setupDocument() |
| 439 |
{ |
| 440 |
// NOLINTBEGIN |
| 441 |
connectAct = Application::Instance->signalActiveDocument.connect( |
| 442 |
std::bind(&PlacementHandler::slotActiveDocument, this, sp::_1) |
| 443 |
); |
| 444 |
// NOLINTEND |
| 445 |
App::Document* activeDoc = App::GetApplication().getActiveDocument(); |
| 446 |
if (activeDoc) { |
| 447 |
appendDocument(activeDoc->getName()); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
void PlacementHandler::slotActiveDocument(const Gui::Document& doc) |
| 452 |
{ |
| 453 |
activatedDocument(doc.getDocument()->getName()); |
| 454 |
} |
| 455 |
|
| 456 |
void PlacementHandler::openCommandIfActive(Gui::Document* doc) |
| 457 |
{ |
| 458 |
if (!ignoreTransaction) { |
| 459 |
doc->openCommand(QT_TRANSLATE_NOOP("Command", "Placement")); |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
void PlacementHandler::commitCommandIfActive(Gui::Document* doc) |
| 464 |
{ |
| 465 |
if (!ignoreTransaction) { |
| 466 |
doc->commitCommand(); |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
void PlacementHandler::abortCommandIfActive(Gui::Document* doc) |
| 471 |
{ |
| 472 |
if (!ignoreTransaction) { |
| 473 |
doc->abortCommand(); |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
// ---------------------------------------------------------------------------- |
| 478 |
|
| 479 |
/* TRANSLATOR Gui::Dialog::Placement */ |
| 480 |
|
| 481 |
Placement::Placement(QWidget* parent, Qt::WindowFlags fl) |
| 482 |
: QDialog(parent, fl) |
| 483 |
, ui {nullptr} |
| 484 |
{ |
| 485 |
setupUi(); |
| 486 |
setupConnections(); |
| 487 |
setupUnits(); |
| 488 |
setupSignalMapper(); |
| 489 |
setupRotationMethod(); |
| 490 |
} |
| 491 |
|
| 492 |
Placement::~Placement() |
| 493 |
{ |
| 494 |
delete ui; |
| 495 |
} |
| 496 |
|
| 497 |
void Placement::setupUi() |
| 498 |
{ |
| 499 |
ui = new Ui_Placement(); |
| 500 |
ui->setupUi(this); |
| 501 |
ui->gridLayout->removeItem(ui->vSpacer); |
| 502 |
} |
| 503 |
|
| 504 |
void Placement::setupConnections() |
| 505 |
{ |
| 506 |
QPushButton* applyButton = ui->buttonBox->button(QDialogButtonBox::Apply); |
| 507 |
connect(applyButton, &QPushButton::clicked, this, &Placement::onApplyButtonClicked); |
| 508 |
connect( |
| 509 |
ui->applyIncrementalPlacement, |
| 510 |
&QCheckBox::toggled, |
| 511 |
this, |
| 512 |
&Placement::onApplyIncrementalPlacementToggled |
| 513 |
); |
| 514 |
connect(ui->resetButton, &QPushButton::clicked, this, &Placement::onResetButtonClicked); |
| 515 |
connect(ui->centerOfMass, &QCheckBox::toggled, this, &Placement::onCenterOfMassToggled); |
| 516 |
connect(ui->selectedVertex, &QPushButton::clicked, this, &Placement::onSelectedVertexClicked); |
| 517 |
connect(ui->applyAxial, &QPushButton::clicked, this, &Placement::onApplyAxialClicked); |
| 518 |
} |
| 519 |
|
| 520 |
void Placement::setupUnits() |
| 521 |
{ |
| 522 |
ui->xPos->setUnit(Base::Unit::Length); |
| 523 |
ui->yPos->setUnit(Base::Unit::Length); |
| 524 |
ui->zPos->setUnit(Base::Unit::Length); |
| 525 |
ui->axialPos->setUnit(Base::Unit::Length); |
| 526 |
ui->xCnt->setValue(Base::Quantity(0, Base::Unit::Length)); |
| 527 |
ui->yCnt->setValue(Base::Quantity(0, Base::Unit::Length)); |
| 528 |
ui->zCnt->setValue(Base::Quantity(0, Base::Unit::Length)); |
| 529 |
ui->angle->setUnit(Base::Unit::Angle); |
| 530 |
ui->yawAngle->setMaximum(180.0F); |
| 531 |
ui->yawAngle->setMinimum(-180.0F); |
| 532 |
ui->yawAngle->setUnit(Base::Unit::Angle); |
| 533 |
ui->yawAngle->checkRangeInExpression(true); |
| 534 |
ui->pitchAngle->setMaximum(90.0F); |
| 535 |
ui->pitchAngle->setMinimum(-90.0F); |
| 536 |
ui->pitchAngle->setUnit(Base::Unit::Angle); |
| 537 |
ui->pitchAngle->checkRangeInExpression(true); |
| 538 |
ui->rollAngle->setMaximum(180.0F); |
| 539 |
ui->rollAngle->setMinimum(-180.0F); |
| 540 |
ui->rollAngle->setUnit(Base::Unit::Angle); |
| 541 |
ui->rollAngle->checkRangeInExpression(true); |
| 542 |
} |
| 543 |
|
| 544 |
void Placement::setupSignalMapper() |
| 545 |
{ |
| 546 |
// create a signal mapper in order to have one slot to perform the change |
| 547 |
signalMapper = new QSignalMapper(this); |
| 548 |
signalMapper->setMapping(this, 0); |
| 549 |
|
| 550 |
int id = 1; |
| 551 |
QList<Gui::QuantitySpinBox*> sb = this->findChildren<Gui::QuantitySpinBox*>(); |
| 552 |
for (const auto& it : sb) { |
| 553 |
connect( |
| 554 |
it, |
| 555 |
qOverload<double>(&QuantitySpinBox::valueChanged), |
| 556 |
signalMapper, |
| 557 |
qOverload<>(&QSignalMapper::map) |
| 558 |
); |
| 559 |
signalMapper->setMapping(it, id++); |
| 560 |
} |
| 561 |
|
| 562 |
connect(signalMapper, &QSignalMapper::mappedInt, this, &Placement::onPlacementChanged); |
| 563 |
} |
| 564 |
|
| 565 |
void Placement::setupRotationMethod() |
| 566 |
{ |
| 567 |
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("Placement"); |
| 568 |
long index = hGrp->GetInt("RotationMethod"); |
| 569 |
ui->rotationInput->setCurrentIndex(index); |
| 570 |
ui->stackedWidget->setCurrentIndex(index); |
| 571 |
} |
| 572 |
|
| 573 |
void Placement::showDefaultButtons(bool ok) |
| 574 |
{ |
| 575 |
ui->buttonBox->setVisible(ok); |
| 576 |
ui->buttonBoxLayout->invalidate(); |
| 577 |
if (ok) { |
| 578 |
ui->buttonBoxLayout->insertSpacerItem(0, ui->buttonBoxSpacer); |
| 579 |
} |
| 580 |
else { |
| 581 |
ui->buttonBoxLayout->removeItem(ui->buttonBoxSpacer); |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
void Placement::open() |
| 586 |
{ |
| 587 |
handler.openTransactionIfNeeded(); |
| 588 |
} |
| 589 |
|
| 590 |
QWidget* Placement::getInvalidInput() const |
| 591 |
{ |
| 592 |
QList<Gui::QuantitySpinBox*> sb = this->findChildren<Gui::QuantitySpinBox*>(); |
| 593 |
for (const auto& it : sb) { |
| 594 |
if (!it->hasValidInput()) { |
| 595 |
return it; |
| 596 |
} |
| 597 |
} |
| 598 |
return nullptr; |
| 599 |
} |
| 600 |
|
| 601 |
void Placement::onPlacementChanged(int) |
| 602 |
{ |
| 603 |
// If there are listeners to the 'placementChanged' signal we rely |
| 604 |
// on that the listener updates any placement. If no listeners |
| 605 |
// are connected the placement is applied to all selected objects |
| 606 |
// automatically. |
| 607 |
bool incr = ui->applyIncrementalPlacement->isChecked(); |
| 608 |
Base::Placement plm = this->getPlacement(); |
| 609 |
handler.applyPlacement(plm, incr); |
| 610 |
|
| 611 |
QVariant data = QVariant::fromValue<Base::Placement>(plm); |
| 612 |
Q_EMIT placementChanged(data, incr, false); |
| 613 |
} |
| 614 |
|
| 615 |
void Placement::onCenterOfMassToggled(bool on) |
| 616 |
{ |
| 617 |
ui->xCnt->setDisabled(on); |
| 618 |
ui->yCnt->setDisabled(on); |
| 619 |
ui->zCnt->setDisabled(on); |
| 620 |
|
| 621 |
if (on) { |
| 622 |
Base::Vector3d pnt = handler.computeCenterOfMass(); |
| 623 |
handler.setCenterOfMass(pnt); |
| 624 |
ui->xCnt->setValue(pnt.x); |
| 625 |
ui->yCnt->setValue(pnt.y); |
| 626 |
ui->zCnt->setValue(pnt.z); |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
void Placement::onSelectedVertexClicked() |
| 631 |
{ |
| 632 |
ui->centerOfMass->setChecked(false); |
| 633 |
|
| 634 |
Base::Vector3d center; |
| 635 |
bool success = false; |
| 636 |
auto [firstSelected, picked] = handler.getSelectedPoints(); |
| 637 |
handler.reselectObjects(); |
| 638 |
|
| 639 |
if (picked.size() == 1) { |
| 640 |
center = picked[0]; |
| 641 |
success = true; |
| 642 |
} |
| 643 |
else if (picked.size() == 2) { |
| 644 |
// average the coords to get center of rotation |
| 645 |
center = (picked[0] + picked[1]) / 2.0; |
| 646 |
|
| 647 |
// setup a customized axis since the user selected 2 points |
| 648 |
// keep any existing angle, but setup our own axis |
| 649 |
Base::Placement plm = getPlacement(); |
| 650 |
Base::Rotation rot = plm.getRotation(); |
| 651 |
Base::Vector3d tmp; |
| 652 |
double angle; |
| 653 |
rot.getRawValue(tmp, angle); |
| 654 |
Base::Vector3d axis; |
| 655 |
if (firstSelected == picked[0]) { |
| 656 |
axis = Base::Vector3d(picked[1] - picked[0]); |
| 657 |
} |
| 658 |
else { |
| 659 |
axis = Base::Vector3d(picked[0] - picked[1]); |
| 660 |
} |
| 661 |
double length = axis.Length(); |
| 662 |
Base::Console().message("Distance: %.8f\n", length); |
| 663 |
if (QApplication::keyboardModifiers() == Qt::ShiftModifier) { // copy to clipboard on |
| 664 |
// Shift+click |
| 665 |
QLocale loc; |
| 666 |
QApplication::clipboard()->setText(loc.toString(length, 'g', 8)); |
| 667 |
} |
| 668 |
else { |
| 669 |
Base::Console().message( |
| 670 |
"(Shift + click selected points button to copy distance to clipboard)\n" |
| 671 |
); |
| 672 |
} |
| 673 |
axis.Normalize(); |
| 674 |
rot.setValue(axis, angle); |
| 675 |
plm.setRotation(rot); |
| 676 |
setPlacementData(plm); // creates custom axis, if needed |
| 677 |
ui->rotationInput->setCurrentIndex(0); // use rotation with axis instead of euler |
| 678 |
ui->stackedWidget->setCurrentIndex(0); |
| 679 |
success = true; |
| 680 |
} |
| 681 |
else if (picked.size() == 3) { |
| 682 |
/* User selected 3 points, so we find the plane defined by those |
| 683 |
* and use the normal vector that contains the first point picked |
| 684 |
* as the axis of rotation. |
| 685 |
*/ |
| 686 |
|
| 687 |
Base::Vector3d a, b(firstSelected), c; // b is on central axis |
| 688 |
if (picked[0] == firstSelected) { |
| 689 |
a = picked[1]; |
| 690 |
c = picked[2]; |
| 691 |
} |
| 692 |
else if (picked[1] == firstSelected) { |
| 693 |
a = picked[0]; |
| 694 |
c = picked[2]; |
| 695 |
} |
| 696 |
else if (picked[2] == firstSelected) { |
| 697 |
a = picked[0]; |
| 698 |
c = picked[1]; |
| 699 |
} |
| 700 |
|
| 701 |
Base::Vector3d norm((a - b).Cross(c - b)); |
| 702 |
norm.Normalize(); |
| 703 |
center = b; |
| 704 |
|
| 705 |
// setup a customized axis normal to the plane |
| 706 |
// keep any existing angle, but setup our own axis |
| 707 |
Base::Placement plm = getPlacement(); |
| 708 |
Base::Rotation rot = plm.getRotation(); |
| 709 |
Base::Vector3d tmp; |
| 710 |
double angle; |
| 711 |
rot.getRawValue(tmp, angle); |
| 712 |
double length = (a - c).Length(); |
| 713 |
Base::Console().message("Distance: %.8f\n", length); |
| 714 |
Base::Vector3d v1(a - b); |
| 715 |
Base::Vector3d v2(c - b); |
| 716 |
v1.Normalize(); |
| 717 |
v2.Normalize(); |
| 718 |
double targetAngle = Base::toDegrees(v2.GetAngle(v1)); |
| 719 |
Base::Console().message( |
| 720 |
"Target angle: %.8f degrees, complementary: %.8f degrees\n", |
| 721 |
targetAngle, |
| 722 |
90.0 - targetAngle |
| 723 |
); |
| 724 |
if (QApplication::keyboardModifiers() == Qt::ShiftModifier) { // copy to clipboard on |
| 725 |
// Shift+click |
| 726 |
QLocale loc; |
| 727 |
QApplication::clipboard()->setText(loc.toString(targetAngle, 'g', 8)); |
| 728 |
Base::Console() |
| 729 |
.message("(Angle copied to clipboard, but you might need to use a negative (-) angle sometimes.)\n"); |
| 730 |
} |
| 731 |
else { |
| 732 |
Base::Console().message( |
| 733 |
"(Shift + click selected points button to copy angle to clipboard)\n" |
| 734 |
); |
| 735 |
} |
| 736 |
rot.setValue(norm, angle); |
| 737 |
plm.setRotation(rot); |
| 738 |
setPlacementData(plm); // creates custom axis, if needed |
| 739 |
ui->rotationInput->setCurrentIndex(0); // use rotation with axis instead of euler |
| 740 |
ui->stackedWidget->setCurrentIndex(0); |
| 741 |
success = true; |
| 742 |
} |
| 743 |
|
| 744 |
handler.setCenterOfMass(center); |
| 745 |
ui->xCnt->setValue(center.x); |
| 746 |
ui->yCnt->setValue(center.y); |
| 747 |
ui->zCnt->setValue(center.z); |
| 748 |
|
| 749 |
if (!success) { |
| 750 |
Base::Console().warning("Placement selection error. Select either 1 or 2 points.\n"); |
| 751 |
QMessageBox msgBox(this); |
| 752 |
msgBox.setText( |
| 753 |
tr("Select 1, 2, or 3 points before clicking this button. A point may be on a vertex, \ |
| 754 |
face, or edge. If on a face or edge the point used will be the point at the mouse position along \ |
| 755 |
face or edge. If 1 point is selected it will be used as the center of rotation. If 2 points are \ |
| 756 |
selected the midpoint between them will be the center of rotation and a new custom axis will be \ |
| 757 |
created, if needed. If 3 points are selected the first point becomes the center of rotation and \ |
| 758 |
lies on the vector that is normal to the plane defined by the 3 points. Some distance and angle \ |
| 759 |
information is provided in the report view, which can be useful when aligning objects. For your \ |
| 760 |
convenience when Shift + click is used the appropriate distance or angle is copied to the clipboard.") |
| 761 |
); |
| 762 |
msgBox.exec(); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
void Placement::onApplyAxialClicked() |
| 767 |
{ |
| 768 |
signalMapper->blockSignals(true); |
| 769 |
double axPos = ui->axialPos->value().getValue(); |
| 770 |
Base::Placement p = getPlacementData(); |
| 771 |
double angle; |
| 772 |
Base::Vector3d axis; |
| 773 |
p.getRotation().getValue(axis, angle); |
| 774 |
Base::Vector3d curPos(p.getPosition()); |
| 775 |
Base::Vector3d newPos; |
| 776 |
Qt::KeyboardModifiers km = QApplication::keyboardModifiers(); |
| 777 |
if (km == Qt::ShiftModifier) { // go opposite direction on Shift+click |
| 778 |
newPos = Base::Vector3d( |
| 779 |
curPos.x - (axis.x * axPos), |
| 780 |
curPos.y - (axis.y * axPos), |
| 781 |
curPos.z - (axis.z * axPos) |
| 782 |
); |
| 783 |
} |
| 784 |
else { |
| 785 |
newPos = Base::Vector3d( |
| 786 |
curPos.x + (axis.x * axPos), |
| 787 |
curPos.y + (axis.y * axPos), |
| 788 |
curPos.z + (axis.z * axPos) |
| 789 |
); |
| 790 |
} |
| 791 |
ui->xPos->setValue(Base::Quantity(newPos.x, Base::Unit::Length)); |
| 792 |
ui->yPos->setValue(Base::Quantity(newPos.y, Base::Unit::Length)); |
| 793 |
ui->zPos->setValue(Base::Quantity(newPos.z, Base::Unit::Length)); |
| 794 |
signalMapper->blockSignals(false); |
| 795 |
onPlacementChanged(0); |
| 796 |
} |
| 797 |
|
| 798 |
void Placement::onApplyIncrementalPlacementToggled(bool on) |
| 799 |
{ |
| 800 |
if (on) { |
| 801 |
handler.setRefPlacement(getPlacementData()); |
| 802 |
onResetButtonClicked(); |
| 803 |
} |
| 804 |
else { |
| 805 |
Base::Placement p = getPlacementData(); |
| 806 |
p = p * handler.getRefPlacement(); |
| 807 |
setPlacementData(p); |
| 808 |
onPlacementChanged(0); |
| 809 |
} |
| 810 |
} |
| 811 |
|
| 812 |
void Placement::keyPressEvent(QKeyEvent* ke) |
| 813 |
{ |
| 814 |
// The placement dialog is embedded into a task panel |
| 815 |
// which is a parent widget and will handle the event |
| 816 |
ke->ignore(); |
| 817 |
} |
| 818 |
|
| 819 |
void Placement::reject() |
| 820 |
{ |
| 821 |
Base::Placement plm; |
| 822 |
handler.applyPlacement(plm, true); |
| 823 |
|
| 824 |
QVariant data = QVariant::fromValue<Base::Placement>(plm); |
| 825 |
Q_EMIT placementChanged(data, true, false); |
| 826 |
|
| 827 |
handler.revertTransformation(); |
| 828 |
|
| 829 |
// One of the quantity spin boxes still can emit a signal when it has the focus |
| 830 |
// but its content is not fully updated. |
| 831 |
// In order to override again the placement the signalMapper is blocked |
| 832 |
// See related forum thread: |
| 833 |
// https://forum.freecad.org/viewtopic.php?f=3&t=44341#p378659 |
| 834 |
QSignalBlocker block(signalMapper); |
| 835 |
QDialog::reject(); |
| 836 |
} |
| 837 |
|
| 838 |
void Placement::accept() |
| 839 |
{ |
| 840 |
if (onApply()) { |
| 841 |
handler.revertTransformation(); |
| 842 |
QDialog::accept(); |
| 843 |
} |
| 844 |
} |
| 845 |
|
| 846 |
void Placement::onApplyButtonClicked() |
| 847 |
{ |
| 848 |
onApply(); |
| 849 |
} |
| 850 |
|
| 851 |
void Placement::showErrorMessage() |
| 852 |
{ |
| 853 |
QMessageBox msg(this); |
| 854 |
msg.setWindowTitle(tr("Incorrect Quantity")); |
| 855 |
msg.setIcon(QMessageBox::Critical); |
| 856 |
msg.setText(tr("There are input fields with incorrect input. Ensure valid placement values!")); |
| 857 |
msg.exec(); |
| 858 |
} |
| 859 |
|
| 860 |
bool Placement::onApply() |
| 861 |
{ |
| 862 |
// only process things when we have valid inputs! |
| 863 |
QWidget* input = getInvalidInput(); |
| 864 |
if (input) { |
| 865 |
input->setFocus(); |
| 866 |
showErrorMessage(); |
| 867 |
return false; |
| 868 |
} |
| 869 |
|
| 870 |
// If there are listeners to the 'placementChanged' signal we rely |
| 871 |
// on that the listener updates any placement. If no listeners |
| 872 |
// are connected the placement is applied to all selected objects |
| 873 |
// automatically. |
| 874 |
bool incr = ui->applyIncrementalPlacement->isChecked(); |
| 875 |
Base::Placement plm = this->getPlacement(); |
| 876 |
handler.applyPlacement(getPlacementString(), incr); |
| 877 |
|
| 878 |
QVariant data = QVariant::fromValue<Base::Placement>(plm); |
| 879 |
Q_EMIT placementChanged(data, incr, true); |
| 880 |
|
| 881 |
if (ui->applyIncrementalPlacement->isChecked()) { |
| 882 |
QList<Gui::QuantitySpinBox*> sb = this->findChildren<Gui::QuantitySpinBox*>(); |
| 883 |
for (auto& it : sb) { |
| 884 |
it->blockSignals(true); |
| 885 |
it->setValue(0); |
| 886 |
it->blockSignals(false); |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
ParameterGrp::handle hGrp = WindowParameter::getDefaultParameter()->GetGroup("Placement"); |
| 891 |
hGrp->SetInt("RotationMethod", ui->rotationInput->currentIndex()); |
| 892 |
|
| 893 |
return true; |
| 894 |
} |
| 895 |
|
| 896 |
void Placement::onResetButtonClicked() |
| 897 |
{ |
| 898 |
QList<Gui::QuantitySpinBox*> sb = this->findChildren<Gui::QuantitySpinBox*>(); |
| 899 |
for (auto& it : sb) { |
| 900 |
it->blockSignals(true); |
| 901 |
it->setValue(0); |
| 902 |
it->blockSignals(false); |
| 903 |
} |
| 904 |
|
| 905 |
onPlacementChanged(0); |
| 906 |
} |
| 907 |
|
| 908 |
/*! |
| 909 |
* \brief Placement::setSelection |
| 910 |
* Sets the array of selection objects. |
| 911 |
* \param selection |
| 912 |
*/ |
| 913 |
void Placement::setSelection(const std::vector<Gui::SelectionObject>& selection) |
| 914 |
{ |
| 915 |
handler.setSelection(selection); |
| 916 |
} |
| 917 |
|
| 918 |
void Placement::setPropertyName(const std::string& name) |
| 919 |
{ |
| 920 |
handler.setPropertyName(name); |
| 921 |
} |
| 922 |
|
| 923 |
void Placement::setIgnoreTransactions(bool value) |
| 924 |
{ |
| 925 |
handler.setIgnoreTransactions(value); |
| 926 |
} |
| 927 |
|
| 928 |
/*! |
| 929 |
* \brief Placement::bindObject |
| 930 |
* Binds the spin boxes to the placement components of the first object of the selection. |
| 931 |
* This requires the call of \a setSelection() beforehand. |
| 932 |
*/ |
| 933 |
void Placement::bindObject() |
| 934 |
{ |
| 935 |
if (const App::DocumentObject* obj = handler.getFirstOfSelection()) { |
| 936 |
std::string propertyName = handler.getPropertyName(); |
| 937 |
bindProperty(obj, propertyName); |
| 938 |
} |
| 939 |
} |
| 940 |
|
| 941 |
/*! |
| 942 |
* \brief Placement::setPlacementAndBindObject |
| 943 |
* Sets the placement, binds the spin boxes to the placement components of the passed object and |
| 944 |
* sets the name of the placement property. |
| 945 |
*/ |
| 946 |
void Placement::setPlacementAndBindObject(const App::DocumentObject* obj, const std::string& propertyName) |
| 947 |
{ |
| 948 |
if (obj) { |
| 949 |
App::PropertyPlacement* prop = find_placement::getProperty(obj, propertyName); |
| 950 |
if (prop) { |
| 951 |
setPlacement(prop->getValue()); |
| 952 |
handler.setPropertyName(propertyName); |
| 953 |
bindProperty(obj, propertyName); |
| 954 |
handler.setSelection({SelectionObject {obj}}); |
| 955 |
} |
| 956 |
} |
| 957 |
} |
| 958 |
|
| 959 |
void Placement::bindProperty(const App::DocumentObject* obj, const std::string& propertyName) |
| 960 |
{ |
| 961 |
// clang-format off |
| 962 |
if (obj) { |
| 963 |
App::ObjectIdentifier path = App::ObjectIdentifier::parse(obj, propertyName); |
| 964 |
if (path.getProperty()) { |
| 965 |
ui->xPos->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Base.x"))); |
| 966 |
ui->yPos->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Base.y"))); |
| 967 |
ui->zPos->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Base.z"))); |
| 968 |
|
| 969 |
ui->xAxis->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Rotation.Axis.x"))); |
| 970 |
ui->yAxis->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Rotation.Axis.y"))); |
| 971 |
ui->zAxis->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Rotation.Axis.z"))); |
| 972 |
ui->angle->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Rotation.Angle"))); |
| 973 |
|
| 974 |
ui->yawAngle ->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Rotation.Yaw"))); |
| 975 |
ui->pitchAngle->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Rotation.Pitch"))); |
| 976 |
ui->rollAngle ->bind(App::ObjectIdentifier::parse(obj, propertyName + std::string(".Rotation.Roll"))); |
| 977 |
|
| 978 |
ui->yawAngle->evaluateExpression(); |
| 979 |
ui->pitchAngle->evaluateExpression(); |
| 980 |
ui->rollAngle->evaluateExpression(); |
| 981 |
} |
| 982 |
} |
| 983 |
// clang-format on |
| 984 |
} |
| 985 |
|
| 986 |
Base::Vector3d Placement::getDirection() const |
| 987 |
{ |
| 988 |
double x = ui->xAxis->value().getValue(); |
| 989 |
double y = ui->yAxis->value().getValue(); |
| 990 |
double z = ui->zAxis->value().getValue(); |
| 991 |
return Base::Vector3d(x, y, z); |
| 992 |
} |
| 993 |
|
| 994 |
void Placement::setPlacement(const Base::Placement& p) |
| 995 |
{ |
| 996 |
setPlacementData(p); |
| 997 |
} |
| 998 |
|
| 999 |
void Placement::setPlacementData(const Base::Placement& p) |
| 1000 |
{ |
| 1001 |
QSignalBlocker block(signalMapper); |
| 1002 |
ui->xPos->setValue(Base::Quantity(p.getPosition().x, Base::Unit::Length)); |
| 1003 |
ui->yPos->setValue(Base::Quantity(p.getPosition().y, Base::Unit::Length)); |
| 1004 |
ui->zPos->setValue(Base::Quantity(p.getPosition().z, Base::Unit::Length)); |
| 1005 |
|
| 1006 |
double Y, P, R; |
| 1007 |
p.getRotation().getYawPitchRoll(Y, P, R); |
| 1008 |
ui->yawAngle->setValue(Base::Quantity(Y, Base::Unit::Angle)); |
| 1009 |
ui->pitchAngle->setValue(Base::Quantity(P, Base::Unit::Angle)); |
| 1010 |
ui->rollAngle->setValue(Base::Quantity(R, Base::Unit::Angle)); |
| 1011 |
|
| 1012 |
double angle; |
| 1013 |
Base::Vector3d axis; |
| 1014 |
p.getRotation().getRawValue(axis, angle); |
| 1015 |
ui->xAxis->setValue(axis.x); |
| 1016 |
ui->yAxis->setValue(axis.y); |
| 1017 |
ui->zAxis->setValue(axis.z); |
| 1018 |
ui->angle->setValue(Base::Quantity(Base::toDegrees(angle), Base::Unit::Angle)); |
| 1019 |
} |
| 1020 |
|
| 1021 |
Base::Placement Placement::getPlacement() const |
| 1022 |
{ |
| 1023 |
Base::Placement p = getPlacementData(); |
| 1024 |
return p; |
| 1025 |
} |
| 1026 |
|
| 1027 |
Base::Rotation Placement::getRotationData() const |
| 1028 |
{ |
| 1029 |
Base::Rotation rot; |
| 1030 |
int index = ui->rotationInput->currentIndex(); |
| 1031 |
if (index == 0) { |
| 1032 |
Base::Vector3d dir = getDirection(); |
| 1033 |
rot.setValue( |
| 1034 |
Base::Vector3d(dir.x, dir.y, dir.z), |
| 1035 |
Base::toRadians(ui->angle->value().getValue()) |
| 1036 |
); |
| 1037 |
} |
| 1038 |
else if (index == 1) { // Euler angles (XY'Z'') |
| 1039 |
rot.setYawPitchRoll( |
| 1040 |
ui->yawAngle->value().getValue(), |
| 1041 |
ui->pitchAngle->value().getValue(), |
| 1042 |
ui->rollAngle->value().getValue() |
| 1043 |
); |
| 1044 |
} |
| 1045 |
|
| 1046 |
return rot; |
| 1047 |
} |
| 1048 |
|
| 1049 |
Base::Vector3d Placement::getPositionData() const |
| 1050 |
{ |
| 1051 |
return Base::Vector3d( |
| 1052 |
ui->xPos->value().getValue(), |
| 1053 |
ui->yPos->value().getValue(), |
| 1054 |
ui->zPos->value().getValue() |
| 1055 |
); |
| 1056 |
} |
| 1057 |
|
| 1058 |
Base::Vector3d Placement::getAnglesData() const |
| 1059 |
{ |
| 1060 |
return Base::Vector3d( |
| 1061 |
ui->yawAngle->value().getValue(), |
| 1062 |
ui->pitchAngle->value().getValue(), |
| 1063 |
ui->rollAngle->value().getValue() |
| 1064 |
); |
| 1065 |
} |
| 1066 |
|
| 1067 |
Base::Vector3d Placement::getCenterData() const |
| 1068 |
{ |
| 1069 |
if (ui->centerOfMass->isChecked()) { |
| 1070 |
return handler.getCenterOfMass(); |
| 1071 |
} |
| 1072 |
return Base::Vector3d( |
| 1073 |
ui->xCnt->value().getValue(), |
| 1074 |
ui->yCnt->value().getValue(), |
| 1075 |
ui->zCnt->value().getValue() |
| 1076 |
); |
| 1077 |
} |
| 1078 |
|
| 1079 |
Base::Placement Placement::getPlacementData() const |
| 1080 |
{ |
| 1081 |
Base::Rotation rot = getRotationData(); |
| 1082 |
Base::Vector3d pos = getPositionData(); |
| 1083 |
Base::Vector3d cnt = getCenterData(); |
| 1084 |
|
| 1085 |
Base::Placement plm(pos, rot, cnt); |
| 1086 |
return plm; |
| 1087 |
} |
| 1088 |
|
| 1089 |
QString Placement::getPlacementFromEulerAngles() const |
| 1090 |
{ |
| 1091 |
Base::Vector3d pos = getPositionData(); |
| 1092 |
Base::Vector3d ypr = getAnglesData(); |
| 1093 |
Base::Vector3d cnt = getCenterData(); |
| 1094 |
return QStringLiteral( |
| 1095 |
"App.Placement(App.Vector(%1,%2,%3), App.Rotation(%4,%5,%6), App.Vector(%7,%8,%9))" |
| 1096 |
) |
| 1097 |
.arg(pos.x) |
| 1098 |
.arg(pos.y) |
| 1099 |
.arg(pos.z) |
| 1100 |
.arg(ypr.x) |
| 1101 |
.arg(ypr.y) |
| 1102 |
.arg(ypr.z) |
| 1103 |
.arg(cnt.x) |
| 1104 |
.arg(cnt.y) |
| 1105 |
.arg(cnt.z); |
| 1106 |
} |
| 1107 |
|
| 1108 |
QString Placement::getPlacementFromAxisWithAngle() const |
| 1109 |
{ |
| 1110 |
Base::Vector3d pos = getPositionData(); |
| 1111 |
Base::Vector3d cnt = getCenterData(); |
| 1112 |
Base::Vector3d dir = getDirection(); |
| 1113 |
double angle = ui->angle->value().getValue(); |
| 1114 |
return QStringLiteral("App.Placement(App.Vector(%1,%2,%3), App.Rotation(App.Vector(%4,%5,%6),%7), App.Vector(%8,%9,%10))") |
| 1115 |
.arg(pos.x) |
| 1116 |
.arg(pos.y) |
| 1117 |
.arg(pos.z) |
| 1118 |
.arg(dir.x) |
| 1119 |
.arg(dir.y) |
| 1120 |
.arg(dir.z) |
| 1121 |
.arg(angle) |
| 1122 |
.arg(cnt.x) |
| 1123 |
.arg(cnt.y) |
| 1124 |
.arg(cnt.z); |
| 1125 |
} |
| 1126 |
|
| 1127 |
QString Placement::getPlacementString() const |
| 1128 |
{ |
| 1129 |
QString cmd; |
| 1130 |
int index = ui->rotationInput->currentIndex(); |
| 1131 |
|
| 1132 |
if (index == 0) { |
| 1133 |
cmd = getPlacementFromAxisWithAngle(); |
| 1134 |
} |
| 1135 |
else if (index == 1) { |
| 1136 |
cmd = getPlacementFromEulerAngles(); |
| 1137 |
} |
| 1138 |
|
| 1139 |
return cmd; |
| 1140 |
} |
| 1141 |
|
| 1142 |
void Placement::changeEvent(QEvent* e) |
| 1143 |
{ |
| 1144 |
if (e->type() == QEvent::LanguageChange) { |
| 1145 |
ui->retranslateUi(this); |
| 1146 |
} |
| 1147 |
else { |
| 1148 |
QDialog::changeEvent(e); |
| 1149 |
} |
| 1150 |
} |
| 1151 |
|
| 1152 |
// ---------------------------------------------- |
| 1153 |
|
| 1154 |
/* TRANSLATOR Gui::Dialog::DockablePlacement */ |
| 1155 |
|
| 1156 |
DockablePlacement::DockablePlacement(QWidget* parent, Qt::WindowFlags fl) |
| 1157 |
: Placement(parent, fl) |
| 1158 |
{ |
| 1159 |
Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance(); |
| 1160 |
QDockWidget* dw = pDockMgr->addDockWindow(QT_TR_NOOP("Placement"), this, Qt::BottomDockWidgetArea); |
| 1161 |
dw->setFeatures(QDockWidget::DockWidgetMovable | QDockWidget::DockWidgetFloatable); |
| 1162 |
dw->show(); |
| 1163 |
} |
| 1164 |
|
| 1165 |
DockablePlacement::~DockablePlacement() = default; |
| 1166 |
|
| 1167 |
void DockablePlacement::accept() |
| 1168 |
{ |
| 1169 |
// closes the dock window |
| 1170 |
Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance(); |
| 1171 |
pDockMgr->removeDockWindow(this); |
| 1172 |
Placement::accept(); |
| 1173 |
} |
| 1174 |
|
| 1175 |
void DockablePlacement::reject() |
| 1176 |
{ |
| 1177 |
// closes the dock window |
| 1178 |
Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance(); |
| 1179 |
pDockMgr->removeDockWindow(this); |
| 1180 |
Placement::reject(); |
| 1181 |
} |
| 1182 |
|
| 1183 |
// ---------------------------------------------- |
| 1184 |
|
| 1185 |
/* TRANSLATOR Gui::Dialog::TaskPlacement */ |
| 1186 |
|
| 1187 |
TaskPlacement::TaskPlacement() |
| 1188 |
{ |
| 1189 |
this->setButtonPosition(TaskPlacement::South); |
| 1190 |
widget = new Placement(); |
| 1191 |
widget->showDefaultButtons(false); |
| 1192 |
addTaskBox(widget); |
| 1193 |
connect(widget, &Placement::placementChanged, this, &TaskPlacement::slotPlacementChanged); |
| 1194 |
} |
| 1195 |
|
| 1196 |
TaskPlacement::~TaskPlacement() = default; |
| 1197 |
|
| 1198 |
|
| 1199 |
/*! |
| 1200 |
* \brief TaskPlacement::setSelection |
| 1201 |
* Sets the array of selection objects. |
| 1202 |
* \param selection |
| 1203 |
*/ |
| 1204 |
void TaskPlacement::setSelection(const std::vector<Gui::SelectionObject>& selection) |
| 1205 |
{ |
| 1206 |
widget->setSelection(selection); |
| 1207 |
} |
| 1208 |
|
| 1209 |
/*! |
| 1210 |
* \brief TaskPlacement::clearSelection |
| 1211 |
* Clears the array of selection objects. |
| 1212 |
*/ |
| 1213 |
void TaskPlacement::clearSelection() |
| 1214 |
{ |
| 1215 |
widget->setSelection({}); |
| 1216 |
} |
| 1217 |
|
| 1218 |
/*! |
| 1219 |
* \brief TaskPlacement::bindObject |
| 1220 |
* Binds the spin boxes to the placement components of the first object of the selection. |
| 1221 |
* This requires the call of \a setSelection() beforehand. |
| 1222 |
*/ |
| 1223 |
void TaskPlacement::bindObject() |
| 1224 |
{ |
| 1225 |
widget->bindObject(); |
| 1226 |
} |
| 1227 |
|
| 1228 |
void TaskPlacement::setPlacementAndBindObject( |
| 1229 |
const App::DocumentObject* obj, |
| 1230 |
const std::string& propertyName |
| 1231 |
) |
| 1232 |
{ |
| 1233 |
widget->setPlacementAndBindObject(obj, propertyName); |
| 1234 |
} |
| 1235 |
|
| 1236 |
void TaskPlacement::open() |
| 1237 |
{ |
| 1238 |
widget->open(); |
| 1239 |
} |
| 1240 |
|
| 1241 |
void TaskPlacement::setPropertyName(const QString& name) |
| 1242 |
{ |
| 1243 |
widget->setPropertyName(name.toStdString()); |
| 1244 |
} |
| 1245 |
|
| 1246 |
QDialogButtonBox::StandardButtons TaskPlacement::getStandardButtons() const |
| 1247 |
{ |
| 1248 |
return QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply; |
| 1249 |
} |
| 1250 |
|
| 1251 |
void TaskPlacement::setPlacement(const Base::Placement& p) |
| 1252 |
{ |
| 1253 |
widget->setPlacement(p); |
| 1254 |
} |
| 1255 |
|
| 1256 |
void TaskPlacement::slotPlacementChanged(const QVariant& p, bool incr, bool data) |
| 1257 |
{ |
| 1258 |
Q_EMIT placementChanged(p, incr, data); |
| 1259 |
} |
| 1260 |
|
| 1261 |
bool TaskPlacement::accept() |
| 1262 |
{ |
| 1263 |
widget->accept(); |
| 1264 |
return (widget->result() == QDialog::Accepted); |
| 1265 |
} |
| 1266 |
|
| 1267 |
bool TaskPlacement::reject() |
| 1268 |
{ |
| 1269 |
widget->reject(); |
| 1270 |
return (widget->result() == QDialog::Rejected); |
| 1271 |
} |
| 1272 |
|
| 1273 |
void TaskPlacement::clicked(int id) |
| 1274 |
{ |
| 1275 |
if (id == QDialogButtonBox::Apply) { |
| 1276 |
widget->onApplyButtonClicked(); |
| 1277 |
} |
| 1278 |
} |
| 1279 |
|
| 1280 |
// ---------------------------------------------- |
| 1281 |
|
| 1282 |
void TaskPlacementPy::init_type() |
| 1283 |
{ |
| 1284 |
behaviors().name("TaskPlacement"); |
| 1285 |
behaviors().doc("TaskPlacement"); |
| 1286 |
behaviors().set_tp_new(PyMake); |
| 1287 |
// you must have overwritten the virtual functions |
| 1288 |
behaviors().supportRepr(); |
| 1289 |
behaviors().supportGetattr(); |
| 1290 |
behaviors().supportSetattr(); |
| 1291 |
// clang-format off |
| 1292 |
add_varargs_method("setPropertyName", &TaskPlacementPy::setPropertyName, |
| 1293 |
"setPropertyName(string)"); |
| 1294 |
add_varargs_method("setPlacement", &TaskPlacementPy::setPlacement, |
| 1295 |
"setPlacement(Placement)"); |
| 1296 |
add_varargs_method("setSelection", &TaskPlacementPy::setSelection, |
| 1297 |
"setSelection(list)"); |
| 1298 |
add_varargs_method("bindObject", &TaskPlacementPy::bindObject, |
| 1299 |
"bindObject()"); |
| 1300 |
add_varargs_method("setPlacementAndBindObject", &TaskPlacementPy::setPlacementAndBindObject, |
| 1301 |
"setPlacementAndBindObject(obj, string)"); |
| 1302 |
add_varargs_method("setIgnoreTransactions", &TaskPlacementPy::setIgnoreTransactions, |
| 1303 |
"setIgnoreTransactions(bool)"); |
| 1304 |
add_varargs_method("showDefaultButtons", &TaskPlacementPy::showDefaultButtons, |
| 1305 |
"showDefaultButtons(bool)"); |
| 1306 |
add_varargs_method("accept", &TaskPlacementPy::accept, |
| 1307 |
"accept()"); |
| 1308 |
add_varargs_method("reject", &TaskPlacementPy::reject, |
| 1309 |
"reject()"); |
| 1310 |
add_varargs_method("clicked", &TaskPlacementPy::clicked, |
| 1311 |
"clicked()"); |
| 1312 |
add_varargs_method("open", &TaskPlacementPy::open, |
| 1313 |
"open()"); |
| 1314 |
add_varargs_method("isAllowedAlterDocument", &TaskPlacementPy::isAllowedAlterDocument, |
| 1315 |
"isAllowedAlterDocument()"); |
| 1316 |
add_varargs_method("isAllowedAlterView", &TaskPlacementPy::isAllowedAlterView, |
| 1317 |
"isAllowedAlterView()"); |
| 1318 |
add_varargs_method("isAllowedAlterSelection", &TaskPlacementPy::isAllowedAlterSelection, |
| 1319 |
"isAllowedAlterSelection()"); |
| 1320 |
add_varargs_method("getStandardButtons", &TaskPlacementPy::getStandardButtons, |
| 1321 |
"getStandardButtons()"); |
| 1322 |
// clang-format on |
| 1323 |
} |
| 1324 |
|
| 1325 |
PyObject* TaskPlacementPy::PyMake(struct _typeobject* type, PyObject* args, PyObject* kwds) |
| 1326 |
{ |
| 1327 |
Q_UNUSED(type) |
| 1328 |
Q_UNUSED(kwds) |
| 1329 |
if (!PyArg_ParseTuple(args, "")) { |
| 1330 |
return nullptr; |
| 1331 |
} |
| 1332 |
return new TaskPlacementPy(); |
| 1333 |
} |
| 1334 |
|
| 1335 |
TaskPlacementPy::TaskPlacementPy() |
| 1336 |
: widget {new Placement} |
| 1337 |
{} |
| 1338 |
|
| 1339 |
TaskPlacementPy::~TaskPlacementPy() = default; |
| 1340 |
|
| 1341 |
Py::Object TaskPlacementPy::repr() |
| 1342 |
{ |
| 1343 |
return Py::String("TaskPlacement"); |
| 1344 |
} |
| 1345 |
|
| 1346 |
Py::Object TaskPlacementPy::getattr(const char* name) |
| 1347 |
{ |
| 1348 |
if (strcmp(name, "form") == 0) { |
| 1349 |
Gui::PythonWrapper wrap; |
| 1350 |
wrap.loadWidgetsModule(); |
| 1351 |
return wrap.fromQWidget(widget, "QDialog"); |
| 1352 |
} |
| 1353 |
return BaseType::getattr(name); |
| 1354 |
} |
| 1355 |
|
| 1356 |
int TaskPlacementPy::setattr(const char* name, const Py::Object& attr) |
| 1357 |
{ |
| 1358 |
if (strcmp(name, "form") == 0 && attr.isNone()) { |
| 1359 |
delete widget; |
| 1360 |
widget = nullptr; |
| 1361 |
return {}; |
| 1362 |
} |
| 1363 |
return BaseType::setattr(name, attr); |
| 1364 |
} |
| 1365 |
|
| 1366 |
Py::Object TaskPlacementPy::setPropertyName(const Py::Tuple& args) |
| 1367 |
{ |
| 1368 |
const char* propname {}; |
| 1369 |
if (!PyArg_ParseTuple(args.ptr(), "s", &propname)) { |
| 1370 |
throw Py::Exception(); |
| 1371 |
} |
| 1372 |
|
| 1373 |
if (widget) { |
| 1374 |
widget->setPropertyName(propname); |
| 1375 |
} |
| 1376 |
return Py::None(); |
| 1377 |
} |
| 1378 |
|
| 1379 |
Py::Object TaskPlacementPy::setPlacement(const Py::Tuple& args) |
| 1380 |
{ |
| 1381 |
PyObject* plm {}; |
| 1382 |
if (!PyArg_ParseTuple(args.ptr(), "O!", &Base::PlacementPy::Type, &plm)) { |
| 1383 |
throw Py::Exception(); |
| 1384 |
} |
| 1385 |
|
| 1386 |
if (widget) { |
| 1387 |
widget->setPlacement(*static_cast<Base::PlacementPy*>(plm)->getPlacementPtr()); |
| 1388 |
} |
| 1389 |
return Py::None(); |
| 1390 |
} |
| 1391 |
|
| 1392 |
Py::Object TaskPlacementPy::setSelection(const Py::Tuple& args) |
| 1393 |
{ |
| 1394 |
std::vector<Gui::SelectionObject> sel; |
| 1395 |
Py::Sequence list(args[0]); |
| 1396 |
|
| 1397 |
for (const auto& obj : list) { |
| 1398 |
if (PyObject_TypeCheck(obj.ptr(), &App::DocumentObjectPy::Type)) { |
| 1399 |
auto doc = static_cast<App::DocumentObjectPy*>(obj.ptr()); |
| 1400 |
sel.emplace_back(doc->getDocumentObjectPtr()); |
| 1401 |
} |
| 1402 |
} |
| 1403 |
|
| 1404 |
if (widget) { |
| 1405 |
widget->setSelection(sel); |
| 1406 |
} |
| 1407 |
return Py::None(); |
| 1408 |
} |
| 1409 |
|
| 1410 |
Py::Object TaskPlacementPy::bindObject(const Py::Tuple& args) |
| 1411 |
{ |
| 1412 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 1413 |
throw Py::Exception(); |
| 1414 |
} |
| 1415 |
|
| 1416 |
if (widget) { |
| 1417 |
widget->bindObject(); |
| 1418 |
} |
| 1419 |
|
| 1420 |
return Py::None(); |
| 1421 |
} |
| 1422 |
|
| 1423 |
Py::Object TaskPlacementPy::setPlacementAndBindObject(const Py::Tuple& args) |
| 1424 |
{ |
| 1425 |
Py::Object object = args[0]; |
| 1426 |
Py::String name = args[1]; |
| 1427 |
std::string propName = static_cast<std::string>(name); |
| 1428 |
|
| 1429 |
if (PyObject_TypeCheck(object.ptr(), &App::DocumentObjectPy::Type)) { |
| 1430 |
auto py = static_cast<App::DocumentObjectPy*>(object.ptr()); |
| 1431 |
auto obj = py->getDocumentObjectPtr(); |
| 1432 |
|
| 1433 |
if (widget) { |
| 1434 |
widget->setPlacementAndBindObject(obj, propName); |
| 1435 |
} |
| 1436 |
} |
| 1437 |
|
| 1438 |
return Py::None(); |
| 1439 |
} |
| 1440 |
|
| 1441 |
Py::Object TaskPlacementPy::setIgnoreTransactions(const Py::Tuple& args) |
| 1442 |
{ |
| 1443 |
if (widget) { |
| 1444 |
widget->setIgnoreTransactions(Py::Boolean(args[0])); |
| 1445 |
} |
| 1446 |
return Py::None(); |
| 1447 |
} |
| 1448 |
|
| 1449 |
Py::Object TaskPlacementPy::showDefaultButtons(const Py::Tuple& args) |
| 1450 |
{ |
| 1451 |
if (widget) { |
| 1452 |
widget->showDefaultButtons(Py::Boolean(args[0])); |
| 1453 |
} |
| 1454 |
return Py::None(); |
| 1455 |
} |
| 1456 |
|
| 1457 |
Py::Object TaskPlacementPy::accept(const Py::Tuple& args) |
| 1458 |
{ |
| 1459 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 1460 |
throw Py::Exception(); |
| 1461 |
} |
| 1462 |
|
| 1463 |
bool res = true; |
| 1464 |
if (widget) { |
| 1465 |
widget->accept(); |
| 1466 |
res = widget->result() == QDialog::Accepted; |
| 1467 |
} |
| 1468 |
return Py::Boolean(res); |
| 1469 |
} |
| 1470 |
|
| 1471 |
Py::Object TaskPlacementPy::reject(const Py::Tuple& args) |
| 1472 |
{ |
| 1473 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 1474 |
throw Py::Exception(); |
| 1475 |
} |
| 1476 |
|
| 1477 |
bool res = true; |
| 1478 |
if (widget) { |
| 1479 |
widget->reject(); |
| 1480 |
res = widget->result() == QDialog::Rejected; |
| 1481 |
} |
| 1482 |
return Py::Boolean(res); |
| 1483 |
} |
| 1484 |
|
| 1485 |
Py::Object TaskPlacementPy::clicked(const Py::Tuple& args) |
| 1486 |
{ |
| 1487 |
int index {}; |
| 1488 |
if (!PyArg_ParseTuple(args.ptr(), "i", &index)) { |
| 1489 |
throw Py::Exception(); |
| 1490 |
} |
| 1491 |
|
| 1492 |
if (widget && index == QDialogButtonBox::Apply) { |
| 1493 |
widget->onApplyButtonClicked(); |
| 1494 |
} |
| 1495 |
return Py::None(); |
| 1496 |
} |
| 1497 |
|
| 1498 |
Py::Object TaskPlacementPy::open(const Py::Tuple& args) |
| 1499 |
{ |
| 1500 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 1501 |
throw Py::Exception(); |
| 1502 |
} |
| 1503 |
|
| 1504 |
if (widget) { |
| 1505 |
widget->open(); |
| 1506 |
} |
| 1507 |
return Py::None(); |
| 1508 |
} |
| 1509 |
|
| 1510 |
Py::Object TaskPlacementPy::isAllowedAlterDocument(const Py::Tuple& args) |
| 1511 |
{ |
| 1512 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 1513 |
throw Py::Exception(); |
| 1514 |
} |
| 1515 |
return Py::Boolean(true); |
| 1516 |
} |
| 1517 |
|
| 1518 |
Py::Object TaskPlacementPy::isAllowedAlterView(const Py::Tuple& args) |
| 1519 |
{ |
| 1520 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 1521 |
throw Py::Exception(); |
| 1522 |
} |
| 1523 |
return Py::Boolean(true); |
| 1524 |
} |
| 1525 |
|
| 1526 |
Py::Object TaskPlacementPy::isAllowedAlterSelection(const Py::Tuple& args) |
| 1527 |
{ |
| 1528 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 1529 |
throw Py::Exception(); |
| 1530 |
} |
| 1531 |
return Py::Boolean(true); |
| 1532 |
} |
| 1533 |
|
| 1534 |
Py::Object TaskPlacementPy::getStandardButtons(const Py::Tuple& args) |
| 1535 |
{ |
| 1536 |
if (!PyArg_ParseTuple(args.ptr(), "")) { |
| 1537 |
throw Py::Exception(); |
| 1538 |
} |
| 1539 |
auto buttons = QDialogButtonBox::Ok | QDialogButtonBox::Cancel | QDialogButtonBox::Apply; |
| 1540 |
return Py::Long(static_cast<int>(buttons)); |
| 1541 |
} |
| 1542 |
|
| 1543 |
#include "moc_Placement.cpp" |