Implement Save function

Fixes #2 plus very minor fixes.
This commit is contained in:
Andrea Scarpino 2014-11-29 12:23:28 +01:00
parent bc250a99fb
commit b7e88d64f3
3 changed files with 67 additions and 10 deletions

View file

@ -23,8 +23,8 @@ Page {
readonly property real defaultStrokeSize: 5 readonly property real defaultStrokeSize: 5
readonly property real defaultRubberSize: 20 readonly property real defaultRubberSize: 20
readonly property string defaultStrokeColor: "#000000" readonly property color defaultStrokeColor: Qt.rgba(0, 0, 0, 1)
readonly property string defaultFillColor: "#ffffff" readonly property color defaultFillColor: Qt.rgba(1, 1, 1, 1)
Column { Column {
anchors.fill: parent anchors.fill: parent
@ -49,7 +49,7 @@ Page {
property real prevLineWidth: defaultRubberSize; property real prevLineWidth: defaultRubberSize;
onCheckedChanged: { onCheckedChanged: {
if (canvas.strokeStyle === defaultStrokeColor) { if (canvas.strokeStyle == defaultStrokeColor) {
icon.source = "image://theme/icon-camera-focus" icon.source = "image://theme/icon-camera-focus"
canvas.strokeStyle = defaultFillColor; canvas.strokeStyle = defaultFillColor;
} else { } else {
@ -71,7 +71,7 @@ Page {
stepSize: 1 stepSize: 1
value: defaultStrokeSize value: defaultStrokeSize
valueText: value valueText: value
width: 300 width: 240
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
@ -84,16 +84,38 @@ Page {
} }
} }
IconButton {
icon.source: "image://theme/icon-m-image"
anchors.verticalCenter: parent.verticalCenter
function pictureName() {
var dateTime = new Date();
return Qt.formatDateTime(dateTime, "yyyy-MM-dd-hh-mm-ss") + ".jpeg";
}
onClicked: {
remorseSave.execute(menu, qsTr("Saving the canvas..."), function() {
canvas.save(papocchioDir + pictureName());
}, 3000);
}
RemorseItem {
id: remorseSave
}
}
IconButton { IconButton {
icon.source: "image://theme/icon-m-clear" icon.source: "image://theme/icon-m-clear"
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
onClicked: { onClicked: {
clear.execute(menu, qsTr("Clearing the canvas..."), function() { canvas.clear() }); remorseClear.execute(menu, qsTr("Clearing the canvas..."), function() {
canvas.clear();
});
} }
RemorseItem { RemorseItem {
id: clear id: remorseClear
} }
} }
} }
@ -112,21 +134,31 @@ Page {
property int finishX property int finishX
property int finishY property int finishY
property bool initialize: true
property real lineWidth: defaultStrokeSize property real lineWidth: defaultStrokeSize
property string strokeStyle: defaultStrokeColor property string strokeStyle: defaultStrokeColor
onLineWidthChanged: requestPaint() onLineWidthChanged: requestPaint()
function clear() function clear() {
{
var ctx = getContext("2d"); var ctx = getContext("2d");
ctx.fillStyle = defaultFillColor;
ctx.fillRect(0, 0, width, height); ctx.fillRect(0, 0, width, height);
requestPaint(); requestPaint();
} }
onPaint: { onPaint: {
var ctx = getContext("2d"); var ctx = getContext("2d");
ctx.fillStyle = defaultFillColor;
// 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;
}
ctx.lineCap = "round"; ctx.lineCap = "round";
ctx.lineJoin = "round"; ctx.lineJoin = "round";
ctx.lineWidth = lineWidth; ctx.lineWidth = lineWidth;

View file

@ -20,6 +20,8 @@
#include <sailfishapp.h> #include <sailfishapp.h>
QString papocchioDir();
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QGuiApplication *app = SailfishApp::application(argc, argv); QGuiApplication *app = SailfishApp::application(argc, argv);
@ -31,8 +33,26 @@ int main(int argc, char *argv[])
view->setSource(SailfishApp::pathTo("qml/Papocchio.qml")); view->setSource(SailfishApp::pathTo("qml/Papocchio.qml"));
view->setPersistentOpenGLContext(true); view->setPersistentOpenGLContext(true);
view->setPersistentSceneGraph(true); view->setPersistentSceneGraph(true);
view->rootContext()->setContextProperty("papocchioDir", papocchioDir());
view->show(); view->show();
return app->exec(); return app->exec();
} }
/**
* @brief papocchioDir just to be sure the directory where to save pictures
* exists.
* @return the path to the directory where to save pictures.
*/
QString papocchioDir()
{
QDir papocchioDir(QDir::homePath() + "/Pictures/Papocchio");
if (!papocchioDir.exists()) {
papocchioDir.mkpath(papocchioDir.absolutePath());
}
return QString(papocchioDir.absolutePath() + "/");
}

View file

@ -12,7 +12,12 @@
<context> <context>
<name>MainPage</name> <name>MainPage</name>
<message> <message>
<location filename="../qml/pages/MainPage.qml" line="92"/> <location filename="../qml/pages/MainPage.qml" line="97"/>
<source>Saving the canvas...</source>
<translation type="unfinished"></translation>
</message>
<message>
<location filename="../qml/pages/MainPage.qml" line="112"/>
<source>Clearing the canvas...</source> <source>Clearing the canvas...</source>
<translation type="unfinished"></translation> <translation type="unfinished"></translation>
</message> </message>