aboutsummaryrefslogtreecommitdiffstats
path: root/apps/com.theqtcompany.cluster/panels/DialPowerPanel.qml
blob: 73e452d895232fc22a5a0352d404eba71e7f33d7 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
/****************************************************************************
**
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Neptune 3 Cluster UI.
**
** $QT_BEGIN_LICENSE:GPL-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite 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 https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 or (at your option) any later version
** approved by the KDE Free Qt Foundation. The licenses are as published by
** the Free Software Foundation and appearing in the file LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
** SPDX-License-Identifier: GPL-3.0
**
****************************************************************************/

import QtQuick 2.9
import QtQuick.Controls 2.3
import "../helpers" 1.0
import shared.Style 1.0
import shared.Sizes 1.0

Item {
    id: root
    width: Sizes.dp(560)
    height: width

    //public
    property int ePower
    property int drivetrain: 0
    property int remainingKm: 180
    property int remainingPower: 20

    //private
    QtObject {
        id: d

        function power2Angle(power) {
            if (power < -25) {
                return -210;
            } else if (power <= 0) {
                //-25 to 0 %
                //map to -210degree to -180degree
                return ((power + 25) / 25 * 30 - 210);
            } else if (power <= 100) {
                //0 to 100 %
                //map to -180degree to 0degree
                return ((power) / 100 * 180 - 180);
            } else {
                return 0;
            }
        }

        property int ePowerOverride
        property bool overrideEPower: false
        readonly property int ePower: overrideEPower ? ePowerOverride : root.ePower
    }

    //states and transitions
    state: "stopped"
    states: [
        State {
            name: "stopped"
            PropertyChanges { target: scaleEnergyArea; opacity: 0 }
            PropertyChanges { target: graduation; opacity: 0; maxDrawValue: -25 }
            PropertyChanges { target: graduationNumber; opacity: 0 }
            PropertyChanges { target: indicatorDrivetrain; opacity: 0 }
            PropertyChanges { target: indicatorEPower; opacity: 0 }
            PropertyChanges { target: signBattery; opacity: 0; x: Sizes.dp(176); y: Sizes.dp(483) }
            PropertyChanges { target: signBatteryRemain; opacity: 0 }
            PropertyChanges { target: signChargeStation; opacity: 0 }
            PropertyChanges { target: signKM; opacity: 0; x: Sizes.dp(272); y: Sizes.dp(480) }
            PropertyChanges { target: signKMRemain; opacity: 0; x: Sizes.dp(207); y: Sizes.dp(464) }
            PropertyChanges { target: signPower; opacity: 0 }
        },
        State {
            name: "normal"
            PropertyChanges { target: scaleEnergyArea; opacity: 1 }
            PropertyChanges { target: graduation; opacity: 1; maxDrawValue: 105 }
            PropertyChanges { target: graduationNumber; opacity: Style.opacityMedium }
            PropertyChanges { target: indicatorDrivetrain; opacity: 1 }
            PropertyChanges { target: indicatorEPower; opacity: Style.opacityHigh }
            PropertyChanges { target: signBattery; opacity: 1; x: Sizes.dp(176); y: Sizes.dp(483) }
            PropertyChanges { target: signBatteryRemain; opacity: Style.opacityHigh }
            PropertyChanges { target: signChargeStation; opacity: 1 }
            PropertyChanges { target: signKM; opacity: Style.opacityLow; x: Sizes.dp(272); y: Sizes.dp(480) }
            PropertyChanges { target: signKMRemain; opacity: Style.opacityHigh; x: Sizes.dp(207); y: Sizes.dp(464) }
            PropertyChanges { target: signPower; opacity: Style.opacityLow }
        },
        State {
            name: "navi"
            PropertyChanges { target: scaleEnergyArea; opacity: 0 }
            PropertyChanges { target: graduation; opacity: 0; maxDrawValue: 105 }
            PropertyChanges { target: graduationNumber; opacity: 0 }
            PropertyChanges { target: indicatorDrivetrain; opacity: 1 }
            PropertyChanges { target: indicatorEPower; opacity: Style.opacityHigh }
            PropertyChanges { target: signBattery; opacity: 1; x: Sizes.dp(210); y: Sizes.dp(380) }
            PropertyChanges { target: signBatteryRemain; opacity: 0 }
            PropertyChanges { target: signChargeStation; opacity: 0 }
            PropertyChanges { target: signKM; opacity: Style.opacityLow; x: Sizes.dp(330); y: Sizes.dp(375) }
            PropertyChanges { target: signKMRemain; opacity: Style.opacityHigh; x: Sizes.dp(250); y: Sizes.dp(362) }
            PropertyChanges { target: signPower; opacity: Style.opacityLow }
        }
    ]

    transitions: [
        Transition {
            from: "stopped"
            to: "*"
            reversible: false
            SequentialAnimation{
                //wait DialFrame to start (1600ms)
                PauseAnimation { duration: 1600 }
                //fade in (640ms)
                PropertyAnimation { targets: [scaleEnergyArea, graduation, graduationNumber]; property: "opacity"; duration: 130 }
                PropertyAnimation { target: graduation; property: "maxDrawValue"; duration: 370 }
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    property: "opacity"
                    duration: 140
                }

                //test the highLight
                ScriptAction { script: {
                    d.ePowerOverride = 0;
                    d.overrideEPower = true;
                }}
                PropertyAnimation { target: d; property: "ePowerOverride"; to: 100; duration: 540 }
                PropertyAnimation { target: d; property: "ePowerOverride"; to: 0; duration: 540 }
                PropertyAnimation { target: d; property: "ePowerOverride"; to: root.ePower; duration: 540 }
                ScriptAction { script: {
                    d.overrideEPower = false;
                }}
            }
        },
        Transition {
            from: "normal"
            to: "stopped"
            reversible: false
            SequentialAnimation{
                //fade out (390ms)
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    property: "opacity"
                    duration: 130
                }
                PropertyAnimation { target: graduation; property: "maxDrawValue"; duration: 130 }
                PropertyAnimation { targets: [scaleEnergyArea, graduation, graduationNumber]; property: "opacity"; duration: 130 }
            }
        },
        Transition {
            from: "normal"
            to: "navi"
            reversible: false
            SequentialAnimation{
                //fade out (160ms)
                PropertyAnimation { targets: [scaleEnergyArea, graduation, graduationNumber]; property: "opacity"; duration: 80 }
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    property: "opacity"
                    to: 0
                    duration: 80
                }
                //wait DialFrame to shrink(160ms)
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    properties: "x,y"
                    duration: 160
                }
                //fade in (160ms)
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    property: "opacity"
                    duration: 130
                }
            }
        },
        Transition {
            from: "navi"
            to: "normal"
            reversible: false
            SequentialAnimation{
                //fade out (160ms)
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    property: "opacity"
                    to: 0
                    duration: 160
                }
                //wait DialFrame to expand(160ms)
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    properties: "x,y"
                    duration: 160
                }
                //fade in (160ms)
                PropertyAnimation { targets: [scaleEnergyArea, graduation, graduationNumber]; property: "opacity"; duration: 80 }
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    property: "opacity"
                    duration: 80
                }
            }
        },
        Transition {
            from: "navi"
            to: "stopped"
            reversible: false
            SequentialAnimation{
                //fade out (640ms)
                PropertyAnimation { targets: [scaleEnergyArea, graduation, graduationNumber]; property: "opacity"; duration: 100 }
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    properties: "opacity"
                    duration: 200
                }
                PropertyAnimation {
                    targets: [indicatorDrivetrain, indicatorEPower, signBattery, signBatteryRemain, signChargeStation, signKM, signKMRemain, signPower]
                    properties: "x, y"
                    duration: 200
                }
            }
        }
    ]

    //visual components
    DialFramePanel {
        id: dialFrame
        width: Sizes.dp(560)
        height: width
        anchors.centerIn: parent
        state: parent.state
        minAng: -210
        maxAng: 0
        zeroAng: -180
        positiveColor: Style.accentColor
        negativeColor: "#80447191"
        highLightAng: d.power2Angle(d.ePower)
    }

    Image {
        id: scaleEnergyArea
        x: Sizes.dp(31)
        y: Sizes.dp(107)
        width: Sizes.dp(71)
        height: Sizes.dp(294)
        source: Utils.localAsset("dial-energy-areas", Style.theme)
    }

    Item {//PRND
        id: indicatorDrivetrain
        x: Sizes.dp(240)
        y: Sizes.dp(165)
        width: Sizes.dp(40)
        opacity: 1
        Label {
            id: indicatorDrivetrainP
            anchors.left: parent.left
            text: "P"
            verticalAlignment: Text.AlignTop
            horizontalAlignment: Text.AlignHCenter
            font.weight: (root.drivetrain === 0) ? Font.Normal : Font.Light
            opacity: (root.drivetrain === 0) ? Style.opacityHigh : Style.opacityLow
            font.pixelSize: Sizes.dp(34)
        }
        Label {
            id: indicatorDrivetrainR
            anchors.left: indicatorDrivetrainP.right
            text: "R"
            verticalAlignment: Text.AlignTop
            horizontalAlignment: Text.AlignHCenter
            font.weight: (root.drivetrain === 3) ? Font.Normal : Font.Light
            opacity: (root.drivetrain === 3) ? Style.opacityHigh : Style.opacityLow
            font.pixelSize: Sizes.dp(34)
        }
        Label {
            id: indicatorDrivetrainN
            anchors.left: indicatorDrivetrainR.right
            text: "N"
            verticalAlignment: Text.AlignTop
            horizontalAlignment: Text.AlignHCenter
            font.weight: (root.drivetrain === 1) ? Font.Normal : Font.Light
            opacity: (root.drivetrain === 1) ? Style.opacityHigh : Style.opacityLow
            font.pixelSize: Sizes.dp(34)
        }
        Label {
            anchors.left: indicatorDrivetrainN.right
            text: "D"
            verticalAlignment: Text.AlignTop
            horizontalAlignment: Text.AlignHCenter
            font.weight: (root.drivetrain === 2) ? Font.Normal : Font.Light
            opacity: (root.drivetrain === 2) ? Style.opacityHigh : Style.opacityLow
            font.pixelSize: Sizes.dp(34)
        }
    }

    Label {
        id: indicatorEPower
        x: Sizes.dp(265)
        y: Sizes.dp(222)
        width: Sizes.dp(40)
        text: Math.abs( Math.round(d.ePower) )
        verticalAlignment: Text.AlignTop
        horizontalAlignment: Text.AlignHCenter
        font.weight: Font.DemiBold
        opacity: Style.opacityHigh
        font.pixelSize: Sizes.dp(80)
    }

    Label {
        id: signPower
        x: Sizes.dp(248)
        y: Sizes.dp(324)
        text: qsTr("% power")
        font.weight: Font.Light
        opacity: Style.opacityLow
        font.pixelSize: Sizes.dp(18)
    }

    Label {
        id: signKM
        x: Sizes.dp(272)
        y: Sizes.dp(480)
        text: qsTr("km")
        verticalAlignment: Text.AlignTop
        horizontalAlignment: Text.AlignHCenter
        font.weight: Font.Light
        opacity: Style.opacityLow
        font.pixelSize: Sizes.dp(18)
    }

    Image {
        id: signBattery
        x: Sizes.dp(176)
        y: Sizes.dp(483)
        width: Sizes.dp(23)
        height: Sizes.dp(14)
        source: Utils.localAsset("ic-battery", Style.theme)
    }

    Label {
        id: signKMRemain
        x: Sizes.dp(207)
        y: Sizes.dp(464)
        width: Sizes.dp(70)
        horizontalAlignment: root.state !== "navi" ? Text.AlignLeft : Text.AlignHCenter
        text: root.remainingKm
        font.weight: Font.Light
        opacity: Style.opacityHigh
        font.pixelSize: Sizes.dp(34)
    }

    Label {
        id: signBatteryRemain
        x: Sizes.dp(303)
        y: Sizes.dp(464)
        width: Sizes.dp(50)
        text: root.remainingPower
        font.weight: Font.Light
        horizontalAlignment: Text.AlignRight
        opacity: Style.opacityHigh
        font.pixelSize: Sizes.dp(34)
    }

    Image {
        id: signChargeStation
        x: Sizes.dp(353)
        y: Sizes.dp(474)
        width: Sizes.dp(24)
        height: Sizes.dp(21)
        source: Utils.localAsset("ic-chargingstation", Style.theme)
    }

    Repeater{
        id: graduationNumber
        anchors.centerIn: parent
        width: Sizes.dp(520)
        height: width
        opacity: Style.opacityMedium

        //size and layout
        readonly property real radius: width / 2 - Sizes.dp(50)
        readonly property real centerX: width / 2
        readonly property real centerY: height / 2

        model: [0, 25, 50, 75, 100]
        delegate: Label {
            property int angle: d.power2Angle(modelData)
            property real radin: angle / 180 * Math.PI

            text: modelData
            x: graduationNumber.centerX + Math.cos(radin) * graduationNumber.radius
            y: graduationNumber.centerY + Math.sin(radin) * graduationNumber.radius
            visible: (modelData < graduation.maxDrawValue) ? true : false

            opacity: graduationNumber.opacity
            font.weight: Font.Light
            font.pixelSize: Sizes.dp(22)
        }
    }

    Canvas {
        id: graduation
        anchors.centerIn: parent
        width: Sizes.dp(520)
        height: width
        renderTarget: Canvas.Image

        readonly property real radius: graduation.width / 2
        readonly property real centerX: graduation.width / 2
        readonly property real centerY: graduation.height / 2
        readonly property real scaleLineLength: Sizes.dp(8)
        readonly property real scaleLineWidth: Sizes.dp(2)
        readonly property real scaleLineBlank: Sizes.dp(8)

        //for startup animation
        property int maxDrawValue: 100
        onMaxDrawValueChanged: {
            requestPaint();
        }
        Component.onCompleted: {
            requestPaint();
        }

        onPaint: {
            var ctx = getContext("2d");
            ctx.clearRect(0, 0, graduation.width, graduation.height);
            ctx.globalCompositeOperation = "source-over";

            ctx.beginPath();
            ctx.lineWidth = scaleLineWidth;
            ctx.strokeStyle = "#916E51";

            var radin;
            for (var i = -25; i <= maxDrawValue && i <= 100; i += 25) {
                radin = d.power2Angle(i) / 180 * Math.PI;
                ctx.moveTo(
                        centerX + Math.cos(radin) * (radius - scaleLineBlank - scaleLineLength),
                        centerY + Math.sin(radin) * (radius - scaleLineBlank - scaleLineLength));
                ctx.lineTo(
                        centerX + Math.cos(radin) * (radius - scaleLineBlank),
                        centerY + Math.sin(radin) * (radius - scaleLineBlank));
            }
            ctx.stroke();
        }
    }
}