summaryrefslogtreecommitdiff
path: root/qorgwatchlet/metawatch-digital.qml
blob: e6e4758e2972c91f968d2d970dac5b59293a05e3 (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
import QtQuick 1.0
import QtMobility.organizer 1.1
import com.javispedro.sowatch.metawatch 1.0

MWPage {
	MWTitle {
		id: title
		anchors.top: parent.top
		anchors.left: parent.left
		anchors.right: parent.right
		text: qsTr("Calendar")
		icon.source: "icon.png"
	}

	MWListView {
		id: list
		anchors.top: title.bottom
		anchors.left: parent.left
		anchors.right: parent.right
		anchors.bottom: parent.bottom
		clip: true
		model: OrganizerModel {
			id: model
			manager: "qtorganizer:mkcal"
		}
		delegate: Rectangle {
			id: itemDelegate
			property bool selected: ListView.isCurrentItem
			width: parent.width
			height: childrenRect.height
			color: ListView.isCurrentItem ? "black" : "white"
			Column {
				width: parent.width
				visible: typeof display !== "undefined"
				MWLabel {
					width: parent.width
					text: typeof item !== "undefined" ? _formatEventTime(item) : ""
					wrapMode: Text.WrapAtWordBoundaryOrAnywhere
					color: itemDelegate.selected ? "white" : "black"
					font.family: "MetaWatch Large caps 8pt"
					font.pixelSize: 8
				}
				MWLabel {
					width: parent.width
					text: typeof display !== "undefined" ? display : ""
					wrapMode: Text.WrapAtWordBoundaryOrAnywhere
					color: itemDelegate.selected ? "white" : "black"
					font.pixelSize: 16
				}
			}
		}
	}

	function update() {
		var now = new Date();
		var end = new Date(now.getFullYear(), now.getMonth() , now.getDate() + 7);

		model.startPeriod = now;
		model.endPeriod = end;
		model.update();
	}

	function _isSameDay(date1, date2) {
		return date1.getYear() === date2.getYear() &&
				date1.getMonth() === date2.getMonth() &&
				date1.getDate() === date2.getDate();
	}

	function _daysTo(date1, date2) {
		var ms_per_day = 24 * 3600 * 1000;
		var ts1 = date1.getTime();
		var ts2 = date2.getTime();
		var diff = ts2 - ts1;
		return Math.round(diff / ms_per_day);
	}

	function _formatEventTime(item) {
		var now = new Date();
		var itemStart = item.itemStartTime;
		var itemEnd = item.itemEndTime;
		if (_isSameDay(now, itemStart) && _isSameDay(now, itemEnd)) {
			return Qt.formatTime(itemStart) + " - " + Qt.formatTime(itemEnd);
		} else if (_isSameDay(itemStart, itemEnd)) {
			if (_daysTo(now, itemStart) < 7) {
				return Qt.formatDate(itemStart, "dddd") + "\n" +
						Qt.formatTime(itemStart) + " - " + Qt.formatTime(itemEnd);
			}
			return Qt.formatDate(itemStart) + "\n" +
					Qt.formatTime(itemStart) + " - " + Qt.formatTime(itemEnd);
		} else {
			return Qt.formatDateTime(itemStart) + " -\n" +
					Qt.formatDateTime(itemEnd);
		}
	}

	Connections {
		target: watch
		onButtonPressed: {
			switch (button) {
			case 1:
				list.scrollUp();
				break;
			case 2:
				list.scrollDown();
				break;
			}
		}
		onActiveChanged: {
			if (watch.active) {
				update();
				list.scrollTop();
			}
		}
	}
}