foxygit / TV-Launcher Log in
commits tags

/qml/ImagePicker.qml · 3.55 KB

raw
import QtQuick
import QtQuick.Controls
import TvLauncher

// Välj en bild bland ~/Bilder, ~/Pictures och systemets bakgrundsmappar.
Popup {
    id: picker

    signal picked(string path)

    property var images: []

    parent: Overlay.overlay
    anchors.centerIn: parent
    width: parent.width - Theme.dp(160)
    height: parent.height - Theme.dp(140)
    modal: true
    focus: true
    padding: Theme.dp(36)

    background: Rectangle { color: Theme.alpha(Theme.surface, 0.97); radius: Theme.r(18); border.color: Theme.borderSoft }
    Overlay.modal: Rectangle { color: Theme.scrim }

    onAboutToShow: images = prefs.findWallpapers()
    onOpened: grid.forceActiveFocus()

    contentItem: Item {
        Text {
            id: title
            text: "Välj bild"
            color: Theme.text
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(36)
        }
        Text {
            anchors { left: title.right; leftMargin: Theme.dp(24); baseline: title.baseline }
            text: "Bilder från ~/Bilder och ~/Pictures"
            color: Theme.textDim
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(22)
        }

        Text {
            anchors.centerIn: parent
            visible: picker.images.length === 0
            width: parent.width * 0.7
            horizontalAlignment: Text.AlignHCenter
            wrapMode: Text.Wrap
            text: "Inga bilder hittades.\nLägg bilder (jpg/png) i ~/Bilder, eller skriv en sökväg eller webbadress i fältet."
            color: Theme.textDim
            font.family: Theme.fontFamily
            font.pixelSize: Theme.dp(26)
        }

        GridView {
            id: grid
            anchors { left: parent.left; right: parent.right; top: title.bottom; topMargin: Theme.dp(24); bottom: parent.bottom }
            clip: true
            model: picker.images
            readonly property int columns: 4
            cellWidth: Math.floor(width / columns)
            cellHeight: Math.round(cellWidth * 0.62)
            keyNavigationWraps: false
            highlightMoveDuration: 120

            delegate: Item {
                id: cell
                required property string modelData
                required property int index
                width: grid.cellWidth
                height: grid.cellHeight
                readonly property bool current: GridView.isCurrentItem && grid.activeFocus

                function choose() { picker.close(); picker.picked(modelData) }
                Keys.onReturnPressed: choose()
                Keys.onEnterPressed: choose()

                Rectangle {
                    anchors.fill: parent
                    anchors.margins: Theme.dp(8)
                    radius: Theme.r(10)
                    color: Theme.card
                    border.width: cell.current ? Theme.dp(4) : 0
                    border.color: Theme.accent
                    clip: true
                    Image {
                        anchors.fill: parent
                        anchors.margins: cell.current ? Theme.dp(4) : 0
                        source: "file://" + cell.modelData
                        sourceSize.width: 400
                        fillMode: Image.PreserveAspectCrop
                        asynchronous: true
                    }
                }
                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    onEntered: { grid.currentIndex = cell.index; grid.forceActiveFocus() }
                    onClicked: cell.choose()
                }
            }
        }
    }
}