// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL // A picked avatar or banner is usually bigger or a different shape than the // frame that will draw it, so the picker goes through here before committing: // the picture is shown as the frame will see it, the user drags a target-shaped // window over the area to keep, and the crop is loaded into the same settings // entry the raw file would have landed in. // // The window itself is the KDE selection framework (org.kde.kquickimageeditor): // SelectionTool owns the drag, the eight edge and corner handles, the clamping // inside the picture and the square lock for avatars; CropBackground dims the // cut-away part and draws the frame's outline. Nothing here fights a drag the // way a hand-rolled pan MouseArea inside OverlaySheet's ScrollView did. import QtQuick import QtQuick.Layouts import QtQuick.Controls as QQC2 import org.kde.kirigami as Kirigami import org.kde.kquickimageeditor import koutnet.app Kirigami.OverlaySheet { id: root // OverlaySheet computes its own implicitHeight from y, and y from // implicitHeight, which the engine reports as a binding loop on every open. // Pin it to a value that depends only on the window: the sheet still fits a // short window, and neither side of the pair feeds back into the other. implicitHeight: parent ? Math.min(parent.height - Kirigami.Units.gridUnit * 2, Kirigami.Units.gridUnit * 42) : Kirigami.Units.gridUnit * 42 property string sourcePath: "" // false is the square avatar crop, true the wide banner crop property bool wide: false signal applied(string outPath) // Avatars stay square at a size a peer card can page; a banner is a wide // strip cropped on top and bottom at display time no matter what is fed in. readonly property real kAspect: root.wide ? 3.0 : 1.0 readonly property int kTargetWidth: root.wide ? 1024 : 512 readonly property int kTargetHeight: root.wide ? 342 : 512 // The painted picture is the image scaled into the stage; the selection // lives in painted coordinates (it is a child of a frame the size of the // painted area itself) and the scale mapping turns that rectangle back into // the file's own pixels on Apply. readonly property bool imageReady: img.status === Image.Ready readonly property real imgX: root.imageReady ? (stage.width - img.paintedWidth) / 2 : 0 readonly property real imgY: root.imageReady ? (stage.height - img.paintedHeight) / 2 : 0 readonly property real scaleX: root.imageReady ? img.sourceSize.width / img.paintedWidth : 1 readonly property real scaleY: root.imageReady ? img.sourceSize.height / img.paintedHeight : 1 property bool cropFailed: false // Centres an area that fits the target shape on the painted picture. function settleSelection() { const pw = sel.width const ph = sel.height if (pw <= 0 || ph <= 0) return let w = Math.min(pw, ph * root.kAspect) let h = w / root.kAspect if (h > ph) { h = ph w = h * root.kAspect } // Set the frame the user actually sees and the committed rect // SelectionTool keeps for its handles, so the first gesture does not // snap the frame somewhere else. const area = sel.selectionArea area.x = (pw - w) / 2 area.y = (ph - h) / 2 area.width = w area.height = h sel.selectionX = area.x sel.selectionY = area.y sel.selectionWidth = w sel.selectionHeight = h root.cropFailed = false } function apply() { root.cropFailed = false const area = sel.selectionArea if (!root.imageReady || area.width <= 0 || area.height <= 0) return const x0 = Math.round(area.x * root.scaleX) const y0 = Math.round(area.y * root.scaleY) const w = Math.max(1, Math.round(area.width * root.scaleX)) const h = Math.max(1, Math.round(area.height * root.scaleY)) const out = imageCrop.writableImagePath(root.wide ? "banner-crop.png" : "avatar-crop.png") if (imageCrop.cropToFile(root.sourcePath, Qt.rect(x0, y0, w, h), root.kTargetWidth, root.kTargetHeight, out)) { root.applied(out) root.close() } else { root.cropFailed = true } } onOpened: Qt.callLater(root.settleSelection) header: ColumnLayout { Layout.fillWidth: true spacing: Kirigami.Units.smallSpacing Kirigami.Heading { level: 2 text: i18nc("@title:window", "Crop the picture") } QQC2.Label { Layout.fillWidth: true text: i18nc("@info", "Drag the window to move it, or pull an edge or a corner to change the size. The rest of the picture is cut away, not shrunk.") color: AppPalette.disabledText wrapMode: Text.WordWrap } } // OverlaySheet's docs require a single child item and forbid overriding // contentItem: the override breaks the sheet's size-hint wiring, leaving // implicitHeight to depend on y and vice versa (the binding loop), and it // kills the swipe-to-dismiss gesture. A plain child lands in the sheet's // own ScrollView, which drives those hints from Layout.preferredHeight. // The template sizes the sheet from the content's size hints (OverlaySheet // template lines 135-155). A plain size-hint-free ColumnLayout makes the // sheet compute from its own implicitHeight, a binding loop the engine // reports on every open. Fixing preferred sizes up front is the branch // the template takes first; the contents then lay out exactly once. ColumnLayout { Layout.preferredWidth: Kirigami.Units.gridUnit * 42 Layout.preferredHeight: Kirigami.Units.gridUnit * 32 spacing: Kirigami.Units.largeSpacing Item { id: stage Layout.fillWidth: true Layout.preferredHeight: Kirigami.Units.gridUnit * 30 clip: true Image { id: img anchors.fill: parent source: root.sourcePath fillMode: Image.PreserveAspectFit asynchronous: true onStatusChanged: if (status === Image.Ready) Qt.callLater(root.settleSelection) } // The selection only gains meaning once the image is in; before // that the stage holds nothing but a grey sheet. The frame pins // both the tool and the dimming to the painted area, so every // coordinate inside it is a painted coordinate and the scale // mapping on Apply needs no stage-offset correction. Item { id: frame x: root.imgX y: root.imgY width: root.imageReady ? img.paintedWidth : 0 height: root.imageReady ? img.paintedHeight : 0 visible: root.imageReady SelectionTool { id: sel anchors.fill: parent // Avatars stay square while the frame is dragged; a banner // is free, because the strip cuts its own box at display. aspectRatio: root.wide ? SelectionTool.AspectRatio.Free : SelectionTool.AspectRatio.Square } // Dims the part that is cut away and draws the frame's outline. // Painted below the selection so its handles stay crisp. CropBackground { anchors.fill: parent insideX: sel.selectionArea.x insideY: sel.selectionArea.y insideWidth: sel.selectionArea.width insideHeight: sel.selectionArea.height } } } Kirigami.InlineMessage { id: cropError Layout.fillWidth: true visible: root.cropFailed type: Kirigami.MessageType.Error text: i18nc("@info:status", "Could not read or save the cropped picture. Try another file.") } } footer: RowLayout { Layout.fillWidth: true spacing: Kirigami.Units.smallSpacing Item { Layout.fillWidth: true } QQC2.Button { text: i18nc("@action:button", "Cancel") onClicked: root.close() } QQC2.Button { text: i18nc("@action:button", "Use this area") icon.name: "dialog-ok" highlighted: true enabled: root.imageReady && sel.selectionArea.width > 0 && sel.selectionArea.height > 0 onClicked: root.apply() } } }