From 5ac14981b03c9c73c08194b1eb992107a096f774 Mon Sep 17 00:00:00 2001 From: Nico Vertriest Date: Thu, 27 Oct 2016 16:11:01 +0200 Subject: Example: Improve the localstorage example MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - insert/update/delete instead of only insert - database connection and sql handled in Database.js Change-Id: I513b23061e569f4511dd166ed79898db7f628139 Reviewed-by: Topi Reiniƶ --- .../quick/localstorage/doc/src/localstorage.qdoc | 11 +- .../quick/localstorage/localstorage/Database.js | 97 ++++++++++ .../quick/localstorage/localstorage/Header.qml | 173 +++++++++++++++++ .../quick/localstorage/localstorage/MyButton.qml | 76 ++++++++ .../quick/localstorage/localstorage/MyDelegate.qml | 74 ++++++++ .../quick/localstorage/localstorage/MyModel.qml | 35 ++++ examples/quick/localstorage/localstorage/hello.qml | 78 -------- .../localstorage/localstorage/localstorage.qml | 209 +++++++++++++++++---- .../localstorage/localstorage.qmlproject | 16 -- .../localstorage/localstorage/localstorage.qrc | 8 +- examples/quick/localstorage/localstorage/main.cpp | 65 +++---- .../localstorage/qml-localstorage-example.png | Bin 0 -> 46168 bytes 12 files changed, 669 insertions(+), 173 deletions(-) create mode 100644 examples/quick/localstorage/localstorage/Database.js create mode 100644 examples/quick/localstorage/localstorage/Header.qml create mode 100644 examples/quick/localstorage/localstorage/MyButton.qml create mode 100644 examples/quick/localstorage/localstorage/MyDelegate.qml create mode 100644 examples/quick/localstorage/localstorage/MyModel.qml delete mode 100644 examples/quick/localstorage/localstorage/hello.qml delete mode 100644 examples/quick/localstorage/localstorage/localstorage.qmlproject create mode 100644 examples/quick/localstorage/localstorage/qml-localstorage-example.png (limited to 'examples') diff --git a/examples/quick/localstorage/doc/src/localstorage.qdoc b/examples/quick/localstorage/doc/src/localstorage.qdoc index b52e0afd80..1bfba147e1 100644 --- a/examples/quick/localstorage/doc/src/localstorage.qdoc +++ b/examples/quick/localstorage/doc/src/localstorage.qdoc @@ -35,8 +35,13 @@ \include examples-run.qdocinc - \section1 Hello World + \section1 Activity Tracker - \e {Hello World} demonstrates creating a simple SQL table and doing - insert and select operations. + \e {Activity tracker} allows you to keep track of walks, hikes, or bike trips. + + All database transactions are handled in Database.js. The database is + checked at startup, and created if it does not exist. LocalStorage uses + SQLite, which is a self-contained, serverless, public-domain database. + Opening a connection to the database is handled at the beginning of each + function that manipulates or retrieves data. */ diff --git a/examples/quick/localstorage/localstorage/Database.js b/examples/quick/localstorage/localstorage/Database.js new file mode 100644 index 0000000000..387033795f --- /dev/null +++ b/examples/quick/localstorage/localstorage/Database.js @@ -0,0 +1,97 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +function dbInit() +{ + var db = LocalStorage.openDatabaseSync("Activity_Tracker_DB", "", "Track exercise", 1000000) + try { + db.transaction(function (tx) { + tx.executeSql('CREATE TABLE IF NOT EXISTS trip_log (date text,trip_desc text,distance numeric)') + }) + } catch (err) { + console.log("Error creating table in database: " + err) + }; +} + +function dbGetHandle() +{ + try { + var db = LocalStorage.openDatabaseSync("Activity_Tracker_DB", "", + "Track exercise", 1000000) + } catch (err) { + console.log("Error opening database: " + err) + } + return db +} + +function dbInsert(Pdate, Pdesc, Pdistance) +{ + var db = dbGetHandle() + var rowid = 0; + db.transaction(function (tx) { + tx.executeSql('INSERT INTO trip_log VALUES(?, ?, ?)', + [Pdate, Pdesc, Pdistance]) + var result = tx.executeSql('SELECT last_insert_rowid()') + rowid = result.insertId + }) + return rowid; +} + +function dbReadAll() +{ + var db = dbGetHandle() + db.transaction(function (tx) { + var results = tx.executeSql( + 'SELECT rowid,date,trip_desc,distance FROM trip_log order by rowid desc') + for (var i = 0; i < results.rows.length; i++) { + listModel.append({ + id: results.rows.item(i).rowid, + checked: " ", + date: results.rows.item(i).date, + trip_desc: results.rows.item(i).trip_desc, + distance: results.rows.item(i).distance + }) + } + }) +} + +function dbUpdate(Pdate, Pdesc, Pdistance, Prowid) +{ + var db = dbGetHandle() + db.transaction(function (tx) { + tx.executeSql( + 'update trip_log set date=?, trip_desc=?, distance=? where rowid = ?', [Pdate, Pdesc, Pdistance, Prowid]) + }) +} + +function dbDeleteRow(Prowid) +{ + var db = dbGetHandle() + db.transaction(function (tx) { + tx.executeSql('delete from trip_log where rowid = ?', [Prowid]) + }) +} diff --git a/examples/quick/localstorage/localstorage/Header.qml b/examples/quick/localstorage/localstorage/Header.qml new file mode 100644 index 0000000000..e0e465ebc9 --- /dev/null +++ b/examples/quick/localstorage/localstorage/Header.qml @@ -0,0 +1,173 @@ + +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ +import QtQuick 2.7 +import QtQuick.Window 2.0 +import QtQuick.LocalStorage 2.0 +import "Database.js" as JS +import QtQuick.Layouts 1.1 + +Item { + id: root + width: Screen.width / 2 + height: Screen.height / 7 + + function insertrec() { + var rowid = parseInt(JS.dbInsert(dateInput.text, descInput.text, distInput.text), 10) + if (rowid) { + listView.model.setProperty(listView.currentIndex, "id", rowid) + listView.forceLayout() + } + return rowid; + } + + function editrec(Pdate, Pdesc, Pdistance, Prowid) { + dateInput.text = Pdate + descInput.text = Pdesc + distInput.text = Pdistance + } + + function initrec_new() { + dateInput.text = "" + descInput.text = "" + distInput.text = "" + listView.model.insert(0, { + date: "", + trip_desc: "", + distance: "" + }) + listView.currentIndex = 0 + dateInput.forceActiveFocus() + } + + function initrec() { + dateInput.text = "" + descInput.text = "" + distInput.text = "" + } + + function setlistview() { + listView.model.setProperty(listView.currentIndex, "date", + dateInput.text) + listView.model.setProperty(listView.currentIndex, "trip_desc", + descInput.text) + listView.model.setProperty(listView.currentIndex, "distance", + distInput.text) + } + + Rectangle { + id: rootrect + border.width: 10 + color: "#161616" + + ColumnLayout { + id: mainLayout + anchors.fill: parent + + Rectangle { + id: gridBox + Layout.fillWidth: true + + GridLayout { + id: gridLayout + rows: 3 + flow: GridLayout.TopToBottom + anchors.fill: parent + + Text { + text: "Date" + font.pixelSize: 22 + rightPadding: 10 + } + + Text { + text: "Description" + font.pixelSize: 22 + rightPadding: 10 + } + + Text { + text: "Distance" + font.pixelSize: 22 + } + + TextInput { + id: dateInput + font.pixelSize: 22 + activeFocusOnPress: true + activeFocusOnTab: true + validator: RegExpValidator { + regExp: /[0-9/,:.]+/ + } + onEditingFinished: { + if (dateInput.text == "") { + statustext.text = "Please fill in the date" + dateInput.forceActiveFocus() + } + } + } + + TextInput { + id: descInput + font.pixelSize: 22 + activeFocusOnPress: true + activeFocusOnTab: true + onEditingFinished: { + if (descInput.text.length < 8) { + statustext.text = "Enter a description of minimum 8 characters" + descInput.forceActiveFocus() + } else { + statustext.text = "" + } + } + } + + TextInput { + id: distInput + font.pixelSize: 22 + activeFocusOnPress: true + activeFocusOnTab: true + validator: RegExpValidator { + regExp: /\d{1,3}/ + } + onEditingFinished: { + if (distInput.text == "") { + statustext.text = "Please fill in the distance" + distInput.forceActiveFocus() + } + } + } + } + } + + MouseArea { + anchors.fill: parent + onClicked: dateInput.forceActiveFocus() + } + } + } +} diff --git a/examples/quick/localstorage/localstorage/MyButton.qml b/examples/quick/localstorage/localstorage/MyButton.qml new file mode 100644 index 0000000000..4659b45f64 --- /dev/null +++ b/examples/quick/localstorage/localstorage/MyButton.qml @@ -0,0 +1,76 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 + +Rectangle { + id: button + + property bool checked: false + property alias text: buttonText.text + + Accessible.name: text + Accessible.description: "This button does " + text + Accessible.role: Accessible.Button + Accessible.onPressAction: button.clicked() + + signal clicked + + implicitWidth: buttonText.implicitWidth + 20 + implicitHeight: 30 + gradient: Gradient { + GradientStop { + position: 0.0 + color: "grey" + } + GradientStop { + position: 1.0 + color: button.focus ? "red" : "grey" + } + } + + radius: 5 + antialiasing: true + + Text { + id: buttonText + text: parent.description + anchors.centerIn: parent + font.pixelSize: parent.height * .5 + style: enabled ? Text.Sunken : Text.Normal + color: enabled ? "white" : "#555" + styleColor: "black" + } + + MouseArea { + id: mouseArea + anchors.fill: parent + onClicked: parent.clicked() + } + + Keys.onSpacePressed: clicked() +} diff --git a/examples/quick/localstorage/localstorage/MyDelegate.qml b/examples/quick/localstorage/localstorage/MyDelegate.qml new file mode 100644 index 0000000000..9a4ac1cc46 --- /dev/null +++ b/examples/quick/localstorage/localstorage/MyDelegate.qml @@ -0,0 +1,74 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtQuick.LocalStorage 2.0 +import QtQuick.Layouts 1.1 +import "Database.js" as JS + +Item { + width: parent.width + height: rDate.implicitHeight + + Rectangle { + id: baseRec + anchors.fill: parent + opacity: 0.8 + color: index % 2 ? "lightgrey" : "grey" + + MouseArea { + anchors.fill: parent + onClicked: listView.currentIndex = index + } + GridLayout { + anchors.fill:parent + columns: 3 + + Text { + id: rDate + text: date + font.pixelSize: 22 + Layout.preferredWidth: parent.width / 4 + color: "black" + } + Text { + id: rDesc + text: trip_desc + Layout.fillWidth: true + font.pixelSize: 22 + color: "black" + } + Text { + id: rDistance + text: distance + font.pixelSize: 22 + Layout.alignment: Qt.AlignRight + color: "black" + } + } + } +} diff --git a/examples/quick/localstorage/localstorage/MyModel.qml b/examples/quick/localstorage/localstorage/MyModel.qml new file mode 100644 index 0000000000..0677ec74d6 --- /dev/null +++ b/examples/quick/localstorage/localstorage/MyModel.qml @@ -0,0 +1,35 @@ +/**************************************************************************** +** +** Copyright (C) 2016 The Qt Company Ltd. +** Contact: http://www.qt.io/licensing/ +** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. +** $QT_END_LICENSE$ +** +****************************************************************************/ + +import QtQuick 2.0 +import QtQuick.LocalStorage 2.0 +import "Database.js" as JS + +ListModel { + id: listModel + Component.onCompleted: JS.dbReadAll() +} diff --git a/examples/quick/localstorage/localstorage/hello.qml b/examples/quick/localstorage/localstorage/hello.qml deleted file mode 100644 index d4f82ba2fe..0000000000 --- a/examples/quick/localstorage/localstorage/hello.qml +++ /dev/null @@ -1,78 +0,0 @@ -/**************************************************************************** -** -** Copyright (C) 2015 The Qt Company Ltd. -** Contact: http://www.qt.io/licensing/ -** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** -** $QT_END_LICENSE$ -** -****************************************************************************/ -//![0] -import QtQuick 2.0 -import QtQuick.LocalStorage 2.0 - -Rectangle { - width: 200 - height: 100 - - Text { - text: "?" - anchors.horizontalCenter: parent.horizontalCenter - - function findGreetings() { - var db = LocalStorage.openDatabaseSync("QQmlExampleDB", "1.0", "The Example QML SQL!", 1000000); - - db.transaction( - function(tx) { - // Create the database if it doesn't already exist - tx.executeSql('CREATE TABLE IF NOT EXISTS Greeting(salutation TEXT, salutee TEXT)'); - - // Add (another) greeting row - tx.executeSql('INSERT INTO Greeting VALUES(?, ?)', [ 'hello', 'world' ]); - - // Show all added greetings - var rs = tx.executeSql('SELECT * FROM Greeting'); - - var r = "" - for(var i = 0; i < rs.rows.length; i++) { - r += rs.rows.item(i).salutation + ", " + rs.rows.item(i).salutee + "\n" - } - text = r - } - ) - } - - Component.onCompleted: findGreetings() - } -} -//![0] diff --git a/examples/quick/localstorage/localstorage/localstorage.qml b/examples/quick/localstorage/localstorage/localstorage.qml index 61e9eff8b6..b85fad4764 100644 --- a/examples/quick/localstorage/localstorage/localstorage.qml +++ b/examples/quick/localstorage/localstorage/localstorage.qml @@ -1,54 +1,183 @@ - /**************************************************************************** +/**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2016 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** -** This file is part of the examples of the Qt Toolkit. +** This file is part of the documentation of the Qt Toolkit. ** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. ** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ -import QtQuick 2.0 -import "../../shared" as Examples +import QtQuick 2.5 +import QtQuick.Window 2.2 +import QtQuick.Layouts 1.1 +import QtQuick.LocalStorage 2.0 +import "Database.js" as JS + +Window { + visible: true + width: Screen.width / 2 + height: Screen.height / 1.8 + color: "#161616" -Item { - height: 480 - width: 320 - Examples.LauncherList { - id: ll + property bool creatingNewEntry: false + property bool editingEntry: false + + Rectangle { anchors.fill: parent - Component.onCompleted: { - addExample("Hello World", "Simple SQL operations with local storage API", Qt.resolvedUrl("hello.qml")); + + ColumnLayout { + anchors.fill: parent + + Header { + id: input + Layout.fillWidth: true + } + RowLayout { + MyButton { + text: "New" + onClicked: { + input.initrec_new() + creatingNewEntry = true + listView.model.setProperty(listView.currentIndex, "id", 0) + } + } + MyButton { + id: saveButton + enabled: (creatingNewEntry || editingEntry) && listView.currentIndex != -1 + text: "Save" + onClicked: { + var insertedRow = false; + if (listView.model.get(listView.currentIndex).id < 1) { + //insert mode + if (input.insertrec()) { + // Successfully inserted a row. + input.setlistview() + insertedRow = true + } else { + // Failed to insert a row; display an error message. + statustext.text = "Failed to insert row" + } + } else { + // edit mode + input.setlistview() + JS.dbUpdate(listView.model.get(listView.currentIndex).date, + listView.model.get(listView.currentIndex).trip_desc, + listView.model.get(listView.currentIndex).distance, + listView.model.get(listView.currentIndex).id) + } + + if (insertedRow) { + input.initrec() + creatingNewEntry = false + editingEntry = false + listView.forceLayout() + } + } + } + MyButton { + id: editButton + text: "Edit" + enabled: !creatingNewEntry && !editingEntry && listView.currentIndex != -1 + onClicked: { + input.editrec(listView.model.get(listView.currentIndex).date, + listView.model.get(listView.currentIndex).trip_desc, + listView.model.get(listView.currentIndex).distance, + listView.model.get(listView.currentIndex).id) + + editingEntry = true + } + } + MyButton { + id: deleteButton + text: "Delete" + enabled: !creatingNewEntry && listView.currentIndex != -1 + onClicked: { + JS.dbDeleteRow(listView.model.get(listView.currentIndex).id) + listView.model.remove(listView.currentIndex, 1) + if (listView.count == 0) { + // ListView doesn't automatically set its currentIndex to -1 + // when the count becomes 0. + listView.currentIndex = -1 + } + } + } + MyButton { + id: cancelButton + text: "Cancel" + enabled: (creatingNewEntry || editingEntry) && listView.currentIndex != -1 + onClicked: { + if (listView.model.get(listView.currentIndex).id === 0) { + // This entry had an id of 0, which means it was being created and hadn't + // been saved to the database yet, so we can safely remove it from the model. + listView.model.remove(listView.currentIndex, 1) + } + listView.forceLayout() + creatingNewEntry = false + editingEntry = false + input.initrec() + } + } + MyButton { + text: "Exit" + onClicked: Qt.quit() + } + } + Component { + id: highlightBar + Rectangle { + width: listView.currentItem.width + height: listView.currentItem.height + color: "lightgreen" + } + } + ListView { + id: listView + Layout.fillWidth: true + Layout.fillHeight: true + model: MyModel {} + delegate: MyDelegate {} + // Don't allow changing the currentIndex while the user is creating/editing values. + enabled: !creatingNewEntry && !editingEntry + + highlight: highlightBar + highlightFollowsCurrentItem: true + focus: true + + header: Component { + Text { + text: "Saved activities" + } + } + } + Text { + id: statustext + color: "red" + Layout.fillWidth: true + font.bold: true + font.pointSize: 20 + + } } } + Component.onCompleted: { + JS.dbInit() + } } diff --git a/examples/quick/localstorage/localstorage/localstorage.qmlproject b/examples/quick/localstorage/localstorage/localstorage.qmlproject deleted file mode 100644 index 6835d23503..0000000000 --- a/examples/quick/localstorage/localstorage/localstorage.qmlproject +++ /dev/null @@ -1,16 +0,0 @@ -import QmlProject 1.0 - -Project { - mainFile: "localstorage.qml" - /* Include .qml, .js, and image files from current directory and subdirectories */ - - QmlFiles { - directory: "." - } - JavaScriptFiles { - directory: "." - } - ImageFiles { - directory: "." - } -} \ No newline at end of file diff --git a/examples/quick/localstorage/localstorage/localstorage.qrc b/examples/quick/localstorage/localstorage/localstorage.qrc index 09ac2de033..079438be2b 100644 --- a/examples/quick/localstorage/localstorage/localstorage.qrc +++ b/examples/quick/localstorage/localstorage/localstorage.qrc @@ -1,6 +1,10 @@ - - hello.qml + + Header.qml + Database.js + MyModel.qml + MyButton.qml + MyDelegate.qml localstorage.qml diff --git a/examples/quick/localstorage/localstorage/main.cpp b/examples/quick/localstorage/localstorage/main.cpp index 6e522753e9..9f8adbb7a5 100644 --- a/examples/quick/localstorage/localstorage/main.cpp +++ b/examples/quick/localstorage/localstorage/main.cpp @@ -1,41 +1,38 @@ /**************************************************************************** ** -** Copyright (C) 2015 The Qt Company Ltd. +** Copyright (C) 2016 The Qt Company Ltd. ** Contact: http://www.qt.io/licensing/ ** -** This file is part of the examples of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:BSD$ -** You may use this file under the terms of the BSD license as follows: -** -** "Redistribution and use in source and binary forms, with or without -** modification, are permitted provided that the following conditions are -** met: -** * Redistributions of source code must retain the above copyright -** notice, this list of conditions and the following disclaimer. -** * Redistributions in binary form must reproduce the above copyright -** notice, this list of conditions and the following disclaimer in -** the documentation and/or other materials provided with the -** distribution. -** * Neither the name of The Qt Company Ltd nor the names of its -** contributors may be used to endorse or promote products derived -** from this software without specific prior written permission. -** -** -** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." -** +** This file is part of the documentation of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:FDL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and The Qt Company. For licensing terms +** and conditions see http://www.qt.io/terms-conditions. For further +** information use the contact form at http://www.qt.io/contact-us. +** +** GNU Free Documentation License Usage +** Alternatively, this file may be used under the terms of the GNU Free +** Documentation License version 1.3 as published by the Free Software +** Foundation and appearing in the file included in the packaging of +** this file. Please review the following information to ensure +** the GNU Free Documentation License version 1.3 requirements +** will be met: http://www.gnu.org/copyleft/fdl.html. ** $QT_END_LICENSE$ ** ****************************************************************************/ -#include "../../shared/shared.h" -DECLARATIVE_EXAMPLE_MAIN(localstorage/localstorage/localstorage) +#include +#include + +int main(int argc, char *argv[]) +{ + QGuiApplication app(argc, argv); + + QQmlApplicationEngine engine; + engine.load(QUrl(QStringLiteral("qrc:/localstorage.qml"))); + + return app.exec(); +} diff --git a/examples/quick/localstorage/localstorage/qml-localstorage-example.png b/examples/quick/localstorage/localstorage/qml-localstorage-example.png new file mode 100644 index 0000000000..7d7edafe63 Binary files /dev/null and b/examples/quick/localstorage/localstorage/qml-localstorage-example.png differ -- cgit v1.2.3