~mzanetti/+junk/stopwatch

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
import QtQuick 2.4
import Ubuntu.Components 1.3
import QtQuick.Layouts 1.1
import Ubuntu.Components.ListItems 1.3
import Stopwatch 1.0

Page {
    id: root
    title: "Stopwatch"

    property date startTime: new Date()
    property date snapshot: startTime
    property bool running: false

    property int timeDiff: snapshot - startTime
    property int oldDiff: 0

    property int totalTimeDiff: timeDiff + oldDiff

    function start() {
        startTime = new Date();
        snapshot = startTime;
        running = true;
    }

    function stop() {
        oldDiff += timeDiff;
        startTime = new Date();
        snapshot = startTime;
        running = false;
    }

    function update() {
        snapshot = new Date();
    }

    function clear() {
        oldDiff = 0;
        startTime = new Date();
        snapshot = startTime;
        history.clear();
    }

    Timer {
        id: refreshTimer
        interval: 45
        repeat: true
        running: root.running

        onTriggered: {
            root.update();
        }
    }

    ColumnLayout {
        id: mainColumn
        anchors {
            fill: parent
            leftMargin: units.gu(1)
            rightMargin: units.gu(1)
            topMargin: units.gu(1)
        }
        spacing: units.gu(2)

        TimeDisplay {
            Layout.fillWidth: true
            milliseconds: root.totalTimeDiff
        }

        RowLayout {
            Layout.fillWidth: true
            spacing: units.gu(2)

            Button {
                Layout.preferredWidth: (mainColumn.width - parent.spacing) / 2
                Layout.preferredHeight: units.gu(10)
                color: root.running ? UbuntuColors.orange : UbuntuColors.red
                font.pixelSize: units.gu(4)
                text: root.running ? i18n.tr("Stop") : i18n.tr("Clear")
                onClicked: {
                    if (root.running) {
                        root.stop();
                    } else {
                        root.clear();
                    }
                }
            }

            Button {
                Layout.preferredWidth: (mainColumn.width - parent.spacing) / 2
                Layout.preferredHeight: units.gu(10)
                color: root.running ? UbuntuColors.blue : UbuntuColors.green
                font.pixelSize: units.gu(4)

                text: root.running ? i18n.tr("Lap") : i18n.tr("Start")
                enabled: !root.running || historyView.count < 999

                onClicked: {
                    if (root.running) {
                        root.update();
                        history.addLap(root.totalTimeDiff);
                    } else {
                        root.start();
                    }
                }
            }
        }

        ListView {
            id: historyView
            Layout.fillWidth: true
            Layout.fillHeight: true
            model: history
            clip: true
            highlightFollowsCurrentItem: true

            onCountChanged: {
                currentIndex = count - 1
            }

            delegate: Empty {
                Row {
                    anchors.fill: parent
                    anchors.topMargin: units.gu(1)
                    anchors.bottomMargin: units.gu(1)
                    spacing: units.gu(2)

                    Row {
                        spacing: units.gu(.5)
                        height: parent.height
                        width: height
                        property bool hundrets: index >= 99
                        Digit {
                            height: parent.height / 2
                            visible: parent.hundrets
                            anchors.verticalCenter: parent.verticalCenter
                            number: Math.floor((index + 1) / 100)
                        }
                        Digit {
                            height: parent.height / (parent.hundrets ? 2 : 1)
                            anchors.verticalCenter: parent.verticalCenter
                            number: Math.floor((index + 1) / 10) % 10
                        }
                        Digit {
                            height: parent.height / (parent.hundrets ? 2 : 1)
                            anchors.verticalCenter: parent.verticalCenter
                            number: (index + 1) % 10
                        }
                    }
                    GridLayout {
                        width: parent.width - parent.spacing - x
                        columns: 3
                        anchors.verticalCenter: parent.verticalCenter

                        Label {
                            Layout.preferredWidth: parent.width / 3
                            text: i18n.tr("Total")
                            color: UbuntuColors.lightGrey
                        }
                        Label {
                            id: lapLabel
                            Layout.preferredWidth: parent.width / 3
                            color: UbuntuColors.lightGrey
                            text: i18n.tr("Lap")
                        }
                        Item {
                            Layout.preferredWidth: parent.width / 3
                            Image {
                                height: lapLabel.height / 2
                                anchors.right: parent.right
                                width: height * 2.5
                                Layout.alignment: Qt.AlignRight
                                source: Qt.resolvedUrl("delta.svg")
                                visible: index > 0
                            }
                        }

                        TimeLabel {
                            Layout.fillWidth: true
                            millis: model.time
                        }
                        TimeLabel {
                            Layout.fillWidth: true
                            millis: model.diffToPrevious
                        }
                        TimeLabel {
                            Layout.fillWidth: true
                            color: model.delta == 0 ? "white" : model.delta < 0 ? UbuntuColors.green : UbuntuColors.red
                            millis: model.delta
                            showSign: true
                            rightAligned: true
                            visible: index > 0
                        }
                    }
                }
            }
        }
    }
}