2014-11-14 04:21:14 +03:00
|
|
|
/*
|
2021-06-05 20:54:54 +03:00
|
|
|
Copyright (C) 2014-2021 Andrea Scarpino <andrea@scarpino.dev>
|
2014-11-14 04:21:14 +03:00
|
|
|
All rights reserved.
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import QtQuick 2.0
|
|
|
|
import Sailfish.Silica 1.0
|
|
|
|
|
|
|
|
Page {
|
|
|
|
|
2014-11-15 21:09:49 +03:00
|
|
|
readonly property real defaultStrokeSize: 5
|
2014-11-25 21:45:57 +03:00
|
|
|
readonly property real defaultRubberSize: 20
|
2014-11-29 14:23:28 +03:00
|
|
|
readonly property color defaultStrokeColor: Qt.rgba(0, 0, 0, 1)
|
|
|
|
readonly property color defaultFillColor: Qt.rgba(1, 1, 1, 1)
|
2014-11-14 04:21:14 +03:00
|
|
|
|
|
|
|
Column {
|
|
|
|
anchors.fill: parent
|
|
|
|
|
|
|
|
Row {
|
|
|
|
id: menu
|
2016-06-03 23:59:10 +03:00
|
|
|
spacing: Theme.paddingMedium
|
2014-11-25 21:45:57 +03:00
|
|
|
width: parent.width
|
|
|
|
// Workaround: we don't want the Slider animation to resize this!
|
|
|
|
height: Theme.itemSizeMedium
|
2014-11-14 04:21:14 +03:00
|
|
|
|
2016-06-03 23:59:10 +03:00
|
|
|
IconButton {
|
2015-07-03 18:19:19 +03:00
|
|
|
id: edit
|
2014-11-25 21:45:57 +03:00
|
|
|
icon.source: "image://theme/icon-s-edit"
|
2014-11-14 21:44:39 +03:00
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
2016-06-03 23:59:10 +03:00
|
|
|
width: 50
|
2014-11-25 21:45:57 +03:00
|
|
|
|
2014-11-14 21:44:39 +03:00
|
|
|
// default value for the rubber
|
2014-11-25 21:45:57 +03:00
|
|
|
property real prevLineWidth: defaultRubberSize;
|
2014-11-14 21:44:39 +03:00
|
|
|
|
2016-06-03 23:59:10 +03:00
|
|
|
onClicked: {
|
2014-11-29 14:23:28 +03:00
|
|
|
if (canvas.strokeStyle == defaultStrokeColor) {
|
2014-11-25 21:45:57 +03:00
|
|
|
icon.source = "image://theme/icon-camera-focus"
|
2014-11-15 21:09:49 +03:00
|
|
|
canvas.strokeStyle = defaultFillColor;
|
2014-11-14 21:44:39 +03:00
|
|
|
} else {
|
2014-11-25 21:45:57 +03:00
|
|
|
icon.source = "image://theme/icon-s-edit"
|
2014-11-15 21:09:49 +03:00
|
|
|
canvas.strokeStyle = defaultStrokeColor;
|
2014-11-14 21:44:39 +03:00
|
|
|
}
|
|
|
|
|
2014-11-16 12:24:24 +03:00
|
|
|
// Remember the stroke/rubber size
|
2014-11-14 21:44:39 +03:00
|
|
|
var currentLineWidth = size.value;
|
|
|
|
size.value = prevLineWidth;
|
|
|
|
prevLineWidth = currentLineWidth;
|
2014-11-14 04:21:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-03 23:59:10 +03:00
|
|
|
IconButton {
|
|
|
|
id: save
|
|
|
|
icon.source: "image://theme/icon-m-image"
|
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
width: 50
|
|
|
|
|
|
|
|
function pictureName() {
|
|
|
|
var dateTime = new Date();
|
|
|
|
return Qt.formatDateTime(dateTime, "yyyy-MM-dd-hh-mm-ss") + ".jpeg";
|
|
|
|
}
|
|
|
|
|
|
|
|
onClicked: {
|
2017-10-27 00:28:16 +03:00
|
|
|
remorseSave.execute(menu, qsTr("Saving the canvas…"), function() {
|
2016-06-03 23:59:10 +03:00
|
|
|
canvas.save(papocchioDir + pictureName());
|
|
|
|
}, 3000);
|
|
|
|
}
|
|
|
|
|
|
|
|
RemorseItem {
|
|
|
|
id: remorseSave
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 21:44:39 +03:00
|
|
|
Slider {
|
|
|
|
id: size
|
|
|
|
minimumValue: 1
|
|
|
|
maximumValue: 30
|
|
|
|
stepSize: 1
|
2014-11-15 21:09:49 +03:00
|
|
|
value: defaultStrokeSize
|
2014-11-14 21:44:39 +03:00
|
|
|
valueText: value
|
2016-06-03 23:59:10 +03:00
|
|
|
width: parent.width - edit.width - save.width - clearBtn.width - quit.width - (Theme.paddingMedium * 4)
|
2014-11-25 21:45:57 +03:00
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
|
|
|
|
|
|
|
// Don't waste space
|
|
|
|
leftMargin: 0
|
|
|
|
rightMargin: 0
|
2014-11-14 21:44:39 +03:00
|
|
|
|
|
|
|
onValueChanged: {
|
|
|
|
valueText = canvas.lineWidth = value;
|
2014-11-14 04:21:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-29 14:23:28 +03:00
|
|
|
IconButton {
|
2016-06-03 23:59:10 +03:00
|
|
|
id: clearBtn
|
|
|
|
icon.source: "image://theme/icon-m-clear"
|
2014-11-29 14:23:28 +03:00
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
2016-06-03 23:59:10 +03:00
|
|
|
width: 50
|
2014-11-29 14:23:28 +03:00
|
|
|
|
|
|
|
onClicked: {
|
2017-10-27 00:28:16 +03:00
|
|
|
remorseClear.execute(menu, qsTr("Clearing the canvas…"), function() {
|
2016-06-03 23:59:10 +03:00
|
|
|
canvas.clear();
|
2014-11-29 14:23:28 +03:00
|
|
|
}, 3000);
|
|
|
|
}
|
|
|
|
|
|
|
|
RemorseItem {
|
2016-06-03 23:59:10 +03:00
|
|
|
id: remorseClear
|
2014-11-29 14:23:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-14 04:21:14 +03:00
|
|
|
IconButton {
|
2016-06-03 23:59:10 +03:00
|
|
|
id: quit
|
|
|
|
icon.source: "image://theme/icon-m-close"
|
2014-11-14 21:44:39 +03:00
|
|
|
anchors.verticalCenter: parent.verticalCenter
|
2016-06-03 23:59:10 +03:00
|
|
|
width: 50
|
2014-11-14 21:44:39 +03:00
|
|
|
|
2014-11-14 04:21:14 +03:00
|
|
|
onClicked: {
|
2017-10-27 00:28:16 +03:00
|
|
|
remorseQuit.execute(menu, qsTr("Quitting…"), function() {
|
2016-06-03 23:59:10 +03:00
|
|
|
Qt.quit();
|
|
|
|
}, 1000);
|
2014-11-16 12:24:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RemorseItem {
|
2016-06-03 23:59:10 +03:00
|
|
|
id: remorseQuit
|
2014-11-14 04:21:14 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Rectangle {
|
2014-11-14 21:44:39 +03:00
|
|
|
height: parent.height - menu.height
|
2014-11-14 04:21:14 +03:00
|
|
|
width: parent.width
|
|
|
|
|
|
|
|
Canvas {
|
|
|
|
id: canvas
|
|
|
|
anchors.fill: parent
|
|
|
|
antialiasing: true
|
|
|
|
|
2014-11-25 21:45:57 +03:00
|
|
|
property int startX
|
|
|
|
property int startY
|
|
|
|
property int finishX
|
|
|
|
property int finishY
|
|
|
|
|
2014-11-29 14:23:28 +03:00
|
|
|
property bool initialize: true
|
|
|
|
|
2014-11-15 21:09:49 +03:00
|
|
|
property real lineWidth: defaultStrokeSize
|
|
|
|
property string strokeStyle: defaultStrokeColor
|
2014-11-14 04:21:14 +03:00
|
|
|
|
|
|
|
onLineWidthChanged: requestPaint()
|
|
|
|
|
2014-11-29 14:23:28 +03:00
|
|
|
function clear() {
|
2014-11-15 21:01:30 +03:00
|
|
|
var ctx = getContext("2d");
|
2014-11-29 14:23:28 +03:00
|
|
|
ctx.fillStyle = defaultFillColor;
|
2014-11-15 21:01:30 +03:00
|
|
|
ctx.fillRect(0, 0, width, height);
|
|
|
|
requestPaint();
|
|
|
|
}
|
|
|
|
|
2014-11-14 04:21:14 +03:00
|
|
|
onPaint: {
|
|
|
|
var ctx = getContext("2d");
|
2014-11-29 14:23:28 +03:00
|
|
|
|
|
|
|
// The context is not available in Component.onCompleted
|
|
|
|
// then we have to use a "exec-once workaround" to init
|
|
|
|
// the canvas background
|
|
|
|
if (initialize) {
|
|
|
|
clear();
|
|
|
|
initialize = false;
|
|
|
|
}
|
|
|
|
|
2014-11-14 04:21:14 +03:00
|
|
|
ctx.lineCap = "round";
|
2014-11-14 21:44:39 +03:00
|
|
|
ctx.lineJoin = "round";
|
2014-11-14 04:21:14 +03:00
|
|
|
ctx.lineWidth = lineWidth;
|
2014-11-14 21:44:39 +03:00
|
|
|
ctx.miterLimit = 1;
|
|
|
|
ctx.strokeStyle = strokeStyle;
|
2014-11-14 04:21:14 +03:00
|
|
|
|
2014-11-15 21:01:30 +03:00
|
|
|
ctx.beginPath();
|
|
|
|
ctx.moveTo(startX, startY);
|
|
|
|
ctx.lineTo(finishX, finishY);
|
|
|
|
ctx.closePath();
|
|
|
|
ctx.stroke();
|
|
|
|
|
|
|
|
startX = finishX;
|
|
|
|
startY = finishY;
|
2014-11-14 04:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MouseArea {
|
|
|
|
anchors.fill: parent
|
|
|
|
|
|
|
|
onPressed: {
|
2014-11-25 21:45:57 +03:00
|
|
|
parent.startX = parent.finishX = mouseX;
|
|
|
|
parent.startY = parent.finishY = mouseY;
|
2014-11-14 04:21:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
onMouseXChanged: {
|
2014-11-25 21:45:57 +03:00
|
|
|
parent.finishX = mouseX;
|
2014-11-14 04:21:14 +03:00
|
|
|
parent.requestPaint();
|
|
|
|
}
|
|
|
|
|
|
|
|
onMouseYChanged: {
|
2014-11-25 21:45:57 +03:00
|
|
|
parent.finishY = mouseY;
|
2014-11-14 04:21:14 +03:00
|
|
|
parent.requestPaint();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|