| 1 |
// SPDX-License-Identifier: LGPL-2.1-or-later |
| 2 |
|
| 3 |
/*************************************************************************** |
| 4 |
* Copyright (c) 2025 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 <QApplication> |
| 25 |
|
| 26 |
#include <boost/statechart/custom_reaction.hpp> |
| 27 |
#include <boost/statechart/state_machine.hpp> |
| 28 |
#include <boost/statechart/state.hpp> |
| 29 |
|
| 30 |
#include "Camera.h" |
| 31 |
#include "SiemensNXNavigationStyle.h" |
| 32 |
#include "View3DInventorViewer.h" |
| 33 |
|
| 34 |
// NOLINTBEGIN(cppcoreguidelines-pro-type-static-cast-downcast, |
| 35 |
// cppcoreguidelines-avoid*, |
| 36 |
// readability-avoid-const-params-in-decls) |
| 37 |
using namespace Gui; |
| 38 |
namespace sc = boost::statechart; |
| 39 |
using SC = NavigationStateChart; |
| 40 |
using NS = SiemensNXNavigationStyle; |
| 41 |
|
| 42 |
struct NS::NaviMachine: public sc::state_machine<NS::NaviMachine, NS::IdleState> |
| 43 |
{ |
| 44 |
using superclass = sc::state_machine<NS::NaviMachine, NS::IdleState>; |
| 45 |
explicit NaviMachine(NS& ns) |
| 46 |
: ns(ns) |
| 47 |
{} |
| 48 |
NS& ns; |
| 49 |
}; |
| 50 |
|
| 51 |
struct NS::IdleState: public sc::state<NS::IdleState, NS::NaviMachine> |
| 52 |
{ |
| 53 |
using reactions = sc::custom_reaction<SC::Event>; |
| 54 |
explicit IdleState(my_context ctx) |
| 55 |
: my_base(ctx) |
| 56 |
{ |
| 57 |
auto& ns = this->outermost_context().ns; |
| 58 |
ns.setViewingMode(NavigationStyle::IDLE); |
| 59 |
} |
| 60 |
sc::result react(const SC::Event& ev) |
| 61 |
{ |
| 62 |
auto& ns = this->outermost_context().ns; |
| 63 |
switch (ns.getViewingMode()) { |
| 64 |
case NavigationStyle::SEEK_WAIT_MODE: { |
| 65 |
if (ev.isPress(SoMouseButtonEvent::BUTTON1)) { |
| 66 |
ns.seekToPoint(ev.inventor_event->getPosition()); |
| 67 |
ns.setViewingMode(NavigationStyle::SEEK_MODE); |
| 68 |
ev.flags->processed = true; |
| 69 |
return transit<NS::AwaitingReleaseState>(); |
| 70 |
} |
| 71 |
break; |
| 72 |
} |
| 73 |
case NavigationStyle::SPINNING: |
| 74 |
case NavigationStyle::SEEK_MODE: { |
| 75 |
if (!ev.flags->processed) { |
| 76 |
if (ev.isMouseButtonEvent()) { |
| 77 |
ev.flags->processed = true; |
| 78 |
return transit<NS::AwaitingReleaseState>(); |
| 79 |
} |
| 80 |
else if (ev.isKeyboardEvent() || ev.isMotion3Event()) { |
| 81 |
ns.setViewingMode(NavigationStyle::IDLE); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
break; |
| 86 |
} |
| 87 |
case NavigationStyle::BOXZOOM: |
| 88 |
return forward_event(); |
| 89 |
} |
| 90 |
|
| 91 |
// right-click |
| 92 |
if (ev.isRelease(SoMouseButtonEvent::BUTTON2) && ev.mbstate() == 0 |
| 93 |
&& !ns.viewer->isEditing() && ns.isPopupMenuEnabled()) { |
| 94 |
ns.openPopupMenu(ev.inventor_event->getPosition()); |
| 95 |
} |
| 96 |
|
| 97 |
if (ev.isPress(SoMouseButtonEvent::BUTTON3)) { |
| 98 |
if (ev.isDownShift()) { |
| 99 |
ev.flags->processed = true; |
| 100 |
return transit<NS::PanState>(); |
| 101 |
} |
| 102 |
|
| 103 |
if (ev.isDownButton(SC::Event::BUTTON3DOWN)) { |
| 104 |
ev.flags->processed = true; |
| 105 |
return transit<NS::AwaitingMoveState>(); |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
// Use processClickEvent() |
| 110 |
|
| 111 |
// Implement selection callback |
| 112 |
// if (ev.isLocation2Event() && ev.isDownButton1()) { |
| 113 |
// ev.flags->processed = true; |
| 114 |
// return transit<NS::SelectionState>(); |
| 115 |
//} |
| 116 |
|
| 117 |
return forward_event(); |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
struct NS::AwaitingReleaseState: public sc::state<NS::AwaitingReleaseState, NS::NaviMachine> |
| 122 |
{ |
| 123 |
using reactions = sc::custom_reaction<NS::Event>; |
| 124 |
explicit AwaitingReleaseState(my_context ctx) |
| 125 |
: my_base(ctx) |
| 126 |
{} |
| 127 |
|
| 128 |
sc::result react(const NS::Event& /*ev*/) |
| 129 |
{ |
| 130 |
return forward_event(); |
| 131 |
} |
| 132 |
}; |
| 133 |
|
| 134 |
struct NS::InteractState: public sc::state<NS::InteractState, NS::NaviMachine> |
| 135 |
{ |
| 136 |
using reactions = sc::custom_reaction<NS::Event>; |
| 137 |
explicit InteractState(my_context ctx) |
| 138 |
: my_base(ctx) |
| 139 |
{ |
| 140 |
auto& ns = this->outermost_context().ns; |
| 141 |
ns.setViewingMode(NavigationStyle::INTERACT); |
| 142 |
} |
| 143 |
|
| 144 |
sc::result react(const NS::Event& /*ev*/) |
| 145 |
{ |
| 146 |
return forward_event(); |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
struct NS::AwaitingMoveState: public sc::state<NS::AwaitingMoveState, NS::NaviMachine> |
| 151 |
{ |
| 152 |
using reactions = sc::custom_reaction<NS::Event>; |
| 153 |
|
| 154 |
private: |
| 155 |
SbVec2s base_pos; |
| 156 |
SbTime since; |
| 157 |
|
| 158 |
public: |
| 159 |
explicit AwaitingMoveState(my_context ctx) |
| 160 |
: my_base(ctx) |
| 161 |
{ |
| 162 |
auto& ns = this->outermost_context().ns; |
| 163 |
ns.setViewingMode(NavigationStyle::DRAGGING); |
| 164 |
this->base_pos |
| 165 |
= static_cast<const NS::Event*>(this->triggering_event())->inventor_event->getPosition(); |
| 166 |
this->since |
| 167 |
= static_cast<const NS::Event*>(this->triggering_event())->inventor_event->getTime(); |
| 168 |
} |
| 169 |
sc::result react(const NS::Event& ev) |
| 170 |
{ |
| 171 |
// this state consumes all mouse events. |
| 172 |
ev.flags->processed = ev.isMouseButtonEvent() || ev.isLocation2Event(); |
| 173 |
|
| 174 |
if (ev.isLocation2Event()) { |
| 175 |
return transit<NS::RotateState>(); |
| 176 |
} |
| 177 |
|
| 178 |
// right-click |
| 179 |
if (ev.isPress(SoMouseButtonEvent::BUTTON2) && ev.isDownButton3()) { |
| 180 |
return transit<NS::PanState>(); |
| 181 |
} |
| 182 |
|
| 183 |
if (ev.isKeyPress(SoKeyboardEvent::LEFT_SHIFT)) { |
| 184 |
ev.flags->processed = true; |
| 185 |
return transit<NS::PanState>(); |
| 186 |
} |
| 187 |
|
| 188 |
// left-click |
| 189 |
if (ev.isPress(SoMouseButtonEvent::BUTTON1) && ev.isDownButton3()) { |
| 190 |
return transit<NS::ZoomState>(); |
| 191 |
} |
| 192 |
|
| 193 |
if (ev.isKeyPress(SoKeyboardEvent::LEFT_CONTROL)) { |
| 194 |
ev.flags->processed = true; |
| 195 |
return transit<NS::ZoomState>(); |
| 196 |
} |
| 197 |
|
| 198 |
// middle-click |
| 199 |
if (ev.isRelease(SoMouseButtonEvent::BUTTON3) && ev.isDownNoButton()) { |
| 200 |
auto& ns = this->outermost_context().ns; |
| 201 |
SbTime tmp = (ev.inventor_event->getTime() - this->since); |
| 202 |
double dci = QApplication::doubleClickInterval() / 1000.0; |
| 203 |
|
| 204 |
// is this a simple middle click? |
| 205 |
if (tmp.getValue() < dci) { |
| 206 |
ev.flags->processed = true; |
| 207 |
SbVec2s pos = ev.inventor_event->getPosition(); |
| 208 |
ns.lookAtPoint(pos); |
| 209 |
} |
| 210 |
return transit<NS::IdleState>(); |
| 211 |
} |
| 212 |
|
| 213 |
return forward_event(); |
| 214 |
} |
| 215 |
}; |
| 216 |
|
| 217 |
struct NS::RotateState: public sc::state<NS::RotateState, NS::NaviMachine> |
| 218 |
{ |
| 219 |
using reactions = sc::custom_reaction<NS::Event>; |
| 220 |
explicit RotateState(my_context ctx) |
| 221 |
: my_base(ctx) |
| 222 |
{ |
| 223 |
auto& ns = this->outermost_context().ns; |
| 224 |
const auto inventorEvent |
| 225 |
= static_cast<const NS::Event*>(this->triggering_event())->inventor_event; |
| 226 |
ns.saveCursorPosition(inventorEvent); |
| 227 |
ns.setViewingMode(NavigationStyle::DRAGGING); |
| 228 |
this->base_pos = inventorEvent->getPosition(); |
| 229 |
} |
| 230 |
|
| 231 |
sc::result react(const NS::Event& ev) |
| 232 |
{ |
| 233 |
if (ev.isLocation2Event()) { |
| 234 |
auto& ns = this->outermost_context().ns; |
| 235 |
ns.addToLog(ev.inventor_event->getPosition(), ev.inventor_event->getTime()); |
| 236 |
const SbVec2s pos = ev.inventor_event->getPosition(); |
| 237 |
const SbVec2f posn = ns.normalizePixelPos(pos); |
| 238 |
ns.spin(posn); |
| 239 |
ns.moveCursorPosition(); |
| 240 |
ev.flags->processed = true; |
| 241 |
} |
| 242 |
|
| 243 |
// right-click |
| 244 |
if (ev.isPress(SoMouseButtonEvent::BUTTON2) && ev.isDownButton3()) { |
| 245 |
ev.flags->processed = true; |
| 246 |
return transit<NS::PanState>(); |
| 247 |
} |
| 248 |
|
| 249 |
if (ev.isKeyPress(SoKeyboardEvent::LEFT_SHIFT)) { |
| 250 |
ev.flags->processed = true; |
| 251 |
return transit<NS::PanState>(); |
| 252 |
} |
| 253 |
|
| 254 |
// left-click |
| 255 |
if (ev.isPress(SoMouseButtonEvent::BUTTON1) && ev.isDownButton3()) { |
| 256 |
ev.flags->processed = true; |
| 257 |
return transit<NS::ZoomState>(); |
| 258 |
} |
| 259 |
|
| 260 |
if (ev.isKeyPress(SoKeyboardEvent::LEFT_CONTROL)) { |
| 261 |
ev.flags->processed = true; |
| 262 |
return transit<NS::ZoomState>(); |
| 263 |
} |
| 264 |
|
| 265 |
if (ev.isRelease(SoMouseButtonEvent::BUTTON3) && ev.isDownNoButton()) { |
| 266 |
ev.flags->processed = true; |
| 267 |
return transit<NS::IdleState>(); |
| 268 |
} |
| 269 |
return forward_event(); |
| 270 |
} |
| 271 |
|
| 272 |
private: |
| 273 |
SbVec2s base_pos; |
| 274 |
}; |
| 275 |
|
| 276 |
struct NS::PanState: public sc::state<NS::PanState, NS::NaviMachine> |
| 277 |
{ |
| 278 |
using reactions = sc::custom_reaction<NS::Event>; |
| 279 |
explicit PanState(my_context ctx) |
| 280 |
: my_base(ctx) |
| 281 |
{ |
| 282 |
auto& ns = this->outermost_context().ns; |
| 283 |
const NS::Event* ev = static_cast<const NS::Event*>(this->triggering_event()); |
| 284 |
ns.setViewingMode(NavigationStyle::PANNING); |
| 285 |
this->base_pos = ev->inventor_event->getPosition(); |
| 286 |
this->ratio = ns.viewer->getSoRenderManager()->getViewportRegion().getViewportAspectRatio(); |
| 287 |
ns.centerTime = ev->inventor_event->getTime(); |
| 288 |
ns.setupPanningPlane(ns.getCamera()); |
| 289 |
} |
| 290 |
sc::result react(const NS::Event& ev) |
| 291 |
{ |
| 292 |
if (ev.isLocation2Event()) { |
| 293 |
ev.flags->processed = true; |
| 294 |
SbVec2s pos = ev.inventor_event->getPosition(); |
| 295 |
auto& ns = this->outermost_context().ns; |
| 296 |
ns.panCamera( |
| 297 |
ns.viewer->getSoRenderManager()->getCamera(), |
| 298 |
this->ratio, |
| 299 |
ns.panningplane, |
| 300 |
ns.normalizePixelPos(pos), |
| 301 |
ns.normalizePixelPos(this->base_pos) |
| 302 |
); |
| 303 |
this->base_pos = pos; |
| 304 |
} |
| 305 |
|
| 306 |
if (ev.isRelease(SoMouseButtonEvent::BUTTON2) && ev.isDownButton3()) { |
| 307 |
ev.flags->processed = true; |
| 308 |
return transit<NS::RotateState>(); |
| 309 |
} |
| 310 |
|
| 311 |
if (ev.isKeyRelease(SoKeyboardEvent::LEFT_SHIFT) && ev.isDownButton3()) { |
| 312 |
ev.flags->processed = true; |
| 313 |
return transit<NS::RotateState>(); |
| 314 |
} |
| 315 |
|
| 316 |
if (ev.isRelease(SoMouseButtonEvent::BUTTON3)) { |
| 317 |
ev.flags->processed = true; |
| 318 |
return transit<NS::IdleState>(); |
| 319 |
} |
| 320 |
|
| 321 |
return forward_event(); |
| 322 |
} |
| 323 |
|
| 324 |
private: |
| 325 |
SbVec2s base_pos; |
| 326 |
float ratio {1.0F}; |
| 327 |
}; |
| 328 |
|
| 329 |
struct NS::ZoomState: public sc::state<NS::ZoomState, NS::NaviMachine> |
| 330 |
{ |
| 331 |
using reactions = sc::custom_reaction<NS::Event>; |
| 332 |
explicit ZoomState(my_context ctx) |
| 333 |
: my_base(ctx) |
| 334 |
{ |
| 335 |
auto& ns = this->outermost_context().ns; |
| 336 |
const NS::Event* ev = static_cast<const NS::Event*>(this->triggering_event()); |
| 337 |
ns.setViewingMode(NavigationStyle::ZOOMING); |
| 338 |
this->base_pos = ev->inventor_event->getPosition(); |
| 339 |
} |
| 340 |
|
| 341 |
sc::result react(const NS::Event& ev) |
| 342 |
{ |
| 343 |
if (ev.isLocation2Event()) { |
| 344 |
ev.flags->processed = true; |
| 345 |
SbVec2s pos = ev.inventor_event->getPosition(); |
| 346 |
auto& ns = this->outermost_context().ns; |
| 347 |
ns.zoomByCursor(ns.normalizePixelPos(pos), ns.normalizePixelPos(this->base_pos)); |
| 348 |
this->base_pos = pos; |
| 349 |
} |
| 350 |
|
| 351 |
if (ev.isRelease(SoMouseButtonEvent::BUTTON1) && ev.isDownButton3()) { |
| 352 |
ev.flags->processed = true; |
| 353 |
return transit<NS::RotateState>(); |
| 354 |
} |
| 355 |
|
| 356 |
if (ev.isKeyRelease(SoKeyboardEvent::LEFT_CONTROL) && ev.isDownButton3()) { |
| 357 |
ev.flags->processed = true; |
| 358 |
return transit<NS::RotateState>(); |
| 359 |
} |
| 360 |
|
| 361 |
if (ev.isRelease(SoMouseButtonEvent::BUTTON3)) { |
| 362 |
ev.flags->processed = true; |
| 363 |
return transit<NS::IdleState>(); |
| 364 |
} |
| 365 |
|
| 366 |
return forward_event(); |
| 367 |
} |
| 368 |
|
| 369 |
private: |
| 370 |
SbVec2s base_pos; |
| 371 |
}; |
| 372 |
|
| 373 |
struct NS::SelectionState: public sc::state<NS::SelectionState, NS::NaviMachine> |
| 374 |
{ |
| 375 |
using reactions = sc::custom_reaction<NS::Event>; |
| 376 |
explicit SelectionState(my_context ctx) |
| 377 |
: my_base(ctx) |
| 378 |
{ |
| 379 |
auto& ns = this->outermost_context().ns; |
| 380 |
const NS::Event* ev = static_cast<const NS::Event*>(this->triggering_event()); |
| 381 |
|
| 382 |
ns.setViewingMode(NavigationStyle::BOXZOOM); |
| 383 |
ns.startSelection(NavigationStyle::Rubberband); |
| 384 |
fakeLeftButtonDown(ev->inventor_event->getPosition()); |
| 385 |
} |
| 386 |
|
| 387 |
void fakeLeftButtonDown(const SbVec2s& pos) |
| 388 |
{ |
| 389 |
SoMouseButtonEvent mbe; |
| 390 |
mbe.setButton(SoMouseButtonEvent::BUTTON1); |
| 391 |
mbe.setState(SoMouseButtonEvent::DOWN); |
| 392 |
mbe.setPosition(pos); |
| 393 |
|
| 394 |
auto& ns = this->outermost_context().ns; |
| 395 |
ns.processEvent(&mbe); |
| 396 |
} |
| 397 |
|
| 398 |
sc::result react(const NS::Event& /*ev*/) |
| 399 |
{ |
| 400 |
// This isn't called while selection mode is active |
| 401 |
return transit<NS::IdleState>(); |
| 402 |
} |
| 403 |
}; |
| 404 |
|
| 405 |
// ---------------------------------------------------------------------------------- |
| 406 |
|
| 407 |
/* TRANSLATOR Gui::SiemensNXNavigationStyle */ |
| 408 |
|
| 409 |
TYPESYSTEM_SOURCE(Gui::SiemensNXNavigationStyle, Gui::UserNavigationStyle) |
| 410 |
|
| 411 |
SiemensNXNavigationStyle::SiemensNXNavigationStyle() |
| 412 |
{ |
| 413 |
naviMachine.reset(new NaviStateMachineT(new NaviMachine(*this))); |
| 414 |
} |
| 415 |
|
| 416 |
SiemensNXNavigationStyle::~SiemensNXNavigationStyle() |
| 417 |
{} |
| 418 |
|
| 419 |
const char* SiemensNXNavigationStyle::mouseButtons(ViewerMode mode) |
| 420 |
{ |
| 421 |
switch (mode) { |
| 422 |
case NavigationStyle::SELECTION: |
| 423 |
return QT_TR_NOOP("Press left mouse button"); |
| 424 |
case NavigationStyle::PANNING: |
| 425 |
return QT_TR_NOOP("Press middle+right click"); |
| 426 |
case NavigationStyle::DRAGGING: |
| 427 |
return QT_TR_NOOP("Press middle mouse button"); |
| 428 |
case NavigationStyle::ZOOMING: |
| 429 |
return QT_TR_NOOP("Scroll mouse wheel"); |
| 430 |
default: |
| 431 |
return "No description"; |
| 432 |
} |
| 433 |
} |
| 434 |
|
| 435 |
std::string SiemensNXNavigationStyle::userFriendlyName() const |
| 436 |
{ |
| 437 |
return {"Siemens NX"}; |
| 438 |
} |
| 439 |
|
| 440 |
SbBool SiemensNXNavigationStyle::processKeyboardEvent(const SoKeyboardEvent* const event) |
| 441 |
{ |
| 442 |
// See https://forum.freecad.org/viewtopic.php?t=96459 |
| 443 |
// Isometric view: Home key button |
| 444 |
// Trimetric view: End key button |
| 445 |
// Fit all: CTRL+F |
| 446 |
// Normal view: F8 |
| 447 |
switch (event->getKey()) { |
| 448 |
case SoKeyboardEvent::F: |
| 449 |
if (event->wasCtrlDown()) { |
| 450 |
viewer->viewAll(); |
| 451 |
return true; |
| 452 |
} |
| 453 |
break; |
| 454 |
case SoKeyboardEvent::HOME: { |
| 455 |
viewer->setCameraOrientation(Camera::rotation(Camera::Isometric)); |
| 456 |
return true; |
| 457 |
} |
| 458 |
case SoKeyboardEvent::END: { |
| 459 |
viewer->setCameraOrientation(Camera::rotation(Camera::Trimetric)); |
| 460 |
return true; |
| 461 |
} |
| 462 |
case SoKeyboardEvent::F8: { |
| 463 |
viewer->setCameraOrientation(Camera::rotation(Camera::Top)); |
| 464 |
return true; |
| 465 |
} |
| 466 |
default: |
| 467 |
break; |
| 468 |
} |
| 469 |
|
| 470 |
return inherited::processKeyboardEvent(event); |
| 471 |
} |
| 472 |
|
| 473 |
// NOLINTEND(cppcoreguidelines-pro-type-static-cast-downcast, |
| 474 |
// cppcoreguidelines-avoid*, |
| 475 |
// readability-avoid-const-params-in-decls) |