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 // In some cases we can trigger a behaviour where the full text is
84 // displayed while we have a size of 0. In that case, explicitly
85 // hide it. This uses opacity to ensure the item is still begin laid
86 // out.
87 opacity: width > 0 ? 1 : 0
88 }
89
90 Label {
91 id: value
92
93 Layout.fillHeight: true
94 Layout.fillWidth: true
95
96 Layout.minimumWidth: Math.min(parent.actualValueWidth, parent.minimumValueWidth)
97
98 text: control.value;
99 elide: Text.ElideRight
100 font: name.font
101
102 verticalAlignment: Qt.AlignVCenter
103 horizontalAlignment: Qt.AlignRight
104 }
105 }
106
107 background: Rectangle {
108 color: Qt.alpha(control.color, 0.25)
109 border.color: control.color
110 border.width: 1
111 radius: Theme.cornerRadius
112 visible: control.highlighted
113 }
114
115 ToolTip.visible: control.hovered && (name.truncated || value.truncated)
116 ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
117 ToolTip.text: "%1: %2".arg(control.name).arg(control.value)
118}
119
QString name(GameStandardAction id)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:03:45 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.