1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2024-11-30 22:51:16 +01:00

Added zooming on double clicks

Fixed "zoom under mouse" algorithm
This commit is contained in:
Martin Tůma 2016-10-29 21:22:26 +02:00
parent 0d1b416c4f
commit eb03fe6ead
2 changed files with 29 additions and 18 deletions

View File

@ -447,11 +447,14 @@ void PathView::rescale()
_mapScale->setZoom(_zoom); _mapScale->setZoom(_zoom);
} }
void PathView::zoom(int z, const QPointF &pos) void PathView::zoom(int z, const QPoint &pos)
{ {
if (_tracks.isEmpty() && _routes.isEmpty() && _waypoints.isEmpty()) if (_tracks.isEmpty() && _routes.isEmpty() && _waypoints.isEmpty())
return; return;
QPoint offset = pos - viewport()->rect().center();
QPointF spos = mapToScene(pos);
qreal scale = _scale; qreal scale = _scale;
_zoom = z; _zoom = z;
@ -466,7 +469,7 @@ void PathView::zoom(int z, const QPointF &pos)
&& br.height() < viewport()->size().height()) && br.height() < viewport()->size().height())
centerOn(br.center()); centerOn(br.center());
else else
centerOn(pos * scale/_scale); centerOn((spos * (scale/_scale)) - offset);
_mapScale->setZoom(_zoom); _mapScale->setZoom(_zoom);
@ -475,14 +478,21 @@ void PathView::zoom(int z, const QPointF &pos)
void PathView::wheelEvent(QWheelEvent *event) void PathView::wheelEvent(QWheelEvent *event)
{ {
if (_tracks.isEmpty() && _routes.isEmpty() && _waypoints.isEmpty())
return;
QPointF pos = mapToScene(event->pos());
int z = (event->delta() > 0) ? int z = (event->delta() > 0) ?
qMin(_zoom + 1, ZOOM_MAX) : qMax(_zoom - 1, ZOOM_MIN); qMin(_zoom + 1, ZOOM_MAX) : qMax(_zoom - 1, ZOOM_MIN);
zoom(z, pos); zoom(z, event->pos());
}
void PathView::mouseDoubleClickEvent(QMouseEvent *event)
{
if (event->button() != Qt::LeftButton && event->button() != Qt::RightButton)
return;
int z = (event->button() == Qt::LeftButton) ?
qMin(_zoom + 1, ZOOM_MAX) : qMax(_zoom - 1, ZOOM_MIN);
zoom(z, event->pos());
} }
void PathView::keyPressEvent(QKeyEvent *event) void PathView::keyPressEvent(QKeyEvent *event)
@ -495,7 +505,7 @@ void PathView::keyPressEvent(QKeyEvent *event)
z = qMax(_zoom - 1, ZOOM_MIN); z = qMax(_zoom - 1, ZOOM_MIN);
if (z >= 0) if (z >= 0)
zoom(z, mapToScene(QRect(QPoint(), size()).center())); zoom(z, QRect(QPoint(), size()).center());
else else
QWidget::keyPressEvent(event); QWidget::keyPressEvent(event);
} }
@ -656,7 +666,7 @@ void PathView::drawBackground(QPainter *painter, const QRectF &rect)
} }
} }
void PathView::resizeEvent(QResizeEvent *e) void PathView::resizeEvent(QResizeEvent *event)
{ {
if (_tracks.isEmpty() && _routes.isEmpty() && _waypoints.isEmpty()) if (_tracks.isEmpty() && _routes.isEmpty() && _waypoints.isEmpty())
return; return;
@ -668,12 +678,12 @@ void PathView::resizeEvent(QResizeEvent *e)
QRectF ba = br.adjusted(-Tile::size(), -Tile::size(), Tile::size(), QRectF ba = br.adjusted(-Tile::size(), -Tile::size(), Tile::size(),
Tile::size()); Tile::size());
if (ba.width() < e->size().width()) { if (ba.width() < event->size().width()) {
qreal diff = e->size().width() - ba.width(); qreal diff = event->size().width() - ba.width();
ba.adjust(-diff/2, 0, diff/2, 0); ba.adjust(-diff/2, 0, diff/2, 0);
} }
if (ba.height() < e->size().height()) { if (ba.height() < event->size().height()) {
qreal diff = e->size().height() - ba.height(); qreal diff = event->size().height() - ba.height();
ba.adjust(0, -diff/2, 0, diff/2); ba.adjust(0, -diff/2, 0, diff/2);
} }
@ -683,7 +693,7 @@ void PathView::resizeEvent(QResizeEvent *e)
resetCachedContent(); resetCachedContent();
} }
void PathView::paintEvent(QPaintEvent *e) void PathView::paintEvent(QPaintEvent *event)
{ {
QPointF scenePos = mapToScene(rect().bottomRight() + QPoint( QPointF scenePos = mapToScene(rect().bottomRight() + QPoint(
-(SCALE_OFFSET + _mapScale->boundingRect().width()), -(SCALE_OFFSET + _mapScale->boundingRect().width()),
@ -691,5 +701,5 @@ void PathView::paintEvent(QPaintEvent *e)
if (_mapScale->pos() != scenePos && !_plot) if (_mapScale->pos() != scenePos && !_plot)
_mapScale->setPos(scenePos); _mapScale->setPos(scenePos);
QGraphicsView::paintEvent(e); QGraphicsView::paintEvent(event);
} }

View File

@ -74,14 +74,15 @@ private:
qreal mapScale(int zoom) const; qreal mapScale(int zoom) const;
void rescale(qreal scale); void rescale(qreal scale);
void rescale(); void rescale();
void zoom(int z, const QPointF &pos); void zoom(int z, const QPoint &pos);
void updatePOIVisibility(); void updatePOIVisibility();
void mouseDoubleClickEvent(QMouseEvent *event);
void wheelEvent(QWheelEvent *event); void wheelEvent(QWheelEvent *event);
void keyPressEvent(QKeyEvent *event); void keyPressEvent(QKeyEvent *event);
void drawBackground(QPainter *painter, const QRectF &rect); void drawBackground(QPainter *painter, const QRectF &rect);
void resizeEvent(QResizeEvent *e); void resizeEvent(QResizeEvent *event);
void paintEvent(QPaintEvent *e); void paintEvent(QPaintEvent *event);
QGraphicsScene *_scene; QGraphicsScene *_scene;
ScaleItem *_mapScale; ScaleItem *_mapScale;