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

« back to all changes in this revision

Viewing changes to documentation/u1db.tutorial.qdoc

  • 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
 
 
3
\page tutorial.html
 
4
 
 
5
\title Tutorial
 
6
  
 
7
  
 
8
\chapter 1.0.0 Overview of U1Db-Qt  
 
9
 
 
10
U1Db-Qt is a QML plugin written in Qt C++. It provides declarative, easy to use, local data storage for QML applications, and in the future will also include remote sync capabilities.
 
11
 
 
12
U1Db-Qt is based on a procedural implementation in Python, but has been designed and developed from the start with declarative programming in mind. 
 
13
 
 
14
While U1Db-Qt makes use of SQLite in the back end, and relies heavily on JSON, these are largely invisible to a QML developer who makes use of the plugin. However, because U1Db-Qt does rely on both technologies it is possible to easily debug applications using existing programs. For example, a developer could make use of SQLiteBrowser to read U1Db database files, which contain human readable JSON objects representing content derived from a QML application. 
 
15
 
 
16
\section1 1.0.1 How to Make U1Db-Qt Available to a QML Application
 
17
 
 
18
Here is an example import statement:
 
19
 
 
20
\code
 
21
import U1db 1.0 as U1db                                 
 
22
\endcode
 
23
 
 
24
\chapter 2.0.0 Database Element
 
25
 
 
26
In U1Db-Qt, the Database element is the the central figure that works in conjunction with Document elements, and in the future indexing (currently under development), and querying (currently under development) elements.      It is the Database element that defines the location of the data repository.
 
27
 
 
28
A Database element is also a valuable tool for cases where specific data from a repository needs to be retrieved quickly, without indexing or querying ahead of time.   The 'contents' object associated with the Database element can be used together with base items such as TextField or TextArea and model-based items like ListView and \l {Standard}{ListItems.Standard}.
 
29
   
 
30
\section1 2.0.1 Database Properties
 
31
 
 
32
\code
 
33
QString path
 
34
QString error   
 
35
\endcode   
 
36
    
 
37
\section1 2.1.0 Creating a Database
 
38
    
 
39
A Database is very simple to create. It requires nothing more than an id and a path where the file will be created. 
 
40
    
 
41
\section2 2.1.1 Example of Creating a Database
 
42
 
 
43
\code
 
44
import QtQuick 2.0
 
45
import U1db 1.0 as U1db 
 
46
 
 
47
Item{   
 
48
        U1db.Database {
 
49
                id: aDatabase
 
50
                path: "aU1DbDatabase"
 
51
        }
 
52
}        
 
53
\endcode
 
54
        
 
55
\chapter 3.0.0 Document Element
 
56
 
 
57
The Document element is the primary vehicle for entering data into a repository, and can be helpful in some cases for getting data out as well. Indexing and querying would normally provide more robust functionality for extracting data from a repository, but in the case of U1Db-Qt both are still under development at the time of writing (and therefore not available for developers to use). 
 
58
 
 
59
However, the Document element together with Database can still help developers in many common situations, and will continue to be very useful even when the indexing and querying functionality becomes available. When a developer wants unfiltered results from a database, or the cost of working with unfiltered results is reasonable, the Document+Database combination is fast and easy to use. In quite a number of use cases this may be exactly what a developer needs.
 
60
 
 
61
\section1 3.0.1 Document Properties
 
62
 
 
63
\code
 
64
U1db.Database database 
 
65
QString docId 
 
66
bool create 
 
67
QVariant defaults 
 
68
QVariant contents   
 
69
\endcode
 
70
 
 
71
\section1 3.1.0 Creating a Basic Document
 
72
 
 
73
A Document declaration should contain at least a unique 'docId' and 'database', but these alone won't do anything by themselves. Additionally, although the 'id' property is not mandatory, this property will need to be set in order to more easily reference it from elsewhere in the program (e.g. within a function call). 
 
74
 
 
75
\section2 3.1.0.1 Example of Creating a Basic Document
 
76
 
 
77
\code
 
78
import QtQuick 2.0
 
79
import U1db 1.0 as U1db 
 
80
import Ubuntu.Components 0.1
 
81
 
 
82
Item{
 
83
 
 
84
        width: units.gu(45)
 
85
        height: units.gu(80)
 
86
        
 
87
        U1db.Database {
 
88
                id: aDatabase
 
89
                path: "aU1DbDatabase"
 
90
        }
 
91
                               
 
92
}
 
93
\endcode
 
94
 
 
95
\section1 3.1.1 Creating a Document At Runtime
 
96
 
 
97
A Document can be declared at runtime, and default data entered into the repository. This requires the same properties to be set as in the basic example ('id', 'database' and 'docId'), plus setting 'create' (to true) and a 'default' string.  
 
98
        
 
99
\section2 3.1.1.1 Example of Creating a Document At Runtime
 
100
 
 
101
\code
 
102
import QtQuick 2.0
 
103
import U1db 1.0 as U1db 
 
104
import Ubuntu.Components 0.1
 
105
 
 
106
Item{
 
107
 
 
108
        width: units.gu(45)
 
109
        height: units.gu(80)
 
110
        
 
111
        U1db.Database {
 
112
                id: aDatabase
 
113
                path: "aU1DbDatabase"
 
114
        }
 
115
                               
 
116
        U1db.Document {
 
117
                id: aDocument
 
118
                database: aDatabase
 
119
                docId: 'helloworld'
 
120
                create: true
 
121
                defaults: { "hello": "Hello World!" }
 
122
        }
 
123
 
 
124
}
 
125
\endcode
 
126
 
 
127
\section1 3.1.2 Creating a Document Dynamically
 
128
 
 
129
Creating a Document in a dynamic fashion is the most common way of putting data into a data repository based on UI activity (e.g. when a user presses a button). 
 
130
 
 
131
\section2 3.1.2.1 Example 1 of Creating a Document Dynamically
 
132
 
 
133
Another way of creating a new Document is to copy an existing Document:
 
134
 
 
135
\code
 
136
import QtQuick 2.0
 
137
import U1db 1.0 as U1db
 
138
import Ubuntu.Components 0.1
 
139
 
 
140
Item{
 
141
 
 
142
        width: units.gu(45)
 
143
        height: units.gu(80)
 
144
        
 
145
        U1db.Database {
 
146
                id: aDatabase
 
147
                path: "aU1DbDatabase"
 
148
        }
 
149
 
 
150
        U1db.Document {
 
151
                id: aDocument
 
152
                database: aDatabase
 
153
                docId: 'helloworld'
 
154
        }
 
155
 
 
156
        function someFunction() {
 
157
                var tempDocument = {}
 
158
                tempDocument = aDocument
 
159
        }
 
160
 
 
161
}
 
162
\endcode
 
163
 
 
164
\section2 3.1.2.2 Example 2 of Creating a Document Dynamically
 
165
 
 
166
One way of creating a new Document dynamically is to make use of Qt.createQmlObject:
 
167
 
 
168
\code
 
169
import QtQuick 2.0
 
170
import U1db 1.0 as U1db 
 
171
import Ubuntu.Components 0.1
 
172
 
 
173
Item{
 
174
 
 
175
        width: units.gu(45)
 
176
        height: units.gu(80)
 
177
        
 
178
        U1db.Database {
 
179
 
 
180
                id: aDatabase
 
181
                path: "aU1DbDatabase"
 
182
                Component.onCompleted: { newDocumentObject() }
 
183
        
 
184
                function newDocumentObject() {
 
185
 
 
186
                        var qmlString = "import QtQuick 2.0; import U1db 1.0 as U1db; U1db.Document {id: aDcoument; database: aDatabase; docId: 'helloworld'; create: true; defaults: { 'hello': 'Hello New Document!' }}"
 
187
 
 
188
                        Qt.createQmlObject(qmlString, aDatabase);
 
189
 
 
190
                }
 
191
 
 
192
        }
 
193
 
 
194
}
 
195
\endcode
 
196
 
 
197
 
 
198
 
 
199
 
 
200
\chapter 4.0.0 U1Db-Qt and QML Elements and Models
 
201
 
 
202
\section1 4.1.0 U1Db-Qt and Standard Elements
 
203
 
 
204
\section2 4.1.1 U1Db-Qt and TextField & TextArea
 
205
 
 
206
\section3 4.1.2 Example of Using U1Db-Qt with Standard Elements
 
207
 
 
208
\code
 
209
import QtQuick 2.0
 
210
import U1db 1.0 as U1db
 
211
import Ubuntu.Components 0.1
 
212
 
 
213
Item{
 
214
 
 
215
        width: units.gu(45)
 
216
        height: units.gu(80)
 
217
        
 
218
        function getContent(fieldName){
 
219
        
 
220
                var tempContents = {};
 
221
                tempContents = aDocument.contents
 
222
                return tempContents[fieldName]
 
223
 
 
224
        }
 
225
 
 
226
        U1db.Database {
 
227
                id: aDatabase
 
228
                path: "aU1DbDatabase"
 
229
        }
 
230
 
 
231
        U1db.Document {
 
232
                id: aDocument
 
233
                database: aDatabase
 
234
                docId: 'helloworld'
 
235
                create: true
 
236
                defaults: { "hello": "Hello World 1!" }
 
237
        }
 
238
 
 
239
 
 
240
        TextField {
 
241
                id: addressBar
 
242
                width: units.gu(45)
 
243
                text: getContent('hello')
 
244
        }
 
245
 
 
246
}
 
247
\endcode
 
248
\section1 4.2.0 U1Db-Qt and Model-Based Elements
 
249
 
 
250
\section2 4.2.1 U1Db-Qt and ListView
 
251
 
 
252
\section3 4.2.2 Example of Using U1Db-Qt with Model-Based Elements 
 
253
 
 
254
\code
 
255
import QtQuick 2.0
 
256
import U1db 1.0 as U1db
 
257
import Ubuntu.Components 0.1
 
258
 
 
259
Item{
 
260
 
 
261
        width: units.gu(45)
 
262
        height: units.gu(80)
 
263
 
 
264
        U1db.Database {
 
265
                id: aDatabase
 
266
                path: "aU1DbDatabase"
 
267
        }
 
268
 
 
269
        U1db.Document {
 
270
                id: aDocument1
 
271
                database: aDatabase
 
272
                docId: 'helloworld'
 
273
                create: true
 
274
                defaults: { "hello": "Hello World 1!" }
 
275
        }
 
276
 
 
277
        U1db.Document {
 
278
                id: aDocument2
 
279
                database: aDatabase
 
280
                docId: 'helloworld'
 
281
                create: true
 
282
                defaults: { "hello": "Hello World 2!" }
 
283
        }
 
284
 
 
285
        ListView {
 
286
 
 
287
                model: aDatabase
 
288
                width: units.gu(45)
 
289
                height: units.gu(80)
 
290
                
 
291
                delegate: Text {
 
292
                x: 66; y: 77
 
293
                text: contents.hello
 
294
                }
 
295
 
 
296
        }
 
297
 
 
298
}
 
299
 
 
300
\endcode
 
301
 
 
302
\chapter 5.0.0 Resources
 
303
 
 
304
\section1 5.0.1 Examples   
 
305
 
 
306
One can find several examples in the bzr branch of U1Db-Qt (bzr branch lp:u1db-qt) either in the subdirectory "examples" or from the following url:
 
307
 
 
308
http://bazaar.launchpad.net/~uonedb-qt/u1db-qt/trunk/files/head:/examples/
 
309
 
 
310
These examples are currently under development (as is U1Db-Qt in general), but should still be able to demonstrate the fundamentals discussed within this document.
 
311
 
 
312
 
 
313
 
 
314
*/