File indexing completed on 2024-04-14 03:49:12

0001 /*
0002     SPDX-FileCopyrightText: 2010 Daniel Laidig <laidig@kde.org>
0003     SPDX-License-Identifier: GPL-2.0-or-later
0004 */
0005 
0006 #ifndef PRACTICE_THEMEDBACKGROUNDRENDERER_H
0007 #define PRACTICE_THEMEDBACKGROUNDRENDERER_H
0008 
0009 #include <QObject>
0010 
0011 #include "imagecache.h"
0012 #include <QFuture>
0013 #include <QFutureWatcher>
0014 #include <QSvgRenderer>
0015 #include <QTimer>
0016 
0017 class QMargins;
0018 class KGameTheme;
0019 
0020 namespace Practice
0021 {
0022 class ThemedBackgroundRenderer : public QObject
0023 {
0024     Q_OBJECT
0025 
0026 public:
0027     enum ScaleBase {
0028         NoScale,
0029         Horizontal,
0030         Vertical,
0031         Rect,
0032     };
0033 
0034     enum Edge {
0035         Top,
0036         Bottom,
0037         Left,
0038         Right,
0039         Center,
0040     };
0041 
0042     enum Align {
0043         Corner,
0044         LeftTop, // left or top (depending on orientation of the edge)
0045         Centered,
0046         Repeated,
0047         RightBottom // right or bottom (depending on orientation of the edge)
0048     };
0049 
0050     ThemedBackgroundRenderer(QObject *parent, const QString &cacheFilename);
0051     ~ThemedBackgroundRenderer() override;
0052 
0053     void setTheme(const QString &theme);
0054 
0055     QSizeF getSizeForId(const QString &id);
0056     QRectF getRectForId(const QString &id);
0057     QPixmap getPixmapForId(const QString &id, QSize size = QSize());
0058     QMargins contentMargins();
0059 
0060     QPixmap getScaledBackground();
0061 
0062     QColor fontColor(const QString &context, const QColor &fallback);
0063 
0064 public Q_SLOTS:
0065     void clearRects();
0066     void addRect(const QString &name, const QRect &rect);
0067     void updateBackground();
0068 
0069     void updateBackgroundTimeout();
0070 
0071     void renderingFinished();
0072 
0073 Q_SIGNALS:
0074     void backgroundChanged(QPixmap pixmap);
0075 
0076 private:
0077     QImage renderBackground(bool fastScale);
0078     void renderRect(const QString &name, const QRect &rect, QPainter *p, bool fastScale);
0079     void renderItem(const QString &idBase,
0080                     const QString &idSuffix,
0081                     const QRect &rect,
0082                     QPainter *p,
0083                     bool fastScale,
0084                     ScaleBase scaleBase,
0085                     Qt::AspectRatioMode aspectRatio,
0086                     Edge edge,
0087                     Align align,
0088                     bool inside);
0089     QRect scaleRect(QRectF itemRect, const QRect &baseRect, ScaleBase scaleBase, Qt::AspectRatioMode aspectRatio);
0090     QRect alignRect(QRect itemRect, const QRect &baseRect, Edge edge, Align align, bool inside);
0091 
0092     ImageCache m_cache;
0093     QFuture<QImage> m_future;
0094     QFutureWatcher<QImage> m_watcher;
0095     KGameTheme *m_theme{nullptr};
0096     QHash<QString, QString> m_rectMappings;
0097     QSvgRenderer m_renderer;
0098     QList<QPair<QString, QRect>> m_rects;
0099     QList<QPair<QString, QRect>> m_lastScaledRenderRects; // the rects used for the last scaled render
0100     QList<QPair<QString, QRect>> m_lastFullRenderRects; // the rects used for the last full render
0101     bool m_haveCache;
0102     bool m_queuedRequest;
0103     bool m_isFastScaledRender;
0104     QTimer m_timer;
0105 };
0106 
0107 }
0108 
0109 #endif // PRACTICE_THEMEDBACKGROUNDRENDERER_H