profile: remove need for MainWindow when rendering the profile

Very annoyingly, to render the profile for printing / export,
the profile still had to be show()n, thus requiring a parent
window.

Analysis of qmlprofile.c showed that this was due to the
transformation matrix not being properly set up on non-show()n
scenes.

Instead, we can simply render via the QGraphicsScene
(circumventing the QGraphicsView).

The code was factored out into the ProfileWidget2::draw()
function. This will hopefully make it easier to change
the size-code of the profile.

Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
This commit is contained in:
Berthold Stoeger 2021-05-06 14:45:09 +02:00 committed by Dirk Hohndel
parent 13d4f595cb
commit b39e88b8c6
5 changed files with 51 additions and 48 deletions

View file

@ -34,32 +34,27 @@ bool ExportCallback::canceled() const
#if !defined(SUBSURFACE_MOBILE)
#include "desktop-widgets/mainwindow.h" // Currently needed for profile printing. TODO: remove.
// Let's say that 800x600 is a "reasonable" profile size. Use four times that for printing.
static constexpr int profileScale = 4;
static constexpr int profileWidth = 800 * profileScale;
static constexpr int profileHeight = 600 * profileScale;
static void exportProfile(ProfileWidget2 *profile, const struct dive *dive, const QString &filename)
{
profile->setProfileState(dive, 0);
profile->plotDive(dive, 0, false, true);
QImage image = QImage(profile->size(), QImage::Format_RGB32);
QImage image = QImage(QSize(profileWidth, profileHeight), QImage::Format_RGB32);
QPainter paint;
paint.begin(&image);
profile->render(&paint);
profile->draw(&paint, QRect(0, 0, profileWidth, profileHeight));
image.save(filename);
}
static std::unique_ptr<ProfileWidget2> getPrintProfile()
{
// Let's say that 800x600 is a "reasonable" profile size. Use four times that for printing.
const int scale = 4;
QSize size(800 * scale, 600 * scale);
// TODO: Annoyingly, this still needs a parent window? Otherwise,
// the profile is shown as its own window, when calling show() below.
auto profile = std::make_unique<ProfileWidget2>(nullptr, MainWindow::instance());
profile->resize(size);
profile->show(); // Ominous: if the scene isn't shown, parts of the plot are missing. Needs investigation.
auto profile = std::make_unique<ProfileWidget2>(nullptr, nullptr);
profile->setPrintMode(true);
profile->setFontPrintScale((double)scale);
profile->setFontPrintScale((double)profileScale);
return profile;
}