summaryrefslogtreecommitdiff
path: root/liveview/liveview.h
blob: 13a8f0a377f83aebbabe46de263a917a027507c7 (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
#ifndef LIVEVIEW_H
#define LIVEVIEW_H

#include <sowatch.h>
#include <sowatchbt.h>

namespace sowatch
{

class LiveViewPaintEngine;

class LiveView : public BluetoothWatch
{
	Q_OBJECT

public:
	explicit LiveView(ConfigKey *settings, QObject *parent = 0);
	~LiveView();

	QPaintEngine* paintEngine() const;
	int metric(PaintDeviceMetric metric) const;

	QString model() const;
	QStringList buttons() const;

	bool busy() const;

	void setDateTime(const QDateTime& dateTime);
	void queryDateTime();
	QDateTime dateTime() const;

	void queryBatteryLevel();
	int batteryLevel() const;

	void queryCharging();
	bool charging() const;

	void displayIdleScreen();
	void displayNotification(Notification *notification);
	void displayApplication();

	void vibrate(int msecs);

	void setWatchletsModel(WatchletsModel *model);
	void setNotificationsModel(NotificationsModel *model);

	static const int MaxBitmapSize;

	// Only for application mode
	QImage* image();
	/** Render a image in a certain position. */
	void renderImage(int x, int y, const QImage& image);
	/** Clear the current display to black. */
	void clear();

protected:
	static const int DelayBetweenMessages = 200;

	enum MessageType {
		NoMessage = 0,
		GetDisplayProperties = 1,
		GetDisplayPropertiesResponse = 2,
		DeviceStatusChange = 7,
		DeviceStatusChangeResponse = 8,
		DisplayBitmap = 19,
		DisplayBitmapResponse = 20,
		DisplayClear = 21,
		DisplayClearResponse = 22,
		SetMenuSize = 23,
		SetMenuSizeResponse = 24,
		MenuItemRequest = 25,
		MenuItemResponse = 26,
		NotificationRequest = 27,
		NotificationResponse = 28,
		Navigation = 29,
		NavigationResponse = 30,
		MenuItemsRequest = 35,
		DateTimeRequest = 38,
		DateTimeResponse = 39,
		EnableLed = 40,
		EnableLedResponse = 41,
		Vibrate = 42,
		VibrateResponse = 43,
		Ack = 44,
		SetScreenMode = 64,
		SetScreenModeResponse = 65,
		GetSoftwareVersion = 68,
		GetSoftwareVersionResponse = 69
	};

	enum ResponseType {
		ResponseOk = 0,
		ResponseError = 1,
		ResponseOutOfMemory = 2,
		ResponseExit = 3,
		ResponseCancel = 4
	};

	enum DeviceStatus {
		DeviceOff = 0,
		DeviceOn = 1,
		DeviceMenu = 2
	};

	enum MenuItemType {
		MenuNotificationList = 0,
		MenuOther = 1
	};

	enum Mode {
		RootMenuMode = 0,
		ApplicationMode,
		NotificationMode,
		NotificationListMode
	};

	enum NavigationEvent {
		UpPress = 1,
		DownPress = 4,
		LeftPress = 7,
		RightPress = 10,
		SelectPress = 13,
		SelectLongPress = 14,
		SelectDoublePress = 15,
		SelectMenu = 32
	};

	enum NotificationAction {
		NotificationShowCurrent = 0,
		NotificationShowFirst = 1,
		NotificationShowLast = 2,
		NotificationShowNext = 3,
		NotificationShowPrev = 4
	};

	enum ScreenBrigthness {
		ScreenOff = 49,
		ScreenDim = 50,
		ScreenMax = 51
	};

	struct Message {
		MessageType type;
		QByteArray data;
		Message(MessageType ntype = NoMessage, QByteArray ndata = QByteArray()) :
			type(ntype), data(ndata)
		{ }
	};

	struct RootMenuNotificationItem {
		QByteArray icon;
		QString title;
		QList<Notification::Type> notificationTypes;
	};

	struct RootMenuItem {
		MenuItemType type;
		QByteArray icon;
		QString title;
		int unread;
		QString watchletId;
		const RootMenuNotificationItem *notificationItem;
	};

	static QMap<MessageType, MessageType> _ackMap;
	static void initializeAckMap();
	static MessageType ackForMessage(MessageType type);

	static QList<RootMenuNotificationItem> _rootNotificationItems;
	static void initializeRootNotificationItems();

	void setupBluetoothWatch();
	void desetupBluetoothWatch();


	void recreateNotificationsMenu();
	/** Recreate the device menu (after watchlets change) */
	void recreateWatchletsMenu();
	/** Update the device menu (after a power on, etc.) */
	void refreshMenu();

	static QByteArray encodeImage(const QImage& image);
	static QByteArray encodeImage(const QUrl& url);

protected:
	void send(const Message& msg);
	void sendResponse(MessageType type, ResponseType response);

	void updateDisplayProperties();
	void updateSoftwareVersion();
	void displayBitmap(unsigned char x, unsigned char y, const QByteArray& image);
	void displayClear();
	void setMenuSize(unsigned char size);
	void sendMenuItem(unsigned char id, MenuItemType type, unsigned short unread, const QString& text, const QByteArray& image);
	void sendNotification(unsigned short id, unsigned short unread, unsigned short count, const QString& date, const QString& header, const QString& body, const QByteArray& image);
	void enableLed(const QColor& color, unsigned short delay, unsigned short time);
	void vibrate(unsigned short delay, unsigned short time);
	void setScreenMode(ScreenBrigthness mode);

	void handleMessage(const Message& msg);
	void handleDeviceStatusChange(const Message& msg);
	void handleMenuItemRequest(const Message& msg);
	void handleNotificationRequest(const Message& msg);
	void handleNavigation(const Message& msg);
	void handleMenuItemsRequest(const Message& msg);
	void handleDateTimeRequest(const Message& msg);
	void handleDisplayProperties(const Message& msg);
	void handleSoftwareVersion(const Message& msg);

private:
	void sendMessageFromQueue();

private slots:
	void handleDataReceived();
	void handleWatchletsChanged();
	void handleNotificationsChanged();

private:
	ConfigKey *_settings;
	WatchletsModel *_watchlets;
	NotificationsModel *_notifications;

	// Configurable settings
	bool _24hMode : 1;

	// Watch properties
	int _screenWidth;
	int _screenHeight;
	QStringList _buttons;

	// Runtime variables
	Mode _mode;
	int _curNotificationIndex;

	// Required by QPaintDevice
	mutable LiveViewPaintEngine* _paintEngine;
	QImage _image;

	QList<RootMenuItem> _rootMenu;
	/** Keeps the index of the first watchlet. */
	int _rootMenuFirstWatchlet;

	/** Outgoing message queue. */
	QQueue<Message> _sendingMsgs;
	MessageType _waitingForAck;
	/** Incomplete message that is being received. */
	Message _receivingMsg;
};

}

#endif // LIVEVIEW_H