summaryrefslogtreecommitdiff
path: root/libsowatch/watchpaintengine.cpp
blob: 6c509cbf91d4189f18bff0977f4cbe05aed91cbf (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <QtCore/QDebug>
#include <math.h>

#include "watch.h"
#include "watchpaintengine.h"

using namespace sowatch;

WatchPaintEngine::WatchPaintEngine(Watch* watch)
	: QPaintEngine(QPaintEngine::AllFeatures),
	  _watch(watch), _painter(),
	  _hasPen(false), _hasBrush(false), _clipEnabled(false)
{

}

void WatchPaintEngine::damageMappedRect(const QRect &r)
{
	if (_clipEnabled) {
		_damaged += _clipRegion.intersected(r);
	} else {
		_damaged += r;
	}
}

void WatchPaintEngine::damageRect(const QRect &r)
{
	damageMappedRect(_transform.mapRect(r));
}

void WatchPaintEngine::damageRect(const QRectF &r)
{
	damageMappedRect(_transform.mapRect(r).toAlignedRect());
}

void WatchPaintEngine::damagePenStroke(const QLineF &line)
{
	if (!_hasPen) return;

	const qreal a = line.angle();
	const qreal sn = sinf(a);
	const qreal cs = cosf(a);
	const qreal w = _penWidth == 0.0 ? 1.0 : _penWidth;
	const qreal x1 = line.x1();
	const qreal x2 = line.x2();
	const qreal y1 = line.y1();
	const qreal y2 = line.y2();

	QPointF p1(x1-(w*sn/2.0f), y1+(w*cs/2.0f));
	QPointF p2(x2+(w*sn/2.0f), y2-(w*cs/2.0f));
	QRectF r = QRectF(p1, p2).normalized();
	damageRect(r);
}

void WatchPaintEngine::updateClipRegion(const QRegion& region, Qt::ClipOperation op)
{
	switch(op) {
		case Qt::NoClip:
			_clipEnabled = false;
			_clipRegion = QRegion(0, 0, _watch->width(), _watch->height());
			break;
		case Qt::ReplaceClip:
			_clipEnabled = true;
			_clipRegion = region;
			break;
		case Qt::IntersectClip:
			_clipEnabled = true;
			_clipRegion &= region;
			break;
		case Qt::UniteClip:
			_clipEnabled = true;
			_clipRegion |= region;
			break;
	}
}

void WatchPaintEngine::drawEllipse(const QRectF &r)
{
	damageRect(r);
	_painter.drawEllipse(r);
}

void WatchPaintEngine::drawEllipse(const QRect &r)
{
	damageRect(r);
	_painter.drawEllipse(r);
}

void WatchPaintEngine::drawImage(const QRectF &r, const QImage &pm, const QRectF &sr, Qt::ImageConversionFlags flags)
{
	damageRect(r);
	_painter.drawImage(r, pm, sr, flags);
}

void WatchPaintEngine::drawLines(const QLineF *lines, int lineCount)
{
	int i;
	for (i = 0; i < lineCount; i++) {
		const QLineF& line = lines[i];
		damagePenStroke(line);
	}
	_painter.drawLines(lines, lineCount);
}

void WatchPaintEngine::drawLines(const QLine *lines, int lineCount)
{
	int i;
	for (i = 0; i < lineCount; i++) {
		const QLine& line = lines[i];
		damagePenStroke(line);
	}
	_painter.drawLines(lines, lineCount);
}

void WatchPaintEngine::drawPath(const QPainterPath &path)
{
	damageRect(path.boundingRect());
	_painter.drawPath(path);
}

void WatchPaintEngine::drawPixmap(const QRectF &r, const QPixmap &pm, const QRectF &sr)
{
	damageRect(r);
	_painter.drawPixmap(r, pm, sr);
}

void WatchPaintEngine::drawPoints(const QPointF *points, int pointCount)
{
	int i;
	for (i = 0; i < pointCount; i++) {
		const QPointF& p = points[i];
		damageRect(QRectF(p.x() - _penWidth/2.0f, p.y() - _penWidth/2.0f,
						  _penWidth, _penWidth));
	}
	_painter.drawPoints(points, pointCount);
}

void WatchPaintEngine::drawPoints(const QPoint *points, int pointCount)
{
	int i;
	for (i = 0; i < pointCount; i++) {
		const QPoint& p = points[i];
		damageRect(QRect(p.x() - _penWidth/2, p.y() - _penWidth/2,
						 _penWidth, _penWidth));
	}
	_painter.drawPoints(points, pointCount);
}

void WatchPaintEngine::drawPolygon(const QPointF *points, int pointCount, PolygonDrawMode mode)
{
	QPolygonF p(pointCount);
	int i;
	for (i = 0; i < pointCount; i++) {
		p[i] = points[i];
	}
	damageRect(p.boundingRect());
	_painter.drawPolygon(points, pointCount,
						 mode == WindingMode ? Qt::WindingFill : Qt::OddEvenFill);
}

void WatchPaintEngine::drawPolygon(const QPoint *points, int pointCount, PolygonDrawMode mode)
{
	QPolygon p(pointCount);
	int i;
	for (i = 0; i < pointCount; i++) {
		p[i] = points[i];
	}
	damageRect(p.boundingRect());
	_painter.drawPolygon(points, pointCount,
						 mode == WindingMode ? Qt::WindingFill : Qt::OddEvenFill);
}

void WatchPaintEngine::drawRects(const QRectF *rects, int rectCount)
{
	int i;
	for (i = 0; i < rectCount; i++) {
		const QRectF& r = rects[i];
		if (_hasBrush) {
			damageRect(r);
		}
		if (_hasPen) {
			damagePenStroke(QLineF(r.left(), r.top(), r.right(), r.top()));
			damagePenStroke(QLineF(r.right(), r.top(), r.right(), r.bottom()));
			damagePenStroke(QLineF(r.left(), r.bottom(), r.right(), r.bottom()));
			damagePenStroke(QLineF(r.left(), r.top(), r.left(), r.bottom()));
		}
	}
	_painter.drawRects(rects, rectCount);
}

void WatchPaintEngine::drawRects(const QRect *rects, int rectCount)
{
	int i;
	for (i = 0; i < rectCount; i++) {
		const QRect& r = rects[i];
		if (_hasBrush) {
			damageRect(r);
		}
		if (_hasPen) {
			damagePenStroke(QLine(r.left(), r.top(), r.right(), r.top()));
			damagePenStroke(QLine(r.right(), r.top(), r.right(), r.bottom()));
			damagePenStroke(QLine(r.left(), r.bottom(), r.right(), r.bottom()));
			damagePenStroke(QLine(r.left(), r.top(), r.left(), r.bottom()));
		}
	}

	_painter.drawRects(rects, rectCount);
}

void WatchPaintEngine::drawTextItem(const QPointF &p, const QTextItem &textItem)
{
	const qreal ascent = textItem.ascent();
	const qreal descent = textItem.descent();
	const qreal w = textItem.width();
	damageRect(QRect(p.x(), p.y() - ascent, w, ascent + descent));
	_painter.drawTextItem(p, textItem);
}

void WatchPaintEngine::drawTiledPixmap(const QRectF &r, const QPixmap &pixmap, const QPointF &s)
{
	damageRect(r);
	_painter.drawTiledPixmap(r, pixmap, s);
}

QPaintEngine::Type WatchPaintEngine::type() const
{
	return QPaintEngine::Raster;
}

void WatchPaintEngine::updateState(const QPaintEngineState &state)
{
	const QPaintEngine::DirtyFlags flags = state.state();
	if (flags & QPaintEngine::DirtyBackground)
	{
		_painter.setBackground(state.backgroundBrush());
	}
	if (flags & QPaintEngine::DirtyBackgroundMode)
	{
		_painter.setBackgroundMode(state.backgroundMode());
	}
	if (flags & QPaintEngine::DirtyBrush)
	{
		QBrush brush = state.brush();
		_hasBrush = brush.style() != Qt::NoBrush;
		_painter.setBrush(brush);
	}
	if (flags & QPaintEngine::DirtyBrushOrigin)
	{
		_painter.setBrushOrigin(state.brushOrigin());
	}
	if (flags & QPaintEngine::DirtyClipEnabled)
	{
		_clipEnabled = state.isClipEnabled();
		_painter.setClipping(_clipEnabled);
	}
	if (flags & QPaintEngine::DirtyClipPath)
	{
		QRegion region = state.clipPath().boundingRect().toAlignedRect();
		updateClipRegion(region, state.clipOperation());
		_painter.setClipPath(state.clipPath(), state.clipOperation());
	}
	if (flags & QPaintEngine::DirtyClipRegion)
	{
		updateClipRegion(state.clipRegion(), state.clipOperation());
		_painter.setClipRegion(state.clipRegion(), state.clipOperation());
	}
	if (flags & QPaintEngine::DirtyCompositionMode)
	{
		_painter.setCompositionMode(state.compositionMode());
	}
	if (flags & QPaintEngine::DirtyFont)
	{
		_painter.setFont(state.font());
	}
	if (flags & QPaintEngine::DirtyHints)
	{
		_painter.setRenderHints(state.renderHints());
	}
	if (flags & QPaintEngine::DirtyPen)
	{
		QPen pen = state.pen();
		_hasPen = pen.style() != Qt::NoPen;
		_penWidth = pen.widthF();
		_painter.setPen(pen);
	}
	if (flags & QPaintEngine::DirtyTransform)
	{
		_transform = state.transform();
		_painter.setTransform(_transform);
	}
}