2
* Copyright (C) 2013 Canonical, Ltd.
5
* Kevin Wright <kevin.wright@canonical.com>
7
* This program is free software; you can redistribute it and/or modify
8
* it under the terms of the GNU Lesser General Public License as published by
9
* the Free Software Foundation; version 3.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU Lesser General Public License for more details.
16
* You should have received a copy of the GNU Lesser General Public License
17
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
import U1db 1.0 as U1db
22
import Ubuntu.Components 0.1
26
This example and tutorial is designed to show a wide variety of U1Db-Qt functionality and usage. The example demonstrates:
29
\li Combining U1Db-Qt with elements and components that do not utilize models
30
\li Blending the U1Db-Qt plugin with QML and Javascript
41
A Database is very simple to create. It only needs an id and a path where the file will be created. A Database is a model, which can be used by elements, such as the ListView further in this example.
57
A Document can be declared at runtime. It requires at the very least a unique 'docId', but that alone won't do anything special. The snipet below snippet demonstrates the basic requirements.
59
In addition to this, this example displays text from the database for a specific docId and id key in a text area called 'documentContent. To update the text area at startup with either the default value or a value from the database the onCompleted function is utilized, which is also demonstrated below.
66
defaults: { "helloworld":"Hello World" }
68
Component.onCompleted: {
69
documentContent.text = aDocument.contents.helloworld
82
defaults: { "helloworld":"Hello World" }
84
Component.onCompleted: {
85
documentContent.text = aDocument.contents.helloworld
92
It should be possible to use a document without a database, as demonstrated in this snippet. Additionally this document will use the concept of sub-keys, as exemplified by the "bookmarks" id key + contents. This example will attempt to use the bookmark document to store docId values from the database, which will be displayed in a ListView on the second tab of the application. The user will be able to select a value from the ListView and the first tab will be modified accordingly.
98
defaults: { "bookmarks": [{}] }
106
id: aBookmarkDocument
109
defaults: { "bookmarks": [{}] }
113
function switchToPreviousDocument(documentObject){
115
aDocument.docId = getPreviousDocumentId(documentObject)
119
function switchToNextDocument(){
121
aDocument.docId = getNextDocumentId(aDocument)
125
function getPreviousDocumentId(documentObject){
127
if(typeof documentObject!='undefined'){
131
The listDocs method retrieves all the docId values from the current database. In this demonstration the values are put into an array, which is then checked to locate the docId for the current and previous documents within the database.
135
documentIds = documentObject.database.listDocs()
137
for(var i = 0; i < documentIds.length; i++){
139
if(documentIds[i]===documentObject.docId && i > 0){
140
return documentIds[i-1]
142
else if(documentIds[i]===documentObject.docId && i==0){
143
return documentIds[documentIds.length-1]
152
documentIds = documentObject.database.listDocs()
154
for(var i = 0; i < documentIds.length; i++){
156
if(documentIds[i]===documentObject.docId && i > 0){
157
return documentIds[i-1]
159
else if(documentIds[i]===documentObject.docId && i==0){
160
return documentIds[documentIds.length-1]
165
return documentIds[0]
178
function getNextDocumentId(documentObject){
180
if(typeof documentObject!='undefined'){
182
var documentIds = documentObject.database.listDocs()
184
for(var i = 0; i < documentIds.length; i++){
186
if(documentIds[i]===documentObject.docId && i < (documentIds.length-1)){
187
return documentIds[i+1]
189
else if(documentIds[i]===documentObject.docId && i==(documentIds.length-1)){
190
return documentIds[0]
195
return documentIds[0]
208
function getCurrentDocumentKey(contentsObject){
210
if(typeof contentsObject!='undefined'){
212
var keys = Object.keys(contentsObject);
225
function updateContentWindow(documentText, addressBarText) {
227
// Somewhere below need to check for things like invalid docId
229
if(documentText!==addressBarText) {
233
These steps demonstrate the creation of a temporary document, based on a copy of the global document. This will then be used to determine if there is already a document in the database with the same docId as the address bar, and additionally with a key id with the same name.
235
var tempDocument = {}
236
var tempFieldName = addressBarText;
237
var tempContents = {};
239
tempDocument = aDocument
240
tempDocument.docId = addressBarText;
242
tempContents = tempDocument.contents
244
NOTE: For simplicity sake this example sometimes uses the same value for both the docId and the key id, as seen here. Real life implimentations can and will differ, and this will be demonstrated elsewhere in the example code.
248
var tempDocument = {}
249
var tempFieldName = addressBarText;
250
var tempContents = {};
252
tempDocument = aDocument
253
tempDocument.docId = addressBarText;
255
tempContents = tempDocument.contents
257
if(typeof tempContents !='undefined' && typeof tempContents[tempFieldName]!='undefined') {
259
aDocument = tempDocument
260
documentContent.text = tempContents[tempFieldName]
267
Here the contents of the temporary document are modified, which then replaces the global document.
269
documentContent.text = 'More Hello World...';
272
tempContents[tempFieldName] = documentContent.text
273
tempDocument.contents = tempContents
274
aDocument = tempDocument
278
documentContent.text = 'More Hello World...';
281
tempContents[tempFieldName] = documentContent.text
282
tempDocument.contents = tempContents
283
aDocument = tempDocument
292
In this instance the current document's content is updated from the text view. The unique key and docId are not modified because the database already contains a record with those properties.
295
tempFieldName = getCurrentDocumentKey(aDocument.contents)
296
tempContents[tempFieldName] = documentContent.text
297
aDocument.contents = tempContents
302
tempFieldName = getCurrentDocumentKey(aDocument.contents)
303
tempContents[tempFieldName] = documentContent.text
304
aDocument.contents = tempContents
314
title: i18n.tr("Hello U1Db!")
320
/*! Here a rectangle is defined that represents the lower portion of our application. It will contain all the main parts of the application.
326
anchors.bottom: parent.bottom
330
// The remainder of the main part of the application goes here ...
348
anchors.bottom: parent.bottom
352
The following TextArea is for displaying contents for the current state of the global document, as defined by the key / name in the address bar.
358
selectByMouse : false
374
selectByMouse : false
386
// This rectangle contains the navigation controls
392
anchors.top: addressBarArea.bottom
400
anchors.verticalCenter: parent.verticalCenter
405
onClicked: updateContentWindow(switchToPreviousDocument(aDocument), addressBar.text)
409
onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),'helloworld')
413
onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),addressBar.text)
417
onClicked: updateContentWindow(switchToNextDocument(aDocument), addressBar.text)
430
anchors.top: parent.top
437
anchors.verticalCenter: parent.verticalCenter
440
hasClearButton: false
444
There is an object within in the 'aDocument' model defined earlier called 'contents', which contains a key called 'hello', which represents a search string. In this example the key will represent the name of a document in the database, which will be displayed in the address bar. Displaying the key is demonstrated here:
446
text: displayKey(aDocument.contents)
448
function displayKey(documentObject){
450
var keys = Object.keys(documentObject);
458
text: getCurrentDocumentKey(aDocument.contents)
463
onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),addressBar.text)
473
title: i18n.tr("Bookmarks")
478
text: aDocument.contents.helloworld