summaryrefslogtreecommitdiffstats
path: root/src/settingsui/timedate/AnalogClock.qml
blob: eb468523c25608c12a38d8c82860ae4f488b9630 (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
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing/
**
** This file is part of the Qt Device Utilities module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL3$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see http://www.qt.io/terms-conditions. For further
** information use the contact form at http://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPLv3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or later as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information to
** ensure the GNU General Public License version 2.0 requirements will be
** met: http://www.gnu.org/licenses/gpl-2.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
import QtQuick 2.6
import Qt.labs.controls 1.0
import Qt.labs.controls.material 1.0
import Qt.labs.controls.universal 1.0
import com.theqtcompany.settings.timedate 1.0

Item {
    property var currentTime: TimeManager.time
    property var newTime: new Date
    property bool editMode: false

    function showTime(time) {
        return time.getHours() + ":" + time.getMinutes()
    }

    onEditModeChanged: if (editMode) newTime = new Date

    Rectangle {
        border.color: "#bdbebf"
        border.width: 1
        color: "white"
        anchors.fill: parent

        Connections {
            target: TimeManager
            onTimeChanged : newTime.setSeconds(currentTime.getSeconds())
            onTimeZoneChanged : Date.timeZoneUpdated()
        }
        Label {
            anchors.horizontalCenter: parent.horizontalCenter
            anchors.topMargin: 10
            anchors.top: parent.top
            text: editMode ? newTime.toTimeString() : currentTime.toTimeString()
        }
        Rectangle {
            id: root
            anchors.fill: parent
            anchors.margins: 40
            color: "white"
            border.color: editMode ? "#d6d6d6" : "#5caa15"
            border.width: Math.round(root.width * 0.120)
            radius: parent.width / 2
            property int handOffset: Math.round(root.width * 0.040)
            antialiasing: true

            Rectangle {
                id: minutes
                x: root.height / 2 - width / 2
                y: root.height / 2 - height + root.handOffset
                color: editMode ? "#d6d6d6" : "#5caa15"
                width: Math.round(root.width * 0.080)
                height: Math.round(root.height / 2 * 0.65 + root.handOffset)
                antialiasing: true
                transform: Rotation {
                    id: minuteRotation
                    origin.x: Math.round(minutes.width / 2)
                    origin.y: Math.round(minutes.height - root.handOffset)
                    angle: currentTime.getMinutes() * 6
                    Behavior on angle {
                        SpringAnimation { spring: 2; damping: 0.2; modulus: 360 }
                    }
                }
            }
            Rectangle {
                id: hours
                x: Math.round(root.height / 2 - width / 2)
                y: Math.round(root.height / 2 - height + root.handOffset)
                height: Math.round(root.height / 2 * 0.4 + root.handOffset)
                width: Math.round(root.width * 0.080)
                color: editMode ? "#d6d6d6" : "#80c342"
                antialiasing: true
                transform: Rotation {
                    id: hourRotation
                    origin.x: Math.round(hours.width / 2)
                    origin.y: hours.height - root.handOffset;
                    angle: (currentTime.getHours() * 30) + (currentTime.getMinutes() * 0.5)
                    Behavior on angle {
                        SpringAnimation { spring: 2; damping: 0.2; modulus: 360 }
                    }
                }
            }
            Rectangle {
                id: seconds
                x: root.height / 2 - width / 2
                y: root.height / 2 - height + root.handOffset
                visible: !editMode
                color: "#46a2da"
                width: Math.round(root.width * 0.0128)
                height: Math.round(root.height / 2 * 0.74)
                radius: Math.round(width / 2)
                antialiasing: true
                transform: Rotation {
                    id: secondRotation
                    origin.x: Math.round(seconds.width / 2)
                    origin.y: Math.round(seconds.height - root.handOffset)
                    angle: currentTime.getSeconds() * 6
                    Behavior on angle {
                        SpringAnimation { spring: 2; damping: 0.2; modulus: 360 }
                    }
                }
            }
            Component {
                id: editor

                Rectangle {
                    id: rect
                    property var angle: mouseArea.drag ? mouseArea.angle : defaultAngle
                    property var defaultAngle: angleBinding
                    x: Math.round(Math.cos((-90+ angle)*Math.PI/180) *
                                  (pos - root.handOffset - width /2 + radius) + root.width / 2 - width / 2)
                    y: Math.round(Math.sin((-90+ angle)*Math.PI/180) *
                                  (pos - root.handOffset - width /2 + radius) + root.height / 2 - width / 2)
                    color: "#5caa15"
                    width: size
                    radius: width / 2
                    height: width
                    antialiasing: true

                    function calcAngle(mouse) {
                        var mouseGlobal = mapToItem(root, mouse.x, mouse.y)
                        var origin = root.width/2
                        var angle = (90+Math.atan2((mouseGlobal.y-origin), (mouseGlobal.x-origin))*180/Math.PI)
                        if (angle < 0)
                            angle += 360;
                        updateAngle(angle);
                        return angle;
                    }
                    MouseArea {
                        id: mouseArea
                        anchors.fill: parent
                        property int startX: 0
                        property int startY: 0
                        property bool drag: false
                        property var angle: 0.0
                        preventStealing: true

                        onPressed: {
                            var mouseGlobal = mapToItem(root, mouse.x, mouse.y);
                            startX = mouseGlobal.x;
                            startY = mouseGlobal.y;
                            angle = calcAngle(mouse);
                            drag = true;
                        }
                        onReleased: {
                            drag = false;
                            ready(angle)
                        }
                        onMouseXChanged: if (drag) angle = calcAngle(mouse)
                    }
                }
            }
            Loader {
                property var angleBinding: (currentTime.getHours() * 30) + (currentTime.getMinutes() * 0.5)
                property int size: Math.round(root.width * 0.120)
                property int pos: hours.height
                property bool pm: false
                visible: editMode

                function updateAngle(angle) {
                    var newHour = Math.floor(angle / 30);
                    var preHour = newTime.getHours();

                    if (preHour == 11 && newHour == 0) {
                        newHour = 12;
                        pm = true;
                    }
                    else if (preHour === 23 && newHour === 0) {
                        pm = false;
                    }
                    else if (preHour == 0 && newHour === 11) {
                        pm = true;
                    }
                    else if (preHour == 12 && newHour == 11) {
                        pm = false;
                    }
                    if (pm == true) {
                        newHour += 12;
                    }
                    newTime.setHours(newHour);
                }

                function ready(val) {
                    var newhours = Math.round(val / 30);
                    currentTime.setHours(newhours);
                    TimeManager.time = currentTime;
                }
                sourceComponent: editor
            }
            Loader {
                visible: editMode
                sourceComponent: editor
                property var angleBinding: currentTime.getMinutes() * 6
                property int size: Math.round(root.width * 0.120)
                property int pos: minutes.height

                function updateAngle(angle) {
                    var newMin = Math.round(angle / 6);
                    var hours = newTime.getHours();
                    newTime.setMinutes(newMin);
                    newTime.setHours(hours);
                }
                function ready(val) {
                    var newmins = Math.round(val / 6);
                    currentTime.setMinutes(newmins);
                    TimeManager.time = currentTime;
                }
            }
        }
    }
}