File indexing completed on 2024-04-28 04:05:02

0001 /*
0002     SPDX-FileCopyrightText: 2010 Stefan Majewsky <majewsky@gmx.net>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-only
0005 */
0006 
0007 #ifndef LIBKDEGAMES_COLORPROXY_P_H
0008 #define LIBKDEGAMES_COLORPROXY_P_H
0009 
0010 // Qt
0011 #include <QHash>
0012 #include <QPaintDevice>
0013 #include <QPaintEngine>
0014 
0015 class QPaintEngineColorProxy;
0016 
0017 #ifndef KDEGAMES_QCOLOR_QHASH
0018 #define KDEGAMES_QCOLOR_QHASH
0019 inline uint qHash(const QColor &color)
0020 {
0021     return color.rgba();
0022 }
0023 #endif // KDEGAMES_QCOLOR_QHASH
0024 
0025 /**
0026  * This QPaintDevice forwards all painting operations performed on it to the
0027  * contained QPaintDevice. The only modification is that certain colors are
0028  * replaced by others in all painting operations (except for drawImage and
0029  * drawPixmap).
0030  *
0031  * @note Using this class results in a non-negligible performance penalty
0032  * (~10-20% longer runtime), so use it only if the set of color replacements is
0033  * non-empty.
0034  */
0035 class QPaintDeviceColorProxy : public QPaintDevice
0036 {
0037 public:
0038     /// @warning Replacement loops (e.g. color1 -> color2 -> color3 -> color1) lead to infinite loops.
0039     /// @warning You should not interact with the @a proxiedDevice during the lifetime of this instance.
0040     QPaintDeviceColorProxy(QPaintDevice *proxiedDevice, const QHash<QColor, QColor> &replacements);
0041     ~QPaintDeviceColorProxy() override;
0042 
0043     QPaintDevice *proxiedDevice() const;
0044     QPaintEngine *paintEngine() const override;
0045 
0046     QBrush map(const QBrush &brush) const;
0047     inline QColor map(const QColor &color) const;
0048     QPen map(const QPen &pen) const;
0049 
0050 protected:
0051     int metric(PaintDeviceMetric metric) const override;
0052 
0053 private:
0054     QPaintDevice *m_proxiedDevice;
0055     QPaintEngine *m_engine;
0056     QHash<QColor, QColor> m_replacements;
0057 };
0058 
0059 class QPaintEngineColorProxy : public QPaintEngine
0060 {
0061 public:
0062     QPaintEngineColorProxy();
0063     ~QPaintEngineColorProxy() override;
0064 
0065     bool begin(QPaintDevice *device) override;
0066     bool end() override;
0067     void drawEllipse(const QRectF &rect) override;
0068     void drawEllipse(const QRect &rect) override;
0069     void drawImage(const QRectF &rectangle, const QImage &image, const QRectF &sr, Qt::ImageConversionFlags flags = Qt::AutoColor) override;
0070     void drawLines(const QLineF *lines, int lineCount) override;
0071     void drawLines(const QLine *lines, int lineCount) override;
0072     void drawPath(const QPainterPath &path) override;
0073     void drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr) override;
0074     void drawPoints(const QPointF *points, int pointCount) override;
0075     void drawPoints(const QPoint *points, int pointCount) override;
0076     void drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode) override;
0077     void drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode) override;
0078     void drawRects(const QRectF *rects, int rectCount) override;
0079     void drawRects(const QRect *rects, int rectCount) override;
0080     void drawTextItem(const QPointF &p, const QTextItem &textItem) override;
0081     void drawTiledPixmap(const QRectF &rect, const QPixmap &pixmap, const QPointF &p) override;
0082     Type type() const override;
0083     void updateState(const QPaintEngineState &state) override;
0084 
0085 private:
0086     QPaintDeviceColorProxy *m_proxy;
0087     QPainter *m_painter;
0088 };
0089 
0090 #endif // LIBKDEGAMES_COLORPROXY_P_H