Various improvements to LinePad + UI flow
[pywienerlinien] / qml / LinePad.qml
1
2 import QtQuick 1.0
3 import QtMobility.feedback 1.1
4
5 Rectangle {
6     id: linePad
7     property alias currentLine: inputLine.text
8
9     /* List of available lines - will be filled w/ real data by LineSheet */
10     property variant availableLines: ['59A', '63A', '58']
11
12     property variant matches: availableLines
13
14     onMatchesChanged: {
15         if (matches !== undefined) {
16             if (matches.length == 1) {
17                 inputLine.text = matches[0];
18             }
19         }
20     }
21
22     function getMatches(prefix) {
23         var result = [];
24
25         for (var i in availableLines) {
26             var line = availableLines[i];
27             if (line.indexOf(prefix) == 0) {
28                 result.push(line);
29             }
30         }
31
32         return result;
33     }
34
35     height: 800
36     width: 480
37
38      HapticsEffect {
39          id: buttonFeedback
40
41          /**
42           * Ideally we would use ThemeEffect here,
43           * but on Harmattan it has no effect (sic)
44           **/
45
46          attackIntensity: 0.5
47          attackTime: 100
48          intensity: 1.0
49          duration: 50
50          fadeTime: 0
51          fadeIntensity: 0.0
52      }
53
54     Text {
55         id: inputLine
56         horizontalAlignment: Text.AlignHCenter
57         verticalAlignment: Text.AlignVCenter
58         height: 100
59         anchors {
60             top: parent.top
61             left: parent.left
62             right: parent.right
63         }
64
65         font {
66             pixelSize: height * .9
67             bold: true
68         }
69
70         text: ''
71         onTextChanged: {
72             if (text != '') {
73                 linePad.matches = linePad.getMatches(text);
74             } else {
75                 linePad.matches = linePad.availableLines;
76             }
77         }
78
79         Image {
80             source: 'image://theme/icon-m-toolbar-backspace'
81             anchors {
82                 verticalCenter: parent.verticalCenter
83                 right: parent.right
84                 margins: 20
85             }
86
87             MouseArea {
88                 anchors {
89                     fill: parent
90                     margins: -(inputLine.height - height)/2
91                 }
92                 onClicked: {
93                     buttonFeedback.start()
94                     inputLine.text = ''
95                 }
96             }
97         }
98     }
99
100     Item {
101         id: inputState
102         property bool isMetro: inputLine.text[0] == 'U'
103     }
104
105     Repeater {
106         model: [1,2,3, 4,5,6, 7,8,9, 'A',0,'B', 'D','U','VRT', 'O','N','WLB']
107
108         Rectangle {
109             id: inputElement
110             property variant ch: modelData
111             property bool isCandidate
112
113             isCandidate: {
114                 for (var i in linePad.matches) {
115                     if (ch == matches[i][inputLine.text.length]) {
116                         return true;
117                     } else if ((ch == 'VRT' || ch == 'WLB') && inputLine.text == '') {
118                         return true;
119                     }
120                 }
121
122                 return false;
123             }
124
125             opacity: isCandidate?1:.15
126             Behavior on opacity { PropertyAnimation { } }
127
128             color: {
129                 if (inputState.isMetro) {
130                     switch (ch) {
131                         case 1: return '#E20A16';
132                         case 2: return '#764785';
133                         case 3: return '#F76013';
134                         case 4: return '#008131';
135                         case 6: return '#88471F';
136                     }
137                 }
138                 return (index%2?'#ddd':'#eee');
139             }
140             width: parent.width/3
141             height: (parent.height-inputLine.height)/6
142             x: width*(index%3)
143             y: inputLine.height + height*parseInt(index/3)
144
145             Text {
146                 anchors.centerIn: parent
147                 text: modelData
148                 font {
149                     pixelSize: parent.height * .5
150                     bold: true
151                 }
152                 color: {
153                     if (inputState.isMetro) {
154                         return 'white';
155                     } else if (inputElement.isCandidate) {
156                         return 'black';
157                     }
158
159                     return '#ddd';
160                 }
161             }
162
163             MouseArea {
164                 anchors.fill: parent
165                 onClicked: {
166                     buttonFeedback.start()
167                     inputLine.text += modelData
168                 }
169             }
170         }
171     }
172 }
173