summaryrefslogtreecommitdiff
path: root/libsowatch/graphicswatchlet.cpp
blob: 195d11b87c73b524a01744f3b07ca889de455ab8 (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
#include <QtCore/QDebug>
#include <QtCore/QEvent>
#include <QtGui/QPainter>

#include "watch.h"
#include "graphicswatchlet.h"

using namespace sowatch;

GraphicsWatchlet::GraphicsWatchlet(WatchServer* server, const QString& id) :
	Watchlet(server, id), _scene(0), _damaged()
{
}

QGraphicsScene* GraphicsWatchlet::scene()
{
	return _scene;
}

void GraphicsWatchlet::setScene(QGraphicsScene *scene)
{
	if (_scene) {
		disconnect(this, SLOT(sceneChanged(QList<QRectF>)));
	}
	_scene = scene;
	if (_scene) {
		connect(_scene, SIGNAL(changed(QList<QRectF>)),
				this, SLOT(sceneChanged(QList<QRectF>)));
	}
}

void GraphicsWatchlet::sceneChanged(const QList<QRectF> &region)
{
	foreach(const QRectF& r, region) {
		_damaged += r.toRect();
	}
	if (!_damaged.isEmpty() && _active && !watch()->busy()) {
		const QVector<QRect> rects = _damaged.rects();
		QPainter p(watch());

		foreach(const QRect& r, rects) {
			_scene->render(&p, r, r, Qt::IgnoreAspectRatio);
		}
		_damaged = QRegion();
	}
}

void GraphicsWatchlet::activate()
{
	Watchlet::activate();
	// We have to assume that the watch has completely forgot about everything.
	QRect area(0, 0, watch()->width(), watch()->height());
	_damaged += area;
	_scene->update(area);
}