2015-12-27 14:01:07 +03:00
|
|
|
|
import QtQuick 2.0
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Meter in half circle
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
Item {
|
|
|
|
|
/// current level
|
2015-12-27 14:22:24 +03:00
|
|
|
|
property double level: 0.5
|
2015-12-27 14:01:07 +03:00
|
|
|
|
/// minimum level
|
2015-12-31 19:12:54 +03:00
|
|
|
|
property double min: -50
|
2015-12-27 14:01:07 +03:00
|
|
|
|
/// maximum level
|
2015-12-31 19:12:54 +03:00
|
|
|
|
property double max: 50
|
2015-12-30 00:47:54 +03:00
|
|
|
|
/// numbers to write on the scale
|
2015-12-31 19:12:54 +03:00
|
|
|
|
property variant marks: [-40, -20, 0, 20, 40]
|
|
|
|
|
/// marks regions colors
|
|
|
|
|
property variant region_color: ["red", "yellow", "green", "yellow", "red"]
|
2015-12-27 14:01:07 +03:00
|
|
|
|
/// theme object
|
|
|
|
|
property QtObject theme
|
|
|
|
|
|
2015-12-31 19:12:54 +03:00
|
|
|
|
property double r_circle_min: 0.85
|
|
|
|
|
property double r_circle_max: 1
|
|
|
|
|
|
|
|
|
|
property double amin: angle(min)
|
|
|
|
|
property double amax: angle(max)
|
|
|
|
|
|
2015-12-30 00:47:54 +03:00
|
|
|
|
/// positions helper functions
|
|
|
|
|
function angle(level) {
|
|
|
|
|
return (level - min) / (max - min) * Math.PI - Math.PI / 2
|
|
|
|
|
}
|
|
|
|
|
function getx(angle, k) {
|
|
|
|
|
return width * 0.5 + width * 0.5 * k * Math.sin(angle)
|
|
|
|
|
}
|
|
|
|
|
function gety(angle, k) {
|
|
|
|
|
// k: [0,1]
|
|
|
|
|
return height - height * k * Math.cos(angle)
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 19:12:54 +03:00
|
|
|
|
/// objects draw
|
|
|
|
|
|
|
|
|
|
function arc(ctx, k) {
|
|
|
|
|
ctx.beginPath()
|
|
|
|
|
ctx.moveTo(getx(amin, k), gety(amin, k))
|
|
|
|
|
for (var i = amin + 0.01; i <= amax; i+=0.01) {
|
|
|
|
|
ctx.lineTo(getx(i,k), gety(i,k))
|
|
|
|
|
}
|
|
|
|
|
ctx.stroke()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function arc_part(ctx, k, a1, a2) {
|
|
|
|
|
ctx.lineTo(getx(a1,k), gety(a1,k))
|
|
|
|
|
var eps = 0.01
|
|
|
|
|
if (a2 > a1) {
|
|
|
|
|
for (var i = a1 + eps; i < a2; i+=eps) {
|
|
|
|
|
ctx.lineTo(getx(i,k), gety(i,k))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
for (var i = a1 - eps; i > a2; i-=eps) {
|
|
|
|
|
ctx.lineTo(getx(i,k), gety(i,k))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ctx.lineTo(getx(a2,k), gety(a2,k))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function line_mark(ctx, value, r_min, r_max) {
|
|
|
|
|
var a = angle(value)
|
|
|
|
|
ctx.beginPath()
|
|
|
|
|
ctx.moveTo(getx(a, r_min), gety(a, r_min))
|
|
|
|
|
ctx.lineTo(getx(a, r_max), gety(a, r_max))
|
|
|
|
|
ctx.stroke()
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 21:00:03 +03:00
|
|
|
|
function find_region() {
|
|
|
|
|
var l1 = min
|
|
|
|
|
var l2
|
|
|
|
|
for (var i = 0; i < marks.length; i++) {
|
|
|
|
|
if (i == marks.length - 1) l2 = max
|
|
|
|
|
else l2 = (marks[i] + marks[i+1]) / 2
|
|
|
|
|
|
|
|
|
|
if (level <= l2) return [i, l1, l2]
|
|
|
|
|
l1 = l2
|
|
|
|
|
}
|
2016-01-01 12:03:04 +03:00
|
|
|
|
return [0,0,0]
|
2015-12-31 21:00:03 +03:00
|
|
|
|
}
|
|
|
|
|
|
2015-12-27 14:22:24 +03:00
|
|
|
|
/// Ellipse
|
2015-12-27 14:01:07 +03:00
|
|
|
|
Canvas {
|
2015-12-27 14:22:24 +03:00
|
|
|
|
id: ellipse
|
2015-12-27 14:01:07 +03:00
|
|
|
|
anchors.fill: parent
|
2015-12-30 00:47:54 +03:00
|
|
|
|
|
2015-12-31 19:12:54 +03:00
|
|
|
|
property double r_text: 0.92
|
|
|
|
|
|
|
|
|
|
property double l_marker: 0.035
|
|
|
|
|
property double h_marker: 7
|
|
|
|
|
property int font_size: 20
|
2015-12-30 00:47:54 +03:00
|
|
|
|
|
2015-12-27 14:01:07 +03:00
|
|
|
|
onPaint: {
|
2015-12-27 14:22:24 +03:00
|
|
|
|
var ctx = getContext('2d');
|
2015-12-27 14:01:07 +03:00
|
|
|
|
ctx.strokeStyle = theme.primaryColor
|
|
|
|
|
ctx.lineWidth = 1
|
2015-12-31 19:12:54 +03:00
|
|
|
|
|
|
|
|
|
arc(ctx, r_circle_min)
|
|
|
|
|
arc(ctx, r_circle_max)
|
|
|
|
|
|
2015-12-30 00:47:54 +03:00
|
|
|
|
ctx.font = font_size + "px sans-serif"
|
|
|
|
|
ctx.textAlign = "center"
|
|
|
|
|
|
2015-12-31 19:12:54 +03:00
|
|
|
|
ctx.lineWidth = h_marker
|
|
|
|
|
ctx.strokeStyle = theme.secondaryColor
|
2015-12-30 00:47:54 +03:00
|
|
|
|
for (var i = 0; i < marks.length; i++) {
|
2015-12-31 19:12:54 +03:00
|
|
|
|
line_mark(ctx, marks[i], r_circle_min - l_marker, r_circle_min + l_marker)
|
2015-12-30 00:47:54 +03:00
|
|
|
|
var a = angle(marks[i])
|
2015-12-31 19:12:54 +03:00
|
|
|
|
ctx.fillText(marks[i], getx(a, r_text), gety(a, r_text) + 4)
|
2015-12-30 00:47:54 +03:00
|
|
|
|
ctx.strokeText()
|
|
|
|
|
}
|
2015-12-31 19:12:54 +03:00
|
|
|
|
|
|
|
|
|
// "beetween" marks
|
|
|
|
|
ctx.lineWidth = 1
|
|
|
|
|
ctx.strokeStyle = theme.primaryColor
|
|
|
|
|
for (var i = 0; i < marks.length - 1; i++) {
|
|
|
|
|
line_mark(ctx, (marks[i] + marks[i+1])/2, r_circle_min, r_circle_max)
|
|
|
|
|
}
|
2015-12-27 14:22:24 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Canvas {
|
|
|
|
|
/// level arrow
|
|
|
|
|
id: arrow
|
|
|
|
|
anchors.fill: parent
|
2015-12-31 19:12:54 +03:00
|
|
|
|
property double k: 0.82
|
2015-12-30 00:47:54 +03:00
|
|
|
|
property double k_base: 0.1
|
|
|
|
|
property double angle: parent.angle(level)
|
2015-12-27 14:22:24 +03:00
|
|
|
|
|
|
|
|
|
onPaint: {
|
|
|
|
|
var ctx = getContext('2d');
|
2015-12-31 19:12:54 +03:00
|
|
|
|
ctx.clearRect(0,0,width,height)
|
2015-12-27 14:22:24 +03:00
|
|
|
|
ctx.strokeStyle = theme.primaryColor
|
|
|
|
|
ctx.lineWidth = 1
|
|
|
|
|
ctx.beginPath()
|
2015-12-30 00:47:54 +03:00
|
|
|
|
ctx.moveTo(getx(angle, k_base), gety(angle, k_base))
|
|
|
|
|
ctx.lineTo(getx(angle, k), gety(angle, k))
|
2015-12-27 14:22:24 +03:00
|
|
|
|
ctx.stroke()
|
2015-12-31 19:12:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-27 14:22:24 +03:00
|
|
|
|
|
2015-12-31 19:12:54 +03:00
|
|
|
|
Canvas {
|
|
|
|
|
/// region colors
|
|
|
|
|
id: regions
|
2016-01-01 12:03:04 +03:00
|
|
|
|
property variant reg_drawed
|
2015-12-31 19:12:54 +03:00
|
|
|
|
anchors.fill: parent
|
2015-12-31 21:00:03 +03:00
|
|
|
|
property variant reg: find_region()
|
2015-12-31 19:12:54 +03:00
|
|
|
|
z: -4
|
|
|
|
|
onPaint: {
|
|
|
|
|
var ctx = getContext('2d');
|
|
|
|
|
ctx.clearRect(0,0,width,height)
|
|
|
|
|
|
2015-12-31 21:00:03 +03:00
|
|
|
|
//var reg = find_region()
|
|
|
|
|
var a1 = angle(reg[1])
|
|
|
|
|
var a2 = angle(reg[2])
|
|
|
|
|
ctx.fillStyle = region_color[reg[0]]
|
|
|
|
|
ctx.beginPath()
|
|
|
|
|
ctx.moveTo(getx(a1, r_circle_min), gety(a1, r_circle_min))
|
|
|
|
|
arc_part(ctx, r_circle_min, a1, a2)
|
|
|
|
|
arc_part(ctx, r_circle_max, a2, a1)
|
|
|
|
|
ctx.lineTo(getx(a1, r_circle_min), gety(a1, r_circle_min))
|
|
|
|
|
ctx.fill()
|
2016-01-01 12:03:04 +03:00
|
|
|
|
reg_drawed = reg
|
|
|
|
|
console.log("redraw")
|
2015-12-31 21:00:03 +03:00
|
|
|
|
}
|
2015-12-31 19:12:54 +03:00
|
|
|
|
|
2015-12-31 21:00:03 +03:00
|
|
|
|
function update_level() {
|
2016-01-01 12:03:04 +03:00
|
|
|
|
if (!reg_drawed || level > reg_drawed[2] || level < reg_drawed[1]) {
|
|
|
|
|
reg = find_region()
|
|
|
|
|
requestPaint()
|
|
|
|
|
}
|
2015-12-31 19:12:54 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Behavior on level {
|
|
|
|
|
NumberAnimation {
|
|
|
|
|
duration: 50
|
|
|
|
|
easing.amplitude: max - min
|
2015-12-27 14:01:07 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-12-31 19:12:54 +03:00
|
|
|
|
|
|
|
|
|
onLevelChanged: {
|
|
|
|
|
arrow.requestPaint()
|
2015-12-31 21:00:03 +03:00
|
|
|
|
regions.update_level()
|
2015-12-31 19:12:54 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
|
anchors.fill: parent
|
|
|
|
|
onClicked: {
|
|
|
|
|
level = Math.random() * (max - min) + min
|
|
|
|
|
}
|
2015-12-31 21:00:03 +03:00
|
|
|
|
}
|
2015-12-27 14:01:07 +03:00
|
|
|
|
}
|