Debian Codegraph @

src/imports/PerformanceMetrics/BarGraph.qml

raw
1 /*
2 * Copyright 2014 Canonical Ltd.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser 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 Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17 import QtQuick 2.4
18 import Lomiri.Components 1.3
19 import Lomiri.PerformanceMetrics 1.0 as PerformanceMetrics
20
21 Item {
22 id: graph
23
24 property var model
25 property real maximumValue
26 property alias labels: labelsRepeater.model
27 property alias backgroundColor: background.color
28 property color color: Qt.rgba(0.4, 0.73, 0.4, 1.0)
29 property real threshold: 16.0
30 property color aboveThresholdColor: "#ff4e00"
31 property string labelFormat: "%1"
32
33 implicitWidth: units.gu(38)
34 implicitHeight: units.gu(8)
35
36 Rectangle {
37 id: background
38 anchors.fill: parent
39 color: Qt.rgba(0.0, 0.0, 0.0, 0.8)
40 }
41
42 PerformanceMetrics.TextureFromImage {
43 id: texture
44 image: graph.model.image
45 }
46
47 ShaderEffect {
48 anchors.fill: parent
49
50 property var texture: texture
51 property real shift: graph.model.shift / graph.model.samples
52 property real maximumValue: graph.maximumValue
53 property color color: graph.color
54 property real threshold: graph.threshold
55 property color aboveThresholdColor: graph.aboveThresholdColor
56
57 vertexShader: "
58 uniform mediump mat4 qt_Matrix;
59 uniform mediump float shift;
60 attribute mediump vec4 qt_Vertex;
61 attribute mediump vec2 qt_MultiTexCoord0;
62 varying mediump vec2 coord;
63 void main() {
64 coord = qt_MultiTexCoord0 + vec2(shift, 0.0);
65 gl_Position = qt_Matrix * qt_Vertex;
66 }"
67
68 fragmentShader: "
69 varying mediump vec2 coord;
70 uniform sampler2D texture;
71 uniform lowp float qt_Opacity;
72 uniform lowp float maximumValue;
73 uniform lowp vec4 color;
74 uniform lowp float threshold;
75 uniform lowp vec4 aboveThresholdColor;
76 void main() {
77 lowp vec4 tex = texture2D(texture, vec2(coord.x, coord.y));
78 lowp float value = tex.r * 255.0;
79 lowp float isOn = 1.0 - step(value / maximumValue, 1.0 - coord.y);
80 lowp float isAboveThreshold = step(threshold, value);
81
82 gl_FragColor = mix(vec4(0.0), mix(color, aboveThresholdColor, isAboveThreshold), isOn) * qt_Opacity;
83 }"
84 }
85
86 Repeater {
87 id: labelsRepeater
88
89 Item {
90 anchors {
91 left: parent.left
92 right: parent.right
93 bottom: parent.bottom
94 bottomMargin: modelData.value / graph.maximumValue * parent.height
95 }
96
97 Label {
98 id: label
99 anchors {
100 left: parent.left
101 leftMargin: units.gu(0.5)
102 verticalCenter: parent.verticalCenter
103 }
104 text: modelData.label
105 color: modelData.color
106 fontSize: "x-small"
107 }
108
109 Rectangle {
110 anchors {
111 left: label.right
112 leftMargin: units.gu(0.5)
113 right: parent.right
114 }
115 height: 1
116 color: modelData.color
117 }
118 }
119 }
120
121 Label {
122 anchors {
123 top: parent.top
124 topMargin: units.gu(1)
125 right: parent.right
126 rightMargin: units.gu(1)
127 }
128 text: graph.labelFormat.arg(graph.model.currentValue)
129 color: "white"
130 }
131 }