CircleMeter: redraw regions more optimized

This commit is contained in:
Louis-Joseph Fournier 2015-12-31 19:00:03 +01:00
parent cdf30d2c3c
commit eb50cf9b08

View file

@ -71,6 +71,18 @@ Item {
ctx.stroke() ctx.stroke()
} }
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
}
}
/// Ellipse /// Ellipse
Canvas { Canvas {
id: ellipse id: ellipse
@ -134,33 +146,30 @@ Item {
Canvas { Canvas {
/// region colors /// region colors
id: regions id: regions
property int i_drawed: -1
anchors.fill: parent anchors.fill: parent
property variant reg: find_region()
z: -4 z: -4
onPaint: { onPaint: {
var ctx = getContext('2d'); var ctx = getContext('2d');
ctx.clearRect(0,0,width,height) ctx.clearRect(0,0,width,height)
var l1 = min //var reg = find_region()
var l2 i_drawed = reg[0]
var a1 = angle(reg[1])
for (var i = 0; i < marks.length; i++) { var a2 = angle(reg[2])
if (i == marks.length - 1) l2 = max ctx.fillStyle = region_color[reg[0]]
else l2 = (marks[i] + marks[i+1]) / 2
if (level <= l2) {
var a1 = angle(l1)
var a2 = angle(l2)
ctx.fillStyle = region_color[i]
ctx.beginPath() ctx.beginPath()
ctx.moveTo(getx(a1, r_circle_min), gety(a1, r_circle_min)) 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_min, a1, a2)
arc_part(ctx, r_circle_max, a2, a1) arc_part(ctx, r_circle_max, a2, a1)
ctx.lineTo(getx(a1, r_circle_min), gety(a1, r_circle_min)) ctx.lineTo(getx(a1, r_circle_min), gety(a1, r_circle_min))
ctx.fill() ctx.fill()
break;
}
l1 = l2
} }
function update_level() {
reg = find_region()
if (reg[0] != i_drawed) requestPaint()
} }
} }
@ -173,7 +182,7 @@ Item {
onLevelChanged: { onLevelChanged: {
arrow.requestPaint() arrow.requestPaint()
regions.requestPaint() regions.update_level()
} }
MouseArea { MouseArea {