summaryrefslogtreecommitdiff
path: root/plugin/src/Caelestia/cachingimagemanager.hpp
blob: f05ea34761b695191e4aa6f2266b64bc89646b53 (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
#pragma once

#include <QtQuick/qquickitem.h>
#include <qobject.h>
#include <qqmlintegration.h>

namespace caelestia {

class CachingImageManager : public QObject {
    Q_OBJECT
    QML_ELEMENT

    Q_PROPERTY(QQuickItem* item READ item WRITE setItem NOTIFY itemChanged REQUIRED)
    Q_PROPERTY(QUrl cacheDir READ cacheDir WRITE setCacheDir NOTIFY cacheDirChanged REQUIRED)

    Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
    Q_PROPERTY(QUrl cachePath READ cachePath NOTIFY cachePathChanged)

public:
    explicit CachingImageManager(QObject* parent = nullptr)
        : QObject(parent)
        , m_item(nullptr) {}

    [[nodiscard]] QQuickItem* item() const;
    void setItem(QQuickItem* item);

    [[nodiscard]] QUrl cacheDir() const;
    void setCacheDir(const QUrl& cacheDir);

    [[nodiscard]] QString path() const;
    void setPath(const QString& path);

    [[nodiscard]] QUrl cachePath() const;

    Q_INVOKABLE void updateSource();
    Q_INVOKABLE void updateSource(const QString& path);

signals:
    void itemChanged();
    void cacheDirChanged();

    void pathChanged();
    void cachePathChanged();
    void usingCacheChanged();

private:
    QString m_shaPath;

    QQuickItem* m_item;
    QUrl m_cacheDir;

    QString m_path;
    QUrl m_cachePath;

    QMetaObject::Connection m_widthConn;
    QMetaObject::Connection m_heightConn;

    [[nodiscard]] qreal effectiveScale() const;
    [[nodiscard]] QSize effectiveSize() const;

    void createCache(const QString& path, const QString& cache, const QString& fillMode, const QSize& size) const;
    [[nodiscard]] static QString sha256sum(const QString& path);
};

} // namespace caelestia