profile: merge plotDive() into draw() call

Rendering resets the size, which now recalculates the axes.
Therefore, plotDive() must be called. The callers were doing
the opposite: call plotDive() first, then draw().

To make it easier for the callers, present a single interface
that handles these subtleties.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-08-30 22:59:45 +02:00 committed by Dirk Hohndel
parent d0beae59f9
commit 8046a05e95
7 changed files with 22 additions and 39 deletions

View file

@ -41,11 +41,10 @@ static constexpr int profileHeight = 600 * profileScale;
static void exportProfile(ProfileScene *profile, const struct dive *dive, const QString &filename)
{
profile->plotDive(dive, 0, nullptr, false, true);
QImage image = QImage(QSize(profileWidth, profileHeight), QImage::Format_RGB32);
QPainter paint;
paint.begin(&image);
profile->draw(&paint, QRect(0, 0, profileWidth, profileHeight));
profile->draw(&paint, QRect(0, 0, profileWidth, profileHeight), dive, 0, nullptr, false);
image.save(filename);
}

View file

@ -610,8 +610,7 @@ void PlannerWidgets::printDecoPlan()
painter.setRenderHint(QPainter::SmoothPixmapTransform);
auto profile = std::make_unique<ProfileScene>(1.0, true, false);
profile->plotDive(&displayed_dive, 0, DivePlannerPointsModel::instance(), true, true);
profile->draw(&painter, QRect(0, 0, pixmap.width(), pixmap.height()));
profile->draw(&painter, QRect(0, 0, pixmap.width(), pixmap.height()), &displayed_dive, 0, DivePlannerPointsModel::instance(), true);
QByteArray byteArray;
QBuffer buffer(&byteArray);

View file

@ -38,8 +38,7 @@ void Printer::putProfileImage(const QRect &profilePlaceholder, const QRect &view
// use the placeHolder and the viewPort position to calculate the relative position of the dive profile.
QRect pos(x, y, profilePlaceholder.width(), profilePlaceholder.height());
profile->plotDive(dive, 0, nullptr, true);
profile->draw(painter, pos);
profile->draw(painter, pos, dive, 0, nullptr, false);
}
void Printer::flowRender()

View file

@ -530,19 +530,21 @@ void ProfileScene::plotDive(const struct dive *dIn, int dcIn, DivePlannerPointsM
diveComputerText->set(dcText, getColor(TIME_TEXT, isGrayscale));
}
QImage ProfileScene::toImage(QSize size)
void ProfileScene::draw(QPainter *painter, const QRect &pos,
const struct dive *d, int dc,
DivePlannerPointsModel *plannerModel, bool inPlanner)
{
// The size of chart with respect to the scene is fixed - by convention - to 100.0.
// We add 2% to the height so that the dive computer name is not cut off.
QRectF sceneRect(0.0, 0.0, 100.0, 102.0);
QSize size = pos.size();
resize(QSizeF(size));
plotDive(d, dc, plannerModel, inPlanner, true, true);
QImage image(size, QImage::Format_ARGB32);
QImage image(pos.size(), QImage::Format_ARGB32);
image.fill(getColor(::BACKGROUND, isGrayscale));
QPainter imgPainter(&image);
imgPainter.setRenderHint(QPainter::Antialiasing);
imgPainter.setRenderHint(QPainter::SmoothPixmapTransform);
render(&imgPainter, QRect(QPoint(), size), sceneRect, Qt::IgnoreAspectRatio);
render(&imgPainter, QRect(QPoint(), size), sceneRect(), Qt::IgnoreAspectRatio);
imgPainter.end();
if (isGrayscale) {
@ -556,12 +558,5 @@ QImage ProfileScene::toImage(QSize size)
}
}
}
return image;
}
void ProfileScene::draw(QPainter *painter, const QRect &pos)
{
QImage img = toImage(pos.size());
painter->drawImage(pos, img);
painter->drawImage(pos, image);
}

View file

@ -48,8 +48,9 @@ public:
void plotDive(const struct dive *d, int dc, DivePlannerPointsModel *plannerModel = nullptr, bool inPlanner = false,
bool instant = false, bool calcMax = true);
void draw(QPainter *painter, const QRect &pos);
QImage toImage(QSize size);
void draw(QPainter *painter, const QRect &pos,
const struct dive *d, int dc,
DivePlannerPointsModel *plannerModel = nullptr, bool inPlanner = false);
const struct dive *d;
int dc;

View file

@ -1,4 +1,4 @@
// SPDX-License-Identifier: GPL-2.0
// SPDX-License-Identifier: GPL-2.
#include "qmlprofile.h"
#include "profilescene.h"
#include "mobile-widgets/qmlmanager.h"
@ -54,7 +54,12 @@ void QMLProfile::paint(QPainter *painter)
// let's look at the intended size of the content and scale our scene accordingly
QRect painterRect = painter->viewport();
m_profileWidget->draw(painter, painterRect);
if (m_diveId < 0)
return;
struct dive *d = get_dive_by_uniq_id(m_diveId);
if (!d)
return;
m_profileWidget->draw(painter, painterRect, d, dc_number, nullptr, false);
}
void QMLProfile::setMargin(int margin)
@ -67,22 +72,9 @@ int QMLProfile::diveId() const
return m_diveId;
}
void QMLProfile::updateProfile()
{
struct dive *d = get_dive_by_uniq_id(m_diveId);
if (!d)
return;
if (verbose)
qDebug() << "update profile for dive #" << d->number << "offeset" << QString::number(m_xOffset, 'f', 1) << "/" << QString::number(m_yOffset, 'f', 1);
m_profileWidget->plotDive(d, dc_number);
}
void QMLProfile::setDiveId(int diveId)
{
m_diveId = diveId;
if (m_diveId < 0)
return;
updateProfile();
}
qreal QMLProfile::devicePixelRatio() const
@ -129,7 +121,6 @@ void QMLProfile::divesChanged(const QVector<dive *> &dives, DiveField)
for (struct dive *d: dives) {
if (d->id == m_diveId) {
qDebug() << "dive #" << d->number << "changed, trigger profile update";
m_profileWidget->plotDive(d, dc_number);
triggerUpdate();
return;
}

View file

@ -40,7 +40,6 @@ private:
int m_margin;
qreal m_xOffset, m_yOffset;
std::unique_ptr<ProfileScene> m_profileWidget;
void updateProfile();
void createProfileView();
private slots: