23
#include "PreCompiled.h"
25
#include <QContextMenuEvent>
30
#include <App/Application.h>
31
#include <Base/Parameter.h>
32
#include <Mod/TechDraw/App/DrawPage.h>
33
#include <Mod/TechDraw/App/Preferences.h>
36
#include "QGVNavStyle.h"
40
using namespace TechDraw;
41
using namespace TechDrawGui;
46
QGVNavStyle::QGVNavStyle(QGVPage* qgvp) : m_viewer(qgvp) { initialize(); }
48
QGVNavStyle::~QGVNavStyle() {}
50
void QGVNavStyle::initialize()
52
this->button1down = false;
53
this->button2down = false;
54
this->button3down = false;
55
this->ctrldown = false;
56
this->shiftdown = false;
57
this->altdown = false;
58
this->invertZoom = App::GetApplication()
59
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
60
->GetBool("InvertZoom", true);
61
this->zoomAtCursor = App::GetApplication()
62
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
63
->GetBool("ZoomAtCursor", true);
64
this->zoomStep = App::GetApplication()
65
.GetParameterGroupByPath("User parameter:BaseApp/Preferences/View")
66
->GetFloat("ZoomStep", 0.2f);
68
m_reversePan = Preferences::getPreferenceGroup("General")->GetInt("KbPan", 1);
69
m_reverseScroll = Preferences::getPreferenceGroup("General")->GetInt("KbScroll", 1);
71
panningActive = false;
72
zoomingActive = false;
73
m_clickPending = false;
75
m_zoomPending = false;
76
m_clickButton = Qt::NoButton;
77
m_saveCursor = getViewer()->cursor();
78
m_wheelDeltaCounter = 0;
79
m_mouseDeltaCounter = 0;
82
void QGVNavStyle::setAnchor()
86
m_viewer->setResizeAnchor(QGraphicsView::AnchorUnderMouse);
87
m_viewer->setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
90
m_viewer->setResizeAnchor(QGraphicsView::AnchorViewCenter);
91
m_viewer->setTransformationAnchor(QGraphicsView::AnchorViewCenter);
96
void QGVNavStyle::handleEnterEvent(QEvent* event)
99
if (getViewer()->isBalloonPlacing()) {
100
getViewer()->getBalloonCursor()->hide();
104
void QGVNavStyle::handleFocusOutEvent(QFocusEvent* event)
107
getViewer()->cancelBalloonPlacing();
110
void QGVNavStyle::handleKeyPressEvent(QKeyEvent* event)
113
if (event->modifiers().testFlag(Qt::ControlModifier)) {
114
switch (event->key()) {
120
case Qt::Key_Minus: {
131
if (event->modifiers().testFlag(Qt::NoModifier)) {
132
switch (event->key()) {
134
getViewer()->kbPanScroll(1, 0);
139
getViewer()->kbPanScroll(0, 1);
143
case Qt::Key_Right: {
144
getViewer()->kbPanScroll(-1, 0);
149
getViewer()->kbPanScroll(0, -1);
153
case Qt::Key_Escape: {
154
getViewer()->cancelBalloonPlacing();
158
case Qt::Key_Shift: {
159
this->shiftdown = true;
171
void QGVNavStyle::handleKeyReleaseEvent(QKeyEvent* event)
173
if (event->modifiers().testFlag(Qt::NoModifier)) {
174
switch (event->key()) {
175
case Qt::Key_Shift: {
176
this->shiftdown = false;
187
void QGVNavStyle::handleLeaveEvent(QEvent* event)
190
if (getViewer()->isBalloonPlacing()) {
192
if (getViewer()->getBalloonCursorPos().x() < 32)
194
else if (getViewer()->getBalloonCursorPos().x()
195
> (getViewer()->contentsRect().right() - 32))
196
left_x = getViewer()->contentsRect().right() - 32;
198
left_x = getViewer()->getBalloonCursorPos().x();
201
if (getViewer()->getBalloonCursorPos().y() < 32)
203
else if (getViewer()->getBalloonCursorPos().y()
204
> (getViewer()->contentsRect().bottom() - 32))
205
left_y = getViewer()->contentsRect().bottom() - 32;
207
left_y = getViewer()->getBalloonCursorPos().y();
210
getViewer()->getBalloonCursor()->setGeometry(left_x, left_y, 32, 32);
211
getViewer()->getBalloonCursor()->show();
215
void QGVNavStyle::handleMousePressEvent(QMouseEvent* event)
218
if (!panningActive && (event->button() == Qt::MiddleButton)) {
219
startPan(event->pos());
224
void QGVNavStyle::handleMouseMoveEvent(QMouseEvent* event)
227
if (getViewer()->isBalloonPlacing()) {
228
balloonCursorMovement(event);
240
void QGVNavStyle::handleMouseReleaseEvent(QMouseEvent* event)
243
if (getViewer()->isBalloonPlacing()) {
244
placeBalloon(event->pos());
247
if (panningActive && (event->button() == Qt::MiddleButton)) {
253
bool QGVNavStyle::allowContextMenu(QContextMenuEvent* event)
263
void QGVNavStyle::pseudoContextEvent() { getViewer()->pseudoContextEvent(); }
265
void QGVNavStyle::handleWheelEvent(QWheelEvent* event)
276
constexpr int wheelDeltaThreshold = 120;
277
m_wheelDeltaCounter += std::abs(event->angleDelta().y());
278
if (m_wheelDeltaCounter < wheelDeltaThreshold) {
281
m_wheelDeltaCounter = 0;
283
int rotationDirection = -event->angleDelta().y() / std::abs(event->angleDelta().y());
285
rotationDirection = -rotationDirection;
287
double zoomFactor = 1 + rotationDirection * zoomStep;
291
void QGVNavStyle::zoom(double factor)
293
constexpr double minimumScale(0.01);
294
QTransform transform = getViewer()->transform();
295
double xScale = transform.m11();
296
if (xScale <= minimumScale && factor < 1.0) {
302
getViewer()->scale(factor, factor);
303
m_zoomPending = false;
306
void QGVNavStyle::startZoom(QPoint p)
310
zoomingActive = true;
311
m_zoomPending = false;
312
getViewer()->setZoomCursor();
315
void QGVNavStyle::stopZoom()
318
zoomingActive = false;
319
m_zoomPending = false;
320
getViewer()->resetCursor();
323
double QGVNavStyle::mouseZoomFactor(QPoint p)
325
constexpr int threshold(20);
326
int verticalTravel = (p - zoomOrigin).y();
327
m_mouseDeltaCounter += std::abs(verticalTravel);
328
if (m_mouseDeltaCounter < threshold) {
332
m_mouseDeltaCounter = 0;
333
double direction = verticalTravel / std::abs(verticalTravel);
335
direction = -direction;
337
double factor = 1.0 + (direction * zoomStep);
342
void QGVNavStyle::zoomIn()
344
zoom(1.0 + zoomStep);
347
void QGVNavStyle::zoomOut()
349
zoom(1.0 - zoomStep);
352
void QGVNavStyle::startPan(QPoint p)
355
panningActive = true;
356
m_panPending = false;
357
getViewer()->setPanCursor();
360
void QGVNavStyle::pan(QPoint p)
362
QScrollBar* horizontalScrollbar = getViewer()->horizontalScrollBar();
363
QScrollBar* verticalScrollbar = getViewer()->verticalScrollBar();
364
QPoint direction = p - panOrigin;
366
horizontalScrollbar->setValue(horizontalScrollbar->value() - m_reversePan * direction.x());
367
verticalScrollbar->setValue(verticalScrollbar->value() - m_reverseScroll * direction.y());
372
void QGVNavStyle::stopPan()
375
panningActive = false;
376
m_panPending = false;
377
getViewer()->resetCursor();
380
void QGVNavStyle::startClick(Qt::MouseButton b)
382
m_clickPending = true;
386
void QGVNavStyle::stopClick()
388
m_clickPending = false;
389
m_clickButton = Qt::MouseButton::NoButton;
392
void QGVNavStyle::placeBalloon(QPoint p)
395
getViewer()->getBalloonCursor()->hide();
397
getViewer()->getScene()->createBalloon(getViewer()->mapToScene(p),
398
getViewer()->getBalloonParent());
399
getViewer()->setBalloonPlacing(false);
402
void QGVNavStyle::balloonCursorMovement(QMouseEvent *event)
404
getViewer()->setBalloonCursorPos(event->pos());
410
KeyCombination::KeyCombination() {}
412
KeyCombination::~KeyCombination() {}
414
void KeyCombination::addKey(int inKey)
419
for (auto& k : keys) {
426
keys.push_back(inKey);
430
void KeyCombination::removeKey(int inKey)
432
std::vector<int> newKeys;
433
for (auto& k : keys) {
435
newKeys.push_back(k);
441
void KeyCombination::clear() { keys.clear(); }
443
bool KeyCombination::empty() { return keys.empty(); }
446
bool KeyCombination::haveCombination(int inCombo)
448
bool matched = false;
450
if (keys.size() < 2) {
454
for (auto& k : keys) {
457
if (combo == inCombo) {