LegendItem.qml Example File
qmllegend/qml/qmllegend/LegendItem.qml
import QtQuick 2.1
import QtQuick.Layouts 1.0
import QtQuick.Window 2.1
import QtDataVisualization 1.0
Rectangle {
property Theme3D theme
property Bar3DSeries series
property point previousSelection
id: legendItem
state: "unselected"
opacity: 0.999
RowLayout {
anchors.fill: parent
spacing: 0
clip: true
Item {
id: markerSpace
Layout.minimumWidth: 20
Layout.minimumHeight: 20
Layout.fillWidth: true
Layout.fillHeight: true
Layout.alignment: Qt.AlignVCenter
Rectangle {
x: parent.x + parent.width / 4
y: parent.y + parent.height / 4
width: parent.width / 2
height: width
border.color: "black"
color: series.baseColor
}
}
Item {
height: markerSpace.height
Layout.fillWidth: true
Layout.fillHeight: true
Layout.alignment: Qt.AlignVCenter
Layout.minimumWidth: 100
Text {
anchors.fill: parent
text: series.name
verticalAlignment: Text.AlignVCenter
clip: true
color: theme.labelTextColor
font: theme.font
}
}
}
MouseArea {
id: mouseArea
anchors.fill: legendItem
onClicked: {
if (legendItem.state === "selected") {
series.selectedBar = series.invalidSelectionPosition
} else {
series.selectedBar = previousSelection
}
}
}
Connections {
target: series
onSelectedBarChanged: {
if (position != series.invalidSelectionPosition) {
previousSelection = position
}
}
}
states: [
State {
name: "selected"
when: series.selectedBar != series.invalidSelectionPosition
PropertyChanges {
target: legendItem
color: series.singleHighlightColor
}
},
State {
name: "unselected"
when: series.selectedBar == series.invalidSelectionPosition
PropertyChanges {
target: legendItem
color: theme.labelBackgroundColor
}
}
]
}