Sort station list alphabetically on startup
[quandoparte] / application / resources / harmattan / qml / StationListPage.qml
1 import QtQuick 1.0
2 import QtMobility.location 1.1
3 import com.nokia.meego 1.0
4 import net.cirulla.quandoparte 1.0
5
6 Page {
7     property variant stationView
8     id: stationListPage
9     Component.onCompleted: {
10         stationListProxyModel.sortingMode = StationListProxyModel.AlphaSorting
11     }
12     tools: ToolBarLayout {
13         id: toolBar
14         ToolIcon { iconId: "icon-m-toolbar-back"; onClicked: pageStack.pop(); }
15         ToolIcon { iconId: "icon-m-toolbar-settings"; onClicked: settingsSheet.open(); }
16         ToolIcon { iconId: "icon-m-toolbar-view-menu"; }
17     }
18
19     function loadStation()
20     {
21         var component = Qt.createComponent("StationPage.qml");
22         if (component.status == Component.Ready) {
23             var view = component.createObject(stationListPage)
24             stationListPage.stationView = view
25             pageStack.push(view)
26             view.html = "<h1>Hello World</h1><p>Lorem ipsum</p>"
27         }
28         else
29             console.log('Cannot load component: ' + component.errorString());
30     }
31
32     function highlightSearch(s)
33     {
34         // TODO compile RegExp on change, or find a way to cleanly use
35         // stationListProxyModel.filterRegExp
36         if (searchField.text.length) {
37             var r = new RegExp(searchField.text, 'i')
38             var match = r.exec(s)
39             return s.replace(r, '<span style="text-decoration:underline">' +
40                              match + '</span>')
41         } else {
42             return s
43         }
44     }
45
46     Column {
47         x: 16
48         y: 16
49         width: parent.width - 32
50         height: parent.height
51         spacing: 16
52         SearchBar {
53             id: searchField
54         }
55         Binding {
56             target: stationListProxyModel
57             property: "searchPattern"
58             value: searchField.text
59         }
60         ListView {
61             id: stationListView
62             clip: true
63             width: parent.width
64             height: parent.height
65             model:  stationListProxyModel
66             delegate: Item {
67                 id: listItem
68                 height: 48
69                 width: parent.width
70                 BorderImage {
71                     id: background
72                     anchors.fill: parent
73                     // Fill page borders
74                     visible: mouseArea.pressed
75                     source: "image://theme/meegotouch-list-background-pressed-center"
76                 }
77                 Row {
78                     anchors.fill: parent
79
80                     Column {
81                         anchors.verticalCenter: parent.verticalCenter
82
83                         Label {
84                             id: mainText
85                             text: highlightSearch(model.display)
86                             font.bold: true
87                         }
88                     }
89                 }
90                 MouseArea {
91                     id: mouseArea
92                     anchors.fill: background
93                     onClicked: {
94                         stationListPage.loadStation(model.display)
95                     }
96                 }
97             }
98         }
99     }
100
101     Sheet {
102         id: settingsSheet
103         acceptButtonText: "Save"
104         rejectButtonText: "Cancel"
105         content: Item {
106             x: 16
107             y: 16
108             width: parent.width - 32
109             height: parent.height - 32
110             Column {
111                 spacing: 16
112                 anchors.fill: parent
113                 Row {
114                     height: 40
115                     spacing: 16
116                     anchors.left: parent.left
117                     anchors.right: parent.right
118                     Label {
119                         font.bold: true
120                         text: "Show Last Station on Startup"
121                         anchors.verticalCenter: parent.verticalCenter
122                     }
123                     Switch {
124                         anchors.verticalCenter: parent.verticalCenter
125                         id: showLastStationSwitch
126                         anchors.right: parent.right
127                     }
128                 }
129                 Row {
130                     height: 40
131                     spacing: 16
132                     anchors.left: parent.left
133                     anchors.right: parent.right
134                     Label {
135                         font.bold: true
136                         text: "Update Display Periodically"
137                         anchors.verticalCenter: parent.verticalCenter
138                     }
139                     Switch {
140                         anchors.verticalCenter: parent.verticalCenter
141                         anchors.right: parent.right
142                         id: periodicCheckSwitch
143                     }
144                 }
145             }
146         }
147     }
148 }
149