Debian Codegraph @

examples/charts/qmlchartsgallery/qml/customlegend/CustomLegend.qml

raw
1 // Copyright (C) 20236 The Qt Company Ltd.
2 // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
3
4 pragma ComponentBehavior: Bound
5 import QtQuick
6
7 Rectangle {
8 id: legend
9 color: "lightgray"
10
11 property int seriesCount: 0
12 property variant seriesNames: []
13 property variant seriesColors: []
14
15 signal entered(string seriesName)
16 signal exited(string seriesName)
17 signal selected(string seriesName)
18
19 function addSeries(seriesName, color) {
20 var names = seriesNames;
21 names[seriesCount] = seriesName;
22 seriesNames = names;
23
24 var colors = seriesColors;
25 colors[seriesCount] = color;
26 seriesColors = colors;
27
28 seriesCount++;
29 }
30
31 Gradient {
32 id: buttonGradient
33 GradientStop { position: 0.0; color: "#F0F0F0" }
34 GradientStop { position: 1.0; color: "#A0A0A0" }
35 }
36
37 Gradient {
38 id: buttonGradientHovered
39 GradientStop { position: 0.0; color: "#FFFFFF" }
40 GradientStop { position: 1.0; color: "#B0B0B0" }
41 }
42
43 //![2]
44 Component {
45 id: legendDelegate
46 Rectangle {
47 id: rect
48 //![2]
49 property string name: legend.seriesNames[DelegateModel.itemsIndex]
50 property color markerColor: legend.seriesColors[DelegateModel.itemsIndex]
51
52 gradient: buttonGradient
53 border.color: "#A0A0A0"
54 border.width: 1
55 radius: 4
56
57 implicitWidth: label.implicitWidth + marker.implicitWidth + 30
58 implicitHeight: label.implicitHeight + marker.implicitHeight + 10
59
60 Row {
61 id: row
62 spacing: 5
63 anchors.verticalCenter: parent.verticalCenter
64 anchors.left: parent.left
65 anchors.leftMargin: 5
66 Rectangle {
67 id: marker
68 anchors.verticalCenter: parent.verticalCenter
69 color: rect.markerColor
70 opacity: 0.3
71 radius: 4
72 width: 12
73 height: 10
74 }
75 Text {
76 id: label
77 anchors.verticalCenter: parent.verticalCenter
78 anchors.verticalCenterOffset: -1
79 text: rect.name
80 }
81 }
82
83 //![3]
84 MouseArea {
85 id: mouseArea
86 anchors.fill: parent
87 hoverEnabled: true
88 onEntered: {
89 rect.gradient = buttonGradientHovered;
90 legend.entered(label.text);
91 }
92 onExited: {
93 rect.gradient = buttonGradient;
94 legend.exited(label.text);
95 marker.opacity = 0.3;
96 marker.height = 10;
97 }
98 onClicked: {
99 legend.selected(label.text);
100 marker.opacity = 1.0;
101 marker.height = 12;
102 }
103 }
104 //![3]
105 }
106 }
107
108 //![1]
109 Row {
110 id: legendRow
111 anchors.centerIn: parent
112 spacing: 10
113
114 Repeater {
115 id: legendRepeater
116 model: legend.seriesCount
117 delegate: legendDelegate
118 }
119 }
120 //![1]
121 }