summaryrefslogtreecommitdiff
path: root/libsowatch/graphicswatchlet.cpp
blob: 08441b0d85ca07347082f1708015d3c7f224cdb5 (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
#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), _frameTimer(),
      _fullUpdateMode(false), _damaged()
{
	_frameTimer.setSingleShot(true);
	connect(&_frameTimer, SIGNAL(timeout()), SLOT(frameTimeout()));
}

GraphicsWatchlet::~GraphicsWatchlet()
{
}

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

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

bool GraphicsWatchlet::fullUpdateMode() const
{
	return _fullUpdateMode;
}

void GraphicsWatchlet::setFullUpdateMode(bool fullUpdateMode)
{
	_fullUpdateMode = fullUpdateMode;
}

QRectF GraphicsWatchlet::sceneRect() const
{
	if (_scene) {
		return _scene->sceneRect();
	} else {
		return QRectF();
	}
}

QRect GraphicsWatchlet::viewportRect() const
{
	if (_active) {
		const Watch *watch = this->watch();
		return QRect(0, 0, watch->width(), watch->height());
	} else {
		return QRect();
	}
}

void GraphicsWatchlet::sceneChanged(const QList<QRectF> &rects)
{
	// Only consider scene updates if the watchlet is active
	if (_active) {
		QRect viewport = viewportRect();
		if (_fullUpdateMode) {
			_damaged += viewport;
		} else {
			foreach(const QRectF& frect, rects) {
				QRect rect = frect.toAlignedRect() & viewport;
				_damaged += rect;
			}
		}

		// Start frame timer if we got new data
		if (!_damaged.isEmpty() && !_frameTimer.isActive()) {
			_frameTimer.start(frameDelay);
		}
	}
}

void GraphicsWatchlet::frameTimeout()
{
	// Do not draw if watchlet is not active
	if (!_active) return;

	if (watch()->busy()) {
		// Watch is busy, delay this frame.
		_frameTimer.start(busyFrameDelay);
		return;
	}

	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
	// So assume the entire viewport is damaged
	QRect viewport = viewportRect();
	_damaged += viewport;
	// This will emit sceneChanged and start the frame timer.
	_scene->update(viewport);
}

void GraphicsWatchlet::deactivate()
{
	// Stop updates
	_frameTimer.stop();
	_damaged = QRegion();

	Watchlet::deactivate();
}