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

import "Grafana.js" as G

// The popup (or the desktop widget): pick a dashboard, its variables and
// time range, and see it drawn on its own grid.
ColumnLayout {
    id: view

    required property var store

    readonly property var entry: (store.dashRev, store.dashboards[store.shownUid] || null)
    readonly property bool ready: store.hasValidConfig && store.hasDashboards

    Layout.minimumWidth: Kirigami.Units.gridUnit * 24
    Layout.minimumHeight: Kirigami.Units.gridUnit * 18
    Layout.preferredWidth: Kirigami.Units.gridUnit * 50
    Layout.preferredHeight: Kirigami.Units.gridUnit * 36
    spacing: Kirigami.Units.smallSpacing

    // ── header ──────────────────────────────────────────────────────────
    RowLayout {
        Layout.fillWidth: true
        spacing: Kirigami.Units.smallSpacing
        visible: view.ready

        PlasmaComponents3.ComboBox {
            id: picker
            Layout.fillWidth: true
            Layout.maximumWidth: Kirigami.Units.gridUnit * 22
            model: view.store.dashboardList
            textRole: "title"
            currentIndex: {
                for (var i = 0; i < view.store.dashboardList.length; i++)
                    if (view.store.dashboardList[i].uid === view.store.shownUid) return i
                return 0
            }
            onActivated: function(index) { view.store.selectDashboard(view.store.dashboardList[index].uid) }
        }

        Item { Layout.fillWidth: true }

        PlasmaComponents3.ComboBox {
            id: rangePicker
            readonly property var choices: ["", "now-15m", "now-1h", "now-3h", "now-6h", "now-12h", "now-24h", "now-2d", "now-7d", "now-30d"]
            Layout.preferredWidth: Kirigami.Units.gridUnit * 9
            model: choices.map(function(c) {
                return c === "" ? i18n("Dashboard: %1", G.rangeLabel(view.entry && view.entry.dashboard.time ? view.entry.dashboard.time : {})) : G.rangeLabel({ from: c, to: "now" })
            })
            currentIndex: Math.max(0, choices.indexOf(view.store.timeOverride))
            onActivated: function(index) { Plasmoid.configuration.timeRange = choices[index] }
            PlasmaComponents3.ToolTip { text: i18n("Time range") }
        }

        PlasmaComponents3.BusyIndicator {
            visible: view.store.anyLoading
            running: visible
            Layout.preferredWidth: Kirigami.Units.iconSizes.smallMedium
            Layout.preferredHeight: Kirigami.Units.iconSizes.smallMedium
        }

        PlasmaComponents3.ToolButton {
            icon.name: "view-refresh"
            onClicked: view.store.refreshDashboard(view.store.shownUid, true, false)
            PlasmaComponents3.ToolTip { text: i18n("Refresh") }
        }

        PlasmaComponents3.ToolButton {
            icon.name: "internet-web-browser"
            onClicked: Qt.openUrlExternally(view.store.api.dashboardUrl(view.store.shownUid))
            PlasmaComponents3.ToolTip { text: i18n("Open in Grafana") }
        }

        PlasmaComponents3.ToolButton {
            icon.name: "configure"
            onClicked: Plasmoid.internalAction("configure").trigger()
            PlasmaComponents3.ToolTip { text: i18n("Configure") }
        }
    }

    VariableBar {
        visible: view.ready && hasVariables
        Layout.fillWidth: true
        store: view.store
        uid: view.store.shownUid
    }

    PlasmaComponents3.Label {
        visible: view.store.lastError !== ""
        Layout.fillWidth: true
        wrapMode: Text.WordWrap
        color: Kirigami.Theme.negativeTextColor
        font: Kirigami.Theme.smallFont
        text: view.store.lastError
    }

    // ── setup prompts ───────────────────────────────────────────────────
    ColumnLayout {
        visible: !view.ready
        Layout.fillWidth: true
        Layout.fillHeight: true
        spacing: Kirigami.Units.largeSpacing

        Item { Layout.fillHeight: true }

        Kirigami.Icon {
            source: view.store.logoSource
            isMask: true
            color: Kirigami.Theme.disabledTextColor
            Layout.alignment: Qt.AlignHCenter
            Layout.preferredWidth: Kirigami.Units.iconSizes.huge
            Layout.preferredHeight: Kirigami.Units.iconSizes.huge
        }

        PlasmaComponents3.Label {
            Layout.fillWidth: true
            horizontalAlignment: Text.AlignHCenter
            wrapMode: Text.WordWrap
            text: !view.store.hasValidConfig ? i18n("Connect the widget to your Grafana with a service account token")
                                            : i18n("Pick the dashboards to show")
        }

        PlasmaComponents3.Button {
            Layout.alignment: Qt.AlignHCenter
            icon.name: "configure"
            text: !view.store.hasValidConfig ? i18n("Configure Connection") : i18n("Choose Dashboards")
            onClicked: Plasmoid.internalAction("configure").trigger()
        }

        Item { Layout.fillHeight: true }
    }

    DashboardView {
        visible: view.ready
        Layout.fillWidth: true
        Layout.fillHeight: true
        store: view.store
        uid: view.store.shownUid
    }
}