Debian Codegraph @

qml/components/StopWatch.qml

raw
1 /*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.12
18 import QtQuick.Window 2.2
19 import Lomiri.Components 1.3
20
21 Item {
22 property int time: 0
23 property alias label: countLabel.text
24
25 width: content.childrenRect.width + content.anchors.leftMargin + content.anchors.rightMargin
26 height: content.childrenRect.height + units.gu(1.5)
27
28 rotation: Screen.angleBetween(Screen.primaryOrientation, Screen.orientation)
29 Behavior on rotation {
30 RotationAnimator {
31 duration: LomiriAnimation.BriskDuration
32 easing: LomiriAnimation.StandardEasing
33 direction: RotationAnimator.Shortest
34 }
35 }
36
37 BorderImage {
38 id: background
39
40 anchors.fill: parent
41 source: "../../assets/lomiri_shape.sci"
42 opacity: 0.3
43 asynchronous: true
44 cache: false
45 }
46
47 Row {
48 id: content
49
50 anchors {
51 left: parent.left
52 leftMargin: units.gu(1)
53 right: parent.right
54 rightMargin: units.gu(2)
55 verticalCenter: parent.verticalCenter
56 }
57 height: childrenRect.height
58 spacing: units.gu(1.5)
59
60 Rectangle {
61 anchors.verticalCenter: countLabel.verticalCenter
62 radius: units.gu(2)
63 width: radius
64 height: radius
65 color: "#AE1623"
66 }
67
68 Label {
69 id: countLabel
70
71 color: "white"
72 fontSize: "large"
73 style: Text.Raised
74 styleColor: "black"
75 text: intern.formattedTime()
76 }
77 }
78
79 QtObject {
80 id: intern
81
82 function pad(text, length) {
83 while (text.length < length) text = '0' + text;
84 return text;
85 }
86
87 function formattedTime() {
88 var prefix = ""
89 if (time < 0) {
90 prefix = "-";
91 time = -time;
92 }
93
94 var seconds_in_minute = 60;
95 var minutes_in_hour = 60;
96 var hours_in_day = 24;
97 var seconds_in_hour = seconds_in_minute * minutes_in_hour;
98
99 var hours = String(Math.floor(time / seconds_in_hour) % hours_in_day);
100 var minutes = String(Math.floor(time / seconds_in_minute) % minutes_in_hour);
101 var seconds = String(time % seconds_in_minute);
102
103 if (hours != "0") {
104 return "%1%2:%3:%4".arg(prefix).arg(intern.pad(hours, 2)).arg(intern.pad(minutes, 2)).arg(intern.pad(seconds, 2));
105 } else {
106 return "%1%2:%3".arg(prefix).arg(intern.pad(minutes, 2)).arg(intern.pad(seconds, 2));
107 }
108 }
109 }
110 }