/usr/share/u1db-qt/examples/u1db-qt-example-3.qml is in libu1db-qt5-examples 0.1.5+14.04.20140313-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 | /*
* Copyright (C) 2013 Canonical, Ltd.
*
* Authors:
* Kevin Wright <kevin.wright@canonical.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 2.0
import U1db 1.0 as U1db
import Ubuntu.Components 0.1
/*!
This example and tutorial is designed to show a wide variety of U1Db-Qt functionality and usage. The example demonstrates:
\list 1
\li Combining U1Db-Qt with elements and components that do not utilize models
\li Blending the U1Db-Qt plugin with QML and Javascript
\endlist
*/
MainView {
width: units.gu(45)
height: units.gu(80)
/*!
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.
U1db.Database {
id: aDatabase
path: "aDatabase3"
}
*/
U1db.Database {
id: aDatabase
path: "aDatabase3"
}
/*!
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.
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.
U1db.Document {
id: aDocument
database: aDatabase
docId: 'helloworld'
create: true
defaults: { "helloworld":"Hello World" }
Component.onCompleted: {
documentContent.text = aDocument.contents.helloworld
}
}
*/
U1db.Document {
id: aDocument
database: aDatabase
docId: 'helloworld'
create: true
defaults: { "helloworld":"Hello World" }
Component.onCompleted: {
documentContent.text = aDocument.contents.helloworld
}
}
function switchToPreviousDocument(documentObject){
aDocument.docId = getPreviousDocumentId(documentObject)
}
function switchToNextDocument(){
aDocument.docId = getNextDocumentId(aDocument)
}
function getPreviousDocumentId(documentObject){
if(typeof documentObject!='undefined'){
/*!
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.
var documentIds = {}
documentIds = documentObject.database.listDocs()
for(var i = 0; i < documentIds.length; i++){
if(documentIds[i]===documentObject.docId && i > 0){
return documentIds[i-1]
}
else if(documentIds[i]===documentObject.docId && i==0){
return documentIds[documentIds.length-1]
}
}
*/
var documentIds = {}
documentIds = documentObject.database.listDocs()
for(var i = 0; i < documentIds.length; i++){
if(documentIds[i]===documentObject.docId && i > 0){
return documentIds[i-1]
}
else if(documentIds[i]===documentObject.docId && i==0){
return documentIds[documentIds.length-1]
}
}
return documentIds[0]
}
else{
print("Error!")
return ''
}
}
function getNextDocumentId(documentObject){
if(typeof documentObject!='undefined'){
var documentIds = documentObject.database.listDocs()
for(var i = 0; i < documentIds.length; i++){
if(documentIds[i]===documentObject.docId && i < (documentIds.length-1)){
return documentIds[i+1]
}
else if(documentIds[i]===documentObject.docId && i==(documentIds.length-1)){
return documentIds[0]
}
}
return documentIds[0]
}
else{
print("Error!")
return ''
}
}
function getCurrentDocumentKey(contentsObject){
if(typeof contentsObject!='undefined'){
var keys = Object.keys(contentsObject);
return keys[0]
}
else{
return ''
}
}
function updateContentWindow(documentText, addressBarText) {
// Somewhere below need to check for things like invalid docId
if(documentText!==addressBarText) {
/*!
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.
var tempDocument = {}
var tempFieldName = addressBarText;
var tempContents = {};
tempDocument = aDocument
tempDocument.docId = addressBarText;
tempContents = tempDocument.contents
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.
*/
var tempDocument = {}
var tempFieldName = addressBarText;
var tempContents = {};
tempDocument = aDocument
tempDocument.docId = addressBarText;
tempContents = tempDocument.contents
if(typeof tempContents !='undefined' && typeof tempContents[tempFieldName]!='undefined') {
aDocument = tempDocument
documentContent.text = tempContents[tempFieldName]
}
else {
/*!
Here the contents of the temporary document are modified, which then replaces the global document.
documentContent.text = 'More Hello World...';
tempContents = {}
tempContents[tempFieldName] = documentContent.text
tempDocument.contents = tempContents
aDocument = tempDocument
*/
documentContent.text = 'More Hello World...';
tempContents = {}
tempContents[tempFieldName] = documentContent.text
tempDocument.contents = tempContents
aDocument = tempDocument
}
}
else {
/*!
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.
tempContents = {}
tempFieldName = getCurrentDocumentKey(aDocument.contents)
tempContents[tempFieldName] = documentContent.text
aDocument.contents = tempContents
*/
tempContents = {}
tempFieldName = getCurrentDocumentKey(aDocument.contents)
tempContents[tempFieldName] = documentContent.text
aDocument.contents = tempContents
}
}
Tabs {
id: tabs
Tab {
title: i18n.tr("Hello U1Db!")
page: Page {
id: helloPage
/*! Here a rectangle is defined that represents the lower portion of our application. It will contain all the main parts of the application.
Rectangle {
width: units.gu(45)
height: units.gu(70)
anchors.bottom: parent.bottom
color: "#00FFFFFF"
// The remainder of the main part of the application goes here ...
}
*/
Rectangle {
width: units.gu(45)
height: units.gu(70)
anchors.bottom: parent.bottom
color: "#00FFFFFF"
Rectangle {
width: units.gu(45)
height: units.gu(60)
anchors.bottom: parent.bottom
/*!
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.
TextArea{
id: documentContent
selectByMouse : false
x: units.gu(1)
y: units.gu(1)
width: units.gu(43)
height: units.gu(58)
color: "#000000"
}
*/
TextArea{
id: documentContent
selectByMouse : false
x: units.gu(1)
y: units.gu(1)
width: units.gu(43)
height: units.gu(58)
color: "#000000"
}
}
// This rectangle contains our navigation controls
Rectangle {
width: units.gu(43)
height: units.gu(5)
anchors.top: addressBarArea.bottom
x: units.gu(1.5)
color: "#00FFFFFF"
Row{
width: units.gu(43)
height: units.gu(5)
anchors.verticalCenter: parent.verticalCenter
spacing: units.gu(2)
Button {
text: "<"
onClicked: updateContentWindow(switchToPreviousDocument(aDocument), addressBar.text)
}
Button {
text: "Home"
onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),'helloworld')
}
Button {
text: "Save"
onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),addressBar.text)
}
Button {
text: ">"
onClicked: updateContentWindow(switchToNextDocument(aDocument), addressBar.text)
}
}
}
Rectangle {
id: addressBarArea
width: units.gu(45)
height: units.gu(5)
anchors.top: parent.top
TextField {
id: addressBar
width: units.gu(43)
anchors.verticalCenter: parent.verticalCenter
x: units.gu(1)
hasClearButton: false
/*!
There is an object within in the 'aDocument' model defined earlier called 'contents', which contains a key called 'helloworld', which represents a search string. In our 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:
text: displayKey(aDocument.contents)
function displayKey(documentObject){
var keys = Object.keys(documentObject);
return keys[0]
}
*/
text: getCurrentDocumentKey(aDocument.contents)
onAccepted: {
onClicked: updateContentWindow(getCurrentDocumentKey(aDocument.contents),addressBar.text)
}
}
}
}
}
}
}
}
|