3.9 KiB · text History 7a88816
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls as QQC2
import org.kde.plasma.components as PlasmaComponents3
import org.kde.kirigami as Kirigami

import "Grafana.js" as G

// The dashboard's variables as pickers, like the bar along the top of a
// Grafana dashboard. Multi-value ones keep their menu open while you tick.
// Picks are remembered per dashboard.
Flow {
    id: bar

    required property var store
    required property string uid

    readonly property var vars: (store.dashRev, store.variablesFor(uid))
    readonly property var shown: {
        var out = []
        for (var name in vars) {
            var v = vars[name]
            // hide: 0 shown, 1 label hidden, 2 variable hidden
            if (v.raw.hide === 2 || v.raw.type === "constant") continue
            out.push(name)
        }
        return out
    }
    readonly property bool hasVariables: shown.length > 0

    spacing: Kirigami.Units.smallSpacing

    Repeater {
        model: bar.shown

        delegate: PlasmaComponents3.ToolButton {
            id: button

            required property string modelData
            readonly property var variable: bar.vars[modelData]
            readonly property bool multi: !!variable && !!variable.raw.multi

            text: (variable && variable.raw.hide === 1 ? "" : (variable.raw.label || modelData) + ": ")
                  + (variable ? G.selectionText(variable) : "")
            icon.name: "arrow-down"
            display: QQC2.AbstractButton.TextBesideIcon
            LayoutMirroring.enabled: true
            onClicked: menu.popup(button, 0, button.height)

            PlasmaComponents3.ToolTip { text: button.variable && button.variable.raw.description ? button.variable.raw.description : button.text }

            QQC2.Menu {
                id: menu

                // working copy for multi-select, committed when the menu closes
                property var picked: []

                onAboutToShow: picked = [].concat(button.variable.selected)
                onClosed: {
                    if (!button.multi) return
                    var was = JSON.stringify([].concat(button.variable.selected))
                    if (JSON.stringify(picked) !== was) bar.store.setVariable(bar.uid, button.modelData, picked.length ? picked : ["$__all"])
                }

                Instantiator {
                    model: button.variable ? button.variable.options : []
                    onObjectAdded: function(index, object) { menu.insertItem(index, object) }
                    onObjectRemoved: function(index, object) { menu.removeItem(object) }

                    delegate: QQC2.MenuItem {
                        required property var modelData
                        text: modelData.text
                        checkable: true
                        checked: menu.picked.indexOf(modelData.value) !== -1
                        onTriggered: {
                            if (!button.multi) {
                                bar.store.setVariable(bar.uid, button.modelData, modelData.value)
                                return
                            }
                            var v = modelData.value
                            var list = menu.picked.slice()
                            if (v === "$__all") {
                                list = ["$__all"]
                            } else {
                                list = list.filter(function(x) { return x !== "$__all" })
                                var at = list.indexOf(v)
                                if (at === -1) list.push(v)
                                else list.splice(at, 1)
                            }
                            menu.picked = list
                            // keep the menu up while ticking several
                            Qt.callLater(function() { menu.open() })
                        }
                    }
                }
            }
        }
    }
}