profile: move axis animation code to ProfileView

This feels quite a bit slower than the non-QtQuick version. This
makes sense, as there is an additional level of indirection. Instead
of painting directly, we paint into an QImage and turn that into
a QSGTexture.

Ultimately one would think that we should render directly using
QtQuick. Alas, we can't, since that would mean no more printing/
exporting of profiles. How sad.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2023-05-20 15:22:02 +02:00
parent 15ea0440ad
commit 196a33ba4b
4 changed files with 58 additions and 49 deletions

View file

@ -6,11 +6,40 @@
#include "core/errorhelper.h"
#include "core/pref.h"
#include "core/settings/qPrefTechnicalDetails.h"
#include "core/settings/qPrefDisplay.h"
#include "qt-quick/chartitem.h"
#include <QAbstractAnimation>
#include <QDebug>
#include <QElapsedTimer>
// Class for animations (if any). Might want to do our own.
class ProfileAnimation : public QAbstractAnimation {
ProfileView &view;
// For historical reasons, speed is actually the duration
// (i.e. the reciprocal of speed). Ouch, that hurts.
int speed;
int duration() const override
{
return speed;
}
void updateCurrentTime(int time) override
{
// Note: we explicitly pass 1.0 at the end, so that
// the callee can do a simple float comparison for "end".
view.anim(time == speed ? 1.0
: static_cast<double>(time) / speed);
}
public:
ProfileAnimation(ProfileView &view, int animSpeed) :
view(view),
speed(animSpeed)
{
start();
}
};
static double calcZoom(int zoomLevel)
{
// Base of exponential zoom function: one wheel-click will increase the zoom by 15%.
@ -97,16 +126,16 @@ void ProfileView::plotDive(const struct dive *dIn, int dcIn, int flags)
DivePlannerPointsModel *model = nullptr;
bool inPlanner = flags & RenderFlags::PlanMode;
QColor backgroundColor = inPlanner ? QColor("#D7E3EF")
: getColor(::BACKGROUND, false);
double zoom = calcZoom(zoomLevel);
int animSpeed = flags & RenderFlags::Instant ? 0 : qPrefDisplay::animation_speed();
profileScene->resize(size());
profileScene->plotDive(d, dc, model, inPlanner, true, //flags & RenderFlags::Instant,
profileScene->plotDive(d, dc, animSpeed, model, inPlanner,
flags & RenderFlags::DontRecalculatePlotInfo,
shouldCalculateMax, zoom, zoomedPosition);
profileItem->draw(size(), backgroundColor, *profileScene);
background = inPlanner ? QColor("#D7E3EF") : getColor(::BACKGROUND, false);
profileItem->draw(size(), background, *profileScene);
//rulerItem->setVisible(prefs.rulergraph && currentState != PLAN && currentState != EDIT);
//toolTipItem->setPlotInfo(profileScene->plotInfo);
@ -136,4 +165,19 @@ void ProfileView::plotDive(const struct dive *dIn, int dcIn, int flags)
qPrefTechnicalDetails::set_calcndltts(false);
report_error("%s", qPrintable(tr("Show NDL / TTS was disabled because of excessive processing time")));
}
// Reset animation.
if (animSpeed <= 0)
animation.reset();
else
animation = std::make_unique<ProfileAnimation>(*this, animSpeed);
}
void ProfileView::anim(double fraction)
{
if (!profileScene || !profileItem)
return;
profileScene->anim(fraction);
profileItem->draw(size(), background, *profileScene);
update();
}