Conversion to using agl-compositor
[apps/agl-cluster-demo-dashboard.git] / app / ValueSource.qml
1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Copyright (C) 2018, 2019 Konsulko Group
5 ** Contact: https://www.qt.io/licensing/
6 **
7 ** This file is part of the examples of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:BSD$
10 ** Commercial License Usage
11 ** Licensees holding valid commercial Qt licenses may use this file in
12 ** accordance with the commercial license agreement provided with the
13 ** Software or, alternatively, in accordance with the terms contained in
14 ** a written agreement between you and The Qt Company. For licensing terms
15 ** and conditions see https://www.qt.io/terms-conditions. For further
16 ** information use the contact form at https://www.qt.io/contact-us.
17 **
18 ** BSD License Usage
19 ** Alternatively, you may use this file under the terms of the BSD license
20 ** as follows:
21 **
22 ** "Redistribution and use in source and binary forms, with or without
23 ** modification, are permitted provided that the following conditions are
24 ** met:
25 **   * Redistributions of source code must retain the above copyright
26 **     notice, this list of conditions and the following disclaimer.
27 **   * Redistributions in binary form must reproduce the above copyright
28 **     notice, this list of conditions and the following disclaimer in
29 **     the documentation and/or other materials provided with the
30 **     distribution.
31 **   * Neither the name of The Qt Company Ltd nor the names of its
32 **     contributors may be used to endorse or promote products derived
33 **     from this software without specific prior written permission.
34 **
35 **
36 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
37 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
38 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
39 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
40 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
46 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
47 **
48 ** $QT_END_LICENSE$
49 **
50 ****************************************************************************/
51
52 import QtQuick 2.2
53 Item {
54     id: valueSource
55     property real kph: 0
56     property bool mphDisplay: true
57     property real speedScaling: mphDisplay == true ? 0.621504 : 1.0
58     property real rpm: 1
59     property real fuel: 0.85
60     property string gear: {
61         var g;
62         if (kph < 30) {
63             return "1";
64         }
65         if (kph < 50) {
66             return "2";
67         }
68         if (kph < 80) {
69             return "3";
70         }
71         if (kph < 120) {
72             return "4";
73         }
74         if (kph < 160) {
75             return "5";
76         }
77     }
78     property string prindle: {
79         var g;
80         if (kph > 0) {
81             return "D";
82         }
83         return "P";
84     }
85
86     property bool start: true
87     property int turnSignal: -1
88     property bool startUp: false
89     property real temperature: 0.6
90     property bool cruiseEnabled: false
91     property bool cruiseSet: false
92     property bool laneDepartureWarnEnabled: false
93     property bool displayNumericSpeeds: true
94
95     function randomDirection() {
96         return Math.random() > 0.5 ? Qt.LeftArrow : Qt.RightArrow;
97     }
98
99     Connections {
100         target: SignalComposer
101
102         onSignalEvent: {
103             if (uid === "event.vehicle.speed") {
104                 var speed_tmp = parseFloat(value)
105                 if(units == "mph") {
106                     speed_tmp *= 1.609
107                 }
108                 if(!runAnimation) {
109                     valueSource.kph = speed_tmp
110                 }
111             }
112             else if (uid === "event.engine.speed") {
113                 if(!runAnimation) {
114                     valueSource.rpm = parseFloat(value) / 1000
115                 }
116             }
117             else if (uid === "event.cruise.enable" && value === "true") {
118                 if(valueSource.cruiseEnabled) {
119                     valueSource.cruiseEnabled = false
120                     valueSource.cruiseSet = false
121                 } else {
122                     valueSource.cruiseEnabled = true
123                 }
124             }
125             else if ((uid === "event.cruise.set" || uid === "event.cruise.resume") &&
126                      value === "true") {
127                 if(valueSource.cruiseEnabled) {
128                     valueSource.cruiseSet = true
129                 }
130             }
131             else if (uid === "event.cruise.cancel" && value === "true") {
132                 valueSource.cruiseSet = false
133             }
134             else if (uid === "event.lane_departure_warning.enable" && value === "true") {
135                 valueSource.laneDepartureWarnEnabled = !valueSource.laneDepartureWarnEnabled
136             }
137             else if (uid === "event.info" && value === "true") {
138                 valueSource.displayNumericSpeeds = !valueSource.displayNumericSpeeds
139             }
140         }
141     }
142
143     SequentialAnimation {
144         running: runAnimation
145         loops: 1
146
147         // We want a small pause at the beginning, but we only want it to happen once.
148         PauseAnimation {
149             duration: 1000
150         }
151
152         PropertyAction {
153             target: valueSource
154             property: "start"
155             value: false
156         }
157
158         SequentialAnimation {
159             loops: Animation.Infinite
160
161             // Simulate startup with indicators blink
162             PropertyAction {
163                 target: valueSource
164                 property: "startUp"
165                 value: true
166             }
167             PauseAnimation {
168                 duration: 1000
169             }
170             PropertyAction {
171                 target: valueSource
172                 property: "startUp"
173                 value: false
174             }
175
176             ParallelAnimation {
177                 NumberAnimation {
178                     target: valueSource
179                     property: "kph"
180                     easing.type: Easing.InOutSine
181                     from: 0
182                     to: 30
183                     duration: 3000
184                 }
185                 NumberAnimation {
186                     target: valueSource
187                     property: "rpm"
188                     easing.type: Easing.InOutSine
189                     from: 1
190                     to: 6.1
191                     duration: 3000
192                 }
193             }
194             ParallelAnimation {
195                 // We changed gears so we lost a bit of speed.
196                 NumberAnimation {
197                     target: valueSource
198                     property: "kph"
199                     easing.type: Easing.InOutSine
200                     from: 30
201                     to: 26
202                     duration: 600
203                 }
204                 NumberAnimation {
205                     target: valueSource
206                     property: "rpm"
207                     easing.type: Easing.InOutSine
208                     from: 6
209                     to: 2.4
210                     duration: 600
211                 }
212             }
213             ParallelAnimation {
214                 NumberAnimation {
215                     target: valueSource
216                     property: "kph"
217                     easing.type: Easing.InOutSine
218                     to: 60
219                     duration: 3000
220                 }
221                 NumberAnimation {
222                     target: valueSource
223                     property: "rpm"
224                     easing.type: Easing.InOutSine
225                     to: 5.6
226                     duration: 3000
227                 }
228             }
229             ParallelAnimation {
230                 // We changed gears so we lost a bit of speed.
231                 NumberAnimation {
232                     target: valueSource
233                     property: "kph"
234                     easing.type: Easing.InOutSine
235                     to: 56
236                     duration: 600
237                 }
238                 NumberAnimation {
239                     target: valueSource
240                     property: "rpm"
241                     easing.type: Easing.InOutSine
242                     to: 2.3
243                     duration: 600
244                 }
245             }
246             ParallelAnimation {
247                 NumberAnimation {
248                     target: valueSource
249                     property: "kph"
250                     easing.type: Easing.InOutSine
251                     to: 100
252                     duration: 3000
253                 }
254                 NumberAnimation {
255                     target: valueSource
256                     property: "rpm"
257                     easing.type: Easing.InOutSine
258                     to: 5.1
259                     duration: 3000
260                 }
261             }
262             ParallelAnimation {
263                 // We changed gears so we lost a bit of speed.
264                 NumberAnimation {
265                     target: valueSource
266                     property: "kph"
267                     easing.type: Easing.InOutSine
268                     to: 96
269                     duration: 600
270                 }
271                 NumberAnimation {
272                     target: valueSource
273                     property: "rpm"
274                     easing.type: Easing.InOutSine
275                     to: 2.2
276                     duration: 600
277                 }
278             }
279
280             ParallelAnimation {
281                 NumberAnimation {
282                     target: valueSource
283                     property: "kph"
284                     easing.type: Easing.InOutSine
285                     to: 140
286                     duration: 3000
287                 }
288                 NumberAnimation {
289                     target: valueSource
290                     property: "rpm"
291                     easing.type: Easing.InOutSine
292                     to: 6.2
293                     duration: 3000
294                 }
295             }
296
297             // Slow down a bit
298             ParallelAnimation {
299                 NumberAnimation {
300                     target: valueSource
301                     property: "kph"
302                     easing.type: Easing.InOutSine
303                     to: 115
304                     duration: 6000
305                 }
306                 NumberAnimation {
307                     target: valueSource
308                     property: "rpm"
309                     easing.type: Easing.InOutSine
310                     to: 5.5
311                     duration: 6000
312                 }
313             }
314
315             // Turn signal on
316             PropertyAction {
317                 target: valueSource
318                 property: "turnSignal"
319                 value: randomDirection()
320             }
321
322             // Cruise for a while
323             ParallelAnimation {
324                 NumberAnimation {
325                     target: valueSource
326                     property: "kph"
327                     easing.type: Easing.InOutSine
328                     to: 110
329                     duration: 10000
330                 }
331                 NumberAnimation {
332                     target: valueSource
333                     property: "rpm"
334                     easing.type: Easing.InOutSine
335                     to: 5.2
336                     duration: 10000
337                 }
338             }
339
340             // Turn signal off
341             PropertyAction {
342                 target: valueSource
343                 property: "turnSignal"
344                 value: -1
345             }
346
347             ParallelAnimation {
348                 NumberAnimation {
349                     target: valueSource
350                     property: "kph"
351                     easing.type: Easing.InOutSine
352                     to: 115
353                     duration: 10000
354                 }
355                 NumberAnimation {
356                     target: valueSource
357                     property: "rpm"
358                     easing.type: Easing.InOutSine
359                     to: 5.5
360                     duration: 10000
361                 }
362             }
363
364             // Start downshifting.
365
366             // Fifth to fourth gear.
367             ParallelAnimation {
368                 NumberAnimation {
369                     target: valueSource
370                     property: "kph"
371                     easing.type: Easing.Linear
372                     to: 100
373                     duration: 5000
374                 }
375
376                 NumberAnimation {
377                     target: valueSource
378                     property: "rpm"
379                     easing.type: Easing.InOutSine
380                     to: 3.1
381                     duration: 5000
382                 }
383             }
384
385             // Fourth to third gear.
386             NumberAnimation {
387                 target: valueSource
388                 property: "rpm"
389                 easing.type: Easing.InOutSine
390                 to: 5.5
391                 duration: 600
392             }
393
394             ParallelAnimation {
395                 NumberAnimation {
396                     target: valueSource
397                     property: "kph"
398                     easing.type: Easing.InOutSine
399                     to: 60
400                     duration: 5000
401                 }
402                 NumberAnimation {
403                     target: valueSource
404                     property: "rpm"
405                     easing.type: Easing.InOutSine
406                     to: 2.6
407                     duration: 5000
408                 }
409             }
410
411             // Third to second gear.
412             NumberAnimation {
413                 target: valueSource
414                 property: "rpm"
415                 easing.type: Easing.InOutSine
416                 to: 6.3
417                 duration: 600
418             }
419
420             ParallelAnimation {
421                 NumberAnimation {
422                     target: valueSource
423                     property: "kph"
424                     easing.type: Easing.InOutSine
425                     to: 30
426                     duration: 5000
427                 }
428                 NumberAnimation {
429                     target: valueSource
430                     property: "rpm"
431                     easing.type: Easing.InOutSine
432                     to: 2.6
433                     duration: 5000
434                 }
435             }
436
437             NumberAnimation {
438                 target: valueSource
439                 property: "rpm"
440                 easing.type: Easing.InOutSine
441                 to: 6.5
442                 duration: 600
443             }
444
445             // Second to first gear.
446             ParallelAnimation {
447                 NumberAnimation {
448                     target: valueSource
449                     property: "kph"
450                     easing.type: Easing.InOutSine
451                     to: 0
452                     duration: 5000
453                 }
454                 NumberAnimation {
455                     target: valueSource
456                     property: "rpm"
457                     easing.type: Easing.InOutSine
458                     to: 1
459                     duration: 4500
460                 }
461             }
462
463             PauseAnimation {
464                 duration: 5000
465             }
466         }
467     }
468 }