profile: move zooming/panning code to ProfileView

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2023-05-20 17:12:33 +02:00
parent 9ab7819fd1
commit 413b236867
4 changed files with 94 additions and 85 deletions

View file

@ -11,6 +11,7 @@
#include "qt-quick/chartitem.h"
#include <QAbstractAnimation>
#include <QCursor>
#include <QDebug>
#include <QElapsedTimer>
@ -53,6 +54,7 @@ ProfileView::ProfileView(QQuickItem *parent) : ChartView(parent, ProfileZValue::
dc(0),
zoomLevel(0),
zoomedPosition(0.0),
panning(false),
empty(true),
shouldCalculateMax(true)
{
@ -86,6 +88,8 @@ ProfileView::ProfileView(QQuickItem *parent) : ChartView(parent, ProfileZValue::
connect(pp_gas, &qPrefPartialPressureGas::pheChanged, this, &ProfileView::replot);
connect(pp_gas, &qPrefPartialPressureGas::pn2Changed, this, &ProfileView::replot);
connect(pp_gas, &qPrefPartialPressureGas::po2Changed, this, &ProfileView::replot);
setAcceptTouchEvents(true);
}
ProfileView::ProfileView() : ProfileView(nullptr)
@ -213,3 +217,82 @@ void ProfileView::anim(double fraction)
profileItem->draw(size(), background, *profileScene);
update();
}
void ProfileView::resetZoom()
{
zoomLevel = 0;
zoomedPosition = 0.0;
}
void ProfileView::setZoom(int level)
{
zoomLevel = level;
plotDive(d, dc, RenderFlags::DontRecalculatePlotInfo);
}
void ProfileView::wheelEvent(QWheelEvent *event)
{
if (!d)
return;
if (panning)
return; // No change in zoom level while panning.
if (event->buttons() == Qt::LeftButton)
return;
if (event->angleDelta().y() > 0 && zoomLevel < 20)
setZoom(++zoomLevel);
else if (event->angleDelta().y() < 0 && zoomLevel > 0)
setZoom(--zoomLevel);
else if (event->angleDelta().x() && zoomLevel > 0) {
double oldPos = zoomedPosition;
zoomedPosition = profileScene->calcZoomPosition(calcZoom(zoomLevel),
oldPos,
oldPos - event->angleDelta().x());
if (oldPos != zoomedPosition)
plotDive(d, dc, RenderFlags::Instant | RenderFlags::DontRecalculatePlotInfo);
}
}
void ProfileView::mousePressEvent(QMouseEvent *event)
{
panning = true;
panningOriginalMousePosition = mapToScene(event->pos()).x();
panningOriginalProfilePosition = zoomedPosition;
setCursor(Qt::ClosedHandCursor);
event->accept();
}
void ProfileView::mouseReleaseEvent(QMouseEvent *)
{
if (panning) {
panning = false;
unsetCursor();
}
//if (currentState == PLAN || currentState == EDIT) {
// shouldCalculateMax = true;
// replot();
//}
}
void ProfileView::mouseMoveEvent(QMouseEvent *event)
{
QPointF pos = mapToScene(event->pos());
if (panning) {
double oldPos = zoomedPosition;
zoomedPosition = profileScene->calcZoomPosition(calcZoom(zoomLevel),
panningOriginalProfilePosition,
panningOriginalMousePosition - pos.x());
if (oldPos != zoomedPosition)
plotDive(d, dc, RenderFlags::Instant | RenderFlags::DontRecalculatePlotInfo); // TODO: animations don't work when scrolling
}
//toolTipItem->refresh(d, mapToScene(mapFromGlobal(QCursor::pos())), currentState == PLAN);
//if (currentState == PLAN || currentState == EDIT) {
//QRectF rect = profileScene->profileRegion;
//auto [miny, maxy] = profileScene->profileYAxis->screenMinMax();
//double x = std::clamp(pos.x(), rect.left(), rect.right());
//double y = std::clamp(pos.y(), miny, maxy);
//mouseFollowerHorizontal->setLine(rect.left(), y, rect.right(), y);
//mouseFollowerVertical->setLine(x, rect.top(), x, rect.bottom());
//}
}