summaryrefslogtreecommitdiffstats
path: root/symbian_dev_guide/chapter_04/step_02.rst
blob: c9b3ce71fbc7849d112fc341001c472dfbd914b4 (plain)
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
..
    ---------------------------------------------------------------------------
    Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
    All rights reserved.
    This work, unless otherwise expressly stated, is licensed under a
    Creative Commons Attribution-ShareAlike 2.5.
    The full license document is available from
    http://creativecommons.org/licenses/by-sa/2.5/legalcode .
    ---------------------------------------------------------------------------

Populating Pages with Data from the Storage
===========================================

Now that our database is ready, we need to replace the test data used in the previous chapter with data provided from the database. In this step, we'll see how to populate the `BoxPage` with data provided by the database.

Earlier we added a `boxId` property to the `BoxPage` which identifies each of the boxes. As we have defined four boxes in the `HomePage`, let's specify an id for each box as follows:

.. table::

    =========== ===========
        Box*       *Id*
    =========== ===========
    Inbox       0
    Today       1
    Next        2
    Later       3
    Archive     4
    =========== ===========

.. Note:: The `Archive` box will be used to archive the `done` tasks.

On the `HomePage`, once a box has been selected, we should provide the corresponding `boxId` to the `BoxPage` in order to read and display all the items it contains from the database as shown in the following figure:

.. image:: img/database-1.png
   :scale: 65%
   :align: center

Let's start by updating the UI components of the `BoxPage` before we get the data from the database. For sake of clarity, we'll proceed as follows:

* Update the listView header and the `TodoItemDelegate`
* Update the BoxPage listView
* Update the HomePage listView
* Load Data from database

Headers and Delegates
---------------------

We have defined several QML components in our code. Now let's determine if we have some code duplicated or if there's a need to make it more efficient for future use. So let's create a generic component for title headers in a new `TitleHeader.qml` file in the *delegate* folder to define a reusable header that can be shared by multiple list views.

.. image:: img/title-header-src.png
   :scale: 65%
   :align: center

The `TitleHeader` implements a :component:`ListHeading <qml-listheading.html>` Component which contains a :component:`ListItemText <qml-listitemtext.html>` and a property alias to display the current list view's title.

.. code-block:: js

    // TitleHeader.qml

    ListHeading {
        id: listHeading
        anchors {
            top: parent.top
            left: parent.left; right: parent.right
        }

        property alias text: itemText.text

        ListItemText {
            id: itemText
            anchors.fill: parent.paddingItem
            role: "Heading"
        }
    }


In the `TodoItemDelegate`, we had defined a `ListItemText` to display the *todo's* title, and a checkbox to check whether or not the *todo* item has been take care of. In order to update the values of the `TodoItemDelegate` fields from the model, we need the following property aliases:

* A `title` to display the todo title
* A `checked` to mark the checkbox if the task has been taken care of

.. code-block:: js

    // TodoItemDelegate.qml

    ListItem {
        id: listItem
        ...
        property alias title: todoTitle.text
        property alias checked: checkBox.checked
        ...
    }


We also add a signal to be emitted when the checkbox is clicked. This will enable us later to update the item on the database:

.. code-block:: js

    // TodoItemDelegate.qml

    ListItem {
        id: listItem
        ...
        signal checkBoxClicked()
        ...
        Row {
            ...
            CheckBox {
                ...
                onClicked: listItem.checkBoxClicked();
            }
            ...
        }
    }


Updating BoxPage
----------------

In `BoxPage`, we add a new property to display the box `title` using `TitleHeader` we defined above. The title should be loaded later from the database:

.. code-block:: js

    // BoxPage.qml

    Page {
        id: root
        ...
        property string title

        TitleHeader {
            id: header
            text:  root.title
        }
    }


The content of the `ListModel` component will be dynamically defined by using the database. To insure that the *done* item's `CheckBox` component will be checked, we need to bind the value of the model's *done* field to the `checked` property of the `TodoItemDelegate` in the list view:

.. code-block:: js

    // BoxPage.qml

    Page {
        ...
        ListModel {
            id: itemModel
        }


        ListView {
            ...
            model: itemModel
            delegate: TodoItemDelegate{
                ...
                // bind the done value of the model
                // to the checked property
                checked: model.done
                ...
            }
         ...
        }
    }


Updating HomePage
-----------------

Let's use `TitleHeader` as the home page's header (which will replace the list view header):

.. code-block:: js

    // HomePage.qml

    TitleHeader {
        id: header
        text: "Todo List"
    }


As we mentioned above, the `HomePage` should provide the corresponding `boxId`, and the `title` of the selected box once we push the `BoxPage` to the stack. The `push` method of the `PageStak` component accepts arguments that allow us to specify values for properties on the page being pushed. In our case, we will use it to set the values to the `boxId` and `title` properties of the `BoxPage`.


.. code-block:: js

    // HomePage.qml

    ListView {
        ...
        model: boxModel
        delegate: BoxItemDelegate {
            ...
            onClicked: {
                // push the BoxPage with the convenient
                // boxId and title values
                root.pageStack.push(
                    window.boxPage,
                    { boxId: model.box, title: model.name }
                );
            }
        }
        ...
    }


Getting Data from the Database
------------------------------

Now that we've updated the UI components of the `HomePage` and `BoxPage`, let's add the function needed to fill the listView with data from the database.

The first thing to do is open the database in the application startup so all pages can connect to the database later.

.. code-block:: js

     // main.qml

    import "js/core.js" as Core
    ...
    Component.onCompleted:
    {
        Core.openDB();
        pageStack.push(Qt.resolvedUrl("HomePage.qml"));
    }

We have reimplimented the main window's `onCompleted` signal handler to call the `openDB` function defined in `core.js` just before pushing the `HomePage` onto the stack. The `openDB` function opens the database and creates a *todo* table, if it doesn't yet exist, by calling a `createTable` function:


.. code-block:: js

    // core.js

    function openDB() {
        _db = openDatabaseSync("TodoDB","1.0","Todo Database",1000000);
        createTable();
    }

    function createTable() {
        _db.transaction( function(tx) {
            tx.executeSql(" \
                CREATE TABLE IF NOT EXISTS \
                todo ( \
                    id INTEGER PRIMARY KEY AUTOINCREMENT, \
                    box INTEGER, \
                    done TEXT, \
                    title TEXT, \
                    note TEXT, \
                    modified TEXT \
            )");
        });
    }


We update the *done* item's `done` field when the `Checkbox` is clicked. Once a TodoItemDelegate's checkbox is clicked, we update the todo's `done` field.

.. code-block:: js

    // BoxPage.qml

    ListView{
        ...
        delegate: TodoItemDelegate {
            ...
               onCheckBoxClicked: {
                if(checked != model.done) {
                    // update the done field value of itemModel
                    itemModel.setProperty(index, 'done', checked);
                    // update Todo in the database
                    Core.updateTodo(model);
                }
            }
            ...
        }
        ...
    }


The `updateTodo` function, defined in `core.js`, updates a *todo* in the database.

.. code-block:: js

    // core.js

    function updateTodo(todoItem)
    {
        _db.transaction( function(tx) {
            tx.executeSql(" \
                UPDATE todo SET BOX = ? , done = ?, \
                title = ?, note = ?, modified = ? WHERE id = ?",
                [ todoItem.box, todoItem.done, todoItem.title,
                  todoItem.note, todoItem.modified, todoItem.id ]
            );
        });
    }

.. Note:: For sake of performance, we should use the `onCheckBoxClicked` signal instead of the `onChecked` signal. The `onChecked` signal is emitted every time the page is activated for each checked item, but we only need to update it when the user has checked the item.

To populate the `ListView` from the database, we perform the following steps:

* Add a new `updateUi` function, which resets the current model
* Read the items that belong to the box with the matching `boxId` into `itemModel` via the `readTodoBox` function
* Reset the `ListView` model to `itemModel`

.. code-block:: js

    // BoxPage.qml

    import "../js/core.js" as Core
    ...

    function updateUi()
    {
        itemView.model = 0;
        Core.readTodoBox(itemModel, boxId);
        itemView.model = itemModel;
    }


The `readTodoBox` function will read all items from the `todo` table that belong to the selected `box` and load them into an empty `model`:

.. code-block:: js

    // core.js

    function readTodoBox(model, boxId)
    {
         // first we clear the model
        model.clear();
        // create a ReadOnly Sql transaction
        _db.readTransaction( function(tx) {
            // read Items from Database where box = boxId
            var rs = tx.executeSql("SELECT * FROM todo WHERE box =? \
                ORDER BY modified DESC", boxId);

            for (var i=0; i< rs.rows.length; i++) {
                // load the result into the model
                model.append(rs.rows.item(i));
            }
        });
    }

The `updateUi` function will be called once the `BoxPage` has been activated:

.. code-block:: js

    // BoxPage.qml

    onStatusChanged: {
        if(status == PageStatus.Activating) {
            updateUi();
        }
    }


Creating Test Data
------------------

In this section, we'll provide an additional page that enables you to test the database connection by resetting the SQL table and adding random items. To do so, we need to create a new `ToolPage` in the `page` folder page that will contain a list of useful actions.

.. image:: img/toolpage-src.png
    :scale: 65%
    :align: center

In our case, the `ToolPage` will provide a `reset` action that resets the `Todo` table from the database and actions to insert 10, 100 or 1000 new items into the database.

Here's how the UI form of `ToolPage` should look:

.. image:: img/toolpage-concept.png
    :scale: 40%
    :align: center

In the `ToolPage`, we need to declare the following components:

 * A `ListModel` to define the actions to be displayed on the `listView`:

.. code-block:: js

    // ToolPage.qml

    import QtQuick 1.0
    import com.nokia.symbian 1.1

    Page {
        id: root

            ListModel {
                id: caseModel

                ListElement { title: "Reset Table";           name : "resetTable"  }
                ListElement { title: "10 Todo Item";          name: "createTodos10" }
                ListElement { title: "100 Random Todo Item";  name: "createTodos100" }
                ListElement { title: "1000 Random Todo Item"; name: "createTodos1000" }
            }
        ...
    }


* A `TitleHeader` for the page:

.. code-block:: js

    // ToolPage.qml

    TitleHeader {
        id: header
        text: "Tool Page"
    }


* And a `ListView` that uses the `BoxItemDelegate` we have previously defined to display the list of actions :

.. code-block:: js

    // ToolPage.qml

    import "../delegates"
    ...
    ListView {
        id: view
        anchors {
            left: root.left; right:  root.right;
            top: header.bottom; bottom: parent.bottom
        }
        clip: true
        model: caseModel
        delegate: BoxItemDelegate {
            id: itemDelegate
            title: model.title
            subItemIndicator: false
            // the tunTest enables to perform the actions
            onClicked : runTest(model.name);
        }
    }


The `runTest` function will call the functions that correspond to the selected action with the convenient arguments:

.. code-block:: js

    // ToolPage.qml

    import "../js/core.js" as Core

    ...

    function runTest(testName)
    {
        print("run test " + testName)
        switch(testName){
        case "resetTable" :
            // Drop And Recreate the table
            Core.dropTable();
            Core.createTable();
            break;
        case "createTodos10" :
            // Add 10 random Todos
            createTodosRandomBox(10);
            break;
        case "createTodos100" :
            // Add 100 random Todos
            createTodosRandomBox(100);
            break;
        case "createTodos1000" :
            // Add 1000 random Todos
            createTodosRandomBox(1000);
            break;
        }
    }

The `createTodosRandomBox` simply creates random data and add it to the database via the `createBathTodos` function defined in the `core.js` file.

.. code-block:: js

    // ToolPage.qml

    function createTodosRandomBox(count)
    {
        var data = []
        for (var i=0; i< count; i++){
            data[i] = {
                box: Math.floor(Math.random()*5),
                done: i%2?true:false,
                title: "Todo"+i+1,
                note: "No Note",
                modified: new Date()
            }
        }
        Core.createBatchTodos(data);
    }


The `createBtachTodos` enables us to add an array of todos into the database as follows:

.. code-block:: js

    // core.js

    function createBatchTodos(data)
    {
        _db.transaction(
                    function(tx){
                        for(var i=0; i<data.length; i++) {
                            var item = data[i]
                            tx.executeSql("INSERT INTO todo \
                            (box, done, title, note, modified) VALUES(?,?,?,?,?)",
                            [item.box, item.done, item.title, item.note, item.modified]);
                        }
                    }
                    )
    }


Finally we add a `ToolBarLayout` that contains a `ToolButton` to go back to the `HomePage`.

.. code-block:: js

    // ToolPage.qml

    tools: ToolBarLayout {
        ToolButton {iconSource: "toolbar-back"; onClicked: root.pageStack.pop();}
    }


Now that our `ToolPage` is ready for use, we add a `ToolButton` in the `HomePage` to open it.

.. code-block:: js

    // HomePage.qml

    tools: ToolBarLayout {
        ...
        ToolButton {
            iconSource: "toolbar-settings";
            onClicked: root.pageStack.push(Qt.resolvedUrl("ToolPage.qml"))
        }
        ...
    }


If your run the code, you should now be able to add random todos into the database and display them in the ``BoxPage``.

.. image:: img/toolpage.png
    :scale: 40%
    :align: center

.. rubric:: What's next?

Now that our application loads items from a database and displays them in the `BoxPage`, we'll start updating the `TodoPage` in the next step to display an item's details using QML Input/Output field components.