1/***************************************************************************
2 * Copyright 2010 Stefan Majewsky <majewsky@gmx.net> *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU Library General Public License *
6 * version 2 as published by the Free Software Foundation *
7 * *
8 * This program is distributed in the hope that it will be useful, *
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
11 * GNU Library General Public License for more details. *
12 * *
13 * You should have received a copy of the GNU Library General Public *
14 * License along with this program; if not, write to the *
15 * Free Software Foundation, Inc., *
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
17 ***************************************************************************/
18
19#ifndef LIBKDEGAMES_COLORPROXY_P_H
20#define LIBKDEGAMES_COLORPROXY_P_H
21
22#include <QtCore/QHash>
23#include <QtGui/QPaintDevice>
24#include <QtGui/QPaintEngine>
25
26class QPaintEngineColorProxy;
27
28#ifndef KDEGAMES_QCOLOR_QHASH
29# define KDEGAMES_QCOLOR_QHASH
30 inline uint qHash(const QColor& color)
31 {
32 return color.rgba();
33 }
34#endif // KDEGAMES_QCOLOR_QHASH
35
36/**
37 * This QPaintDevice forwards all painting operations performed on it to the
38 * contained QPaintDevice. The only modification is that certain colors are
39 * replaced by others in all painting operations (except for drawImage and
40 * drawPixmap).
41 *
42 * @note Using this class results in a non-negligible performance penalty
43 * (~10-20% longer runtime), so use it only if the set of color replacements is
44 * non-empty.
45 */
46class QPaintDeviceColorProxy : public QPaintDevice
47{
48 public:
49 ///@warning Replacement loops (e.g. color1 -> color2 -> color3 -> color1) lead to infinite loops.
50 ///@warning You should not interact with the @a proxiedDevice during the lifetime of this instance.
51 QPaintDeviceColorProxy(QPaintDevice* proxiedDevice, const QHash<QColor, QColor>& replacements);
52 ~QPaintDeviceColorProxy();
53
54 QPaintDevice* proxiedDevice() const;
55 virtual QPaintEngine* paintEngine() const;
56
57 QBrush map(const QBrush& brush) const;
58 inline QColor map(const QColor& color) const;
59 QPen map(const QPen& pen) const;
60 protected:
61 virtual int metric(PaintDeviceMetric metric) const;
62 private:
63 QPaintDevice* m_proxiedDevice;
64 QPaintEngine* m_engine;
65 QHash<QColor, QColor> m_replacements;
66};
67
68class QPaintEngineColorProxy : public QPaintEngine
69{
70 public:
71 QPaintEngineColorProxy();
72 ~QPaintEngineColorProxy();
73
74 virtual bool begin(QPaintDevice* device);
75 virtual bool end();
76 virtual void drawEllipse(const QRectF& rect);
77 virtual void drawEllipse(const QRect& rect);
78 virtual void drawImage(const QRectF& rectangle, const QImage& image, const QRectF& sr, Qt::ImageConversionFlags flags = Qt::AutoColor);
79 virtual void drawLines(const QLineF* lines, int lineCount);
80 virtual void drawLines(const QLine* lines, int lineCount);
81 virtual void drawPath(const QPainterPath& path);
82 virtual void drawPixmap(const QRectF& r, const QPixmap& pm, const QRectF& sr);
83 virtual void drawPoints(const QPointF* points, int pointCount);
84 virtual void drawPoints(const QPoint* points, int pointCount);
85 virtual void drawPolygon(const QPointF* points, int pointCount, PolygonDrawMode mode);
86 virtual void drawPolygon(const QPoint* points, int pointCount, PolygonDrawMode mode);
87 virtual void drawRects(const QRectF* rects, int rectCount);
88 virtual void drawRects(const QRect* rects, int rectCount);
89 virtual void drawTextItem(const QPointF& p, const QTextItem& textItem);
90 virtual void drawTiledPixmap(const QRectF& rect, const QPixmap& pixmap, const QPointF& p);
91 virtual Type type() const;
92 virtual void updateState(const QPaintEngineState& state);
93 private:
94 QPaintDeviceColorProxy* m_proxy;
95 QPainter* m_painter;
96};
97
98#endif // LIBKDEGAMES_COLORPROXY_P_H
99