summaryrefslogtreecommitdiff
path: root/Graph.qml
blob: 1c181bade015b2f29d37e66dde36c23672b802aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/* SPDXLicenseID: MPL-2.0
*
* Copyright (C) 2014, GENIVI Alliance
*
* This file is part of AudioManager Monitor
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License (MPL), v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* For further information see http://www.genivi.org/.
*
* List of changes:
*/

import QtQuick 2.0
import com.windriver.ammonitor 1.0
import "code.js" as Code

Rectangle {
    property string title : "No name"
    property string description : "No description"
    property string backgroundColor : "#F0F0F0"
    property string backgroundLineColor : "#aaaaaa"
    property real backgroundLineWidth : 0.1
    property int refreshInterval : 500
    property string graphName : ""
    property int defaultValue : 0
    property int maxDataLength : 100
    property int maxValue : 100
    property int graphLineWidth : 1
    property int type : 0
    property var graph : null
    property var graphInfo : ({})

    Component.onCompleted: {
        graph = new Code.Graph(type);
        refreshTimer.start();
    }

    function getGraphNodeColor(nodeName) {
        return graph.getGraphNodeColor(nodeName);
    }

    function updateData(name, id, value) {
        var index = graph.checkGraphNodeInList(name, id);
        if (index < 0) {
            console.log("Try to add graphNode(name: " + name + ", id: " + id + " )");
            graph.addGraphNode(name, id, maxDataLength, value);
            return;
        }

        graph.updateGraphNode(index, value);
    }

    function removeData(name, id) {
        var index = graph.checkGraphNodeInList(name, id);
        if (index < 0)
            return;

        console.log("Try to remove graphNode(name: " + name + ", id: " + id + " )");
        graph.removeGraphNode(index);
    }

    function startTimer() {
        refreshTimer.start();
    }

    function stopTimer() {
        refreshTimer.stop();
    }

    Text {
        text: title
        font.pixelSize: parent.height / 20
        anchors.bottom: canvas.top
        anchors.left:  canvas.left
    }

    Text {
        text: maxValue
        font.pixelSize: parent.height / 30
        anchors.right: canvas.left
        anchors.rightMargin: 5
        y: parent.height * 0.25
    }
    Text {
        text: maxValue / 2
        font.pixelSize: parent.height / 30
        anchors.right: canvas.left
        anchors.verticalCenter: parent.verticalCenter
        anchors.rightMargin: 5
        y: parent.height * 0.5
    }
    Text {
        id: volume0
        text: "0"
        font.pixelSize: parent.height / 30
        anchors.right: canvas.left
        anchors.rightMargin: 5
        y: parent.height * 0.70
    }

    Text {
        text: "Volume"
        font.pixelSize: parent.height / 30
        anchors.right: canvas.left
        anchors.rightMargin: 2
        anchors.top: volume0.bottom
    }

    Canvas {
        id: canvas
        width: parent.width
        height: parent.height
        anchors.fill: parent
        anchors.margins: parent.width / 10
        antialiasing: true

        signal refreshGraphNode();

        Component.onCompleted: {
            graphInfo.width = width + 91;
            graphInfo.height = height - 22;
            graphInfo.maxDataLength = parent.maxDataLength;
            graphInfo.lineWidth = parent.graphLineWidth;
            graphInfo.maxValue = parent.maxValue;
            graphInfo.backgroundColor = parent.backgroundColor;
            graphInfo.backgroundLineColor = parent.backgroundLineColor;
            graphInfo.backgroundLineWidth = parent.backgroundLineWidth;

            graph.loadGraphInfo(graphInfo);
        }

        onRefreshGraphNode: {
            graph.refreshGraphNode();
        }

        onPaint: {
            var context = canvas.getContext("2d");
            context.reset();

            if (graph == null) {
                console.log("Error: Cannot create graph, " + graph.name);
                return;
            }

            graph.drawBackground(context);
            graph.drawGraphNode(context);
        }
    }

    Timer {
        id: refreshTimer
        interval: parent.refreshInterval
        repeat: true
        onTriggered: {
            canvas.refreshGraphNode();
            canvas.requestPaint();
        }
    }

    Text {
        text: "("+description+")"
        font.pixelSize: parent.height / 30
        anchors.bottom: parent.bottom
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.bottomMargin: parent.height/14
   }
}