2.5 KiB · text History cc97953
import QtQuick
import QtQuick.Layouts
import org.kde.plasma.components as PlasmaComponents3
import org.kde.kirigami as Kirigami

import "Lambent.js" as L

// Point a running machine at a device. Named "<machine>-<device>" and on
// every channel; the web UI has the full form for anything finer.
RowLayout {
    id: row

    required property var store

    // only rebuilt when the set of names changes, so an open dropdown
    // isn't reset by every publish
    property var machineNames: []
    property var deviceMacs: []
    property var deviceLabels: []

    function refreshChoices() {
        var names = Object.keys(store.machines).sort()
        if (JSON.stringify(names) !== JSON.stringify(machineNames)) machineNames = names

        var focus = store.focusDef
        var macs = (focus ? focus.macs : Object.keys(store.devices)).filter(function(m) { return !!store.devices[m] })
        macs.sort(function(a, b) {
            return L.deviceLabel(store.devices[a], a).localeCompare(L.deviceLabel(store.devices[b], b))
        })
        var labels = macs.map(function(m) {
            var d = store.devices[m]
            return L.deviceLabel(d, m) + (d && d.place && !focus ? " (" + d.place + ")" : "")
        })
        if (JSON.stringify(macs) !== JSON.stringify(deviceMacs) || JSON.stringify(labels) !== JSON.stringify(deviceLabels)) {
            deviceMacs = macs
            deviceLabels = labels
        }
    }

    Connections {
        target: row.store
        function onRevChanged() { row.refreshChoices() }
    }
    Component.onCompleted: refreshChoices()

    spacing: Kirigami.Units.smallSpacing
    enabled: store.connected && machineNames.length > 0 && deviceMacs.length > 0

    PlasmaComponents3.ComboBox {
        id: source
        Layout.fillWidth: true
        Layout.preferredWidth: 1
        model: row.machineNames
        displayText: currentIndex >= 0 ? currentText : i18n("machine")
    }

    PlasmaComponents3.Label { text: "→" }

    PlasmaComponents3.ComboBox {
        id: target
        Layout.fillWidth: true
        Layout.preferredWidth: 1
        model: row.deviceLabels
    }

    PlasmaComponents3.Button {
        icon.name: "list-add"
        text: i18n("Link")
        enabled: source.currentIndex >= 0 && target.currentIndex >= 0
        onClicked: row.store.createLink(row.machineNames[source.currentIndex], row.deviceMacs[target.currentIndex])
        PlasmaComponents3.ToolTip { text: i18n("Drive this device with this machine, on every channel") }
    }
}