sample/app/main: Remove flickable and make it more resilient
[apps/onscreenapp.git] / sample / app / main.qml
1 import QtQuick 2.7
2 import QtQuick.Window 2.2
3 import QtQuick.Controls 2.0
4 import QtQuick.Layouts 1.0
5 import AGL.Demo.Controls 1.0
6
7 ApplicationWindow {
8     id: root
9     visible: true
10     width: Screen.width
11     height: Screen.height
12
13     property string onsId: qsTr("onscreenapp")
14     property string onsTitle: qsTr("One Button title")
15     property string onsType: "critical"
16     property string onsContents: qsTr("An operating system is a program that manages a computer’s hardware.")
17     property string onsButton1: qsTr("Yes")
18     property string onsButton2: qsTr("")
19     property string onsButton3: qsTr("")
20     property string postmsg: qsTr("")
21     property string btndata: qsTr("")
22     property int pid: -1
23     property bool onscreen_role_set: false
24
25     property string mapp_id: ''
26     property string mapp_data_msg: ''
27
28     // position of the pop-up
29     property int x: 200
30     property int y: 200
31
32     Label {
33         id: title
34         width: parent.width
35         height: 40
36         text: "OnScreen Test App"
37         font.pixelSize: 40
38         font.bold: true
39         color: "white"
40     }
41
42     ColumnLayout {
43         spacing: 20
44         anchors.top: title.bottom
45         anchors.topMargin: 40
46         anchors.horizontalCenter: title.horizontalCenter
47
48         // show received reply information area
49         //Flickable {
50         //     id: flickable
51         //     width: 400
52         //     height: 220
53         //     Layout.alignment: Qt.AlignCenter
54         //     flickableDirection: Flickable.VerticalFlick
55         //     boundsBehavior: Flickable.StopAtBounds
56
57         //     TextArea.flickable: TextArea {
58         //         id: output
59         //         text: "show received reply information area\n...\n...\n...\n...\n"
60         //         font.pixelSize: 12
61         //         wrapMode: TextArea.Wrap
62         //         color: '#00ADDC'
63         //     }
64
65         //     ScrollBar.vertical: ScrollBar { }
66         // }
67
68         // select onscreen type area
69         GroupBox {
70             label: Label {
71                 text: qsTr("Please select send OnScreen Message")
72                 font.pixelSize: 32
73                 font.bold: true
74                 color: "white"
75             }
76
77             width: 800
78             height: 100
79             Layout.alignment: Qt.AlignLeft
80
81             RowLayout {
82                 anchors.fill: parent
83                 RadioButton {
84                     text: qsTr("One Button")
85                     font.pixelSize: 20
86                     checked: true
87                     onClicked: {
88                         selectOneButton();
89                     }
90                 }
91                 RadioButton {
92                     text: qsTr("Two Buttons")
93                     font.pixelSize: 20
94                     onClicked: {
95                         selectTwoButtons();
96                     }
97                 }
98                 RadioButton {
99                     text: qsTr("Three Buttons")
100                     font.pixelSize: 20
101                     onClicked: {
102                         selectThreeButtons();
103                     }
104                 }
105                 RadioButton {
106                     text: qsTr("No Button")
107                     font.pixelSize: 20
108                     onClicked: {
109                         selectNoButton();
110                     }
111                 }
112             }
113         }
114
115         // edit post message area
116         GroupBox {
117             label: Label {
118                 text: qsTr("Post Infomations")
119                 font.pixelSize: 32
120                 font.bold: true
121                 color: "white"
122             }
123             width: 800
124             height: 400
125             Layout.alignment: Qt.AlignLeft
126             Layout.maximumWidth: 800
127             Layout.maximumHeight: 400
128
129             ColumnLayout {
130                 spacing:  20
131                 RowLayout {
132                     spacing: 20
133                     Label {
134                         id: ons_title
135                         anchors.left: parent.left
136                         anchors.leftMargin: 30
137                         text: qsTr("ons_title:")
138                         font.pixelSize: 20
139                         font.italic: true
140                         color: '#00ADDC'
141                     }
142
143                     Label {
144                         id: ons_title_data
145                         text: onsTitle
146                         font.pixelSize: 20
147                         font.italic: true
148                         anchors.left: ons_title.right
149                         anchors.leftMargin: 20
150                         color: '#00ADDC'
151                         Layout.maximumWidth: 600
152                         Layout.maximumHeight: 40
153                         maximumLineCount: 1
154                         wrapMode: Text.Wrap
155                         elide: Text.ElideRight
156                         horizontalAlignment: Label.AlignHCenter
157                         verticalAlignment: Label.AlignVCenter
158                     }
159                 }
160                 RowLayout {
161                     spacing: 20
162                     Label {
163                         id: ons_type
164                         anchors.left: parent.left
165                         anchors.leftMargin: 30
166                         text: qsTr("type:")
167                         font.pixelSize: 20
168                         font.italic: true
169                         color: '#00ADDC'
170                     }
171
172                     Label {
173                         id: ons_type_data
174                         text: onsType
175                         font.pixelSize: 20
176                         font.italic: true
177                         anchors.left: ons_type.right
178                         anchors.leftMargin: 20
179                         color: '#00ADDC'
180                     }
181                 }
182                 RowLayout {
183                     spacing: 20
184                     Label {
185                         id: ons_contents
186                         anchors.left: parent.left
187                         anchors.leftMargin: 30
188                         text: qsTr("contents:")
189                         font.pixelSize: 20
190                         font.italic: true
191                         color: '#00ADDC'
192                     }
193                     Label {
194                         id: ons_contents_data
195                         text: onsContents
196                         font.pixelSize: 20
197                         font.italic: true
198                         anchors.left: ons_contents.right
199                         anchors.leftMargin: 20
200                         color: '#00ADDC'
201                         Layout.maximumWidth: 600
202                         Layout.maximumHeight: 200
203                         maximumLineCount: 4
204                         wrapMode: Text.Wrap
205                         elide: Text.ElideRight
206                         horizontalAlignment: Label.AlignLeft
207                         verticalAlignment: Label.AlignVCenter
208                     }
209                 }
210                 RowLayout {
211                     spacing: 20
212                     Label {
213                         id: btn1
214                         anchors.left: parent.left
215                         anchors.leftMargin: 30
216                         text: qsTr("Button1")
217                         font.pixelSize: 20
218                         font.italic: true
219                         color: '#00ADDC'
220                     }
221
222                     Label {
223                         id: btn1_data
224                         text: onsButton1
225                         font.pixelSize: 20
226                         font.italic: true
227                         anchors.left: btn1.right
228                         anchors.leftMargin: 20
229                         color: '#00ADDC'
230                     }
231                 }
232                 RowLayout {
233                     spacing: 20
234                     Label {
235                         id: btn2
236                         anchors.left: parent.left
237                         anchors.leftMargin: 30
238                         text: qsTr("Button2")
239                         font.pixelSize: 20
240                         font.italic: true
241                         color: '#00ADDC'
242                     }
243                     Label {
244                         id: btn2_data
245                         text: onsButton2
246                         font.pixelSize: 20
247                         font.italic: true
248                         anchors.left: btn2.right
249                         anchors.leftMargin: 20
250                         color: '#00ADDC'
251                     }
252                 }
253                 RowLayout {
254                     spacing: 20
255                     Label {
256                         id: btn3
257                         anchors.left: parent.left
258                         anchors.leftMargin: 30
259                         text: qsTr("Button3")
260                         font.pixelSize: 20
261                         font.italic: true
262                         color: '#00ADDC'
263                     }
264                     Label {
265                         id: btn3_data
266                         text: onsButton3
267                         font.pixelSize: 20
268                         font.italic: true
269                         anchors.left: btn3.right
270                         anchors.leftMargin: 20
271                         color: '#00ADDC'
272                     }
273                 }
274             }
275         }
276
277         // post button
278         Button {
279             id: poster
280             text: "Post"
281             Layout.alignment: Qt.AlignCenter
282             onClicked: {
283                 postMessage();
284             }
285         }
286     }
287
288     function selectOneButton() {
289         console.log("select one button!")
290         onsTitle = qsTr("One Button title")
291         onsType = qsTr("critical")
292         onsContents = qsTr("An operating system is a program that manages a computer’s hardware.")
293         onsButton1 = qsTr("Yes")
294         onsButton2 = qsTr("")
295         onsButton3 = qsTr("")
296     }
297
298     function selectTwoButtons() {
299         console.log("select two buttons!")
300         onsTitle = qsTr("Two Buttons title")
301         onsType = qsTr("exclamation")
302         onsContents = qsTr("Beforewe can explore the details of computer system operation, we need to know something about system structure. We thus discuss the basic functions of system startup, I/O, and storage early in this chapter. We also describe the basic computer architecture that makes it possible to write a functional operating system.")
303         onsButton1 = qsTr("Yes")
304         onsButton2 = qsTr("No")
305         onsButton3 = qsTr("")
306     }
307
308     function selectThreeButtons() {
309         console.log("select three buttons!")
310         onsTitle = qsTr("Three Buttons title")
311         onsType = qsTr("information")
312         onsContents = qsTr("We can also view a computer system as consisting of hardware, software,and data. The operating system provides the means for proper use of these resources in the operation of the computer system.")
313         onsButton1 = qsTr("Yes")
314         onsButton2 = qsTr("Abort")
315         onsButton3 = qsTr("No")
316     }
317
318     function selectNoButton() {
319         console.log("select no button!")
320         onsTitle = qsTr("No Button title,very long title beyond screen wide which will show ellipsis at the end")
321         onsType = qsTr("question")
322         onsContents = qsTr("Recently, many varieties of mobile computers, such as smartphones and tablets, have come into fashion. Most mobile computers are standalone units for individual users. Quite often, they are connected to networks through cellular or other wireless technologies. Increasingly, these mobile devices are replacing desktop and laptop computers for people who are primarily interested in using computers for e-mail and web browsing. The user interface for mobile computers generally features a touch screen, where the user interacts with the system by pressing and swiping fingers across the screen rather than using a physical keyboard and mouse.")
323         onsButton1 = qsTr("")
324         onsButton2 = qsTr("")
325         onsButton3 = qsTr("")
326     }
327
328     Timer {
329                 id: activate_timer
330                 interval: 500
331                 running: false
332                 repeat: false
333                 onTriggered: {
334                         console.log("calling eventHandler.showWindow for " + mapp_id + " and data msg " + mapp_data_msg)
335                         eventHandler.showWindow(mapp_id, mapp_data_msg)
336                         mapp_id = ''
337                         mapp_data_msg = ''
338                 }
339     }
340
341
342     function armTimer(app_id, msg) {
343         mapp_id = app_id
344         mapp_data_msg = msg
345         activate_timer.running = true
346     }
347
348     function postMessage() {
349         console.log("poster pressed")
350         btndata = ""
351         postmsg = "{\"title\": \"" + onsTitle + "\"," + "\"type\": \"" + onsType + "\"," + "\"contents\": \"" + onsContents + "\"";
352         if (onsButton1 != "") {
353             btndata = "\"" + onsButton1 + "\"";
354         }
355         if (onsButton2 != "") {
356             if (btndata != "")
357                 btndata += ",";
358             btndata += "\"" + onsButton2 + "\"";
359         }
360         if (onsButton3 != "") {
361             if (btndata != "")
362                 btndata += ",";
363             btndata += "\"" + onsButton3 + "\"";
364         }
365
366         if(btndata != "")
367             postmsg += ",\"buttons\":[" + btndata + "]}"
368         else
369             postmsg += "}"
370
371         if (!onscreen_role_set) {
372                 console.log("onscreen_role_set is not set")
373                 eventHandler.set_window_popup(onsId, x, y)
374                 console.log("setting for popup for " + onsId)
375                 onscreen_role_set = true
376         }
377
378         // if the application is not already started, start it
379         if (pid === -1) {
380                 // if the application is not started, then the first time
381                 // we start we also display it using the default policy engine
382                 pid = eventHandler.start(onsId, postmsg)
383                 console.log("calling eventHandler.start for " + onsId + " with pid " + pid)
384
385                 // this is necessary jus the first time as we don't queue the
386                 // 'activate_app' event and pass it over once onscreenapp is
387                 // started. It is implementation detail in case it is required
388                 console.log("calling armTimer for " + onsId)
389                 armTimer(onsId, postmsg)
390         } else {
391                 // onscreenapp is already start we just need to activate + pass the data
392                 if (!eventHandler.is_running(onsId) && pid > 0) {
393                         // this is mostly for testing, in case onscreenapp died
394                         // unexpectedly
395                         eventHandler.set_window_popup(onsId, x, y)
396                         pid = eventHandler.start(onsId, postmsg)
397                         armTimer(onsId, postmsg)
398                 } else {
399                         eventHandler.showWindow(onsId, postmsg)
400                 }
401         }
402
403     }
404
405     function qmlOnReplyShowWindow(text) {
406         console.log("onstestapp received:",text);
407         //output.text = text;
408     }
409 }