KQuickCharts

Legend.qml
1/*
2 * This file is part of KQuickCharts
3 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
4 *
5 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6 */
7
8import QtQuick
9import QtQuick.Layouts
10import QtQuick.Controls
11
12import org.kde.quickcharts as Charts
13import org.kde.quickcharts.controls
14
15/**
16 * A pre-made legend control that displays a legend for charts.
17 */
18Control {
19 id: control
21 /**
22 * The chart to display the legend for.
23 */
24 property Charts.Chart chart
25 /**
26 * The delegate to use to display legend information.
27 *
28 * \sa Legend::delegate
29 */
30 property alias delegate: legendRepeater.delegate
31 /**
32 *
33 */
34 property alias model: legendRepeater.model
35
36 property alias horizontalSpacing: legend.horizontalSpacing
37 property alias verticalSpacing: legend.verticalSpacing
38
39 property real maximumDelegateWidth: Theme.gridUnit * 10
40
41 property var formatValue: function(input, index) { return input }
42 property var maximumValueWidth: function(input, index) { return -1 }
43
44 property alias preferredWidth: legend.preferredWidth
45
46 property string nameRole: "name"
47 property string shortNameRole: "shortName"
48 property string colorRole: "color"
49 property string valueRole: "value"
50
51 property bool highlightEnabled: false
52 property int highlightedIndex: -1
53
54 default property alias _children: legend.children
55
56 leftPadding: 0
57 rightPadding: 0
58 topPadding: 0
59 bottomPadding: 0
60
61 implicitWidth: Math.max(implicitContentWidth, implicitBackgroundWidth) + leftPadding + rightPadding
62 implicitHeight: Math.max(implicitContentHeight, implicitBackgroundHeight) + topPadding + bottomPadding
63
64 contentItem: Flickable {
65 anchors.fill: parent
66
67 contentHeight: legend.implicitHeight
68 clip: true
69 boundsBehavior: Flickable.StopAtBounds
70
71 implicitHeight: legend.implicitHeight
72 implicitWidth: legend.implicitWidth
73
74 // Limit maximum flick velocity to ensure we can scroll one line per
75 // mouse wheel "tick" when the legend's height is very constrained.
76 maximumFlickVelocity: Theme.gridUnit * 50
77 LegendLayout {
78 id: legend
79
80 width: parent.width
81
82 Repeater {
83 id: legendRepeater
84
85 model: LegendModel { chart: control.chart }
86
87 delegate: LegendDelegate {
88 property var itemData: typeof modelData !== "undefined" ? modelData : model
89
90 name: itemData[control.nameRole] ?? ""
91 shortName: itemData[control.shortNameRole] ?? ""
92 color: itemData[control.colorRole] ?? "white"
93 value: control.formatValue(itemData[control.valueRole] ?? "", index)
94
95 highlighted: control.highlightEnabled && hovered
96
97 maximumValueWidth: {
98 var result = control.maximumValueWidth(model.value, index)
99 if (result > 0) {
100 return result
101 }
102
103 return -1
104 }
105
106 LegendLayout.minimumWidth: minimumWidth
107 LegendLayout.preferredWidth: preferredWidth
108 LegendLayout.maximumWidth: Math.max(control.maximumDelegateWidth, preferredWidth)
109
110 onHoveredChanged: {
111 if (control.highlightEnabled) {
112 if (hovered) {
113 control.highlightedIndex = index
114 } else if (control.highlightedIndex == index) {
115 control.highlightedIndex = -1
116 }
117 }
118 }
119 }
120 }
121
122 horizontalSpacing: Theme.smallSpacing
123 verticalSpacing: Theme.smallSpacing
124 }
125
126 children: [
127 Item {
128 width: parent.width;
129 height: 1;
130 visible: parent.contentY > 0
131
132 ToolButton {
133 anchors {
134 horizontalCenter: parent.horizontalCenter
135 top: parent.top
136 }
137
138 width: Theme.smallIconSize
139 height: Theme.smallIconSize
140
141 icon.name: "arrow-up-symbolic"
142 icon.width: Theme.smallIconSize
143 icon.height: Theme.smallIconSize
144 enabled: false
145 }
146 },
147 Item {
148 y: parent.height - height
149 width: parent.width;
150 height: 1;
151 visible: parent.contentY + parent.height < legend.height
152
153 ToolButton {
154 anchors {
155 horizontalCenter: parent.horizontalCenter
156 bottom: parent.bottom
157 }
158
159 width: Theme.smallIconSize
160 height: Theme.smallIconSize
161
162 icon.name: "arrow-down-symbolic"
163 icon.width: Theme.smallIconSize
164 icon.height: Theme.smallIconSize
165 enabled: false
166 }
167 }
168 ]
169 }
170}
A delegate that can be used as part of a Legend.
A model that extracts information from a chart that can be displayed as a legend.
Definition LegendModel.h:31
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Oct 11 2024 12:10:05 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.