1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
|
/*
* SPDX-FileCopyrightText: (C) 2020 Carl Schwan <carl@carlschwan.eu>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
import QtCore
import QtQuick
import QtQuick.Controls as QQC2
import QtQuick.Layouts
import org.kde.kirigami as Kirigami
import QtQuick.Dialogs
import org.kde.kquickimageeditor as KQuickImageEditor
Kirigami.ApplicationWindow {
id: root
Component.onCompleted: {
pageStack.layers.push(editorComponent);
}
pageStack.initialPage: Kirigami.Page {
QQC2.Button {
text: "Open Editor"
onClicked: root.pageStack.layers.push(editorComponent);
}
}
Component {
id: editorComponent
Kirigami.Page {
id: rootEditorView
property bool resizing: false;
property string imagePath: '/home/carl/kde6/usr/share/wallpapers/Next/contents/images/5120x2880.png'
signal imageEdited();
title: "Edit"
leftPadding: 0
rightPadding: 0
topPadding: 0
bottomPadding: 0
function crop() {
rootEditorView.resizing = false
imageDoc.crop(selectionTool.selectionX / editImage.ratioX,
selectionTool.selectionY / editImage.ratioY,
selectionTool.selectionWidth / editImage.ratioX,
selectionTool.selectionHeight / editImage.ratioY);
}
actions: [
Kirigami.Action {
id: saveAction
visible: imageDoc.edited
text: "Save"
icon.name: "document-save"
onTriggered: {
if (!imageDoc.save()) {
msg.type = Kirigami.MessageType.Error
msg.text = "Unable to save file. Check if you have the correct permission to edit this file."
msg.visible = true;
}
rootEditorView.imageEdited();
applicationWindow().pageStack.layers.pop();
}
},
Kirigami.Action {
id: undoAction
text: "Undo"
icon.name: "edit-undo"
onTriggered: {
if (imageDoc.edited) {
imageDoc.undo();
}
}
visible: imageDoc.edited
},
Kirigami.Action {
icon.name: rootEditorView.resizing ? "dialog-ok" : "transform-crop"
text: rootEditorView.resizing ? "Accept" : "Crop"
onTriggered: rootEditorView.resizing = !rootEditorView.resizing;
},
Kirigami.Action {
icon.name: "dialog-cancel"
visible: rootEditorView.resizing
text: "Cancel"
onTriggered: rootEditorView.resizing = !rootEditorView.resizing
},
Kirigami.Action {
icon.name: "object-rotate-left"
text: "Rotate left";
onTriggered: imageDoc.rotate(-90);
visible: !rootEditorView.resizing
},
Kirigami.Action {
icon.name: "object-rotate-right"
text: "@action:button Rotate an image to the right", "Rotate right";
onTriggered: imageDoc.rotate(90);
visible: !rootEditorView.resizing
},
Kirigami.Action {
icon.name: "object-flip-vertical"
text: "Flip"
onTriggered: imageDoc.mirror(false, true);
visible: !rootEditorView.resizing
},
Kirigami.Action {
icon.name: "object-flip-horizontal"
text: "Mirror"
onTriggered: imageDoc.mirror(true, false);
visible: !rootEditorView.resizing
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: QQC2.ToolSeparator {
leftPadding: Kirigami.Units.largeSpacing
rightPadding: leftPadding
}
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: QQC2.ComboBox {
id: mode
model: [
SelTool.Free,
SelTool.Square
]
displayText: {
switch (currentValue) {
case SelTool.Free: return 'Free';
case SelTool.Square: return 'Square';
}
}
delegate: QQC2.ItemDelegate {
width: mode.width
text: {
switch (model.modelData) {
case SelTool.Free: return 'Free';
case SelTool.Square: return 'Square';
}
}
highlighted: mode.highlightedIndex === model.index
onClicked: mode.currentIndex = model.index
}
onCurrentValueChanged: selectionTool.aspectRatio = currentValue
}
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: QQC2.Label {
text: "Size:"
}
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: EditorSpinBox {
minimumContentWidth: widthTextMetrics.width
from: 1
to: editImage.nativeWidth
value: selectionTool.selectionWidth / editImage.ratioX
onValueModified: selectionTool.selectionWidth = value * editImage.ratioX
}
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: EditorSpinBox {
minimumContentWidth: heightTextMetrics.width
from: 1
to: editImage.nativeHeight
value: selectionTool.selectionHeight / editImage.ratioY
onValueModified: selectionTool.selectionHeight = value * editImage.ratioY
}
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: Item {
implicitWidth: Kirigami.Units.largeSpacing
}
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: QQC2.Label {
text: "Position:"
}
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: EditorSpinBox {
minimumContentWidth: widthTextMetrics.width
from: 0
to: editImage.nativeWidth - (selectionTool.selectionWidth / editImage.ratioX)
value: selectionTool.selectionX / editImage.ratioX
onValueModified: selectionTool.selectionX = value * editImage.ratioX
}
},
Kirigami.Action {
visible: rootEditorView.resizing
displayComponent: EditorSpinBox {
minimumContentWidth: heightTextMetrics.width
from: 0
to: editImage.nativeHeight - (selectionTool.selectionHeight / editImage.ratioY)
value: selectionTool.selectionY / editImage.ratioY
onValueModified: selectionTool.selectionY = value * editImage.ratioY
}
}
]
TextMetrics {
id: widthTextMetrics
text: editImage.nativeWidth.toLocaleString(rootEditorView.locale, 'f', 0)
}
TextMetrics {
id: heightTextMetrics
text: editImage.nativeHeight.toLocaleString(rootEditorView.locale, 'f', 0)
}
component EditorSpinBox : QQC2.SpinBox {
id: control
property real minimumContentWidth: 0
contentItem: QQC2.TextField {
id: textField
implicitWidth: control.minimumContentWidth + leftPadding + rightPadding + 2
implicitHeight: Math.ceil(contentHeight) + topPadding + bottomPadding
palette: control.palette
leftPadding: control.spacing
rightPadding: control.spacing
topPadding: 0
bottomPadding: 0
text: control.displayText
font: control.font
color: Kirigami.Theme.textColor
selectionColor: Kirigami.Theme.highlightColor
selectedTextColor: Kirigami.Theme.highlightedTextColor
horizontalAlignment: Qt.AlignHCenter
verticalAlignment: Qt.AlignVCenter
readOnly: !control.editable
validator: control.validator
inputMethodHints: control.inputMethodHints
selectByMouse: true
background: null
}
}
FileDialog {
id: fileDialog
title: "Save As"
currentFolder: StandardPaths.standardLocations(StandardPaths.HomeLocation)[0]
onAccepted: {
if (imageDoc.saveAs(fileDialog.selectedFile)) {;
imagePath = fileDialog.selectedFile;
msg.type = Kirigami.MessageType.Information
msg.text = "You are now editing a new file."
msg.visible = true;
} else {
msg.type = Kirigami.MessageType.Error
msg.text = "Unable to save file. Check if you have the correct permission to edit this file."
msg.visible = true;
}
fileDialog.close()
}
onRejected: {
fileDialog.close()
}
Component.onCompleted: visible = false
}
KQuickImageEditor.ImageItem {
id: editImage
readonly property real ratioX: editImage.paintedWidth / editImage.nativeWidth;
readonly property real ratioY: editImage.paintedHeight / editImage.nativeHeight;
// Assigning this to the contentItem and setting the padding causes weird positioning issues
anchors.fill: parent
anchors.margins: Kirigami.Units.gridUnit
fillMode: KQuickImageEditor.ImageItem.PreserveAspectFit
image: imageDoc.image
Shortcut {
sequence: StandardKey.Undo
onActivated: undoAction.trigger();
}
Shortcut {
sequences: [StandardKey.Save, "Enter"]
onActivated: saveAction.trigger();
}
Shortcut {
sequence: StandardKey.SaveAs
onActivated: saveAsAction.trigger();
}
KQuickImageEditor.ImageDocument {
id: imageDoc
path: rootEditorView.imagePath
}
KQuickImageEditor.SelectionTool {
id: selectionTool
visible: rootEditorView.resizing
width: editImage.paintedWidth
height: editImage.paintedHeight
x: editImage.horizontalPadding
y: editImage.verticalPadding
KQuickImageEditor.CropBackground {
anchors.fill: parent
z: -1
insideX: selectionTool.selectionX
insideY: selectionTool.selectionY
insideWidth: selectionTool.selectionWidth
insideHeight: selectionTool.selectionHeight
}
Connections {
target: selectionTool.selectionArea
function onDoubleClicked() {
rootEditorView.crop()
}
}
}
onImageChanged: {
selectionTool.selectionX = 0
selectionTool.selectionY = 0
selectionTool.selectionWidth = Qt.binding(() => selectionTool.width)
selectionTool.selectionHeight = Qt.binding(() => selectionTool.height)
}
}
footer: Kirigami.InlineMessage {
id: msg
type: Kirigami.MessageType.Error
showCloseButton: true
position: Kirigami.InlineMessage.Footer
visible: false
}
}
}
}
|