Lomiri
Loading...
Searching...
No Matches
Circle.qml
1/*
2 * Copyright (C) 2013, 2016 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17import QtQuick 2.12
18
19Canvas {
20 id: root
21 property color color
22 property real circleScale
23 property var centerCircle
24
25 onColorChanged: requestPaint()
26 onCircleScaleChanged: requestPaint()
27 onCenterCircleChanged: requestPaint()
28
29 onPaint: {
30 if (circleScale <= 0 || width <= 0 || height <= 0) {
31 return;
32 }
33
34 var ctx = getContext("2d");
35
36 // draw big circle
37 ctx.save();
38 ctx.fillStyle = color;
39 ctx.beginPath();
40 ctx.arc(width / 2, height / 2, circleScale * width / 2, 0, 2 * Math.PI, false);
41 ctx.fill();
42 ctx.restore();
43
44 // chop out inner infographics circle
45 if (centerCircle) {
46 var circleMiddle = mapFromItem(centerCircle,
47 centerCircle.width / 2,
48 centerCircle.height / 2);
49 ctx.save();
50 ctx.globalCompositeOperation = "destination-out";
51 ctx.beginPath();
52 ctx.arc(circleMiddle.x, circleMiddle.y, centerCircle.width / 2, 0, 2 * Math.PI, false);
53 ctx.fill();
54 ctx.restore();
55 }
56 }
57}