8
\chapter 1.0.0 Overview of U1Db-Qt
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.
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.
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.
16
\section1 1.0.1 How to Make U1Db-Qt Available to a QML Application
18
Here is an example import statement:
21
import U1db 1.0 as U1db
24
\chapter 2.0.0 Database Element
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.
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}.
30
\section1 2.0.1 Database Properties
37
\section1 2.1.0 Creating a Database
39
A Database is very simple to create. It requires nothing more than an id and a path where the file will be created.
41
\section2 2.1.1 Example of Creating a Database
45
import U1db 1.0 as U1db
55
\chapter 3.0.0 Document Element
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).
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.
61
\section1 3.0.1 Document Properties
64
U1db.Database database
71
\section1 3.1.0 Creating a Basic Document
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).
75
\section2 3.1.0.1 Example of Creating a Basic Document
79
import U1db 1.0 as U1db
80
import Ubuntu.Components 0.1
95
\section1 3.1.1 Creating a Document At Runtime
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.
99
\section2 3.1.1.1 Example of Creating a Document At Runtime
103
import U1db 1.0 as U1db
104
import Ubuntu.Components 0.1
113
path: "aU1DbDatabase"
121
defaults: { "hello": "Hello World!" }
127
\section1 3.1.2 Creating a Document Dynamically
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).
131
\section2 3.1.2.1 Example 1 of Creating a Document Dynamically
133
Another way of creating a new Document is to copy an existing Document:
137
import U1db 1.0 as U1db
138
import Ubuntu.Components 0.1
147
path: "aU1DbDatabase"
156
function someFunction() {
157
var tempDocument = {}
158
tempDocument = aDocument
164
\section2 3.1.2.2 Example 2 of Creating a Document Dynamically
166
One way of creating a new Document dynamically is to make use of Qt.createQmlObject:
170
import U1db 1.0 as U1db
171
import Ubuntu.Components 0.1
181
path: "aU1DbDatabase"
182
Component.onCompleted: { newDocumentObject() }
184
function newDocumentObject() {
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!' }}"
188
Qt.createQmlObject(qmlString, aDatabase);
200
\chapter 4.0.0 U1Db-Qt and QML Elements and Models
202
\section1 4.1.0 U1Db-Qt and Standard Elements
204
\section2 4.1.1 U1Db-Qt and TextField & TextArea
206
\section3 4.1.2 Example of Using U1Db-Qt with Standard Elements
210
import U1db 1.0 as U1db
211
import Ubuntu.Components 0.1
218
function getContent(fieldName){
220
var tempContents = {};
221
tempContents = aDocument.contents
222
return tempContents[fieldName]
228
path: "aU1DbDatabase"
236
defaults: { "hello": "Hello World 1!" }
243
text: getContent('hello')
248
\section1 4.2.0 U1Db-Qt and Model-Based Elements
250
\section2 4.2.1 U1Db-Qt and ListView
252
\section3 4.2.2 Example of Using U1Db-Qt with Model-Based Elements
256
import U1db 1.0 as U1db
257
import Ubuntu.Components 0.1
266
path: "aU1DbDatabase"
274
defaults: { "hello": "Hello World 1!" }
282
defaults: { "hello": "Hello World 2!" }
302
\chapter 5.0.0 Resources
304
\section1 5.0.1 Examples
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:
308
http://bazaar.launchpad.net/~uonedb-qt/u1db-qt/trunk/files/head:/examples/
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.