summaryrefslogtreecommitdiff
path: root/metawatch/metawatchdigitalsimulator.cpp
blob: dabc0f394d7328ea7afbfc71f3b0e9a08246cfc5 (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
#include <QtCore/QDebug>
#include <QtGui/QPainter>

#include "metawatchdigitalsimulator.h"

#define SIMULATE_DAMAGES 1
#define SIMULATE_FRAMERATE 1

using namespace sowatch;

MetaWatchDigitalSimulator::MetaWatchDigitalSimulator(ConfigKey *config, QObject *parent) :
	MetaWatchDigital(config, parent),
	_form(new MetaWatchDigitalSimulatorForm),
	_nextFrame(QTime::currentTime())
{
	_pixmap[IdleMode] = QPixmap(screenWidth, screenHeight);
	_pixmap[ApplicationMode] = QPixmap(screenWidth, screenHeight);
	_pixmap[NotificationMode] = QPixmap(screenWidth, screenHeight);
	_form->showNormal();
	connect(_form, SIGNAL(buttonPressed(int)), SIGNAL(buttonPressed(int)));
	connect(_form, SIGNAL(buttonReleased(int)), SIGNAL(buttonReleased(int)));
	connect(_form, SIGNAL(destroyed()), SLOT(handleFormDestroyed()));
}

MetaWatchDigitalSimulator::~MetaWatchDigitalSimulator()
{
	_connected = false;
	delete _form;
}

bool MetaWatchDigitalSimulator::busy() const
{
#if SIMULATE_FRAMERATE
	return _nextFrame > QTime::currentTime();
#else
	return false;
#endif
}

void MetaWatchDigitalSimulator::displayIdleScreen()
{
	MetaWatchDigital::displayIdleScreen();
	_form->refreshScreen(_pixmap[_currentMode]);
}

void MetaWatchDigitalSimulator::displayNotification(Notification *notification)
{
	MetaWatchDigital::displayNotification(notification);
	_form->refreshScreen(_pixmap[_currentMode]);
}

void MetaWatchDigitalSimulator::displayApplication()
{
	MetaWatchDigital::displayApplication();
	// No need to refresh.
}

void MetaWatchDigitalSimulator::clear(Mode mode, bool black)
{
	_pixmap[mode].fill(black ? Qt::black : Qt::white);
	if (mode == _currentMode) {
		_form->refreshScreen(_pixmap[mode]);
	}
}

void MetaWatchDigitalSimulator::update(Mode mode, const QList<QRect> &rects)
{
#if SIMULATE_DAMAGES
	const QRect imageRect = _image[mode].rect();
	QPainter p;
	QVector<bool> rows(96, false);

	p.begin(&_pixmap[mode]);
	foreach (const QRect& rect, rects) {
		QRect r = rect.intersect(imageRect);
		for (int i = r.top(); i <= r.bottom(); i++) {
			rows[i] = true;
		}

		p.drawImage(r, _image[mode], r);
	}
	p.end();

	int totalRows = rows.count(true);

	qDebug() << "updated" << totalRows << "lines";
	_nextFrame = QTime::currentTime().addMSecs(((totalRows / 2) + 1) * DelayBetweenMessages);
#else
	Q_UNUSED(rects);
	_pixmap[mode] = QPixmap::fromImage(_image[mode]);
	_nextFrame = QTime::currentTime().addMSecs(DelayBetweenMessages);
#endif
	if (mode == _currentMode) {
		_form->refreshScreen(_pixmap[mode]);
	}
}

void MetaWatchDigitalSimulator::vibrate(bool on)
{
	qDebug() << "vibrate" << on;
}

void MetaWatchDigitalSimulator::connectToWatch()
{
	if (!_connected && _form) {
		qDebug() << "simulator connected";

		_connected = true;
		_currentMode = IdleMode;
		_paintMode = IdleMode;

		handleWatchConnected();

		emit connected();
	}
}

void MetaWatchDigitalSimulator::send(const Message &msg)
{
	// Do not send messages
	Q_UNUSED(msg);
}

void MetaWatchDigitalSimulator::handleFormDestroyed()
{
	if (_connected) {
		_connected = false;
		qDebug() << "simulator disconnected";
		emit disconnected();
		_form = 0;
	}
}