| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2015 Thomas Anderson <blobfish[at]gmx.com> * |
| 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 |
#include <cassert> |
| 24 |
#include <limits> |
| 25 |
#include <QApplication> |
| 26 |
|
| 27 |
#include <View3DInventorViewer.h> |
| 28 |
#include <Utilities.h> |
| 29 |
|
| 30 |
#include <App/Document.h> |
| 31 |
#include <App/GeoFeature.h> |
| 32 |
#include <App/Services.h> |
| 33 |
#include <Base/Precision.h> |
| 34 |
#include <Base/ServiceProvider.h> |
| 35 |
#include <Base/Tools.h> |
| 36 |
|
| 37 |
#include "Document.h" // must be before TaskTransform.h |
| 38 |
#include "Application.h" |
| 39 |
#include "BitmapFactory.h" |
| 40 |
#include "Command.h" |
| 41 |
#include "Inventor/Draggers/SoTransformDragger.h" |
| 42 |
#include "QuantitySpinBox.h" |
| 43 |
#include "ViewProviderDragger.h" |
| 44 |
#include "TaskView/TaskView.h" |
| 45 |
|
| 46 |
#include "TaskTransform.h" |
| 47 |
#include "ui_TaskTransform.h" |
| 48 |
|
| 49 |
#include <Inventor/nodes/SoPickStyle.h> |
| 50 |
|
| 51 |
using namespace Gui; |
| 52 |
|
| 53 |
namespace |
| 54 |
{ |
| 55 |
|
| 56 |
void alignGridLayoutColumns(const std::list<QGridLayout*>& layouts, unsigned column = 0) |
| 57 |
{ |
| 58 |
std::vector<int> widths; |
| 59 |
|
| 60 |
auto getActualWidth = [&](const QGridLayout* layout) -> int { |
| 61 |
if (auto const item = layout->itemAtPosition(0, column)) { |
| 62 |
return item->geometry().width(); |
| 63 |
} |
| 64 |
|
| 65 |
return 0; |
| 66 |
}; |
| 67 |
|
| 68 |
for (const auto layout : layouts) { |
| 69 |
widths.push_back(getActualWidth(layout)); |
| 70 |
} |
| 71 |
|
| 72 |
const auto maxWidth = *std::max_element(widths.begin(), widths.end()); |
| 73 |
for (const auto layout : layouts) { |
| 74 |
layout->setColumnMinimumWidth(column, maxWidth); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
} // namespace |
| 79 |
|
| 80 |
TaskTransform::TaskTransform( |
| 81 |
Gui::ViewProviderDragger* vp, |
| 82 |
Gui::SoTransformDragger* dragger, |
| 83 |
QWidget* parent, |
| 84 |
App::SubObjectPlacementProvider* subObjectPlacementProvider, |
| 85 |
App::CenterOfMassProvider* centerOfMassProvider |
| 86 |
) |
| 87 |
: TaskBox(Gui::BitmapFactory().pixmap("Std_TransformManip.svg"), tr("Transform"), false, parent) |
| 88 |
, vp(vp) |
| 89 |
, subObjectPlacementProvider(subObjectPlacementProvider) |
| 90 |
, centerOfMassProvider(centerOfMassProvider) |
| 91 |
, dragger(dragger) |
| 92 |
, ui(new Ui_TaskTransformDialog) |
| 93 |
{ |
| 94 |
blockSelection(true); |
| 95 |
|
| 96 |
dragger->addStartCallback(dragStartCallback, this); |
| 97 |
dragger->addMotionCallback(dragMotionCallback, this); |
| 98 |
|
| 99 |
vp->resetTransformOrigin(); |
| 100 |
|
| 101 |
referencePlacement = vp->getObjectPlacement(); |
| 102 |
referenceRotation = referencePlacement.getRotation(); |
| 103 |
|
| 104 |
globalOrigin = vp->getObjectPlacement() |
| 105 |
* App::GeoFeature::getGlobalPlacement(vp->getObject()).inverse(); |
| 106 |
|
| 107 |
setupGui(); |
| 108 |
} |
| 109 |
|
| 110 |
TaskTransform::~TaskTransform() |
| 111 |
{ |
| 112 |
Gui::Application::Instance->commandManager() |
| 113 |
.getCommandByName("Std_OrthographicCamera") |
| 114 |
->setEnabled(true); |
| 115 |
|
| 116 |
Gui::Application::Instance->commandManager() |
| 117 |
.getCommandByName("Std_PerspectiveCamera") |
| 118 |
->setEnabled(true); |
| 119 |
|
| 120 |
savePreferences(); |
| 121 |
} |
| 122 |
|
| 123 |
void TaskTransform::dragStartCallback([[maybe_unused]] void* data, [[maybe_unused]] SoDragger* dragger) |
| 124 |
{ |
| 125 |
// This is called when a manipulator is about to manipulating |
| 126 |
if (firstDrag) { |
| 127 |
Gui::Application::Instance->activeDocument()->openCommand( |
| 128 |
QT_TRANSLATE_NOOP("Command", "Transform") |
| 129 |
); |
| 130 |
firstDrag = false; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
void TaskTransform::dragMotionCallback(void* data, [[maybe_unused]] SoDragger* dragger) |
| 135 |
{ |
| 136 |
auto task = static_cast<TaskTransform*>(data); |
| 137 |
|
| 138 |
const auto currentRotation = task->vp->getOriginalDraggerPlacement().getRotation(); |
| 139 |
const auto updatedRotation = task->vp->getDraggerPlacement().getRotation(); |
| 140 |
|
| 141 |
const auto rotationAxisHasChanged = [task](auto first, auto second) { |
| 142 |
double alpha, beta, gamma; |
| 143 |
|
| 144 |
(first.inverse() * second).getEulerAngles(task->eulerSequence(), alpha, beta, gamma); |
| 145 |
|
| 146 |
auto angles = {alpha, beta, gamma}; |
| 147 |
const int changed = std::count_if(angles.begin(), angles.end(), [](double angle) { |
| 148 |
return std::fabs(angle) > tolerance; |
| 149 |
}); |
| 150 |
|
| 151 |
// if representation of both differs by more than one axis the axis of rotation must be |
| 152 |
// different |
| 153 |
return changed > 1; |
| 154 |
}; |
| 155 |
|
| 156 |
if (!updatedRotation.isSame(currentRotation, tolerance)) { |
| 157 |
task->resetReferencePlacement(); |
| 158 |
|
| 159 |
if (rotationAxisHasChanged(task->referenceRotation, updatedRotation)) { |
| 160 |
task->referenceRotation = currentRotation; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
task->updatePositionAndRotationUi(); |
| 165 |
} |
| 166 |
|
| 167 |
void TaskTransform::loadPlacementModeItems() const |
| 168 |
{ |
| 169 |
ui->placementComboBox->clear(); |
| 170 |
|
| 171 |
ui->placementComboBox->addItem( |
| 172 |
tr("Object origin"), |
| 173 |
QVariant::fromValue(PlacementMode::ObjectOrigin) |
| 174 |
); |
| 175 |
|
| 176 |
if (centerOfMassProvider->supports(vp->getObject())) { |
| 177 |
ui->placementComboBox->addItem( |
| 178 |
tr("Center of mass / centroid"), |
| 179 |
QVariant::fromValue(PlacementMode::Centroid) |
| 180 |
); |
| 181 |
} |
| 182 |
|
| 183 |
if (subObjectPlacementProvider) { |
| 184 |
ui->placementComboBox->addItem(tr("Custom"), QVariant::fromValue(PlacementMode::Custom)); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
void TaskTransform::loadPositionModeItems() const |
| 189 |
{ |
| 190 |
ui->positionModeComboBox->clear(); |
| 191 |
ui->positionModeComboBox->addItem(tr("Local"), QVariant::fromValue(PositionMode::Local)); |
| 192 |
ui->positionModeComboBox->addItem(tr("Global"), QVariant::fromValue(PositionMode::Global)); |
| 193 |
} |
| 194 |
|
| 195 |
void TaskTransform::setupGui() |
| 196 |
{ |
| 197 |
auto proxy = new QWidget(this); |
| 198 |
ui->setupUi(proxy); |
| 199 |
this->groupLayout()->addWidget(proxy); |
| 200 |
|
| 201 |
loadPlacementModeItems(); |
| 202 |
loadPositionModeItems(); |
| 203 |
|
| 204 |
ui->referencePickerWidget->hide(); |
| 205 |
ui->alignRotationCheckBox->hide(); |
| 206 |
|
| 207 |
for (auto positionSpinBox : |
| 208 |
{ui->translationIncrementSpinBox, |
| 209 |
ui->xPositionSpinBox, |
| 210 |
ui->yPositionSpinBox, |
| 211 |
ui->zPositionSpinBox}) { |
| 212 |
positionSpinBox->setUnit(Base::Unit::Length); |
| 213 |
} |
| 214 |
|
| 215 |
for (auto rotationSpinBox : |
| 216 |
{ui->rotationIncrementSpinBox, |
| 217 |
ui->xRotationSpinBox, |
| 218 |
ui->yRotationSpinBox, |
| 219 |
ui->zRotationSpinBox}) { |
| 220 |
rotationSpinBox->setUnit(Base::Unit::Angle); |
| 221 |
} |
| 222 |
|
| 223 |
connect( |
| 224 |
ui->translationIncrementSpinBox, |
| 225 |
qOverload<double>(&QuantitySpinBox::valueChanged), |
| 226 |
this, |
| 227 |
[this](double) { updateIncrements(); } |
| 228 |
); |
| 229 |
connect( |
| 230 |
ui->rotationIncrementSpinBox, |
| 231 |
qOverload<double>(&QuantitySpinBox::valueChanged), |
| 232 |
this, |
| 233 |
[this](double) { updateIncrements(); } |
| 234 |
); |
| 235 |
connect( |
| 236 |
ui->positionModeComboBox, |
| 237 |
qOverload<int>(&QComboBox::currentIndexChanged), |
| 238 |
this, |
| 239 |
&TaskTransform::onCoordinateSystemChange |
| 240 |
); |
| 241 |
connect( |
| 242 |
ui->placementComboBox, |
| 243 |
qOverload<int>(&QComboBox::currentIndexChanged), |
| 244 |
this, |
| 245 |
&TaskTransform::onPlacementModeChange |
| 246 |
); |
| 247 |
connect( |
| 248 |
ui->pickTransformOriginButton, |
| 249 |
&QPushButton::clicked, |
| 250 |
this, |
| 251 |
&TaskTransform::onPickTransformOrigin |
| 252 |
); |
| 253 |
connect(ui->alignToOtherObjectButton, &QPushButton::clicked, this, &TaskTransform::onAlignToOtherObject); |
| 254 |
connect(ui->moveOptionsButton, &QPushButton::toggled, ui->frameMoveOptions, &QWidget::setVisible); |
| 255 |
connect(ui->translateCheckbox, &QCheckBox::toggled, this, [this](bool translateChecked) { |
| 256 |
ui->matchXcheckbox->setEnabled(translateChecked); |
| 257 |
ui->matchYcheckbox->setEnabled(translateChecked); |
| 258 |
ui->matchZcheckbox->setEnabled(translateChecked); |
| 259 |
}); |
| 260 |
connect(ui->rotateCheckbox, &QCheckBox::toggled, this, [this](bool rotateChecked) { |
| 261 |
ui->alignXcheckbox->setEnabled(rotateChecked); |
| 262 |
ui->alignYcheckbox->setEnabled(rotateChecked); |
| 263 |
ui->alignZcheckbox->setEnabled(rotateChecked); |
| 264 |
}); |
| 265 |
|
| 266 |
connect(ui->flipPartButton, &QPushButton::clicked, this, &TaskTransform::onFlip); |
| 267 |
|
| 268 |
connect(ui->alignRotationCheckBox, &QCheckBox::clicked, this, &TaskTransform::onAlignRotationChanged); |
| 269 |
|
| 270 |
for (auto positionSpinBox : {ui->xPositionSpinBox, ui->yPositionSpinBox, ui->zPositionSpinBox}) { |
| 271 |
connect(positionSpinBox, qOverload<double>(&QuantitySpinBox::valueChanged), this, [this](double) { |
| 272 |
onPositionChange(); |
| 273 |
}); |
| 274 |
} |
| 275 |
|
| 276 |
for (auto rotationSpinBox : {ui->xRotationSpinBox, ui->yRotationSpinBox, ui->zRotationSpinBox}) { |
| 277 |
connect( |
| 278 |
rotationSpinBox, |
| 279 |
qOverload<double>(&QuantitySpinBox::valueChanged), |
| 280 |
this, |
| 281 |
[this, rotationSpinBox](double) { onRotationChange(rotationSpinBox); } |
| 282 |
); |
| 283 |
} |
| 284 |
|
| 285 |
alignGridLayoutColumns( |
| 286 |
{ui->absolutePositionLayout, |
| 287 |
ui->absoluteRotationLayout, |
| 288 |
ui->transformOriginLayout, |
| 289 |
ui->referencePickerLayout} |
| 290 |
); |
| 291 |
|
| 292 |
loadPreferences(); |
| 293 |
|
| 294 |
updateInputLabels(); |
| 295 |
updateDraggerLabels(); |
| 296 |
updateIncrements(); |
| 297 |
updatePositionAndRotationUi(); |
| 298 |
} |
| 299 |
|
| 300 |
void TaskTransform::loadPreferences() |
| 301 |
{ |
| 302 |
double lastTranslationIncrement = hGrp->GetFloat("LastTranslationIncrement", 1.0); |
| 303 |
double lastRotationIncrement = hGrp->GetFloat("LastRotationIncrement", 5.0); |
| 304 |
|
| 305 |
ui->translationIncrementSpinBox->setValue(lastTranslationIncrement); |
| 306 |
ui->rotationIncrementSpinBox->setValue(lastRotationIncrement); |
| 307 |
ui->moveOptionsButton->setIcon(Gui::BitmapFactory().pixmap("Std_DlgParameter")); |
| 308 |
} |
| 309 |
|
| 310 |
void TaskTransform::savePreferences() |
| 311 |
{ |
| 312 |
hGrp->SetFloat("LastTranslationIncrement", ui->translationIncrementSpinBox->rawValue()); |
| 313 |
hGrp->SetFloat("LastRotationIncrement", ui->rotationIncrementSpinBox->rawValue()); |
| 314 |
} |
| 315 |
|
| 316 |
void TaskTransform::updatePositionAndRotationUi() const |
| 317 |
{ |
| 318 |
const auto referencePlacement = currentCoordinateSystem().origin; |
| 319 |
|
| 320 |
const auto xyzPlacement = vp->getDraggerPlacement(); |
| 321 |
const auto uvwPlacement = referencePlacement.inverse() * xyzPlacement; |
| 322 |
|
| 323 |
auto fixNegativeZero = [](const double value) { |
| 324 |
return std::fabs(value) < Base::Precision::Confusion() ? 0.0 : value; |
| 325 |
}; |
| 326 |
|
| 327 |
auto setPositionValues = [&](const Base::Vector3d& vec, auto* x, auto* y, auto* z) { |
| 328 |
[[maybe_unused]] |
| 329 |
auto blockers |
| 330 |
= {QSignalBlocker(x), QSignalBlocker(y), QSignalBlocker(z)}; |
| 331 |
|
| 332 |
x->setValue(fixNegativeZero(vec.x)); |
| 333 |
y->setValue(fixNegativeZero(vec.y)); |
| 334 |
z->setValue(fixNegativeZero(vec.z)); |
| 335 |
}; |
| 336 |
|
| 337 |
auto setRotationValues = [&](const Base::Rotation& rot, auto* x, auto* y, auto* z) { |
| 338 |
[[maybe_unused]] |
| 339 |
auto blockers |
| 340 |
= {QSignalBlocker(x), QSignalBlocker(y), QSignalBlocker(z)}; |
| 341 |
|
| 342 |
double alpha, beta, gamma; |
| 343 |
rot.getEulerAngles(eulerSequence(), alpha, beta, gamma); |
| 344 |
|
| 345 |
x->setValue(fixNegativeZero(alpha)); |
| 346 |
y->setValue(fixNegativeZero(beta)); |
| 347 |
z->setValue(fixNegativeZero(gamma)); |
| 348 |
}; |
| 349 |
|
| 350 |
setPositionValues( |
| 351 |
uvwPlacement.getPosition(), |
| 352 |
ui->xPositionSpinBox, |
| 353 |
ui->yPositionSpinBox, |
| 354 |
ui->zPositionSpinBox |
| 355 |
); |
| 356 |
|
| 357 |
setRotationValues( |
| 358 |
positionMode == PositionMode::Local ? referenceRotation.inverse() * xyzPlacement.getRotation() |
| 359 |
: uvwPlacement.getRotation(), |
| 360 |
ui->xRotationSpinBox, |
| 361 |
ui->yRotationSpinBox, |
| 362 |
ui->zRotationSpinBox |
| 363 |
); |
| 364 |
} |
| 365 |
|
| 366 |
void TaskTransform::updateInputLabels() const |
| 367 |
{ |
| 368 |
auto [xLabel, yLabel, zLabel] = currentCoordinateSystem().labels; |
| 369 |
|
| 370 |
ui->xPositionLabel->setText(QString::fromStdString(xLabel)); |
| 371 |
ui->yPositionLabel->setText(QString::fromStdString(yLabel)); |
| 372 |
ui->zPositionLabel->setText(QString::fromStdString(zLabel)); |
| 373 |
|
| 374 |
ui->xRotationLabel->setText(QString::fromStdString(xLabel)); |
| 375 |
ui->yRotationLabel->setText(QString::fromStdString(yLabel)); |
| 376 |
ui->zRotationLabel->setText(QString::fromStdString(zLabel)); |
| 377 |
} |
| 378 |
|
| 379 |
void TaskTransform::updateDraggerLabels() const |
| 380 |
{ |
| 381 |
auto coordinateSystem = isDraggerAlignedToCoordinateSystem() ? globalCoordinateSystem() |
| 382 |
: localCoordinateSystem(); |
| 383 |
|
| 384 |
auto [xLabel, yLabel, zLabel] = coordinateSystem.labels; |
| 385 |
|
| 386 |
dragger->xAxisLabel.setValue(xLabel.c_str()); |
| 387 |
dragger->yAxisLabel.setValue(yLabel.c_str()); |
| 388 |
dragger->zAxisLabel.setValue(zLabel.c_str()); |
| 389 |
} |
| 390 |
|
| 391 |
void TaskTransform::updateIncrements() const |
| 392 |
{ |
| 393 |
dragger->translationIncrement.setValue( |
| 394 |
std::max(ui->translationIncrementSpinBox->rawValue(), 0.001) |
| 395 |
); |
| 396 |
dragger->rotationIncrement.setValue( |
| 397 |
Base::toRadians(std::max(ui->rotationIncrementSpinBox->rawValue(), 0.01)) |
| 398 |
); |
| 399 |
} |
| 400 |
|
| 401 |
void TaskTransform::setSelectionMode(SelectionMode mode) |
| 402 |
{ |
| 403 |
Gui::Selection().clearSelection(); |
| 404 |
|
| 405 |
SoPickStyle* draggerPickStyle = SO_GET_PART(dragger, "pickStyle", SoPickStyle); |
| 406 |
|
| 407 |
ui->pickTransformOriginButton->setText(tr("Pick Reference")); |
| 408 |
ui->alignToOtherObjectButton->setText(tr("Move to Other Object")); |
| 409 |
|
| 410 |
switch (mode) { |
| 411 |
case SelectionMode::SelectTransformOrigin: |
| 412 |
draggerPickStyle->style = SoPickStyle::UNPICKABLE; |
| 413 |
draggerPickStyle->setOverride(true); |
| 414 |
blockSelection(false); |
| 415 |
ui->referenceLineEdit->setText(tr("Select face, edge, or vertex…")); |
| 416 |
ui->pickTransformOriginButton->setText(tr("Cancel")); |
| 417 |
break; |
| 418 |
|
| 419 |
case SelectionMode::SelectAlignTarget: |
| 420 |
draggerPickStyle->style = SoPickStyle::UNPICKABLE; |
| 421 |
draggerPickStyle->setOverride(true); |
| 422 |
ui->alignToOtherObjectButton->setText(tr("Cancel")); |
| 423 |
blockSelection(false); |
| 424 |
break; |
| 425 |
|
| 426 |
case SelectionMode::None: |
| 427 |
draggerPickStyle->style = SoPickStyle::SHAPE_ON_TOP; |
| 428 |
draggerPickStyle->setOverride(false); |
| 429 |
blockSelection(true); |
| 430 |
|
| 431 |
vp->setTransformOrigin(vp->getTransformOrigin()); |
| 432 |
|
| 433 |
break; |
| 434 |
} |
| 435 |
|
| 436 |
selectionMode = mode; |
| 437 |
|
| 438 |
updateSpinBoxesReadOnlyStatus(); |
| 439 |
} |
| 440 |
|
| 441 |
TaskTransform::SelectionMode TaskTransform::getSelectionMode() const |
| 442 |
{ |
| 443 |
return selectionMode; |
| 444 |
} |
| 445 |
|
| 446 |
TaskTransform::CoordinateSystem TaskTransform::localCoordinateSystem() const |
| 447 |
{ |
| 448 |
auto origin = referencePlacement; |
| 449 |
origin.setRotation(vp->getDraggerPlacement().getRotation()); |
| 450 |
|
| 451 |
return {{"U", "V", "W"}, origin}; |
| 452 |
} |
| 453 |
|
| 454 |
TaskTransform::CoordinateSystem TaskTransform::globalCoordinateSystem() const |
| 455 |
{ |
| 456 |
return {{"X", "Y", "Z"}, globalOrigin}; |
| 457 |
} |
| 458 |
|
| 459 |
TaskTransform::CoordinateSystem TaskTransform::currentCoordinateSystem() const |
| 460 |
{ |
| 461 |
return ui->positionModeComboBox->currentIndex() == 0 ? localCoordinateSystem() |
| 462 |
: globalCoordinateSystem(); |
| 463 |
} |
| 464 |
|
| 465 |
Base::Rotation::EulerSequence TaskTransform::eulerSequence() const |
| 466 |
{ |
| 467 |
return positionMode == PositionMode::Local ? Base::Rotation::Intrinsic_XYZ |
| 468 |
: Base::Rotation::Extrinsic_XYZ; |
| 469 |
} |
| 470 |
|
| 471 |
void TaskTransform::onSelectionChanged(const SelectionChanges& msg) |
| 472 |
{ |
| 473 |
const auto isSupportedMessage = msg.Type == SelectionChanges::AddSelection |
| 474 |
|| msg.Type == SelectionChanges::SetPreselect; |
| 475 |
|
| 476 |
if (!isSupportedMessage) { |
| 477 |
return; |
| 478 |
} |
| 479 |
|
| 480 |
if (!subObjectPlacementProvider) { |
| 481 |
return; |
| 482 |
} |
| 483 |
|
| 484 |
if (!msg.pOriginalMsg) { |
| 485 |
// this should not happen! Original should contain unresolved message. |
| 486 |
return; |
| 487 |
} |
| 488 |
|
| 489 |
auto doc = Application::Instance->getDocument(msg.pDocName); |
| 490 |
auto obj = doc->getDocument()->getObject(msg.pObjectName); |
| 491 |
|
| 492 |
auto orgDoc = Application::Instance->getDocument(msg.pOriginalMsg->pDocName); |
| 493 |
auto orgObj = orgDoc->getDocument()->getObject(msg.pOriginalMsg->pObjectName); |
| 494 |
|
| 495 |
auto globalPlacement = App::GeoFeature::getGlobalPlacement(obj, orgObj, msg.pOriginalMsg->pSubName); |
| 496 |
auto localPlacement = App::GeoFeature::getPlacementFromProp(obj, "Placement"); |
| 497 |
auto rootPlacement = App::GeoFeature::getGlobalPlacement(vp->getObject()); |
| 498 |
auto attachedPlacement = subObjectPlacementProvider->calculate(msg.Object, localPlacement); |
| 499 |
|
| 500 |
auto selectedObjectPlacement = rootPlacement.inverse() * globalPlacement * attachedPlacement; |
| 501 |
|
| 502 |
auto label = QStringLiteral("%1#%2.%3") |
| 503 |
.arg( |
| 504 |
QLatin1String(msg.pOriginalMsg->pObjectName), |
| 505 |
QLatin1String(msg.pObjectName), |
| 506 |
QLatin1String(msg.pSubName) |
| 507 |
); |
| 508 |
|
| 509 |
switch (selectionMode) { |
| 510 |
case SelectionMode::SelectTransformOrigin: { |
| 511 |
if (msg.Type == SelectionChanges::AddSelection) { |
| 512 |
ui->referenceLineEdit->setText(label); |
| 513 |
customTransformOrigin = selectedObjectPlacement; |
| 514 |
updateTransformOrigin(); |
| 515 |
setSelectionMode(SelectionMode::None); |
| 516 |
} |
| 517 |
else { |
| 518 |
vp->setTransformOrigin(selectedObjectPlacement); |
| 519 |
} |
| 520 |
|
| 521 |
break; |
| 522 |
} |
| 523 |
|
| 524 |
case SelectionMode::SelectAlignTarget: { |
| 525 |
vp->setDraggerPlacement(vp->getObjectPlacement() * selectedObjectPlacement); |
| 526 |
|
| 527 |
if (msg.Type == SelectionChanges::AddSelection) { |
| 528 |
moveObjectToDragger(getRelevantComponents()); |
| 529 |
|
| 530 |
setSelectionMode(SelectionMode::None); |
| 531 |
} |
| 532 |
|
| 533 |
break; |
| 534 |
} |
| 535 |
|
| 536 |
default: |
| 537 |
// no-op |
| 538 |
break; |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
void TaskTransform::onAlignRotationChanged() |
| 543 |
{ |
| 544 |
updateDraggerLabels(); |
| 545 |
updateTransformOrigin(); |
| 546 |
} |
| 547 |
|
| 548 |
void TaskTransform::onAlignToOtherObject() |
| 549 |
{ |
| 550 |
if (selectionMode == SelectionMode::SelectAlignTarget) { |
| 551 |
setSelectionMode(SelectionMode::None); |
| 552 |
return; |
| 553 |
} |
| 554 |
|
| 555 |
setSelectionMode(SelectionMode::SelectAlignTarget); |
| 556 |
} |
| 557 |
|
| 558 |
ViewProviderDragger::DraggerComponents TaskTransform::getRelevantComponents() |
| 559 |
{ |
| 560 |
// Check which dragger components should be considered |
| 561 |
ViewProviderDragger::DraggerComponents components; |
| 562 |
|
| 563 |
if (ui->matchXcheckbox->isChecked()) { |
| 564 |
components |= ViewProviderDragger::DraggerComponent::XPos; |
| 565 |
} |
| 566 |
if (ui->matchYcheckbox->isChecked()) { |
| 567 |
components |= ViewProviderDragger::DraggerComponent::YPos; |
| 568 |
} |
| 569 |
if (ui->matchZcheckbox->isChecked()) { |
| 570 |
components |= ViewProviderDragger::DraggerComponent::ZPos; |
| 571 |
} |
| 572 |
if (ui->alignXcheckbox->isChecked()) { |
| 573 |
components |= ViewProviderDragger::DraggerComponent::XRot; |
| 574 |
} |
| 575 |
if (ui->alignYcheckbox->isChecked()) { |
| 576 |
components |= ViewProviderDragger::DraggerComponent::YRot; |
| 577 |
} |
| 578 |
if (ui->alignZcheckbox->isChecked()) { |
| 579 |
components |= ViewProviderDragger::DraggerComponent::ZRot; |
| 580 |
} |
| 581 |
if (!ui->translateCheckbox->isChecked()) { |
| 582 |
components &= ~ViewProviderDragger::DraggerComponent::XPos; |
| 583 |
components &= ~ViewProviderDragger::DraggerComponent::YPos; |
| 584 |
components &= ~ViewProviderDragger::DraggerComponent::ZPos; |
| 585 |
} |
| 586 |
if (!ui->rotateCheckbox->isChecked()) { |
| 587 |
components &= ~ViewProviderDragger::DraggerComponent::XRot; |
| 588 |
components &= ~ViewProviderDragger::DraggerComponent::YRot; |
| 589 |
components &= ~ViewProviderDragger::DraggerComponent::ZRot; |
| 590 |
} |
| 591 |
|
| 592 |
return components; |
| 593 |
} |
| 594 |
|
| 595 |
void TaskTransform::moveObjectToDragger(ViewProviderDragger::DraggerComponents components) |
| 596 |
{ |
| 597 |
vp->updateTransformFromDragger(); |
| 598 |
vp->updatePlacementFromDragger(components); |
| 599 |
|
| 600 |
resetReferenceRotation(); |
| 601 |
resetReferencePlacement(); |
| 602 |
|
| 603 |
updatePositionAndRotationUi(); |
| 604 |
} |
| 605 |
|
| 606 |
void TaskTransform::onFlip() |
| 607 |
{ |
| 608 |
auto placement = vp->getDraggerPlacement(); |
| 609 |
|
| 610 |
placement.setRotation( |
| 611 |
placement.getRotation() * Base::Rotation::fromNormalVector(Base::Vector3d(0, 0, -1)) |
| 612 |
); |
| 613 |
|
| 614 |
vp->setDraggerPlacement(placement); |
| 615 |
|
| 616 |
moveObjectToDragger(); |
| 617 |
} |
| 618 |
|
| 619 |
void TaskTransform::onPickTransformOrigin() |
| 620 |
{ |
| 621 |
setSelectionMode( |
| 622 |
selectionMode == SelectionMode::None ? SelectionMode::SelectTransformOrigin |
| 623 |
: SelectionMode::None |
| 624 |
); |
| 625 |
} |
| 626 |
|
| 627 |
void TaskTransform::onPlacementModeChange([[maybe_unused]] int index) |
| 628 |
{ |
| 629 |
placementMode = ui->placementComboBox->currentData().value<PlacementMode>(); |
| 630 |
|
| 631 |
updateTransformOrigin(); |
| 632 |
} |
| 633 |
|
| 634 |
void TaskTransform::updateTransformOrigin() |
| 635 |
{ |
| 636 |
auto getTransformOrigin = [this](const PlacementMode& mode) -> Base::Placement { |
| 637 |
switch (mode) { |
| 638 |
case PlacementMode::ObjectOrigin: |
| 639 |
return {}; |
| 640 |
case PlacementMode::Centroid: |
| 641 |
if (const auto com = centerOfMassProvider->ofDocumentObject(vp->getObject())) { |
| 642 |
return {*com, {}}; |
| 643 |
} |
| 644 |
return {}; |
| 645 |
case PlacementMode::Custom: |
| 646 |
return customTransformOrigin.value_or(Base::Placement {}); |
| 647 |
default: |
| 648 |
return {}; |
| 649 |
} |
| 650 |
}; |
| 651 |
|
| 652 |
ui->referencePickerWidget->setVisible(placementMode == PlacementMode::Custom); |
| 653 |
|
| 654 |
if (placementMode == PlacementMode::Custom && !customTransformOrigin.has_value()) { |
| 655 |
setSelectionMode(SelectionMode::SelectTransformOrigin); |
| 656 |
return; |
| 657 |
} |
| 658 |
|
| 659 |
auto transformOrigin = getTransformOrigin(placementMode); |
| 660 |
if (isDraggerAlignedToCoordinateSystem()) { |
| 661 |
transformOrigin.setRotation( |
| 662 |
(vp->getObjectPlacement().inverse() * globalCoordinateSystem().origin).getRotation() |
| 663 |
); |
| 664 |
} |
| 665 |
|
| 666 |
vp->setTransformOrigin(transformOrigin); |
| 667 |
|
| 668 |
resetReferencePlacement(); |
| 669 |
resetReferenceRotation(); |
| 670 |
|
| 671 |
updatePositionAndRotationUi(); |
| 672 |
updateDraggerLabels(); |
| 673 |
} |
| 674 |
|
| 675 |
void TaskTransform::updateSpinBoxesReadOnlyStatus() const |
| 676 |
{ |
| 677 |
const bool isReadOnly = selectionMode != SelectionMode::None; |
| 678 |
|
| 679 |
const auto controls = { |
| 680 |
ui->xPositionSpinBox, |
| 681 |
ui->yPositionSpinBox, |
| 682 |
ui->zPositionSpinBox, |
| 683 |
ui->xRotationSpinBox, |
| 684 |
ui->yRotationSpinBox, |
| 685 |
ui->zRotationSpinBox, |
| 686 |
}; |
| 687 |
|
| 688 |
for (const auto& control : controls) { |
| 689 |
control->setReadOnly(isReadOnly); |
| 690 |
} |
| 691 |
} |
| 692 |
|
| 693 |
void TaskTransform::resetReferencePlacement() |
| 694 |
{ |
| 695 |
referencePlacement = vp->getDraggerPlacement(); |
| 696 |
} |
| 697 |
|
| 698 |
void TaskTransform::resetReferenceRotation() |
| 699 |
{ |
| 700 |
referenceRotation = vp->getDraggerPlacement().getRotation(); |
| 701 |
} |
| 702 |
|
| 703 |
bool TaskTransform::isDraggerAlignedToCoordinateSystem() const |
| 704 |
{ |
| 705 |
return positionMode == PositionMode::Global && ui->alignRotationCheckBox->isChecked(); |
| 706 |
} |
| 707 |
|
| 708 |
void TaskTransform::onTransformOriginReset() |
| 709 |
{ |
| 710 |
vp->resetTransformOrigin(); |
| 711 |
} |
| 712 |
|
| 713 |
void TaskTransform::onCoordinateSystemChange([[maybe_unused]] int mode) |
| 714 |
{ |
| 715 |
positionMode = ui->positionModeComboBox->currentData().value<PositionMode>(); |
| 716 |
|
| 717 |
ui->alignRotationCheckBox->setVisible(positionMode != PositionMode::Local); |
| 718 |
|
| 719 |
updateInputLabels(); |
| 720 |
updatePositionAndRotationUi(); |
| 721 |
updateTransformOrigin(); |
| 722 |
} |
| 723 |
|
| 724 |
void TaskTransform::onPositionChange() |
| 725 |
{ |
| 726 |
const auto uvwPosition = Base::Vector3d( |
| 727 |
ui->xPositionSpinBox->rawValue(), |
| 728 |
ui->yPositionSpinBox->rawValue(), |
| 729 |
ui->zPositionSpinBox->rawValue() |
| 730 |
); |
| 731 |
|
| 732 |
const auto xyzPosition = currentCoordinateSystem().origin.getPosition() |
| 733 |
+ currentCoordinateSystem().origin.getRotation().multVec(uvwPosition); |
| 734 |
|
| 735 |
const auto placement = vp->getDraggerPlacement(); |
| 736 |
|
| 737 |
vp->setDraggerPlacement({xyzPosition, placement.getRotation()}); |
| 738 |
|
| 739 |
vp->updateTransformFromDragger(); |
| 740 |
vp->updatePlacementFromDragger(); |
| 741 |
} |
| 742 |
|
| 743 |
void TaskTransform::onRotationChange(QuantitySpinBox* changed) |
| 744 |
{ |
| 745 |
if (positionMode == PositionMode::Local) { |
| 746 |
for (auto rotationSpinBox : |
| 747 |
{ui->xRotationSpinBox, ui->yRotationSpinBox, ui->zRotationSpinBox}) { |
| 748 |
QSignalBlocker blocker(rotationSpinBox); |
| 749 |
|
| 750 |
// if any other spinbox contains non-zero value we need to reset rotation reference first |
| 751 |
if (std::fabs(rotationSpinBox->rawValue()) > tolerance && rotationSpinBox != changed) { |
| 752 |
resetReferenceRotation(); |
| 753 |
rotationSpinBox->setValue(0.0); |
| 754 |
} |
| 755 |
} |
| 756 |
} |
| 757 |
|
| 758 |
const auto uvwRotation = Base::Rotation::fromEulerAngles( |
| 759 |
eulerSequence(), |
| 760 |
ui->xRotationSpinBox->rawValue(), |
| 761 |
ui->yRotationSpinBox->rawValue(), |
| 762 |
ui->zRotationSpinBox->rawValue() |
| 763 |
); |
| 764 |
|
| 765 |
auto referenceRotation = positionMode == PositionMode::Local |
| 766 |
? this->referenceRotation |
| 767 |
: currentCoordinateSystem().origin.getRotation(); |
| 768 |
|
| 769 |
const auto xyzRotation = referenceRotation * uvwRotation; |
| 770 |
|
| 771 |
const auto placement = vp->getDraggerPlacement(); |
| 772 |
|
| 773 |
vp->setDraggerPlacement({placement.getPosition(), xyzRotation}); |
| 774 |
|
| 775 |
vp->updateTransformFromDragger(); |
| 776 |
vp->updatePlacementFromDragger(); |
| 777 |
|
| 778 |
resetReferencePlacement(); |
| 779 |
} |
| 780 |
|
| 781 |
TaskTransformDialog::TaskTransformDialog(ViewProviderDragger* vp, SoTransformDragger* dragger) |
| 782 |
: vp(vp) |
| 783 |
{ |
| 784 |
transform = new TaskTransform(vp, dragger); |
| 785 |
Content.push_back(transform); |
| 786 |
} |
| 787 |
|
| 788 |
void TaskTransformDialog::open() |
| 789 |
{ |
| 790 |
// we can't have user switching camera types while dragger is shown. |
| 791 |
Gui::Application::Instance->commandManager() |
| 792 |
.getCommandByName("Std_OrthographicCamera") |
| 793 |
->setEnabled(false); |
| 794 |
|
| 795 |
Gui::Application::Instance->commandManager() |
| 796 |
.getCommandByName("Std_PerspectiveCamera") |
| 797 |
->setEnabled(false); |
| 798 |
|
| 799 |
Gui::TaskView::TaskDialog::open(); |
| 800 |
|
| 801 |
openCommand(); |
| 802 |
} |
| 803 |
|
| 804 |
void TaskTransformDialog::openCommand() |
| 805 |
{ |
| 806 |
if (auto document = vp->getDocument()) { |
| 807 |
if (!document->hasPendingCommand()) { |
| 808 |
document->openCommand(QT_TRANSLATE_NOOP("Command", "Transform")); |
| 809 |
} |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
void TaskTransformDialog::updateDraggerPlacement() |
| 814 |
{ |
| 815 |
const auto placement = vp->getObjectPlacement(); |
| 816 |
vp->setDraggerPlacement(placement); |
| 817 |
} |
| 818 |
|
| 819 |
void TaskTransformDialog::onUndo() |
| 820 |
{ |
| 821 |
updateDraggerPlacement(); |
| 822 |
openCommand(); |
| 823 |
} |
| 824 |
|
| 825 |
void TaskTransformDialog::onRedo() |
| 826 |
{ |
| 827 |
updateDraggerPlacement(); |
| 828 |
openCommand(); |
| 829 |
} |
| 830 |
|
| 831 |
bool TaskTransformDialog::accept() |
| 832 |
{ |
| 833 |
if (auto document = vp->getDocument()) { |
| 834 |
document->commitCommand(); |
| 835 |
document->resetEdit(); |
| 836 |
document->getDocument()->recompute(); |
| 837 |
} |
| 838 |
|
| 839 |
return Gui::TaskView::TaskDialog::accept(); |
| 840 |
} |
| 841 |
|
| 842 |
bool TaskTransformDialog::reject() |
| 843 |
{ |
| 844 |
if (auto document = vp->getDocument()) { |
| 845 |
document->abortCommand(); |
| 846 |
document->resetEdit(); |
| 847 |
document->getDocument()->recompute(); |
| 848 |
} |
| 849 |
|
| 850 |
return Gui::TaskView::TaskDialog::reject(); |
| 851 |
} |
| 852 |
|
| 853 |
#include "moc_TaskTransform.cpp" |