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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
// SPDX-FileCopyrightText: 2024 Mathis BrĂ¼chert <mbb@kaidan.im>
// SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
import QtQuick
import QtQuick.Controls as Controls
import QtQuick.Layouts
import org.kde.kirigami as Kirigami
import org.kde.kirigamiaddons.delegates as Delegates
import org.kde.kirigamiaddons.components as Components
import QtQuick.Templates as T
import org.kde.marknote
import "components"
Controls.Dialog {
id: root
property string notePath
property alias imagePath: saveButton.imagePath
parent: applicationWindow().overlay
modal: true
width: Math.min(900, parent.width)
height: 600
leftPadding: 1
rightPadding: 1
bottomPadding: 1
topPadding: 0
anchors.centerIn: applicationWindow().overlay
background: Components.DialogRoundedBackground {}
Canvas {
id: canvas
anchors.fill: parent
property real lastX
property real lastY
property color color: "black"
property int strokeWidth: 2
property bool erase: eraserButton.checked
property bool mousePressed: false;
property var drawing: []; // array of stroke objects
property var currentStroke: ({
points: [],
color: "",
width: 0,
isEraser: false
}); // stroke object that contains points, color, width
property bool repaintRequired: false;
function undo(): void {
if (mousePressed) {
return;
}
if (history.undoAvailable){
history.undoStroke();
drawing.pop();
canvas.repaintRequired = true;
canvas.requestPaint();
}
}
function redo(): void {
if (mousePressed) {
return;
}
if (history.redoAvailable) {
let cppStroke = history.redoStroke();
let jsStroke = {
points: cppStroke.points,
color: cppStroke.color.toString(),
width: cppStroke.width,
isEraser: cppStroke.isEraser
}
drawing.push(jsStroke);
canvas.repaintRequired = true;
canvas.requestPaint();
}
}
function makeStroke(): void {
if (mousePressed) {
return;
}
history.submitStroke(currentStroke.points, currentStroke.color, currentStroke.width, currentStroke.isEraser);
drawing.push(currentStroke);
}
HistoryController {
id: history
}
onPaint: {
var ctx = getContext('2d')
if (!repaintRequired) {
if (canvas.erase === true) {
ctx.globalCompositeOperation = 'destination-out'
}
else {
ctx.globalCompositeOperation = 'source-over'
}
ctx.lineWidth = canvas.strokeWidth
ctx.strokeStyle = canvas.color
ctx.lineCap = "round"
ctx.beginPath()
ctx.moveTo(lastX, lastY)
currentStroke.points.push(Qt.vector2d(lastX, lastY))
lastX = area.mouseX
lastY = area.mouseY
ctx.lineTo(lastX, lastY)
ctx.stroke()
}
else{
ctx.reset();
ctx.lineCap = "round"
for (const stroke of drawing){
ctx.globalCompositeOperation = stroke.isEraser === true ? 'destination-out' : 'source-over';
ctx.lineWidth = stroke.width
ctx.strokeStyle = stroke.color
for (let i=0; i<stroke.points.length-1; i++){
ctx.beginPath();
ctx.moveTo(stroke.points[i].x, stroke.points[i].y);
ctx.lineTo(stroke.points[i+1].x, stroke.points[i+1].y);
ctx.stroke();
}
}
}
}
MouseArea {
id: area
anchors.fill: parent
onPressed: {
canvas.mousePressed = true;
canvas.lastX = mouseX
canvas.lastY = mouseY
canvas.currentStroke = {
points: [],
color: canvas.color.toString(),
width: canvas.strokeWidth,
isEraser: canvas.erase
}
}
onReleased: {
canvas.mousePressed = false;
canvas.currentStroke.points.push(Qt.vector2d(canvas.lastX, canvas.lastY));
canvas.makeStroke();
}
onPositionChanged: {
canvas.repaintRequired = false;
canvas.requestPaint()
}
}
}
Components.FloatingToolBar {
id: colorToolBarContainer
z: 600000
anchors {
top: parent.top
margins: Kirigami.Units.largeSpacing
horizontalCenter: parent.horizontalCenter
}
contentItem: RowLayout {
id: colorLayout
Controls.ButtonGroup {
buttons: colorLayout.children
}
Controls.ToolButton {
id: undoButton
implicitHeight: Kirigami.Units.gridUnit * 2
autoExclusive: false
checkable: false
background.visible: true
enabled: history.undoAvailable;
display: Controls.AbstractButton.IconOnly
icon.name: "edit-undo-symbolic"
onClicked: canvas.undo()
}
Controls.ToolButton {
id: redoButton
implicitHeight: Kirigami.Units.gridUnit * 2
autoExclusive: false
checkable: false
background.visible: true
enabled: history.redoAvailable;
display: Controls.AbstractButton.IconOnly
icon.name: "edit-redo-symbolic"
onClicked: canvas.redo()
}
Controls.ToolButton {
id: eraserButton
implicitHeight: Kirigami.Units.gridUnit * 2
autoExclusive: true
checkable: true
background.visible: false
contentItem: Item {
width: height
Kirigami.ShadowedRectangle {
anchors.centerIn: parent
color: "white"
radius: Kirigami.Units.mediumSpacing
width: eraserButton.checked ? parent.width - 10 : parent.width - 4
height: width
border{
width: 1
color: Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.2)
}
shadow {
size: 5
yOffset: 3
color: Qt.rgba(0, 0, 0, 0.2)
}
Behavior on width {
NumberAnimation {
duration: Kirigami.Units.shortDuration
easing.type: Easing.InOutQuart
}
}
}
}
}
Repeater {
model: ListModel {
ListElement { color: "#1A1B1D" }
ListElement { color: "#de324c" }
ListElement { color: "#f4895f" }
ListElement { color: "#f8e16f" }
ListElement { color: "#95cf92" }
ListElement { color: "#369acc" }
ListElement { color: "#9656a2" }
}
Controls.ToolButton {
id: delegate
implicitHeight: Kirigami.Units.gridUnit * 2
autoExclusive: true
checkable: true
onClicked: canvas.color = color
required property string color
background.visible: false
contentItem: Item {
width: height
Kirigami.ShadowedRectangle {
anchors.centerIn: parent
color: delegate.color
radius: Kirigami.Units.mediumSpacing
width: delegate.checked ? parent.width - 10 : parent.width - 4
height: width
border{
width: 1
color: Kirigami.ColorUtils.tintWithAlpha(Kirigami.Theme.backgroundColor, Kirigami.Theme.textColor, 0.2)
}
shadow {
size: 5
yOffset: 3
color: Qt.rgba(0, 0, 0, 0.2)
}
Behavior on width {
NumberAnimation {
duration: Kirigami.Units.shortDuration
easing.type: Easing.InOutQuart
}
}
}
}
}
}
}
}
Components.FloatingToolBar {
id: widthToolBarContainer
z: 600000
anchors {
left: parent.left
verticalCenter: parent.verticalCenter
margins: Kirigami.Units.largeSpacing
}
contentItem: ColumnLayout {
id: widthLayout
Controls.ButtonGroup {
buttons: widthLayout.children
}
Repeater {
model: ListModel {
ListElement { strokeWidth: 2 }
ListElement { strokeWidth: 4 }
ListElement { strokeWidth: 6 }
ListElement { strokeWidth: 8 }
}
Controls.ToolButton {
id: widthDelegate
implicitHeight: Kirigami.Units.gridUnit * 2
autoExclusive: true
checkable: true
onClicked: canvas.strokeWidth = strokeWidth
required property int strokeWidth
contentItem: Item {
width: height
Kirigami.ShadowedRectangle {
anchors.centerIn: parent
color: Kirigami.Theme.textColor
radius: 200
width: widthDelegate.strokeWidth * 2
height: width
Behavior on width {
NumberAnimation {
duration: Kirigami.Units.shortDuration
easing.type: Easing.InOutQuart
}
}
}
}
}
}
}
}
Components.FloatingToolBar {
id: bottomToolBarContainer
z: 600000
anchors {
bottom: parent.bottom
horizontalCenter: parent.horizontalCenter
margins: Kirigami.Units.largeSpacing
}
contentItem: RowLayout{
Controls.ToolButton {
text: "Cancel"
icon.name: "dialog-close"
onClicked:{
root.close()
}
}
Controls.ToolButton {
id: saveButton
property string imagePath
text: "Save"
icon.name: "answer-correct"
onClicked:{
var notepath = root.notePath.slice(7, -3)/*.replace(" ", "_")*/
imagePath = notepath + Math.random() * 1000 +".png"
print(canvas.save(imagePath))
root.close()
}
}
}
}
onClosed: {
history.reset();
canvas.drawing = [];
canvas.repaintRequired = true;
canvas.requestPaint();
}
}
|