Warning, /maui/mauikit-filebrowsing/examples/KirigamiExample.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  * SPDX-FileCopyrightText: (C) 2020 Carl Schwan <carl@carlschwan.eu>
0003  *
0004  * SPDX-License-Identifier: BSD-2-Clause
0005  */
0006 
0007 import QtQuick 2.10
0008 import QtQuick.Controls 2.1 as QQC2
0009 import QtQuick.Layouts 1.12
0010 import org.kde.kirigami 2.12 as Kirigami
0011 import QtQuick.Dialogs 1.2
0012 import org.kde.kquickimageeditor 1.0 as KQuickImageEditor
0013 import QtGraphicalEffects 1.12
0014 
0015 Kirigami.ApplicationWindow {
0016     id: root
0017     Component.onCompleted: {
0018         pageStack.layers.push(editorComponent);
0019     }
0020     
0021     pageStack.initialPage: Kirigami.Page {
0022         QQC2.Button {
0023             text: "Open Editor"
0024             onClicked: root.pageStack.layers.push(editorComponent);
0025         }
0026     }
0027     
0028     Component { 
0029         id: editorComponent
0030         Kirigami.Page {
0031             id: rootEditorView
0032             title: i18nd("mauikitfilebrowsing", "Edit")
0033             leftPadding: 0
0034             rightPadding: 0
0035     
0036             Shortcut {
0037                 sequence: StandardKey.Undo
0038                 onActivated: undoAction.trigger();
0039             }
0040             
0041             Shortcut {
0042                 sequences: [StandardKey.Save, "Enter"]
0043                 onActivated: saveAction.trigger();
0044             }
0045             
0046             Shortcut {
0047                 sequence: StandardKey.SaveAs
0048                 onActivated: saveAsAction.trigger();
0049             }
0050 
0051             property bool resizing: false;
0052             property string imagePath: "/usr/share/wallpapers/Next/contents/images/5120x2880.jpg";
0053 
0054             function crop() {
0055                 const ratioX = editImage.paintedWidth / editImage.nativeWidth;
0056                 const ratioY = editImage.paintedHeight / editImage.nativeHeight;
0057                 rootEditorView.resizing = false
0058                 imageDoc.crop(resizeRectangle.insideX / ratioX, resizeRectangle.insideY / ratioY, resizeRectangle.insideWidth / ratioX, resizeRectangle.insideHeight / ratioY);
0059             }
0060 
0061             actions {
0062                 right: Kirigami.Action {
0063                     id: undoAction
0064                     text: i18nc("@action:button Undo modification", "Undo")
0065                     iconName: "edit-undo"
0066                     onTriggered: imageDoc.undo();
0067                     visible: imageDoc.edited
0068                 }
0069                 contextualActions: [
0070                     Kirigami.Action {
0071                         id: saveAction
0072                         text: i18nc("@action:button Save the image as a new image", "Save As")
0073                         iconName: "document-save-as"
0074                         onTriggered: fileDialog.visible = true;
0075                     },
0076                     Kirigami.Action {
0077                         id: saveAsAction
0078                         text: i18nc("@action:button Save the image", "Save")
0079                         iconName: "document-save"
0080                         onTriggered: {
0081                             if (!imageDoc.save()) {
0082                                 msg.type = Kirigami.MessageType.Error
0083                                 msg.text = i18nd("mauikitfilebrowsing", "Unable to save file. Check if you have the correct permission to edit this file.")
0084                                 msg.visible = true;
0085                             }
0086                         }
0087                         visible: imageDoc.edited
0088                     }
0089                 ]
0090             }
0091 
0092 
0093             FileDialog {
0094                 id: fileDialog
0095                 title: i18nd("mauikitfilebrowsing", "Save As")
0096                 folder: shortcuts.home
0097                 selectMultiple: false
0098                 selectExisting: false
0099                 onAccepted: {
0100                     if (imageDoc.saveAs(fileDialog.fileUrl)) {;
0101                         imagePath = fileDialog.fileUrl;
0102                         msg.type = Kirigami.MessageType.Information
0103                         msg.text = i18nd("mauikitfilebrowsing", "You are now editing a new file.")
0104                         msg.visible = true;
0105                     } else {
0106                         msg.type = Kirigami.MessageType.Error
0107                         msg.text = i18nd("mauikitfilebrowsing", "Unable to save file. Check if you have the correct permission to edit this file.")
0108                         msg.visible = true;
0109                     }
0110                     fileDialog.close()
0111                 }
0112                 onRejected: {
0113                     fileDialog.close()
0114                 }
0115                 Component.onCompleted: visible = false
0116             }
0117             
0118             KQuickImageEditor.ImageDocument {
0119                 id: imageDoc
0120                 path: rootEditorView.imagePath
0121             }
0122 
0123             KQuickImageEditor.ImageItem {
0124                 id: editImage
0125                 fillMode: KQuickImageEditor.ImageItem.PreserveAspectFit
0126                 image: imageDoc.image
0127                 anchors.fill: parent
0128             }
0129 
0130             header: QQC2.ToolBar {
0131                 contentItem: Kirigami.ActionToolBar {
0132                     id: actionToolBar
0133                     display: QQC2.Button.TextBesideIcon
0134                     actions: [
0135                         Kirigami.Action {
0136                             iconName: rootEditorView.resizing ? "dialog-cancel" : "transform-crop"
0137                             text: rootEditorView.resizing ? i18nd("mauikitfilebrowsing", "Cancel") : i18nc("@action:button Crop an image", "Crop");
0138                             onTriggered: rootEditorView.resizing = !rootEditorView.resizing;
0139                         },
0140                         Kirigami.Action {
0141                             iconName: "dialog-ok"
0142                             visible: rootEditorView.resizing
0143                             text: i18nc("@action:button Rotate an image to the right", "Crop");
0144                             onTriggered: rootEditorView.crop();
0145                         },
0146                         Kirigami.Action {
0147                             iconName: "object-rotate-left"
0148                             text: i18nc("@action:button Rotate an image to the left", "Rotate left");
0149                             onTriggered: imageDoc.rotate(-90);
0150                             visible: !rootEditorView.resizing
0151                         },
0152                         Kirigami.Action {
0153                             iconName: "object-rotate-right"
0154                             text: i18nc("@action:button Rotate an image to the right", "Rotate right");
0155                             onTriggered: imageDoc.rotate(90);
0156                             visible: !rootEditorView.resizing
0157                         },
0158                         Kirigami.Action {
0159                             iconName: "object-flip-vertical"
0160                             text: i18nc("@action:button Mirror an image vertically", "Flip");
0161                             onTriggered: imageDoc.mirror(false, true);
0162                             visible: !rootEditorView.resizing
0163                         },
0164                         Kirigami.Action {
0165                             iconName: "object-flip-horizontal"
0166                             text: i18nc("@action:button Mirror an image horizontally", "Mirror");
0167                             onTriggered: imageDoc.mirror(true, false);
0168                             visible: !rootEditorView.resizing
0169                         }
0170                     ]
0171                 }
0172             }
0173             
0174             footer: Kirigami.InlineMessage {
0175                 id: msg
0176                 type: Kirigami.MessageType.Error
0177                 showCloseButton: true
0178                 visible: false
0179             }
0180 
0181             KQuickImageEditor.ResizeRectangle {
0182                 id: resizeRectangle
0183 
0184                 visible: rootEditorView.resizing
0185 
0186                 width: editImage.paintedWidth
0187                 height: editImage.paintedHeight
0188                 x: 0
0189                 y: editImage.verticalPadding
0190                 
0191                 insideX: 100
0192                 insideY: 100
0193                 insideWidth: 100
0194                 insideHeight: 100
0195 
0196                 onAcceptSize: rootEditorView.crop();
0197                 
0198                 //resizeHandle: KQuickImageEditor.BasicResizeHandle { }
0199 
0200                 /*Rectangle {
0201                     radius: 2
0202                     width: Maui.Style.units.gridUnit * 8
0203                     height: Maui.Style.units.gridUnit * 3
0204                     anchors.centerIn: parent
0205                     Maui.Theme.colorSet: Maui.Theme.View
0206                     color: Maui.Theme.backgroundColor
0207                     QQC2.Label {
0208                         anchors.centerIn: parent
0209                         text: "x: " + (resizeRectangle.x - rootEditorView.contentItem.width + editImage.paintedWidth)
0210                             + " y: " +  (resizeRectangle.y - rootEditorView.contentItem.height + editImage.paintedHeight)
0211                             + "\nwidth: " + resizeRectangle.width
0212                             + " height: " + resizeRectangle.height
0213                     }
0214                 }*/
0215             }
0216         }
0217     }
0218 }