Warning, /network/neochat/src/qml/ImageEditorPage.qml is written in an unsupported language. File is not indexed.

0001 // SPDX-FileCopyrightText: 2020 Carl Schwan <carl@carlschwan.eu>
0002 // SPDX-License-Identifier: BSD-2-Clause
0003 
0004 import QtQuick
0005 import QtQuick.Controls as QQC2
0006 import QtQuick.Layouts
0007 import Qt.labs.platform as Platform
0008 
0009 import org.kde.kirigami as Kirigami
0010 import org.kde.kirigamiaddons.labs.components as KirigamiComponents
0011 import org.kde.kquickimageeditor as KQuickImageEditor
0012 
0013 Kirigami.Page {
0014     id: rootEditorView
0015 
0016     property bool resizing: false
0017     required property string imagePath
0018 
0019     signal newPathChanged(string newPath)
0020 
0021     title: i18n("Edit")
0022     leftPadding: 0
0023     rightPadding: 0
0024     topPadding: 0
0025     bottomPadding: 0
0026 
0027     function crop() {
0028         const ratioX = editImage.paintedWidth / editImage.nativeWidth;
0029         const ratioY = editImage.paintedHeight / editImage.nativeHeight;
0030         rootEditorView.resizing = false;
0031         imageDoc.crop(selectionTool.selectionX / ratioX, selectionTool.selectionY / ratioY, selectionTool.selectionWidth / ratioX, selectionTool.selectionHeight / ratioY);
0032     }
0033 
0034     actions: [
0035         Kirigami.Action {
0036             id: undoAction
0037             text: i18nc("@action:button Undo modification", "Undo")
0038             icon.name: "edit-undo"
0039             onTriggered: imageDoc.undo()
0040             visible: imageDoc.edited
0041         },
0042         Kirigami.Action {
0043             id: okAction
0044             text: i18nc("@action:button Accept image modification", "Accept")
0045             icon.name: "dialog-ok"
0046             onTriggered: {
0047                 let newPath = Platform.StandardPaths.writableLocation(Platform.StandardPaths.CacheLocation) + "/" + (new Date()).getTime() + "." + imagePath.split('.').pop();
0048                 if (imageDoc.saveAs(newPath)) {
0049                     newPathChanged(newPath);
0050                 } else {
0051                     msg.type = Kirigami.MessageType.Error;
0052                     msg.text = i18n("Unable to save file. Check if you have the correct permission to edit the cache directory.");
0053                     msg.visible = true;
0054                 }
0055             }
0056         }
0057     ]
0058 
0059     KQuickImageEditor.ImageItem {
0060         id: editImage
0061         // Assigning this to the contentItem and setting the padding causes weird positioning issues
0062         anchors.fill: parent
0063         anchors.margins: Kirigami.Units.gridUnit
0064         fillMode: KQuickImageEditor.ImageItem.PreserveAspectFit
0065         image: imageDoc.image
0066 
0067         Shortcut {
0068             sequence: StandardKey.Undo
0069             onActivated: undoAction.trigger()
0070         }
0071 
0072         Shortcut {
0073             sequences: [StandardKey.Save, "Enter"]
0074             onActivated: saveAction.trigger()
0075         }
0076 
0077         Shortcut {
0078             sequence: StandardKey.SaveAs
0079             onActivated: saveAsAction.trigger()
0080         }
0081 
0082         KQuickImageEditor.ImageDocument {
0083             id: imageDoc
0084             path: rootEditorView.imagePath
0085         }
0086 
0087         KQuickImageEditor.SelectionTool {
0088             id: selectionTool
0089             visible: rootEditorView.resizing
0090             width: editImage.paintedWidth
0091             height: editImage.paintedHeight
0092             x: editImage.horizontalPadding
0093             y: editImage.verticalPadding
0094             KQuickImageEditor.CropBackground {
0095                 anchors.fill: parent
0096                 z: -1
0097                 insideX: selectionTool.selectionX
0098                 insideY: selectionTool.selectionY
0099                 insideWidth: selectionTool.selectionWidth
0100                 insideHeight: selectionTool.selectionHeight
0101             }
0102             Connections {
0103                 target: selectionTool.selectionArea
0104                 function onDoubleClicked() {
0105                     rootEditorView.crop();
0106                 }
0107             }
0108         }
0109         onImageChanged: {
0110             selectionTool.selectionX = 0;
0111             selectionTool.selectionY = 0;
0112             selectionTool.selectionWidth = Qt.binding(() => selectionTool.width);
0113             selectionTool.selectionHeight = Qt.binding(() => selectionTool.height);
0114         }
0115     }
0116 
0117     header: QQC2.ToolBar {
0118         contentItem: Kirigami.ActionToolBar {
0119             id: actionToolBar
0120             display: QQC2.Button.TextBesideIcon
0121             actions: [
0122                 Kirigami.Action {
0123                     icon.name: rootEditorView.resizing ? "dialog-cancel" : "transform-crop"
0124                     text: rootEditorView.resizing ? i18n("Cancel") : i18nc("@action:button Crop an image", "Crop")
0125                     onTriggered: {
0126                         resizeRectangle.width = editImage.paintedWidth;
0127                         resizeRectangle.height = editImage.paintedHeight;
0128                         resizeRectangle.x = editImage.horizontalPadding;
0129                         resizeRectangle.y = editImage.verticalPadding;
0130                         resizeRectangle.insideX = 100;
0131                         resizeRectangle.insideY = 100;
0132                         resizeRectangle.insideWidth = 100;
0133                         resizeRectangle.insideHeight = 100;
0134                         rootEditorView.resizing = !rootEditorView.resizing;
0135                     }
0136                 },
0137                 Kirigami.Action {
0138                     icon.name: "dialog-ok"
0139                     visible: rootEditorView.resizing
0140                     text: i18nc("@action:button Crop an image", "Crop")
0141                     onTriggered: rootEditorView.crop()
0142                 },
0143                 Kirigami.Action {
0144                     icon.name: "object-rotate-left"
0145                     text: i18nc("@action:button Rotate an image to the left", "Rotate left")
0146                     onTriggered: imageDoc.rotate(-90)
0147                     visible: !rootEditorView.resizing
0148                 },
0149                 Kirigami.Action {
0150                     icon.name: "object-rotate-right"
0151                     text: i18nc("@action:button Rotate an image to the right", "Rotate right")
0152                     onTriggered: imageDoc.rotate(90)
0153                     visible: !rootEditorView.resizing
0154                 },
0155                 Kirigami.Action {
0156                     icon.name: "object-flip-vertical"
0157                     text: i18nc("@action:button Mirror an image vertically", "Flip")
0158                     onTriggered: imageDoc.mirror(false, true)
0159                     visible: !rootEditorView.resizing
0160                 },
0161                 Kirigami.Action {
0162                     icon.name: "object-flip-horizontal"
0163                     text: i18nc("@action:button Mirror an image horizontally", "Mirror")
0164                     onTriggered: imageDoc.mirror(true, false)
0165                     visible: !rootEditorView.resizing
0166                 }
0167             ]
0168         }
0169     }
0170 
0171     footer: KirigamiComponents.Banner {
0172         id: msg
0173         type: Kirigami.MessageType.Error
0174         showCloseButton: true
0175         visible: false
0176     }
0177 }