1 /*
2     SPDX-FileCopyrightText: 2010 Daniel Laidig <laidig@kde.org>
3     SPDX-License-Identifier: GPL-2.0-or-later
4 */
5 
6 #ifndef PRACTICE_THEMEDBACKGROUNDRENDERER_H
7 #define PRACTICE_THEMEDBACKGROUNDRENDERER_H
8 
9 #include <QObject>
10 
11 #include "imagecache.h"
12 #include <QFuture>
13 #include <QFutureWatcher>
14 #include <QSvgRenderer>
15 #include <QTimer>
16 
17 class QMargins;
18 class KGameTheme;
19 
20 namespace Practice
21 {
22 class ThemedBackgroundRenderer : public QObject
23 {
24     Q_OBJECT
25 
26 public:
27     enum ScaleBase {
28         NoScale,
29         Horizontal,
30         Vertical,
31         Rect,
32     };
33 
34     enum Edge {
35         Top,
36         Bottom,
37         Left,
38         Right,
39         Center,
40     };
41 
42     enum Align {
43         Corner,
44         LeftTop, // left or top (depending on orientation of the edge)
45         Centered,
46         Repeated,
47         RightBottom // right or bottom (depending on orientation of the edge)
48     };
49 
50     ThemedBackgroundRenderer(QObject *parent, const QString &cacheFilename);
51     ~ThemedBackgroundRenderer() override;
52 
53     void setTheme(const QString &theme);
54 
55     QSizeF getSizeForId(const QString &id);
56     QRectF getRectForId(const QString &id);
57     QPixmap getPixmapForId(const QString &id, QSize size = QSize());
58     QMargins contentMargins();
59 
60     QPixmap getScaledBackground();
61 
62     QColor fontColor(const QString &context, const QColor &fallback);
63 
64 public Q_SLOTS:
65     void clearRects();
66     void addRect(const QString &name, const QRect &rect);
67     void updateBackground();
68 
69     void updateBackgroundTimeout();
70 
71     void renderingFinished();
72 
73 signals:
74     void backgroundChanged(QPixmap pixmap);
75 
76 private:
77     QImage renderBackground(bool fastScale);
78     void renderRect(const QString &name, const QRect &rect, QPainter *p, bool fastScale);
79     void renderItem(const QString &idBase,
80                     const QString &idSuffix,
81                     const QRect &rect,
82                     QPainter *p,
83                     bool fastScale,
84                     ScaleBase scaleBase,
85                     Qt::AspectRatioMode aspectRatio,
86                     Edge edge,
87                     Align align,
88                     bool inside);
89     QRect scaleRect(QRectF itemRect, const QRect &baseRect, ScaleBase scaleBase, Qt::AspectRatioMode aspectRatio);
90     QRect alignRect(QRect itemRect, const QRect &baseRect, Edge edge, Align align, bool inside);
91 
92     ImageCache m_cache;
93     QFuture<QImage> m_future;
94     QFutureWatcher<QImage> m_watcher;
95     KGameTheme *m_theme{nullptr};
96     QHash<QString, QString> m_rectMappings;
97     QSvgRenderer m_renderer;
98     QList<QPair<QString, QRect>> m_rects;
99     QList<QPair<QString, QRect>> m_lastScaledRenderRects; // the rects used for the last scaled render
100     QList<QPair<QString, QRect>> m_lastFullRenderRects; // the rects used for the last full render
101     bool m_haveCache;
102     bool m_queuedRequest;
103     bool m_isFastScaledRender;
104     QTimer m_timer;
105 };
106 
107 }
108 
109 #endif // PRACTICE_THEMEDBACKGROUNDRENDERER_H
110