Warning, /frameworks/kquickcharts/controls/Legend.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 2.13
0009 import QtQuick.Layouts 1.13
0010 import QtQuick.Controls 2.13
0011 
0012 import org.kde.quickcharts 1.0 as Charts
0013 import org.kde.quickcharts.controls 1.0
0014 
0015 /**
0016  * A pre-made legend control that displays a legend for charts.
0017  */
0018 Control {
0019     id: control
0020 
0021     /**
0022      * The chart to display the legend for.
0023      */
0024     property Charts.Chart chart
0025     /**
0026      * The delegate to use to display legend information.
0027      *
0028      * \sa Legend::delegate
0029      */
0030     property alias delegate: legendRepeater.delegate
0031     /**
0032      *
0033      */
0034     property alias model: legendRepeater.model
0035 
0036     property alias horizontalSpacing: legend.horizontalSpacing
0037     property alias verticalSpacing: legend.verticalSpacing
0038 
0039     property real maximumDelegateWidth: Theme.gridUnit * 10
0040 
0041     property var formatValue: function(input, index) { return input }
0042     property var maximumValueWidth: function(input, index) { return -1 }
0043 
0044     property alias preferredWidth: legend.preferredWidth
0045 
0046     property string nameRole: "name"
0047     property string shortNameRole: "shortName"
0048     property string colorRole: "color"
0049     property string valueRole: "value"
0050 
0051     property int flow
0052     onFlowChanged: Logging.deprecated("Legend", "flow", "5.82", "Legend uses a dynamic column layout now")
0053     property int sourceIndex
0054     onSourceIndexChanged: Logging.deprecated("Legend", "sourceIndex", "5.82", "Use Chart's indexingMode property")
0055 
0056     property bool valueVisible: false
0057     onValueVisibleChanged: Logging.deprecated("Legend", "valueVisible", "5.82", "Customise the delegate instead")
0058     property real valueWidth: -1
0059     onValueWidthChanged: Logging.deprecated("Legend", "valueWidth", "5.82", "Customise the delegate instead")
0060     property bool colorVisible: true
0061     onColorVisibleChanged: Logging.deprecated("Legend", "colorVisible", "5.82", "Customise the delegate instead")
0062     property real colorWidth: 0
0063     onColorWidthChanged: Logging.deprecated("Legend", "colorWidth", "5.82", "Customise the delegate instead")
0064 
0065     default property alias _children: legend.children
0066 
0067     leftPadding: 0
0068     rightPadding: 0
0069     topPadding: 0
0070     bottomPadding: 0
0071 
0072     implicitWidth: Math.max(implicitContentWidth, implicitBackgroundWidth) + leftPadding + rightPadding
0073     implicitHeight: Math.max(implicitContentHeight, implicitBackgroundHeight) + topPadding + bottomPadding
0074 
0075     contentItem: Flickable {
0076         anchors.fill: parent
0077 
0078         contentHeight: legend.implicitHeight
0079         clip: true
0080         boundsBehavior: Flickable.StopAtBounds
0081 
0082         implicitHeight: legend.implicitHeight
0083         implicitWidth: legend.implicitWidth
0084 
0085         // Limit maximum flick velocity to ensure we can scroll one line per
0086         // mouse wheel "tick" when the legend's height is very constrained.
0087         maximumFlickVelocity: Theme.gridUnit * 50
0088         Charts.LegendLayout {
0089             id: legend
0090 
0091             width: parent.width
0092 
0093             Repeater {
0094                 id: legendRepeater
0095 
0096                 model: Charts.LegendModel { chart: control.chart }
0097 
0098                 delegate: LegendDelegate {
0099                     property var itemData: typeof modelData !== "undefined" ? modelData : model
0100 
0101                     name: itemData[control.nameRole] !== undefined ? itemData[control.nameRole] : ""
0102                     shortName: itemData[control.shortNameRole] !== undefined ? itemData[control.shortNameRole] : ""
0103                     color: itemData[control.colorRole] !== undefined ? itemData[control.colorRole] : "white"
0104                     value: itemData[control.valueRole] !== undefined ? control.formatValue(itemData[control.valueRole], index) : ""
0105 
0106                     maximumValueWidth: {
0107                         var result = control.maximumValueWidth(model.value, index)
0108                         if (result > 0) {
0109                             return result
0110                         }
0111 
0112                         // Backward compatibility: While valueWidth is deprecated, it may still end up
0113                         // being set as a size hint, so we should use that rather than nothing if it is
0114                         // set.
0115                         if (control.valueWidth > 0) {
0116                             return control.valueWidth
0117                         }
0118 
0119                         return -1
0120                     }
0121 
0122                     Charts.LegendLayout.minimumWidth: minimumWidth
0123                     Charts.LegendLayout.preferredWidth: preferredWidth
0124                     Charts.LegendLayout.maximumWidth: Math.max(control.maximumDelegateWidth, preferredWidth)
0125                 }
0126             }
0127 
0128             horizontalSpacing: Theme.largeSpacing
0129             verticalSpacing: Theme.smallSpacing
0130         }
0131 
0132         children: [
0133             Item {
0134                 width: parent.width;
0135                 height: 1;
0136                 visible: parent.contentY > 0
0137 
0138                 ToolSeparator {
0139                     anchors.left: parent.horizontalCenter
0140                     anchors.top: parent.bottom
0141                     width: 1
0142                     height: Theme.smallSpacing
0143                     transformOrigin: Item.Top
0144                     rotation: 45
0145                 }
0146 
0147                 ToolSeparator {
0148                     anchors.right: parent.horizontalCenter
0149                     anchors.top: parent.bottom
0150                     width: 1
0151                     height: Theme.smallSpacing
0152                     transformOrigin: Item.Top
0153                     rotation: -45
0154                 }
0155             },
0156             Item {
0157                 y: parent.height - height
0158                 width: parent.width;
0159                 height: 1;
0160                 visible: parent.contentY + parent.height < legend.height
0161 
0162                 ToolSeparator {
0163                     anchors.left: parent.horizontalCenter
0164                     anchors.bottom: parent.top
0165                     width: 1
0166                     height: Theme.smallSpacing
0167                     transformOrigin: Item.Bottom
0168                     rotation: 45
0169                 }
0170 
0171                 ToolSeparator {
0172                     anchors.right: parent.horizontalCenter
0173                     anchors.bottom: parent.top
0174                     width: 1
0175                     height: Theme.smallSpacing
0176                     transformOrigin: Item.Bottom
0177                     rotation: -45
0178                 }
0179             }
0180         ]
0181     }
0182 }