1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-28 13:41:16 +01:00

Added support for graph zooming

This commit is contained in:
Martin Tůma 2019-08-26 21:03:40 +02:00
parent 1addb1118d
commit 548c03d543
2 changed files with 43 additions and 4 deletions

View File

@ -1,8 +1,7 @@
#include <QGraphicsScene> #include <QGraphicsScene>
#include <QEvent> #include <QEvent>
#include <QMouseEvent> #include <QMouseEvent>
#include <QPaintEngine> #include <QScrollBar>
#include <QPaintDevice>
#include <QGraphicsSimpleTextItem> #include <QGraphicsSimpleTextItem>
#include <QPalette> #include <QPalette>
#include <QLocale> #include <QLocale>
@ -64,6 +63,8 @@ GraphView::GraphView(QWidget *parent)
_units = Metric; _units = Metric;
_graphType = Distance; _graphType = Distance;
_xLabel = tr("Distance"); _xLabel = tr("Distance");
_zoom = 1.0;
} }
GraphView::~GraphView() GraphView::~GraphView()
@ -283,6 +284,7 @@ void GraphView::redraw(const QSizeF &size)
sx = (size.width() - (my.width() + mx.width())) / r.width(); sx = (size.width() - (my.width() + mx.width())) / r.width();
sy = (size.height() - (mx.height() + my.height()) sy = (size.height() - (mx.height() + my.height())
- _info->boundingRect().height()) / r.height(); - _info->boundingRect().height()) / r.height();
sx *= _zoom;
for (int i = 0; i < _graphs.size(); i++) for (int i = 0; i < _graphs.size(); i++)
_graphs.at(i)->setScale(sx, sy); _graphs.at(i)->setScale(sx, sy);
@ -330,6 +332,39 @@ void GraphView::mousePressEvent(QMouseEvent *e)
QGraphicsView::mousePressEvent(e); QGraphicsView::mousePressEvent(e);
} }
void GraphView::wheelEvent(QWheelEvent *e)
{
static int deg = 0;
deg += e->delta() / 8;
if (qAbs(deg) < 15)
return;
deg = 0;
QPointF pos = mapToScene(e->pos());
QRectF gr(_grid->boundingRect());
QPointF r(pos.x() / gr.width(), pos.y() / gr.height());
_zoom = (e->delta() > 0) ? _zoom * 1.25 : qMax(_zoom / 1.25, 1.0);
redraw();
gr = _grid->boundingRect();
QPointF npos(mapFromScene(QPointF(r.x() * gr.width(), r.y() * gr.height())));
QScrollBar *sb = horizontalScrollBar();
sb->setSliderPosition(sb->sliderPosition() + npos.x() - e->pos().x());
QGraphicsView::wheelEvent(e);
}
void GraphView::paintEvent(QPaintEvent *event)
{
QRectF viewRect(mapToScene(rect()).boundingRect());
_info->setPos(QPointF(viewRect.left() + (viewRect.width()
- _info->boundingRect().width())/2.0, _info->pos().y()));
QGraphicsView::paintEvent(event);
}
void GraphView::plot(QPainter *painter, const QRectF &target, qreal scale) void GraphView::plot(QPainter *painter, const QRectF &target, qreal scale)
{ {
QSizeF canvas = QSizeF(target.width() / scale, target.height() / scale); QSizeF canvas = QSizeF(target.width() / scale, target.height() / scale);
@ -355,6 +390,7 @@ void GraphView::clear()
_bounds = QRectF(); _bounds = QRectF();
_sliderPos = 0; _sliderPos = 0;
_zoom = 1.0;
_scene->setSceneRect(0, 0, 0, 0); _scene->setSceneRect(0, 0, 0, 0);
} }

View File

@ -69,8 +69,6 @@ protected:
void addInfo(const QString &key, const QString &value); void addInfo(const QString &key, const QString &value);
void clearInfo(); void clearInfo();
void changeEvent(QEvent *e);
GraphType _graphType; GraphType _graphType;
Units _units; Units _units;
Palette _palette; Palette _palette;
@ -92,6 +90,9 @@ private:
void resizeEvent(QResizeEvent *e); void resizeEvent(QResizeEvent *e);
void mousePressEvent(QMouseEvent *e); void mousePressEvent(QMouseEvent *e);
void wheelEvent(QWheelEvent *e);
void changeEvent(QEvent *e);
void paintEvent(QPaintEvent *event);
QGraphicsScene *_scene; QGraphicsScene *_scene;
@ -112,6 +113,8 @@ private:
QString _xLabel, _yLabel; QString _xLabel, _yLabel;
int _precision; int _precision;
qreal _minYRange; qreal _minYRange;
qreal _zoom;
}; };
#endif // GRAPHVIEW_H #endif // GRAPHVIEW_H