1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
// Copyright 2015 - 2025, GIBIS-UNIFESP and the wiRedPanda contributors
// SPDX-License-Identifier: GPL-3.0-or-later
#include "graphicsview.h"
#include <QApplication>
#include <QDebug>
#include <QKeyEvent>
#include <QScrollBar>
GraphicsView::GraphicsView(QWidget *parent)
: QGraphicsView(parent)
{
setAcceptDrops(true);
setMouseTracking(true);
setTransformationAnchor(QGraphicsView::AnchorUnderMouse);
setResizeAnchor(QGraphicsView::AnchorUnderMouse);
setViewportUpdateMode(QGraphicsView::BoundingRectViewportUpdate);
setCacheMode(QGraphicsView::CacheBackground);
}
bool GraphicsView::canZoomIn() const
{
return m_zoomLevel < 3;
}
bool GraphicsView::canZoomOut() const
{
return m_zoomLevel > -9;
}
void GraphicsView::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton) {
m_pan = true;
m_panStartX = event->pos().x();
m_panStartY = event->pos().y();
viewport()->setCursor(Qt::ClosedHandCursor);
event->accept();
return;
}
QGraphicsView::mousePressEvent(event);
}
void GraphicsView::mouseMoveEvent(QMouseEvent *event)
{
if (m_pan || m_space) {
horizontalScrollBar()->setValue(horizontalScrollBar()->value() - (event->pos().x() - m_panStartX));
verticalScrollBar()->setValue(verticalScrollBar()->value() - (event->pos().y() - m_panStartY));
m_panStartX = event->pos().x();
m_panStartY = event->pos().y();
event->accept();
return;
}
m_panStartX = event->pos().x();
m_panStartY = event->pos().y();
QGraphicsView::mouseMoveEvent(event);
}
void GraphicsView::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::MiddleButton) {
m_pan = false;
viewport()->unsetCursor();
event->accept();
return;
}
QGraphicsView::mouseReleaseEvent(event);
}
void GraphicsView::keyPressEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space) {
m_space = true;
viewport()->setCursor(Qt::ClosedHandCursor);
event->accept();
}
QGraphicsView::keyPressEvent(event);
}
void GraphicsView::keyReleaseEvent(QKeyEvent *event)
{
if (event->key() == Qt::Key_Space) {
m_space = false;
viewport()->unsetCursor();
event->accept();
}
QGraphicsView::keyReleaseEvent(event);
}
void GraphicsView::wheelEvent(QWheelEvent *event)
{
const int zoomDirection = event->angleDelta().y();
if (zoomDirection > 0 && canZoomIn()) {
if (m_redirectZoom) {
emit scaleIn();
} else {
zoomIn();
}
} else if (zoomDirection < 0 && canZoomOut()) {
if (m_redirectZoom) {
emit scaleOut();
} else {
zoomOut();
}
}
#if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
centerOn(mapToScene(event->position().toPoint()));
#else
centerOn(mapToScene(event->pos()));
#endif
event->accept();
}
void GraphicsView::setRedirectZoom(const bool value)
{
m_redirectZoom = value;
}
void GraphicsView::setFastMode(const bool fastMode)
{
setRenderHint(QPainter::Antialiasing, !fastMode);
setRenderHint(QPainter::TextAntialiasing, !fastMode);
setRenderHint(QPainter::SmoothPixmapTransform, !fastMode);
}
void GraphicsView::zoomIn()
{
scale(1.25, 1.25);
++m_zoomLevel;
emit zoomChanged();
}
void GraphicsView::zoomOut()
{
scale(0.8, 0.8);
m_zoomLevel--;
emit zoomChanged();
}
void GraphicsView::resetZoom()
{
resetTransform();
m_zoomLevel = 0;
emit zoomChanged();
}
|