File: LegendDelegate.qml

package info (click to toggle)
kquickcharts 5.116.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 880 kB
  • sloc: cpp: 6,146; sh: 48; makefile: 11
file content (127 lines) | stat: -rw-r--r-- 4,298 bytes parent folder | download | duplicates (2)
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
/*
 * This file is part of KQuickCharts
 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
 *
 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
 */

import QtQuick 2.12
import QtQuick.Layouts 1.12
import QtQuick.Controls 2.12

import org.kde.quickcharts 1.0 as Charts
import org.kde.quickcharts.controls 1.0

/**
 * A delegate that can be used as part of a Legend.
 */
Control {
    id: control

    property string name
    property string shortName
    property color color
    property string value

    property real maximumValueWidth

    property Component indicator: Rectangle {
        implicitWidth: Theme.smallSpacing
        color: control.color
    }

    readonly property real minimumWidth: contentItem.minimumWidth
    readonly property real preferredWidth: contentItem.preferredWidth

    property real valueWidth
    onValueWidthChanged: Logging.deprecated("LegendDelegate", "valueWidth", "5.82", "Use maximumValueWidth instead")
    property real colorWidth
    onColorWidthChanged: Logging.deprecated("LegendDelegate", "colorWidth", "5.82", "Customize the indicator instead")
    property color valueColor
    onValueColorChanged: Logging.deprecated("LegendDelegate", "valueColor", "5.82", "Customize the delegate instead")
    property bool colorVisible: true
    onColorVisibleChanged: Logging.deprecated("LegendDelegate", "colorVisible", "5.82", "Use an empty indicator instead")
    property bool valueVisible: true
    onValueVisibleChanged: Logging.deprecated("LegendDelegate", "valueVisible", "5.82", "Customize the delegate instead")
    property real layoutWidth
    onLayoutWidthChanged: Logging.deprecated("LegendDelegate", "layoutWidth", "5.82", "Unused")

    implicitHeight: Math.max(implicitContentHeight, implicitBackgroundHeight) + topPadding + bottomPadding

    // Note: Do not use implicitContentWidth here as it indirectly depends on the item width and will lead to a
    // nasty infinite layout loop. Instead use something more stable that doesn't change depending on the item
    // width.
    implicitWidth: Math.max(contentItem.preferredWidth, implicitBackgroundWidth) + leftPadding + rightPadding

    hoverEnabled: true

    leftPadding: 0
    rightPadding: 0
    topPadding: 0
    bottomPadding: 0

    spacing: Theme.smallSpacing

    contentItem: RowLayout {
        property real actualValueWidth: control.maximumValueWidth > 0 ? control.maximumValueWidth : value.implicitWidth
        property real minimumValueWidth: control.width - indicator.width - control.spacing

        property real minimumWidth: indicator.width + actualValueWidth + control.spacing
        property real preferredWidth: indicator.width + labelContainer.implicitWidth + actualValueWidth + control.spacing * 2

        spacing: control.spacing

        Loader {
            id: indicator

            Layout.preferredWidth: item ? item.implicitWidth : 0
            Layout.fillHeight: true

            sourceComponent: control.colorVisible ? control.indicator : null
        }

        Item {
            id: labelContainer

            Layout.fillHeight: true
            Layout.fillWidth: true

            implicitWidth: metrics.advanceWidth(control.name)

            Label {
                id: name
                anchors.fill: parent
                text: control.name + (control.shortName.length > 0 ? "\x9C" + control.shortName : "")
                elide: Text.ElideRight
                font: control.font
                verticalAlignment: Qt.AlignVCenter
            }

            FontMetrics {
                id: metrics
                font: control.font
            }
        }

        Label {
            id: value

            Layout.fillHeight: true
            Layout.fillWidth: true

            Layout.minimumWidth: Math.min(parent.actualValueWidth, parent.minimumValueWidth)

            text: control.value;
            elide: Text.ElideRight
            font: name.font

            verticalAlignment: Qt.AlignVCenter
            horizontalAlignment: Qt.AlignRight
        }
    }

    ToolTip.visible: control.hovered && (name.truncated || value.truncated)
    ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
    ToolTip.text: "%1: %2".arg(control.name).arg(control.value)
}