2020-11-27 00:11:24 -06:00
|
|
|
/*
|
2021-01-05 22:48:49 -05:00
|
|
|
* Copyright (c) 2020-2021 Alex Spataru <https://github.com/alex-spataru>
|
2020-11-27 00:11:24 -06:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import QtQuick 2.12
|
2020-12-25 22:25:31 -06:00
|
|
|
import QtQuick.Controls 2.12
|
2020-11-27 00:11:24 -06:00
|
|
|
|
2020-12-25 22:25:31 -06:00
|
|
|
Rectangle {
|
2020-12-27 18:38:26 -06:00
|
|
|
id: root
|
2020-11-27 00:11:24 -06:00
|
|
|
|
2020-12-25 22:25:31 -06:00
|
|
|
//
|
|
|
|
// Custom properties
|
|
|
|
//
|
|
|
|
property string title: ""
|
|
|
|
property int lastNumber: 8
|
|
|
|
property int firstNumber: 0
|
2020-12-27 00:00:31 -06:00
|
|
|
property real numberSize: 16
|
2020-12-25 22:25:31 -06:00
|
|
|
property real currentValue: 0
|
|
|
|
property real maximumValue: 0
|
|
|
|
property real minimumValue: 0
|
|
|
|
property real indicatorWidth: 4
|
|
|
|
property bool maximumVisible: true
|
|
|
|
property bool minimumVisible: true
|
|
|
|
property bool currentVisible: true
|
|
|
|
property bool numbersVisible: true
|
|
|
|
property bool valueLabelVisible: true
|
|
|
|
|
|
|
|
//
|
|
|
|
// Colors
|
|
|
|
//
|
2021-01-22 12:46:11 -05:00
|
|
|
property color valueColor: "#8ecd9d"
|
|
|
|
property color titleColor: "#517497"
|
|
|
|
property color numbersColor: "#e6e0b2"
|
|
|
|
property color indicatorColor: "#e6e0b2"
|
|
|
|
property color indicatorMaxColor: "#d72d60"
|
|
|
|
property color indicatorMinColor: "#2d6073"
|
2020-12-25 22:25:31 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Redraw indicators automatically
|
|
|
|
//
|
|
|
|
onCurrentValueChanged: indicatorCanvas.requestPaint()
|
|
|
|
|
|
|
|
//
|
|
|
|
// Properties
|
|
|
|
//
|
|
|
|
border.width: 2
|
2021-01-22 12:46:11 -05:00
|
|
|
color: "#121218"
|
2020-12-25 22:25:31 -06:00
|
|
|
radius: width / 2
|
2021-01-22 12:46:11 -05:00
|
|
|
border.color: "#e6e0b2"
|
2020-12-25 22:25:31 -06:00
|
|
|
|
|
|
|
//
|
|
|
|
// Redraw numbers automatically
|
|
|
|
//
|
|
|
|
onWidthChanged: numbersCanvas.requestPaint()
|
|
|
|
onHeightChanged: numbersCanvas.requestPaint()
|
|
|
|
|
|
|
|
//
|
|
|
|
// Label
|
|
|
|
//
|
|
|
|
Label {
|
|
|
|
font.pixelSize: 14
|
2020-12-27 18:38:26 -06:00
|
|
|
text: root.title
|
|
|
|
color: root.titleColor
|
2020-12-25 22:25:31 -06:00
|
|
|
anchors.centerIn: parent
|
|
|
|
font.family: app.monoFont
|
|
|
|
anchors.verticalCenterOffset: 32
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Value label
|
|
|
|
//
|
|
|
|
Label {
|
|
|
|
font.pixelSize: 14
|
2020-12-27 18:38:26 -06:00
|
|
|
color: root.valueColor
|
2020-12-25 22:25:31 -06:00
|
|
|
anchors.centerIn: parent
|
|
|
|
font.family: app.monoFont
|
2020-12-27 18:38:26 -06:00
|
|
|
visible: root.valueLabelVisible
|
2020-12-25 22:25:31 -06:00
|
|
|
anchors.verticalCenterOffset: 56
|
2020-12-27 18:38:26 -06:00
|
|
|
text: root.currentValue.toFixed(3)
|
2020-12-25 22:25:31 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Numbers painter
|
|
|
|
//
|
|
|
|
Canvas {
|
|
|
|
opacity: 0.8
|
|
|
|
id: numbersCanvas
|
|
|
|
anchors.fill: parent
|
|
|
|
Component.onCompleted: requestPaint()
|
|
|
|
onPaint: {
|
|
|
|
var ctx = getContext('2d')
|
|
|
|
ctx.reset()
|
|
|
|
|
2020-12-27 18:38:26 -06:00
|
|
|
if (!root.numbersVisible)
|
2020-12-25 22:25:31 -06:00
|
|
|
return
|
|
|
|
|
|
|
|
var range = lastNumber - firstNumber
|
|
|
|
for (var i = 0; i <= range; ++i) {
|
2020-12-27 18:38:26 -06:00
|
|
|
var radius = Math.min(root.width, root.height) / 2
|
2020-12-25 22:25:31 -06:00
|
|
|
|
|
|
|
var startupTheta = -180
|
|
|
|
var theta = (startupTheta + i * 360 / (range + 1)) * (Math.PI / 180)
|
2020-12-27 18:38:26 -06:00
|
|
|
var dX = (radius - root.numberSize) * Math.cos(theta) + radius - root.numberSize / 2
|
|
|
|
var dY = (radius - root.numberSize) * Math.sin(theta) + radius + root.numberSize / 2
|
2020-12-25 22:25:31 -06:00
|
|
|
|
2020-12-27 18:38:26 -06:00
|
|
|
ctx.font = "bold " + root.numberSize + "px " + app.monoFont
|
|
|
|
ctx.fillStyle = root.indicatorColor
|
2020-12-25 22:25:31 -06:00
|
|
|
ctx.fillText(i + firstNumber, dX, dY)
|
|
|
|
|
|
|
|
if (i === range) {
|
|
|
|
var x = radius
|
|
|
|
var y = radius
|
|
|
|
var spacing = 10 * Math.PI / 180.0;
|
|
|
|
var startAngle = theta + spacing
|
|
|
|
var finishAngle = Math.PI - spacing
|
|
|
|
|
|
|
|
ctx.lineWidth = 2
|
2020-12-27 18:38:26 -06:00
|
|
|
ctx.strokeStyle = root.indicatorColor
|
2020-12-25 22:25:31 -06:00
|
|
|
|
|
|
|
ctx.beginPath();
|
2020-12-27 18:38:26 -06:00
|
|
|
ctx.arc(x, y, radius - (root.numberSize + 3), startAngle, finishAngle)
|
2020-12-25 22:25:31 -06:00
|
|
|
ctx.stroke()
|
|
|
|
ctx.beginPath();
|
2020-12-27 18:38:26 -06:00
|
|
|
ctx.arc(x, y, radius - (root.numberSize - 3), startAngle, finishAngle)
|
2020-12-25 22:25:31 -06:00
|
|
|
ctx.stroke()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Indicator painter
|
|
|
|
//
|
|
|
|
Canvas {
|
|
|
|
id: indicatorCanvas
|
|
|
|
anchors.fill: parent
|
|
|
|
Component.onCompleted: requestPaint()
|
|
|
|
onPaint: {
|
|
|
|
var ctx = getContext('2d')
|
|
|
|
|
|
|
|
function drawLineWithArrows(x0,y0,x1,y1,aWidth,aLength){
|
|
|
|
var dx=x1-x0;
|
|
|
|
var dy=y1-y0;
|
|
|
|
var angle=Math.atan2(dy,dx);
|
|
|
|
var length=Math.sqrt(dx*dx+dy*dy);
|
|
|
|
|
|
|
|
ctx.translate(x0,y0);
|
|
|
|
ctx.rotate(angle);
|
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(0,0);
|
|
|
|
ctx.lineTo(length,0);
|
|
|
|
|
|
|
|
ctx.moveTo(length-aLength,-aWidth);
|
|
|
|
ctx.lineTo(length,0);
|
|
|
|
ctx.lineTo(length-aLength,aWidth);
|
|
|
|
|
|
|
|
ctx.stroke();
|
|
|
|
ctx.setTransform(1,0,0,1,0,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawIndicator(value, color, width, lenGain) {
|
2020-12-27 18:38:26 -06:00
|
|
|
var deg = ((Math.min(value, root.lastNumber) / (root.lastNumber + 1)) * 360) - 180
|
2020-12-25 22:25:31 -06:00
|
|
|
var rad = deg * (Math.PI / 180)
|
2020-12-27 18:38:26 -06:00
|
|
|
var len = Math.min(root.width, root.height) * lenGain
|
2020-12-25 22:25:31 -06:00
|
|
|
|
2020-12-27 18:38:26 -06:00
|
|
|
var x = root.width / 2
|
|
|
|
var y = root.height / 2
|
2020-12-25 22:25:31 -06:00
|
|
|
var x1 = x + Math.cos(rad) * len
|
|
|
|
var y1 = y + Math.sin(rad) * len
|
|
|
|
|
|
|
|
ctx.lineWidth = width
|
|
|
|
ctx.strokeStyle = color
|
|
|
|
drawLineWithArrows(x, y, x1, y1, width, width * 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.reset()
|
|
|
|
|
2020-12-27 18:38:26 -06:00
|
|
|
if (root.maximumVisible)
|
|
|
|
drawIndicator(root.maximumValue, root.indicatorMaxColor, root.indicatorWidth * 0.8, 0.20)
|
2020-12-25 22:25:31 -06:00
|
|
|
|
2020-12-27 18:38:26 -06:00
|
|
|
if (root.minimumVisible)
|
|
|
|
drawIndicator(root.minimumValue, root.indicatorMinColor, root.indicatorWidth * 0.8, 0.20)
|
2020-12-25 22:25:31 -06:00
|
|
|
|
2020-12-27 18:38:26 -06:00
|
|
|
if (root.currentVisible)
|
|
|
|
drawIndicator(root.currentValue, root.indicatorColor, root.indicatorWidth, 0.28)
|
2020-12-25 22:25:31 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Central gauge
|
|
|
|
//
|
|
|
|
Rectangle {
|
|
|
|
width: 24
|
|
|
|
height: 24
|
|
|
|
color: "#111"
|
|
|
|
radius: width / 2
|
|
|
|
anchors.centerIn: parent
|
2020-12-27 18:38:26 -06:00
|
|
|
border.color: root.indicatorColor
|
|
|
|
border.width: root.indicatorWidth - 1
|
2020-12-25 22:25:31 -06:00
|
|
|
}
|
2020-11-27 00:11:24 -06:00
|
|
|
}
|