1 /****************************************************************************
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
18 ** Alternatively, you may use this file under the terms of the BSD license
21 ** "Redistribution and use in source and binary forms, with or without
22 ** modification, are permitted provided that the following conditions are
24 ** * Redistributions of source code must retain the above copyright
25 ** notice, this list of conditions and the following disclaimer.
26 ** * Redistributions in binary form must reproduce the above copyright
27 ** notice, this list of conditions and the following disclaimer in
28 ** the documentation and/or other materials provided with the
30 ** * Neither the name of The Qt Company Ltd nor the names of its
31 ** contributors may be used to endorse or promote products derived
32 ** from this software without specific prior written permission.
35 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
49 ****************************************************************************/
53 import QtQuick.Controls.Styles 1.4
56 tickmarkInset: toPixels(0.04)
57 minorTickmarkInset: tickmarkInset
59 labelInset: toPixels(0.23)
61 property real xCenter: outerRadius
62 property real yCenter: outerRadius
63 property real needleLength: outerRadius - tickmarkInset * 1.25
64 property real needleTipWidth: toPixels(0.02)
65 property real needleBaseWidth: toPixels(0.06)
66 property bool halfGauge: false
68 function toPixels(percentage) {
69 return percentage * outerRadius;
72 function degToRad(degrees) {
73 return degrees * (Math.PI / 180);
76 function radToDeg(radians) {
77 return radians * (180 / Math.PI);
80 function paintBackground(ctx) {
83 ctx.rect(0, 0, ctx.canvas.width, ctx.canvas.height / 2);
88 ctx.fillStyle = "black";
89 ctx.ellipse(0, 0, ctx.canvas.width, ctx.canvas.height);
93 ctx.lineWidth = tickmarkInset;
94 ctx.strokeStyle = "black";
95 ctx.arc(xCenter, yCenter, outerRadius - ctx.lineWidth / 2, outerRadius - ctx.lineWidth / 2, 0, Math.PI * 2);
99 ctx.lineWidth = tickmarkInset / 2;
100 ctx.strokeStyle = "#222";
101 ctx.arc(xCenter, yCenter, outerRadius - ctx.lineWidth / 2, outerRadius - ctx.lineWidth / 2, 0, Math.PI * 2);
105 var gradient = ctx.createRadialGradient(xCenter, yCenter, 0, xCenter, yCenter, outerRadius * 1.5);
106 gradient.addColorStop(0, Qt.rgba(1, 1, 1, 0));
107 gradient.addColorStop(0.7, Qt.rgba(1, 1, 1, 0.13));
108 gradient.addColorStop(1, Qt.rgba(1, 1, 1, 1));
109 ctx.fillStyle = gradient;
110 ctx.arc(xCenter, yCenter, outerRadius - tickmarkInset, outerRadius - tickmarkInset, 0, Math.PI * 2);
116 var ctx = getContext("2d");
118 paintBackground(ctx);
123 font.pixelSize: toPixels(0.3)
126 horizontalAlignment: Text.AlignRight
127 anchors.horizontalCenter: parent.horizontalCenter
128 anchors.top: parent.verticalCenter
129 anchors.topMargin: toPixels(0.1)
130 visible: valueSource.displayNumericSpeeds === true
132 readonly property int kphInt: control.value
136 text: valueSource.mphDisplay ? "MPH" : "km/h"
138 font.pixelSize: toPixels(0.09)
139 anchors.top: speedText.bottom
140 anchors.horizontalCenter: parent.horizontalCenter
145 implicitWidth: needleBaseWidth
146 implicitHeight: needleLength
148 property real xCenter: width / 2
149 property real yCenter: height / 2
152 var ctx = getContext("2d");
156 ctx.moveTo(xCenter, height);
157 ctx.lineTo(xCenter - needleBaseWidth / 2, height - needleBaseWidth / 2);
158 ctx.lineTo(xCenter - needleTipWidth / 2, 0);
159 ctx.lineTo(xCenter, yCenter - needleLength);
160 ctx.lineTo(xCenter, 0);
162 ctx.fillStyle = Qt.rgba(0.66, 0, 0, 0.66);
166 ctx.moveTo(xCenter, height)
167 ctx.lineTo(width, height - needleBaseWidth / 2);
168 ctx.lineTo(xCenter + needleTipWidth / 2, 0);
169 ctx.lineTo(xCenter, 0);
171 ctx.fillStyle = Qt.lighter(Qt.rgba(0.66, 0, 0, 0.66));