| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2023 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 5 |
* * |
| 6 |
* This file is part of FreeCAD. * |
| 7 |
* * |
| 8 |
* FreeCAD is free software: you can redistribute it and/or modify it * |
| 9 |
* under the terms of the GNU Lesser General Public License as * |
| 10 |
* published by the Free Software Foundation, either version 2.1 of the * |
| 11 |
* License, or (at your option) any later version. * |
| 12 |
* * |
| 13 |
* FreeCAD is distributed in the hope that it will be useful, but * |
| 14 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * |
| 16 |
* Lesser General Public License for more details. * |
| 17 |
* * |
| 18 |
* You should have received a copy of the GNU Lesser General Public * |
| 19 |
* License along with FreeCAD. If not, see * |
| 20 |
* <https://www.gnu.org/licenses/>. * |
| 21 |
* * |
| 22 |
**************************************************************************/ |
| 23 |
|
| 24 |
#include <QDialog> |
| 25 |
#include <QPushButton> |
| 26 |
#include <QAction> |
| 27 |
#include <QKeyEvent> |
| 28 |
#include <map> |
| 29 |
#include <Inventor/SoPickedPoint.h> |
| 30 |
#include <Inventor/events/SoLocation2Event.h> |
| 31 |
#include <Inventor/events/SoButtonEvent.h> |
| 32 |
#include <Inventor/events/SoMouseButtonEvent.h> |
| 33 |
#include <Inventor/events/SoKeyboardEvent.h> |
| 34 |
|
| 35 |
#include <Base/Console.h> |
| 36 |
#include <Base/Precision.h> |
| 37 |
#include <Base/Quantity.h> |
| 38 |
#include <Base/Tools.h> |
| 39 |
#include <App/Document.h> |
| 40 |
#include <Gui/Application.h> |
| 41 |
#include <Gui/BitmapFactory.h> |
| 42 |
#include <Gui/Camera.h> |
| 43 |
#include <Gui/Document.h> |
| 44 |
#include <Gui/EditableDatumLabel.h> |
| 45 |
#include <Gui/View3DInventor.h> |
| 46 |
#include <Gui/View3DInventorViewer.h> |
| 47 |
#include <Gui/ViewProviderDocumentObject.h> |
| 48 |
#include <Gui/TaskView/TaskView.h> |
| 49 |
|
| 50 |
#include "TaskImage.h" |
| 51 |
#include "ui_TaskImage.h" |
| 52 |
|
| 53 |
|
| 54 |
using namespace Gui; |
| 55 |
|
| 56 |
/* TRANSLATOR Gui::TaskImage */ |
| 57 |
|
| 58 |
TaskImage::TaskImage(Image::ImagePlane* obj, QWidget* parent) |
| 59 |
: QWidget(parent) |
| 60 |
, ui(new Ui_TaskImage) |
| 61 |
, feature(obj) |
| 62 |
, aspectRatio(1.0) |
| 63 |
{ |
| 64 |
ui->setupUi(this); |
| 65 |
ui->groupBoxCalibration->hide(); |
| 66 |
|
| 67 |
initialiseTransparency(); |
| 68 |
|
| 69 |
// NOLINTNEXTLINE |
| 70 |
aspectRatio = obj->XSize.getValue() / obj->YSize.getValue(); |
| 71 |
|
| 72 |
connectSignals(); |
| 73 |
} |
| 74 |
|
| 75 |
TaskImage::~TaskImage() |
| 76 |
{ |
| 77 |
if (!feature.expired() && scale) { |
| 78 |
if (scale->isActive()) { |
| 79 |
scale->deactivate(); |
| 80 |
} |
| 81 |
scale->deleteLater(); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
void TaskImage::connectSignals() |
| 86 |
{ |
| 87 |
// clang-format off |
| 88 |
connect(ui->Reverse_checkBox, &QCheckBox::clicked, |
| 89 |
this, &TaskImage::onPreview); |
| 90 |
connect(ui->XY_radioButton, &QRadioButton::clicked, |
| 91 |
this, &TaskImage::onPreview); |
| 92 |
connect(ui->XZ_radioButton, &QRadioButton::clicked, |
| 93 |
this, &TaskImage::onPreview); |
| 94 |
connect(ui->YZ_radioButton, &QRadioButton::clicked, |
| 95 |
this, &TaskImage::onPreview); |
| 96 |
connect(ui->spinBoxZ, qOverload<double>(&QuantitySpinBox::valueChanged), |
| 97 |
this, &TaskImage::onPreview); |
| 98 |
connect(ui->spinBoxX, qOverload<double>(&QuantitySpinBox::valueChanged), |
| 99 |
this, &TaskImage::onPreview); |
| 100 |
connect(ui->spinBoxY, qOverload<double>(&QuantitySpinBox::valueChanged), |
| 101 |
this, &TaskImage::onPreview); |
| 102 |
connect(ui->spinBoxRotation, qOverload<double>(&QuantitySpinBox::valueChanged), |
| 103 |
this, &TaskImage::onPreview); |
| 104 |
connect(ui->spinBoxTransparency, qOverload<int>(&QSpinBox::valueChanged), |
| 105 |
this, &TaskImage::changeTransparency); |
| 106 |
connect(ui->sliderTransparency, qOverload<int>(&QSlider::valueChanged), |
| 107 |
this, &TaskImage::changeTransparency); |
| 108 |
|
| 109 |
connect(ui->spinBoxWidth, qOverload<double>(&QuantitySpinBox::valueChanged), |
| 110 |
this, &TaskImage::changeWidth); |
| 111 |
connect(ui->spinBoxHeight, qOverload<double>(&QuantitySpinBox::valueChanged), |
| 112 |
this, &TaskImage::changeHeight); |
| 113 |
connect(ui->pushButtonScale, &QPushButton::clicked, |
| 114 |
this, &TaskImage::onInteractiveScale); |
| 115 |
connect(ui->pushButtonApply, &QPushButton::clicked, |
| 116 |
this, &TaskImage::acceptScale); |
| 117 |
connect(ui->pushButtonCancel, &QPushButton::clicked, |
| 118 |
this, &TaskImage::rejectScale); |
| 119 |
// clang-format on |
| 120 |
} |
| 121 |
|
| 122 |
void TaskImage::initialiseTransparency() |
| 123 |
{ |
| 124 |
// NOLINTBEGIN |
| 125 |
auto vp = Application::Instance->getViewProvider(feature.get()); |
| 126 |
App::Property* prop = vp->getPropertyByName("Transparency"); |
| 127 |
if (prop && prop->isDerivedFrom<App::PropertyInteger>()) { |
| 128 |
auto Transparency = static_cast<App::PropertyInteger*>(prop); |
| 129 |
ui->spinBoxTransparency->setValue(Transparency->getValue()); |
| 130 |
ui->sliderTransparency->setValue(Transparency->getValue()); |
| 131 |
} |
| 132 |
// NOLINTEND |
| 133 |
} |
| 134 |
|
| 135 |
void TaskImage::changeTransparency(int val) |
| 136 |
{ |
| 137 |
if (feature.expired()) { |
| 138 |
return; |
| 139 |
} |
| 140 |
|
| 141 |
auto vp = Application::Instance->getViewProvider(feature.get()); |
| 142 |
App::Property* prop = vp->getPropertyByName("Transparency"); |
| 143 |
if (auto Transparency = dynamic_cast<App::PropertyInteger*>(prop)) { |
| 144 |
Transparency->setValue(val); |
| 145 |
|
| 146 |
QSignalBlocker block(ui->spinBoxTransparency); |
| 147 |
QSignalBlocker blocks(ui->sliderTransparency); |
| 148 |
ui->spinBoxTransparency->setValue(val); |
| 149 |
ui->sliderTransparency->setValue(val); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
void TaskImage::changeWidth() |
| 154 |
{ |
| 155 |
if (!feature.expired()) { |
| 156 |
double val = ui->spinBoxWidth->value().getValue(); |
| 157 |
feature->XSize.setValue(val); |
| 158 |
|
| 159 |
if (ui->checkBoxRatio->isChecked()) { |
| 160 |
feature->YSize.setValue(val / aspectRatio); |
| 161 |
QSignalBlocker block(ui->spinBoxHeight); |
| 162 |
ui->spinBoxHeight->setValue(val / aspectRatio); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
void TaskImage::changeHeight() |
| 168 |
{ |
| 169 |
if (!feature.expired()) { |
| 170 |
double val = ui->spinBoxHeight->value().getValue(); |
| 171 |
feature->YSize.setValue(val); |
| 172 |
|
| 173 |
if (ui->checkBoxRatio->isChecked()) { |
| 174 |
feature->XSize.setValue(val * aspectRatio); |
| 175 |
QSignalBlocker block(ui->spinBoxWidth); |
| 176 |
ui->spinBoxWidth->setValue(val * aspectRatio); |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
View3DInventorViewer* TaskImage::getViewer() const |
| 182 |
{ |
| 183 |
if (!feature.expired()) { |
| 184 |
auto vp = Application::Instance->getViewProvider(feature.get()); |
| 185 |
auto doc = static_cast<ViewProviderDocumentObject*>(vp)->getDocument(); // NOLINT |
| 186 |
auto view = qobject_cast<View3DInventor*>(doc->getViewOfViewProvider(vp)); |
| 187 |
if (view) { |
| 188 |
return view->getViewer(); |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
return nullptr; |
| 193 |
} |
| 194 |
|
| 195 |
void TaskImage::scaleImage(double factor) |
| 196 |
{ |
| 197 |
if (!feature.expired()) { |
| 198 |
feature->XSize.setValue(feature->XSize.getValue() * factor); |
| 199 |
feature->YSize.setValue(feature->YSize.getValue() * factor); |
| 200 |
|
| 201 |
QSignalBlocker blockW(ui->spinBoxWidth); |
| 202 |
ui->spinBoxWidth->setValue(feature->XSize.getValue()); |
| 203 |
QSignalBlocker blockH(ui->spinBoxHeight); |
| 204 |
ui->spinBoxHeight->setValue(feature->YSize.getValue()); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
void TaskImage::startScale() |
| 209 |
{ |
| 210 |
if (scale) { |
| 211 |
scale->activate(); |
| 212 |
ui->pushButtonScale->hide(); |
| 213 |
ui->groupBoxCalibration->show(); |
| 214 |
ui->pushButtonApply->setEnabled(false); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
void TaskImage::acceptScale() |
| 219 |
{ |
| 220 |
if (scale) { |
| 221 |
scaleImage(scale->getScaleFactor()); |
| 222 |
rejectScale(); |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
void TaskImage::enableApplyBtn() |
| 227 |
{ |
| 228 |
ui->pushButtonApply->setEnabled(true); |
| 229 |
} |
| 230 |
|
| 231 |
void TaskImage::rejectScale() |
| 232 |
{ |
| 233 |
if (scale) { |
| 234 |
scale->deactivate(); |
| 235 |
ui->pushButtonScale->show(); |
| 236 |
ui->groupBoxCalibration->hide(); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
void TaskImage::onInteractiveScale() |
| 241 |
{ |
| 242 |
if (!feature.expired() && !scale) { |
| 243 |
View3DInventorViewer* viewer = getViewer(); |
| 244 |
if (viewer) { |
| 245 |
auto vp = Application::Instance->getViewProvider(feature.get()); |
| 246 |
scale = new InteractiveScale(viewer, vp, feature->globalPlacement()); |
| 247 |
connect(scale, &InteractiveScale::scaleRequired, this, &TaskImage::acceptScale); |
| 248 |
connect(scale, &InteractiveScale::scaleCanceled, this, &TaskImage::rejectScale); |
| 249 |
connect(scale, &InteractiveScale::enableApplyBtn, this, &TaskImage::enableApplyBtn); |
| 250 |
} |
| 251 |
} |
| 252 |
|
| 253 |
startScale(); |
| 254 |
} |
| 255 |
|
| 256 |
void TaskImage::open() |
| 257 |
{ |
| 258 |
if (!feature.expired()) { |
| 259 |
App::Document* doc = feature->getDocument(); |
| 260 |
doc->openTransaction(QT_TRANSLATE_NOOP("Command", "Edit image")); |
| 261 |
restore(feature->Placement.getValue()); |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
void TaskImage::accept() |
| 266 |
{ |
| 267 |
if (!feature.expired()) { |
| 268 |
App::Document* doc = feature->getDocument(); |
| 269 |
doc->commitTransaction(); |
| 270 |
doc->recompute(); |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
void TaskImage::reject() |
| 275 |
{ |
| 276 |
if (!feature.expired()) { |
| 277 |
App::Document* doc = feature->getDocument(); |
| 278 |
doc->abortTransaction(); |
| 279 |
feature->purgeTouched(); |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
void TaskImage::onPreview() |
| 284 |
{ |
| 285 |
updateIcon(); |
| 286 |
updatePlacement(); |
| 287 |
} |
| 288 |
|
| 289 |
// NOLINTNEXTLINE |
| 290 |
void TaskImage::restoreAngles(const Base::Rotation& rot) |
| 291 |
{ |
| 292 |
Base::Vector3d vec(0, 0, 1); |
| 293 |
rot.multVec(vec, vec); |
| 294 |
|
| 295 |
double yaw {}; |
| 296 |
double pitch {}; |
| 297 |
double roll {}; |
| 298 |
rot.getYawPitchRoll(yaw, pitch, roll); |
| 299 |
|
| 300 |
bool reverse = false; |
| 301 |
|
| 302 |
const double tol = 1.0e-5; |
| 303 |
const double angle1 = 90.0; |
| 304 |
const double angle2 = 180.0; |
| 305 |
|
| 306 |
auto isTopOrBottom = [=](bool& reverse) { |
| 307 |
if (std::fabs(vec.z - 1.0) < tol) { |
| 308 |
return true; |
| 309 |
} |
| 310 |
if (std::fabs(vec.z + 1.0) < tol) { |
| 311 |
reverse = true; |
| 312 |
return true; |
| 313 |
} |
| 314 |
|
| 315 |
return false; |
| 316 |
}; |
| 317 |
|
| 318 |
auto isFrontOrRear = [&](bool& reverse) { |
| 319 |
if (std::fabs(vec.y + 1.0) < tol) { |
| 320 |
if (std::fabs(yaw - angle2) < tol) { |
| 321 |
pitch = -angle2 - pitch; |
| 322 |
} |
| 323 |
return true; |
| 324 |
} |
| 325 |
if (std::fabs(vec.y - 1.0) < tol) { |
| 326 |
if (std::fabs(yaw) < tol) { |
| 327 |
pitch = -angle2 - pitch; |
| 328 |
} |
| 329 |
reverse = true; |
| 330 |
return true; |
| 331 |
} |
| 332 |
|
| 333 |
return false; |
| 334 |
}; |
| 335 |
|
| 336 |
auto isRightOrLeft = [&](bool& reverse) { |
| 337 |
if (std::fabs(vec.x - 1.0) < tol) { |
| 338 |
if (std::fabs(yaw + angle1) < tol) { |
| 339 |
pitch = -angle2 - pitch; |
| 340 |
} |
| 341 |
return true; |
| 342 |
} |
| 343 |
if (std::fabs(vec.x + 1.0) < tol) { |
| 344 |
if (std::fabs(yaw - angle1) < tol) { |
| 345 |
pitch = -angle2 - pitch; |
| 346 |
} |
| 347 |
reverse = true; |
| 348 |
return true; |
| 349 |
} |
| 350 |
|
| 351 |
return false; |
| 352 |
}; |
| 353 |
|
| 354 |
if (isTopOrBottom(reverse)) { |
| 355 |
int inv = reverse ? -1 : 1; |
| 356 |
ui->XY_radioButton->setChecked(true); |
| 357 |
ui->spinBoxRotation->setValue(yaw * inv); |
| 358 |
} |
| 359 |
else if (isFrontOrRear(reverse)) { |
| 360 |
ui->XZ_radioButton->setChecked(true); |
| 361 |
ui->spinBoxRotation->setValue(-pitch); |
| 362 |
} |
| 363 |
else if (isRightOrLeft(reverse)) { |
| 364 |
ui->YZ_radioButton->setChecked(true); |
| 365 |
ui->spinBoxRotation->setValue(-pitch); |
| 366 |
} |
| 367 |
ui->Reverse_checkBox->setChecked(reverse); |
| 368 |
} |
| 369 |
|
| 370 |
void TaskImage::restore(const Base::Placement& plm) |
| 371 |
{ |
| 372 |
if (feature.expired()) { |
| 373 |
return; |
| 374 |
} |
| 375 |
|
| 376 |
QSignalBlocker blockW(ui->spinBoxWidth); |
| 377 |
QSignalBlocker blockH(ui->spinBoxHeight); |
| 378 |
ui->spinBoxWidth->setValue(feature->XSize.getValue()); |
| 379 |
ui->spinBoxHeight->setValue(feature->YSize.getValue()); |
| 380 |
|
| 381 |
Base::Rotation rot = plm.getRotation(); // NOLINT |
| 382 |
Base::Vector3d pos = plm.getPosition(); |
| 383 |
|
| 384 |
restoreAngles(rot); |
| 385 |
|
| 386 |
Base::Vector3d R0(0, 0, 0); |
| 387 |
Base::Vector3d RX(1, 0, 0); |
| 388 |
Base::Vector3d RY(0, 1, 0); |
| 389 |
|
| 390 |
RX = rot.multVec(RX); |
| 391 |
RY = rot.multVec(RY); |
| 392 |
pos.TransformToCoordinateSystem(R0, RX, RY); |
| 393 |
ui->spinBoxX->setValue(pos.x); |
| 394 |
ui->spinBoxY->setValue(pos.y); |
| 395 |
ui->spinBoxZ->setValue(pos.z); |
| 396 |
|
| 397 |
onPreview(); |
| 398 |
} |
| 399 |
|
| 400 |
void TaskImage::updatePlacement() |
| 401 |
{ |
| 402 |
double angle = ui->spinBoxRotation->value().getValue(); |
| 403 |
bool reverse = ui->Reverse_checkBox->isChecked(); |
| 404 |
|
| 405 |
// NOLINTBEGIN |
| 406 |
Base::Placement Pos; |
| 407 |
Base::Rotation rot; |
| 408 |
double dir = reverse ? 180. : 0.; |
| 409 |
int inv = reverse ? -1 : 1; |
| 410 |
|
| 411 |
if (ui->XY_radioButton->isChecked()) { |
| 412 |
rot.setYawPitchRoll(inv * angle, 0., dir); |
| 413 |
} |
| 414 |
else if (ui->XZ_radioButton->isChecked()) { |
| 415 |
rot.setYawPitchRoll(dir, -angle, 90.); |
| 416 |
} |
| 417 |
else if (ui->YZ_radioButton->isChecked()) { |
| 418 |
rot.setYawPitchRoll(90. - dir, -angle, 90.); |
| 419 |
} |
| 420 |
else if (!feature.expired()) { |
| 421 |
Base::Placement plm = feature->Placement.getValue(); |
| 422 |
rot = plm.getRotation(); |
| 423 |
} |
| 424 |
// NOLINTEND |
| 425 |
|
| 426 |
Base::Vector3d offset = Base::Vector3d( |
| 427 |
ui->spinBoxX->value().getValue(), |
| 428 |
ui->spinBoxY->value().getValue(), |
| 429 |
ui->spinBoxZ->value().getValue() |
| 430 |
); |
| 431 |
offset = rot.multVec(offset); |
| 432 |
Pos = Base::Placement(offset, rot); |
| 433 |
|
| 434 |
if (!feature.expired()) { |
| 435 |
feature->Placement.setValue(Pos); |
| 436 |
if (scale) { |
| 437 |
scale->setPlacement(feature->globalPlacement()); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
|
| 442 |
void TaskImage::updateIcon() |
| 443 |
{ |
| 444 |
std::string icon; |
| 445 |
bool reverse = ui->Reverse_checkBox->isChecked(); |
| 446 |
if (ui->XY_radioButton->isChecked()) { |
| 447 |
icon = reverse ? "view-bottom" : "view-top"; |
| 448 |
} |
| 449 |
else if (ui->XZ_radioButton->isChecked()) { |
| 450 |
icon = reverse ? "view-rear" : "view-front"; |
| 451 |
} |
| 452 |
else if (ui->YZ_radioButton->isChecked()) { |
| 453 |
icon = reverse ? "view-left" : "view-right"; |
| 454 |
} |
| 455 |
|
| 456 |
ui->previewLabel->setPixmap( |
| 457 |
Gui::BitmapFactory().pixmapFromSvg(icon.c_str(), ui->previewLabel->size()) |
| 458 |
); |
| 459 |
} |
| 460 |
|
| 461 |
// ---------------------------------------------------------------------------- |
| 462 |
|
| 463 |
InteractiveScale::InteractiveScale( |
| 464 |
View3DInventorViewer* view, |
| 465 |
ViewProvider* vp, |
| 466 |
const Base::Placement& plc |
| 467 |
) // NOLINT |
| 468 |
: active(false) |
| 469 |
, placement(plc) |
| 470 |
, viewer(view) |
| 471 |
, viewProv(vp) |
| 472 |
, midPoint(SbVec3f(0, 0, 0)) |
| 473 |
{ |
| 474 |
measureLabel = new EditableDatumLabel(viewer, placement, SbColor(1.0F, 0.149F, 0.0F)); // NOLINT |
| 475 |
} |
| 476 |
|
| 477 |
InteractiveScale::~InteractiveScale() |
| 478 |
{ |
| 479 |
delete measureLabel; |
| 480 |
} |
| 481 |
|
| 482 |
void InteractiveScale::activate() |
| 483 |
{ |
| 484 |
if (viewer) { |
| 485 |
viewer->setEditing(true); |
| 486 |
viewer->addEventCallback( |
| 487 |
SoLocation2Event::getClassTypeId(), |
| 488 |
InteractiveScale::getMousePosition, |
| 489 |
this |
| 490 |
); |
| 491 |
viewer->addEventCallback(SoButtonEvent::getClassTypeId(), InteractiveScale::soEventFilter, this); |
| 492 |
viewer->setSelectionEnabled(false); |
| 493 |
viewer->getWidget()->setCursor(QCursor(Qt::CrossCursor)); |
| 494 |
active = true; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
void InteractiveScale::deactivate() |
| 499 |
{ |
| 500 |
if (viewer) { |
| 501 |
points.clear(); |
| 502 |
measureLabel->deactivate(); |
| 503 |
viewer->setEditing(false); |
| 504 |
viewer->removeEventCallback( |
| 505 |
SoLocation2Event::getClassTypeId(), |
| 506 |
InteractiveScale::getMousePosition, |
| 507 |
this |
| 508 |
); |
| 509 |
viewer->removeEventCallback( |
| 510 |
SoButtonEvent::getClassTypeId(), |
| 511 |
InteractiveScale::soEventFilter, |
| 512 |
this |
| 513 |
); |
| 514 |
viewer->setSelectionEnabled(true); |
| 515 |
viewer->getWidget()->setCursor(QCursor(Qt::ArrowCursor)); |
| 516 |
active = false; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
double InteractiveScale::getScaleFactor() const |
| 521 |
{ |
| 522 |
if ((points[0] - points[1]).length() == 0.) { |
| 523 |
return 1.0; |
| 524 |
} |
| 525 |
|
| 526 |
return measureLabel->getValue() / (points[0] - points[1]).length(); |
| 527 |
} |
| 528 |
|
| 529 |
double InteractiveScale::getDistance(const SbVec3f& pt) const |
| 530 |
{ |
| 531 |
if (points.empty()) { |
| 532 |
return 0.0; |
| 533 |
} |
| 534 |
|
| 535 |
return (points[0] - pt).length(); |
| 536 |
} |
| 537 |
|
| 538 |
void InteractiveScale::setDistance(const SbVec3f& pos3d) |
| 539 |
{ |
| 540 |
Base::Quantity quantity; |
| 541 |
quantity.setValue(getDistance(pos3d)); |
| 542 |
quantity.setUnit(Base::Unit::Length); |
| 543 |
|
| 544 |
// Update the displayed distance |
| 545 |
double factor {}; |
| 546 |
std::string unitStr; |
| 547 |
std::string valueStr; |
| 548 |
valueStr = quantity.getUserString(factor, unitStr); |
| 549 |
measureLabel->label->string = SbString(valueStr.c_str()); |
| 550 |
measureLabel->label->setPoints(getCoordsOnImagePlane(points[0]), getCoordsOnImagePlane(pos3d)); |
| 551 |
} |
| 552 |
|
| 553 |
void InteractiveScale::findPointOnImagePlane(SoEventCallback* ecb) |
| 554 |
{ |
| 555 |
const SoEvent* mbe = ecb->getEvent(); |
| 556 |
auto view = static_cast<Gui::View3DInventorViewer*>(ecb->getUserData()); |
| 557 |
std::unique_ptr<SoPickedPoint> pp(view->getPointOnRay(mbe->getPosition(), viewProv)); |
| 558 |
if (pp) { |
| 559 |
auto pos3d = pp->getPoint(); |
| 560 |
|
| 561 |
collectPoint(pos3d); |
| 562 |
} |
| 563 |
} |
| 564 |
|
| 565 |
void InteractiveScale::collectPoint(const SbVec3f& pos3d) |
| 566 |
{ |
| 567 |
if (points.empty()) { |
| 568 |
points.push_back(pos3d); |
| 569 |
|
| 570 |
measureLabel->label->setPoints(getCoordsOnImagePlane(pos3d), getCoordsOnImagePlane(pos3d)); |
| 571 |
measureLabel->activate(); |
| 572 |
} |
| 573 |
else if (points.size() == 1) { |
| 574 |
double distance = getDistance(pos3d); |
| 575 |
if (distance > Base::Precision::Confusion()) { |
| 576 |
points.push_back(pos3d); |
| 577 |
|
| 578 |
midPoint = (points[0] + points[1]) / 2; |
| 579 |
|
| 580 |
measureLabel->startEdit(getDistance(points[1]), this, true); |
| 581 |
|
| 582 |
Q_EMIT enableApplyBtn(); |
| 583 |
} |
| 584 |
else { |
| 585 |
Base::Console().warning( |
| 586 |
std::string("Image scale"), |
| 587 |
"The second point is too close. Retry!\n" |
| 588 |
); |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
void InteractiveScale::getMousePosition(void* ud, SoEventCallback* ecb) |
| 594 |
{ |
| 595 |
auto scale = static_cast<InteractiveScale*>(ud); |
| 596 |
const SoEvent* l2e = ecb->getEvent(); |
| 597 |
auto view = static_cast<Gui::View3DInventorViewer*>(ecb->getUserData()); |
| 598 |
|
| 599 |
if (scale->points.size() == 1) { |
| 600 |
ecb->setHandled(); |
| 601 |
|
| 602 |
std::unique_ptr<SoPickedPoint> pp(view->getPointOnRay(l2e->getPosition(), scale->viewProv)); |
| 603 |
if (pp) { |
| 604 |
SbVec3f pos3d = pp->getPoint(); |
| 605 |
scale->setDistance(pos3d); |
| 606 |
} |
| 607 |
} |
| 608 |
} |
| 609 |
|
| 610 |
void InteractiveScale::soEventFilter(void* ud, SoEventCallback* ecb) |
| 611 |
{ |
| 612 |
auto scale = static_cast<InteractiveScale*>(ud); |
| 613 |
|
| 614 |
const SoEvent* soEvent = ecb->getEvent(); |
| 615 |
if (soEvent->isOfType(SoKeyboardEvent::getClassTypeId())) { |
| 616 |
/* If user presses escape, then we cancel the tool.*/ |
| 617 |
const auto kbe = static_cast<const SoKeyboardEvent*>(soEvent); // NOLINT |
| 618 |
|
| 619 |
if (kbe->getKey() == SoKeyboardEvent::ESCAPE && kbe->getState() == SoButtonEvent::UP) { |
| 620 |
ecb->setHandled(); |
| 621 |
Q_EMIT scale->scaleCanceled(); |
| 622 |
} |
| 623 |
} |
| 624 |
else if (soEvent->isOfType(SoMouseButtonEvent::getClassTypeId())) { |
| 625 |
const auto mbe = static_cast<const SoMouseButtonEvent*>(soEvent); // NOLINT |
| 626 |
|
| 627 |
if (mbe->getButton() == SoMouseButtonEvent::BUTTON1 |
| 628 |
&& mbe->getState() == SoButtonEvent::DOWN) { |
| 629 |
ecb->setHandled(); |
| 630 |
scale->findPointOnImagePlane(ecb); |
| 631 |
} |
| 632 |
if (mbe->getButton() == SoMouseButtonEvent::BUTTON2 |
| 633 |
&& mbe->getState() == SoButtonEvent::DOWN) { |
| 634 |
ecb->setHandled(); |
| 635 |
Q_EMIT scale->scaleCanceled(); |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
bool InteractiveScale::eventFilter(QObject* object, QEvent* event) |
| 641 |
{ |
| 642 |
if (event->type() == QEvent::KeyRelease) { |
| 643 |
auto keyEvent = static_cast<QKeyEvent*>(event); // NOLINT |
| 644 |
|
| 645 |
/* If user press enter in the spinbox, then we validate the tool.*/ |
| 646 |
if ((keyEvent->key() == Qt::Key_Enter || keyEvent->key() == Qt::Key_Return) |
| 647 |
&& qobject_cast<QuantitySpinBox*>(object)) { |
| 648 |
Q_EMIT scaleRequired(); |
| 649 |
} |
| 650 |
|
| 651 |
/* If user press escape, then we cancel the tool. Required here as well for when checkbox |
| 652 |
* has focus.*/ |
| 653 |
if (keyEvent->key() == Qt::Key_Escape) { |
| 654 |
Q_EMIT scaleCanceled(); |
| 655 |
} |
| 656 |
} |
| 657 |
return false; |
| 658 |
} |
| 659 |
|
| 660 |
void InteractiveScale::setPlacement(const Base::Placement& plc) |
| 661 |
{ |
| 662 |
placement = plc; |
| 663 |
measureLabel->setPlacement(plc); |
| 664 |
} |
| 665 |
|
| 666 |
SbVec3f InteractiveScale::getCoordsOnImagePlane(const SbVec3f& point) |
| 667 |
{ |
| 668 |
// Plane form |
| 669 |
Base::Vector3d RX(1, 0, 0); |
| 670 |
Base::Vector3d RY(0, 1, 0); |
| 671 |
|
| 672 |
// move to position of Sketch |
| 673 |
Base::Rotation tmp(placement.getRotation()); |
| 674 |
RX = tmp.multVec(RX); |
| 675 |
RY = tmp.multVec(RY); |
| 676 |
Base::Vector3d pos = placement.getPosition(); |
| 677 |
|
| 678 |
// we use pos as the Base because in setPlacement we set transform->translation using |
| 679 |
// placement.getPosition() to fix the Zoffset. But this applies the X & Y translation too. |
| 680 |
Base::Vector3d pnt(point[0], point[1], point[2]); |
| 681 |
pnt.TransformToCoordinateSystem(pos, RX, RY); |
| 682 |
|
| 683 |
return {float(pnt.x), float(pnt.y), 0.0F}; |
| 684 |
} |
| 685 |
|
| 686 |
// ---------------------------------------------------------------------------- |
| 687 |
|
| 688 |
TaskImageDialog::TaskImageDialog(Image::ImagePlane* obj) |
| 689 |
: widget {new TaskImage(obj)} |
| 690 |
{ |
| 691 |
addTaskBox(Gui::BitmapFactory().pixmap("image-plane"), widget); |
| 692 |
|
| 693 |
associateToObject3dView(obj); |
| 694 |
} |
| 695 |
|
| 696 |
void TaskImageDialog::open() |
| 697 |
{ |
| 698 |
widget->open(); |
| 699 |
} |
| 700 |
|
| 701 |
bool TaskImageDialog::accept() |
| 702 |
{ |
| 703 |
widget->accept(); |
| 704 |
return true; |
| 705 |
} |
| 706 |
|
| 707 |
bool TaskImageDialog::reject() |
| 708 |
{ |
| 709 |
widget->reject(); |
| 710 |
return true; |
| 711 |
} |
| 712 |
|
| 713 |
#include "moc_TaskImage.cpp" |