~ubuntu-branches/ubuntu/wily/u1db-qt/wily

« back to all changes in this revision

Viewing changes to examples/u1db-qt-example-4/u1db-qt-example-4.qml

  • Committer: Package Import Robot
  • Author(s): Ubuntu daily release
  • Date: 2013-08-07 10:28:13 UTC
  • Revision ID: package-import@ubuntu.com-20130807102813-uk4un76xcsoy3n12
Tags: upstream-0.1.5+13.10.20130807
ImportĀ upstreamĀ versionĀ 0.1.5+13.10.20130807

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2013 Canonical, Ltd.
 
3
 *
 
4
 * Authors:
 
5
 *  Kevin Wright <kevin.wright@canonical.com>
 
6
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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/>.
 
18
 */
 
19
 
 
20
import QtQuick 2.0
 
21
import U1db 1.0 as U1db
 
22
import Ubuntu.Components 0.1
 
23
 
 
24
/*!
 
25
 
 
26
This example and tutorial is designed to show a wide variety of U1Db-Qt functionality and usage. The example demonstrates:
 
27
 
 
28
\list 1
 
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
 
31
\endlist
 
32
 
 
33
  */
 
34
 
 
35
MainView {
 
36
        width: units.gu(45)
 
37
        height: units.gu(80)
 
38
 
 
39
        /*!
 
40
 
 
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.
 
42
 
 
43
            U1db.Database {
 
44
                id: aDatabase
 
45
                path: "aDatabase4"
 
46
            }
 
47
 
 
48
        */
 
49
 
 
50
        U1db.Database {
 
51
            id: aDatabase
 
52
            path: "aDatabase4"
 
53
        }
 
54
 
 
55
        /*!
 
56
 
 
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.
 
58
 
 
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.
 
60
 
 
61
            U1db.Document {
 
62
                id: aDocument
 
63
                database: aDatabase
 
64
                docId: 'helloworld'
 
65
                create: true
 
66
                defaults: { "helloworld":"Hello World" }
 
67
 
 
68
                Component.onCompleted: {
 
69
                    documentContent.text = aDocument.contents.helloworld
 
70
                }
 
71
 
 
72
            }
 
73
 
 
74
        */
 
75
 
 
76
 
 
77
       U1db.Document {
 
78
            id: aDocument
 
79
            database: aDatabase
 
80
            docId: 'helloworld'
 
81
            create: true
 
82
            defaults: { "helloworld":"Hello World" }
 
83
 
 
84
            Component.onCompleted: {
 
85
                documentContent.text = aDocument.contents.helloworld
 
86
            }
 
87
 
 
88
        }
 
89
 
 
90
       /*!
 
91
 
 
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.
 
93
 
 
94
       U1db.Document {
 
95
            id: aBookmarkDocument
 
96
            docId: 'bookmarks'
 
97
            create: true
 
98
            defaults: { "bookmarks": [{}] }
 
99
       }
 
100
 
 
101
 
 
102
         */
 
103
 
 
104
 
 
105
       U1db.Document {
 
106
            id: aBookmarkDocument
 
107
            docId: 'bookmarks'
 
108
            create: true
 
109
            defaults: { "bookmarks": [{}] }
 
110
       }
 
111
 
 
112
 
 
113
       function switchToPreviousDocument(documentObject){
 
114
 
 
115
          aDocument.docId = getPreviousDocumentId(documentObject)
 
116
 
 
117
          }
 
118
 
 
119
       function switchToNextDocument(){
 
120
 
 
121
          aDocument.docId = getNextDocumentId(aDocument)
 
122
 
 
123
        }
 
124
 
 
125
       function getPreviousDocumentId(documentObject){
 
126
 
 
127
           if(typeof documentObject!='undefined'){
 
128
 
 
129
               /*!
 
130
 
 
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.
 
132
 
 
133
               var documentIds = {}
 
134
 
 
135
               documentIds = documentObject.database.listDocs()
 
136
 
 
137
               for(var i = 0; i < documentIds.length; i++){
 
138
 
 
139
                   if(documentIds[i]===documentObject.docId && i > 0){
 
140
                       return documentIds[i-1]
 
141
                   }
 
142
                   else if(documentIds[i]===documentObject.docId && i==0){
 
143
                       return documentIds[documentIds.length-1]
 
144
                   }
 
145
 
 
146
               }
 
147
 
 
148
                 */
 
149
 
 
150
               var documentIds = {}
 
151
 
 
152
               documentIds = documentObject.database.listDocs()
 
153
 
 
154
               for(var i = 0; i < documentIds.length; i++){
 
155
 
 
156
                   if(documentIds[i]===documentObject.docId && i > 0){
 
157
                       return documentIds[i-1]
 
158
                   }
 
159
                   else if(documentIds[i]===documentObject.docId && i==0){
 
160
                       return documentIds[documentIds.length-1]
 
161
                   }
 
162
 
 
163
               }
 
164
 
 
165
               return documentIds[0]
 
166
 
 
167
           }
 
168
 
 
169
           else{
 
170
 
 
171
               print("Error!")
 
172
 
 
173
               return ''
 
174
           }
 
175
 
 
176
       }
 
177
 
 
178
       function getNextDocumentId(documentObject){
 
179
 
 
180
           if(typeof documentObject!='undefined'){
 
181
 
 
182
               var documentIds = documentObject.database.listDocs()
 
183
 
 
184
               for(var i = 0; i < documentIds.length; i++){
 
185
 
 
186
                   if(documentIds[i]===documentObject.docId && i < (documentIds.length-1)){
 
187
                       return documentIds[i+1]
 
188
                   }
 
189
                   else if(documentIds[i]===documentObject.docId && i==(documentIds.length-1)){
 
190
                       return documentIds[0]
 
191
                   }
 
192
 
 
193
               }
 
194
 
 
195
               return documentIds[0]
 
196
 
 
197
           }
 
198
 
 
199
           else{
 
200
 
 
201
               print("Error!")
 
202
 
 
203
               return ''
 
204
           }
 
205
 
 
206
       }
 
207
 
 
208
        function getCurrentDocumentKey(contentsObject){
 
209
 
 
210
            if(typeof contentsObject!='undefined'){
 
211
 
 
212
                var keys = Object.keys(contentsObject);
 
213
 
 
214
                return keys[0]
 
215
 
 
216
            }
 
217
 
 
218
            else{
 
219
 
 
220
                return ''
 
221
            }
 
222
 
 
223
        }
 
224
 
 
225
        function updateContentWindow(documentText, addressBarText) {
 
226
 
 
227
            // Somewhere below need to check for things like invalid docId
 
228
 
 
229
            if(documentText!==addressBarText) {
 
230
 
 
231
                /*!
 
232
 
 
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.
 
234
 
 
235
                var tempDocument = {}
 
236
                var tempFieldName = addressBarText;
 
237
                var tempContents = {};
 
238
 
 
239
                tempDocument = aDocument
 
240
                tempDocument.docId = addressBarText;
 
241
 
 
242
                tempContents = tempDocument.contents
 
243
 
 
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.
 
245
 
 
246
                */
 
247
 
 
248
                var tempDocument = {}
 
249
                var tempFieldName = addressBarText;
 
250
                var tempContents = {};
 
251
 
 
252
                tempDocument = aDocument
 
253
                tempDocument.docId = addressBarText;
 
254
 
 
255
                tempContents = tempDocument.contents
 
256
 
 
257
                if(typeof tempContents !='undefined' && typeof tempContents[tempFieldName]!='undefined') {
 
258
 
 
259
                    aDocument = tempDocument
 
260
                    documentContent.text = tempContents[tempFieldName]
 
261
 
 
262
                }
 
263
                else {
 
264
 
 
265
                    /*!
 
266
 
 
267
                    Here the contents of the temporary document are modified, which then replaces the global document.
 
268
 
 
269
                    documentContent.text = 'More Hello World...';
 
270
 
 
271
                    tempContents = {}
 
272
                    tempContents[tempFieldName] = documentContent.text
 
273
                    tempDocument.contents = tempContents
 
274
                    aDocument = tempDocument
 
275
 
 
276
                    */
 
277
 
 
278
                    documentContent.text = 'More Hello World...';
 
279
 
 
280
                    tempContents = {}
 
281
                    tempContents[tempFieldName] = documentContent.text
 
282
                    tempDocument.contents = tempContents
 
283
                    aDocument = tempDocument
 
284
 
 
285
                }
 
286
 
 
287
            }
 
288
            else {
 
289
 
 
290
                /*!
 
291
 
 
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.
 
293
 
 
294
                tempContents = {}
 
295
                tempFieldName = getCurrentDocumentKey(aDocument.contents)
 
296
                tempContents[tempFieldName] = documentContent.text
 
297
                aDocument.contents = tempContents
 
298
 
 
299
                */
 
300
 
 
301
                tempContents = {}
 
302
                tempFieldName = getCurrentDocumentKey(aDocument.contents)
 
303
                tempContents[tempFieldName] = documentContent.text
 
304
                aDocument.contents = tempContents
 
305
 
 
306
            }
 
307
 
 
308
        }
 
309
 
 
310
        Tabs {
 
311
            id: tabs
 
312
 
 
313
            Tab {
 
314
                title: i18n.tr("Hello U1Db!")
 
315
 
 
316
                page: Page {
 
317
 
 
318
                    id: helloPage
 
319
 
 
320
                    /*! Here a rectangle is defined that represents the lower portion of our application. It will contain all the main parts of the application.
 
321
 
 
322
                    Rectangle {
 
323
 
 
324
                         width: units.gu(45)
 
325
                         height: units.gu(70)
 
326
                         anchors.bottom: parent.bottom
 
327
 
 
328
                         color: "#00FFFFFF"
 
329
 
 
330
                         // The remainder of the main part of the application goes here ...
 
331
 
 
332
                         }
 
333
 
 
334
                     */
 
335
 
 
336
                    Rectangle {
 
337
 
 
338
                         width: units.gu(45)
 
339
                         height: units.gu(70)
 
340
                         anchors.fill: parent
 
341
 
 
342
                         color: "#00FFFFFF"
 
343
 
 
344
                         Rectangle {
 
345
 
 
346
                            width: units.gu(45)
 
347
                            height: units.gu(60)
 
348
                            anchors.bottom: parent.bottom
 
349
 
 
350
                            /*!
 
351
 
 
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.
 
353
 
 
354
                            TextArea{
 
355
 
 
356
                                id: documentContent
 
357
 
 
358
                                selectByMouse : false
 
359
 
 
360
                                x: units.gu(1)
 
361
                                y: units.gu(1)
 
362
                                width: units.gu(43)
 
363
                                height: units.gu(58)
 
364
                                color: "#000000"
 
365
 
 
366
                            }
 
367
 
 
368
                             */
 
369
 
 
370
                            TextArea {
 
371
 
 
372
                                id: documentContent
 
373
 
 
374
                                selectByMouse : false
 
375
 
 
376
                                x: units.gu(1)
 
377
                                y: units.gu(1)
 
378
                                width: units.gu(43)
 
379
                                height: units.gu(58)
 
380
                                color: "#000000"
 
381
 
 
382
                            }
 
383
 
 
384
                         }
 
385
 
 
386
                         // This rectangle contains the navigation controls
 
387
 
 
388
                         Rectangle {
 
389
 
 
390
                              width: units.gu(43)
 
391
                              height: units.gu(5)
 
392
                              anchors.top: addressBarArea.bottom
 
393
                              x: units.gu(1.5)
 
394
                              color: "#00FFFFFF"
 
395
 
 
396
                              Row {
 
397
 
 
398
                                 width: units.gu(43)
 
399
                                 height: units.gu(5)
 
400
                                 anchors.verticalCenter: parent.verticalCenter
 
401
                                 spacing: units.gu(2)
 
402
 
 
403
                                 Button {
 
404
                                 text: "<"
 
405
                                 onClicked: updateContentWindow(switchToPreviousDocument(aDocument), addressBar.text)
 
406
                                 }
 
407
                                 Button {
 
408
                                 text: "Home"
 
409
                                 onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),'helloworld')
 
410
                                                                 }
 
411
                                 Button {
 
412
                                 text: "Save"
 
413
                                 onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),addressBar.text)
 
414
                                 }
 
415
                                 Button {
 
416
                                 text: ">"
 
417
                                 onClicked: updateContentWindow(switchToNextDocument(aDocument), addressBar.text)
 
418
                                 }
 
419
 
 
420
                              }
 
421
 
 
422
                          }
 
423
 
 
424
                          Rectangle {
 
425
 
 
426
                            id: addressBarArea
 
427
 
 
428
                            width: units.gu(45)
 
429
                            height: units.gu(5)
 
430
                            anchors.top: parent.top
 
431
 
 
432
                            TextField {
 
433
 
 
434
                                    id: addressBar
 
435
 
 
436
                                    width: units.gu(43)
 
437
                                    anchors.verticalCenter: parent.verticalCenter
 
438
                                    x: units.gu(1)
 
439
 
 
440
                                    hasClearButton: false
 
441
 
 
442
                                    /*!
 
443
 
 
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:
 
445
 
 
446
                                    text: displayKey(aDocument.contents)
 
447
 
 
448
                                    function displayKey(documentObject){
 
449
 
 
450
                                        var keys = Object.keys(documentObject);
 
451
 
 
452
                                        return keys[0]
 
453
 
 
454
                                    }
 
455
 
 
456
                                    */
 
457
 
 
458
                                    text: getCurrentDocumentKey(aDocument.contents)
 
459
 
 
460
 
 
461
                                    onAccepted: {
 
462
 
 
463
                                        onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),addressBar.text)
 
464
 
 
465
                                    }
 
466
                                }
 
467
                        }
 
468
                    }
 
469
                }
 
470
            }
 
471
 
 
472
            Tab {
 
473
                title: i18n.tr("Bookmarks")
 
474
                page: Page {
 
475
                    id: bookmarkPage
 
476
 
 
477
                    Label {
 
478
                        text: aDocument.contents.helloworld
 
479
                    }
 
480
 
 
481
                }
 
482
            }
 
483
        }
 
484
}