Warning, /frameworks/kquickcharts/controls/LegendDelegate.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * This file is part of KQuickCharts
0003  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  *
0005  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0006  */
0007 
0008 import QtQuick
0009 import QtQuick.Layouts
0010 import QtQuick.Controls
0011 
0012 import org.kde.quickcharts as Charts
0013 import org.kde.quickcharts.controls
0014 
0015 /**
0016  * A delegate that can be used as part of a Legend.
0017  */
0018 Control {
0019     id: control
0020 
0021     property string name
0022     property string shortName
0023     property color color
0024     property string value
0025 
0026     property real maximumValueWidth
0027 
0028     property Component indicator: Rectangle {
0029         implicitWidth: Theme.smallSpacing
0030         color: control.color
0031     }
0032 
0033     readonly property real minimumWidth: contentItem.minimumWidth
0034     readonly property real preferredWidth: contentItem.preferredWidth
0035 
0036     implicitHeight: Math.max(implicitContentHeight, implicitBackgroundHeight) + topPadding + bottomPadding
0037 
0038     // Note: Do not use implicitContentWidth here as it indirectly depends on the item width and will lead to a
0039     // nasty infinite layout loop. Instead use something more stable that doesn't change depending on the item
0040     // width.
0041     implicitWidth: Math.max(contentItem.preferredWidth, implicitBackgroundWidth) + leftPadding + rightPadding
0042 
0043     hoverEnabled: true
0044 
0045     leftPadding: 0
0046     rightPadding: 0
0047     topPadding: 0
0048     bottomPadding: 0
0049 
0050     spacing: Theme.smallSpacing
0051 
0052     contentItem: RowLayout {
0053         property real actualValueWidth: control.maximumValueWidth > 0 ? control.maximumValueWidth : value.implicitWidth
0054         property real minimumValueWidth: control.width - indicator.width - control.spacing
0055 
0056         property real minimumWidth: indicator.width + actualValueWidth + control.spacing
0057         property real preferredWidth: indicator.width + labelContainer.implicitWidth + actualValueWidth + control.spacing * 2
0058 
0059         spacing: control.spacing
0060 
0061         Loader {
0062             id: indicator
0063 
0064             Layout.preferredWidth: item ? item.implicitWidth : 0
0065             Layout.fillHeight: true
0066 
0067             sourceComponent: control.indicator
0068         }
0069 
0070         Item {
0071             id: labelContainer
0072 
0073             Layout.fillHeight: true
0074             Layout.fillWidth: true
0075 
0076             implicitWidth: metrics.advanceWidth(control.name)
0077 
0078             Label {
0079                 id: name
0080                 anchors.fill: parent
0081                 text: control.name + (control.shortName.length > 0 ? "\x9C" + control.shortName : "")
0082                 elide: Text.ElideRight
0083                 font: control.font
0084                 verticalAlignment: Qt.AlignVCenter
0085             }
0086 
0087             FontMetrics {
0088                 id: metrics
0089                 font: control.font
0090             }
0091         }
0092 
0093         Label {
0094             id: value
0095 
0096             Layout.fillHeight: true
0097             Layout.fillWidth: true
0098 
0099             Layout.minimumWidth: Math.min(parent.actualValueWidth, parent.minimumValueWidth)
0100 
0101             text: control.value;
0102             elide: Text.ElideRight
0103             font: name.font
0104 
0105             verticalAlignment: Qt.AlignVCenter
0106             horizontalAlignment: Qt.AlignRight
0107         }
0108     }
0109 
0110     ToolTip.visible: control.hovered && (name.truncated || value.truncated)
0111     ToolTip.delay: Qt.styleHints.mousePressAndHoldInterval
0112     ToolTip.text: "%1: %2".arg(control.name).arg(control.value)
0113 }
0114