aboutsummaryrefslogtreecommitdiff
path: root/libs/installer_fomod_plus/installer/ui/ScaleLabel.cpp
blob: 3a336403a29e0fdab64b7b6919ff846af329d358 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "ScaleLabel.h"
#include <QResizeEvent>
#include <iostream>

// Taken from https://github.com/ModOrganizer2/modorganizer-installer_fomod/blob/master/src/scalelabel.h
static bool isResourceMovie(const QString& path)
{
    const auto formats = QMovie::supportedFormats();
    return std::ranges::any_of(formats, [&path](const QByteArray& format) {
        return path.endsWith("." + QString::fromUtf8(format));
    });
}

void ScaleLabel::setScalableResource(const QString& path)
{
    if (const auto m = movie()) {
        setMovie(nullptr);
        delete m;
        mOriginalMovieSize = QSize();
    }
    if (!pixmap().isNull()) {
        setPixmap(QPixmap());
        mUnscaledImage = QImage();
    }

    if (path.isEmpty()) {
        return;
    }

    if (isResourceMovie(path)) {
        setScalableMovie(path);
    } else {
        setScalableImage(path);
    }
}

void ScaleLabel::setStatic(const bool isStatic)
{
    misStatic = isStatic;

    if (const auto m = movie()) {
        if (isStatic) {
            m->stop();
        } else {
            m->start();
        }
    }
}

void ScaleLabel::setScalableMovie(const QString& path)
{
    const auto m = new QMovie(path);
    if (!m->isValid()) {
        qWarning(">%s< is an invalid movie. Reason: %s", qUtf8Printable(path),
            m->lastErrorString().toStdString().c_str());
        delete m;
        return;
    }

    m->setParent(this);
    setMovie(m);
    m->start();
    m->stop();
    mOriginalMovieSize = m->currentImage().size();

    m->setScaledSize(mOriginalMovieSize.scaled(size(), Qt::KeepAspectRatio));
    if (!misStatic) {
        m->start();
    }
    mHasResource = true;
}

void ScaleLabel::setScalableImage(const QString& path)
{
    if (const QImage image(path); image.isNull()) {
        qWarning(">%s< is a null image", qUtf8Printable(path));
    } else {
        mUnscaledImage = image;
        setPixmap(QPixmap::fromImage(image).scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
        mHasResource = true;
    }
}

void ScaleLabel::resizeEvent(QResizeEvent* event)
{
    if (const auto m = movie()) {
        m->stop();
        m->setScaledSize(mOriginalMovieSize.scaled(event->size(), Qt::KeepAspectRatio));
        m->start();

        // We can't just skip the start() above since that is what triggers the label to
        // resize the movie The only way to resize the movie but keep it paused is to start
        // and then re-stop it
        if (misStatic) {
            m->stop();
        }
    }
    if (const auto p = pixmap(); !p.isNull()) {
        setPixmap(
            QPixmap::fromImage(mUnscaledImage).scaled(event->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
}

void ScaleLabel::showEvent(QShowEvent* event)
{
    QLabel::showEvent(event);

    if (const auto m = movie()) {
        m->stop();
        m->setScaledSize(mOriginalMovieSize.scaled(size(), Qt::KeepAspectRatio));
        m->start();

        if (misStatic) {
            m->stop();
        }
    }
    if (const auto p = pixmap(); !p.isNull()) {
        setPixmap(QPixmap::fromImage(mUnscaledImage).scaled(size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
    }
}