harbour-Papocchio/qml/pages/MainPage.qml

151 lines
4.5 KiB
QML
Raw Normal View History

2014-11-14 04:21:14 +03:00
/*
Copyright (C) 2014 Andrea Scarpino <me@andreascarpino.it>
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
property int startX
property int startY
property int finishX
property int finishY
readonly property real defaultStrokeSize: 5
readonly property string defaultStrokeColor: "#000000"
readonly property string defaultFillColor: "#ffffff"
2014-11-14 04:21:14 +03:00
Column {
anchors.fill: parent
Row {
id: menu
anchors.horizontalCenter: parent.horizontalCenter
spacing: 3
// Workaround: we don't want the Slider animation!
height: 95
2014-11-14 04:21:14 +03:00
IconButton {
icon.source: "image://theme/icon-camera-focus"
anchors.verticalCenter: parent.verticalCenter
// default value for the rubber
property real prevLineWidth: 20;
2014-11-14 04:21:14 +03:00
onClicked: {
2014-11-15 21:09:49 +03:00
if (canvas.strokeStyle === defaultStrokeColor) {
canvas.strokeStyle = defaultFillColor;
} else {
2014-11-15 21:09:49 +03:00
canvas.strokeStyle = defaultStrokeColor;
}
var currentLineWidth = size.value;
size.value = prevLineWidth;
prevLineWidth = currentLineWidth;
2014-11-14 04:21:14 +03:00
}
}
Slider {
id: size
minimumValue: 1
maximumValue: 30
stepSize: 1
2014-11-15 21:09:49 +03:00
value: defaultStrokeSize
valueText: value
width: 400
// Workaround: we don't want the Slider animation!
height: 120
onValueChanged: {
valueText = canvas.lineWidth = value;
2014-11-14 04:21:14 +03:00
}
}
IconButton {
icon.source: "image://theme/icon-m-clear"
anchors.verticalCenter: parent.verticalCenter
2014-11-14 04:21:14 +03:00
onClicked: {
canvas.clear();
2014-11-14 04:21:14 +03:00
}
}
}
Rectangle {
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-15 21:09:49 +03:00
property real lineWidth: defaultStrokeSize
property string strokeStyle: defaultStrokeColor
2014-11-14 04:21:14 +03:00
onLineWidthChanged: requestPaint()
function clear()
{
var ctx = getContext("2d");
ctx.fillRect(0, 0, width, height);
requestPaint();
}
2014-11-14 04:21:14 +03:00
onPaint: {
var ctx = getContext("2d");
2014-11-15 21:09:49 +03:00
ctx.fillStyle = defaultFillColor;
2014-11-14 04:21:14 +03:00
ctx.lineCap = "round";
ctx.lineJoin = "round";
2014-11-14 04:21:14 +03:00
ctx.lineWidth = lineWidth;
ctx.miterLimit = 1;
ctx.strokeStyle = strokeStyle;
2014-11-14 04:21:14 +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: {
startX = finishX = mouseX;
startY = finishY = mouseY;
}
onMouseXChanged: {
finishX = mouseX;
parent.requestPaint();
}
onMouseYChanged: {
finishY = mouseY;
parent.requestPaint();
}
}
}
}
}
}