1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-10-06 06:43:22 +02:00

Added graph scrolling using the mouse

This commit is contained in:
Martin Tůma 2024-06-05 08:35:10 +02:00
parent 69951fe248
commit 173f618d0b
2 changed files with 45 additions and 17 deletions

View File

@ -20,12 +20,29 @@
#include "graphicsscene.h" #include "graphicsscene.h"
#include "graphview.h" #include "graphview.h"
#define MARGIN 10.0 #define MARGIN 10.0
#define IW(item) ((item)->boundingRect().width()) #define IW(item) ((item)->boundingRect().width())
#define IH(item) ((item)->boundingRect().height()) #define IH(item) ((item)->boundingRect().height())
static inline QPoint POS(QWheelEvent *e)
{
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
return e->pos();
#else // QT 5.15
return e->position().toPoint();
#endif // QT 5.15
}
static inline QPoint POS(QMouseEvent *e)
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
return e->pos();
#else // QT 5.15
return e->position().toPoint();
#endif // QT 5.15
}
GraphView::GraphView(QWidget *parent) GraphView::GraphView(QWidget *parent)
: QGraphicsView(parent) : QGraphicsView(parent)
{ {
@ -77,6 +94,9 @@ GraphView::GraphView(QWidget *parent)
_xLabel = tr("Distance"); _xLabel = tr("Distance");
_zoom = 1.0; _zoom = 1.0;
_angleDelta = 0;
_dragStart = 0;
} }
GraphView::~GraphView() GraphView::~GraphView()
@ -345,25 +365,33 @@ void GraphView::resizeEvent(QResizeEvent *e)
void GraphView::mousePressEvent(QMouseEvent *e) void GraphView::mousePressEvent(QMouseEvent *e)
{ {
if (e->button() == Qt::LeftButton) if (e->button() == Qt::LeftButton)
newSliderPosition(mapToScene(e->pos())); newSliderPosition(mapToScene(POS(e)));
else if (e->button() == Qt::RightButton)
_dragStart = POS(e).x();
QGraphicsView::mousePressEvent(e); QGraphicsView::mousePressEvent(e);
} }
void GraphView::mouseMoveEvent(QMouseEvent *e)
{
if (e->buttons() & Qt::RightButton) {
QScrollBar *sb = horizontalScrollBar();
int x = POS(e).x();
sb->setSliderPosition(sb->sliderPosition() - (x - _dragStart));
_dragStart = x;
}
QGraphicsView::mouseMoveEvent(e);
}
void GraphView::wheelEvent(QWheelEvent *e) void GraphView::wheelEvent(QWheelEvent *e)
{ {
static int deg8 = 0; _angleDelta += e->angleDelta().y();
if (qAbs(_angleDelta) < (15 * 8))
deg8 += e->angleDelta().y();
if (qAbs(deg8) < (15 * 8))
return; return;
deg8 = deg8 % (15 * 8); _angleDelta = _angleDelta % (15 * 8);
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) QPointF pos = mapToScene(POS(e));
QPointF pos = mapToScene(e->pos());
#else // QT 5.15
QPointF pos = mapToScene(e->position().toPoint());
#endif // QT 5.15
QRectF gr(_grid->boundingRect()); QRectF gr(_grid->boundingRect());
QPointF r(pos.x() / gr.width(), pos.y() / gr.height()); QPointF r(pos.x() / gr.width(), pos.y() / gr.height());
@ -374,11 +402,7 @@ void GraphView::wheelEvent(QWheelEvent *e)
QPointF npos(mapFromScene(QPointF(r.x() * ngr.width(), QPointF npos(mapFromScene(QPointF(r.x() * ngr.width(),
r.y() * ngr.height()))); r.y() * ngr.height())));
QScrollBar *sb = horizontalScrollBar(); QScrollBar *sb = horizontalScrollBar();
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0) sb->setSliderPosition(sb->sliderPosition() + npos.x() - POS(e).x());
sb->setSliderPosition(sb->sliderPosition() + npos.x() - e->pos().x());
#else // QT 5.15
sb->setSliderPosition(sb->sliderPosition() + npos.x() - e->position().x());
#endif // QT 5.15
QGraphicsView::wheelEvent(e); QGraphicsView::wheelEvent(e);
} }

View File

@ -58,6 +58,7 @@ protected:
void resizeEvent(QResizeEvent *e); void resizeEvent(QResizeEvent *e);
void mousePressEvent(QMouseEvent *e); void mousePressEvent(QMouseEvent *e);
void mouseMoveEvent(QMouseEvent *e);
void wheelEvent(QWheelEvent *e); void wheelEvent(QWheelEvent *e);
void changeEvent(QEvent *e); void changeEvent(QEvent *e);
void paintEvent(QPaintEvent *e); void paintEvent(QPaintEvent *e);
@ -122,6 +123,9 @@ private:
qreal _minYRange; qreal _minYRange;
qreal _zoom; qreal _zoom;
int _angleDelta;
int _dragStart;
}; };
#endif // GRAPHVIEW_H #endif // GRAPHVIEW_H