KQuickCharts

LegendDelegate.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 delegate that can be used as part of a Legend.
17 */
18Control {
19 id: control
20
21 property string name
22 property string shortName
23 property color color
24 property string value
25
26 property real maximumValueWidth
27
28 property Component indicator: Rectangle {
29 implicitWidth: Theme.cornerRadius
30 color: control.color
31 }
32
33 property bool highlighted: false
34
35 readonly property real minimumWidth: contentItem.minimumWidth
36 readonly property real preferredWidth: contentItem.preferredWidth
37
38 implicitHeight: Math.max(implicitContentHeight, implicitBackgroundHeight) + topPadding + bottomPadding
39
40 // Note: Do not use implicitContentWidth here as it indirectly depends on the item width and will lead to a
41 // nasty infinite layout loop. Instead use something more stable that doesn't change depending on the item
42 // width.
43 implicitWidth: Math.max(contentItem.preferredWidth, implicitBackgroundWidth) + leftPadding + rightPadding
44
45 hoverEnabled: true
46
47 leftPadding: 0
48 rightPadding: Theme.cornerRadius
49 topPadding: 0
50 bottomPadding: 0
51
52 spacing: Theme.smallSpacing
53
54 contentItem: RowLayout {
55 property real actualValueWidth: control.maximumValueWidth > 0 ? control.maximumValueWidth : value.implicitWidth
56 property real minimumValueWidth: control.width - indicator.width - control.spacing
57
58 property real minimumWidth: indicator.width + actualValueWidth + control.spacing
59 property real preferredWidth: Math.ceil(indicator.width) + Math.ceil(name.implicitWidth) + Math.ceil(actualValueWidth) + Math.ceil(control.spacing * 2) + control.leftPadding + control.rightPadding
60
61 spacing: control.spacing
62
63 Loader {
64 id: indicator
65
66 Layout.preferredWidth: item ? item.implicitWidth : 0
67 Layout.fillHeight: true
68
69 sourceComponent: control.indicator
70 }
71
72 Label {
73 id: name
74
75 Layout.fillWidth: true
76 Layout.fillHeight: true
77
78 text: control.name + (control.shortName.length > 0 ? "\x9C" + control.shortName : "")
79 elide: Text.ElideRight
80 font: control.font
81 verticalAlignment: Qt.AlignVCenter
82 }
83
84 Label {
85 id: value
86
87 Layout.fillHeight: true
88 Layout.fillWidth: true
89
90 Layout.minimumWidth: Math.min(parent.actualValueWidth, parent.minimumValueWidth)
91
92 text: control.value;
93 elide: Text.ElideRight
94 font: name.font
95
96 verticalAlignment: Qt.AlignVCenter
97 horizontalAlignment: Qt.AlignRight
98 }
99 }
100
101 background: Rectangle {
102 color: Qt.alpha(control.color, 0.25)
103 border.color: control.color
104 border.width: 1
105 radius: Theme.cornerRadius
106 visible: control.highlighted
107 }
108
109 ToolTip.visible: control.hovered && (name.truncated || value.truncated)
110 ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
111 ToolTip.text: "%1: %2".arg(control.name).arg(control.value)
112}
113
QString name(StandardAction id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:54:36 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.