forked from JackV2020/life
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollMenu.qml
More file actions
378 lines (315 loc) · 11.5 KB
/
Copy pathScrollMenu.qml
File metadata and controls
378 lines (315 loc) · 11.5 KB
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
import QtQuick 2.1
import BasicUIControls 1.0
/*
Filename : ScrollMenu.qml
Author : JackV2020 (github) JackV (Toon)
Purpose : Provide an easy way to implement a drop down selection menu on Toon
Date : august 2023
Most important properties :
scrollmenuArray : input array with all items the same type
selectedItemIndex : index of selected item in scrollMenu 0 .. scrollMenu.length - 1
selectedItem : value of the selected item
Basic feedback action
When you want to start an action immediately after selecting an item :
start action like : onItemSelected : { aFunctionInYourApp(selectedItemIndex) }
or : onItemSelected : { aFunctionInYourApp(selectedItem) }
Controlled feedback action
When you do not want an immediate action after selecting an item you have 2 options :
- do not use onItemSelected like above but create your own button OR.......
- use an optional Go button which can be shown on top of the main button :
use : goButton : true
start action like : onGoButtonClicked : { aFunctionInYourApp(selectedItemIndex) }
or : onGoButtonClicked : { aFunctionInYourApp(selectedItem) }
Check the code below for more properties you can use.
Example implementations are in 'LifeScreen.qml' of the app named 'life'
*/
Item {
id: scrollmenu
// ********************************************************** Properties
// Data Interface properties
property variant scrollmenuArray // Input array with all items the same type
property int selectedItemIndex // Output
property string selectedItem : "" // Output
property string scrollMenuTitle : "" // If empty you see the next on top of the list : Select 1 of 'scrollmenuArray.length'
property bool cellNumberPrefix : false // optional prefix items with 1), 2), 3) etc
width : buttonWidth
height : buttonHeight
property variant prevScrollmenuArray : [] // We remember the previous menu so we can see if need to rebuild it
// the next forces the scroll menu to be shown on top of all siblings
z : showScrollMenu ? 1 : 0
// Configuration properties
// Default is to hide the Go button
property bool goButton : false
// Auto hide the scroll menu when not scrolling for some seconds
property bool autoHideScrollMenu : true
property int autoHideScrollMenuSeconds : 10
property int buttonWidth : isNxt ? 200 : 160 // Total width, including optional Go button
property int buttonHeight : isNxt ? 50 : 40 // Total height, including optional Go button
property int buttonRadius : 5 // Radius of the button(s)
property int buttonBorderWidth : 2 // Border width of the button(s)
property int showItems : isNxt ? 5 : 4 // Number of items to show
property bool showScrollMenu : false // Initially the menu is hidden
property int itemWidth : isNxt ? 300 : 240
property int itemHeight : isNxt ? 60 : 48
property int itemRadius : itemHeight / 2
property int itemBorderWidth : 1
property string fontFamily : qfont.regular.name
property string buttonText : "buttonText"
property string buttonColor : "grey"
property string buttonBorderColor : "black"
property string buttonTextColor : "black"
property bool buttonTextBold : false
property string itemTextColor : "black"
property bool itemTextBold : false
// gradientColor1 is highlighting the selected cell
// gradientColor2 is for making other cells darker
// another nice combination :
property string gradientColor1 : colors.canvas // "lightsteelblue"
property string gradientColor2 : "dimgrey" // "blue"
property int buttonPixelSize : isNxt ? 30 : 24
property int itemPixelSize : isNxt ? 50 : 40
property bool alreadySelectedOnce
// *********************************************************** Functions
// ***** Main Button
function mainButtonClicked(){
alreadySelectedOnce = false
// Setup / update scrollmenuModel with current contents of scrollmenuArray
// After you made a selection the buttonText is updated to the item you selected
// so the next time you press the button we want to highlight your last selection
// using the index of the buttonText
var differenceFound = (prevScrollmenuArray.length != scrollmenuArray.length)
var i = 0
while ( ( ! differenceFound ) && (i < scrollmenuArray.length )){ differenceFound = (prevScrollmenuArray[i] != scrollmenuArray[i] ) ; i++ }
try { selectedItemIndex=Math.max(0,scrollmenuArray.indexOf(buttonText)) }
catch(err) { selectedItemIndex=0 }
if (differenceFound) {
if (scrollmenuModel.count == 0) { // first time button is pressed
scrollmenuModel.append({id: 0 , text : ""}) // this is filled with the right value at the end of this function
var count = 0 // count all items
var itemText = ""
for (var i = 0 ; i < scrollmenuArray.length ; i++ ){
itemText = scrollmenuArray[i]
if (itemText.charAt(0) != " " ) { count++ } // Entries starting with a " " are used as seperators
if (cellNumberPrefix && ( itemText.charAt(0) != " " )) {
scrollmenuModel.append({id: (i+1) , text : (count) + ") "+ itemText})
} else {
scrollmenuModel.append({id: (i+1) , text : itemText})
}
}
} else { // not first time so we need to update because array may have changed
var i = 0
var count = 0 // count all items
var itemText = ""
while ( ( i < scrollmenuArray.length ) && (i+1 < scrollmenuModel.count) ) {
itemText = scrollmenuArray[i]
if (itemText.charAt(0) != " " ) { count++ } // Entries starting with a " " are used as seperators
if (cellNumberPrefix && ( itemText.charAt(0) != " " )) {
scrollmenuModel.setProperty(i+1, "text", (count) + ") "+ itemText )
} else {
scrollmenuModel.setProperty(i+1, "text", itemText )
}
i=i+1
}
while ( i < scrollmenuArray.length ) { // the array may have more items than before
itemText = scrollmenuArray[i]
if (itemText.charAt(0) != " " ) { count++ } // Entries starting with a " " are used as seperators
if (cellNumberPrefix && ( itemText.charAt(0) != " " )) {
scrollmenuModel.append({id: (i+1) , text : (count) + ") "+ itemText})
} else {
scrollmenuModel.append({id: (i+1) , text : itemText})
}
i=i+1
}
// the array may have less items than before so we may need to remove delegates
var end = scrollmenuModel.count - 1
while (end > scrollmenuArray.length ) {
scrollmenuModel.remove(end)
end = end - 1
}
}
if (scrollMenuTitle == "") { scrollmenuModel.setProperty(0, "text", "Select 1 of " + count ) }
else { scrollmenuModel.setProperty(0, "text", scrollMenuTitle) }
}
showScrollMenu = true
}
Timer {
id: hideScrollMenuTimer
interval: autoHideScrollMenuSeconds * 1000
running: showScrollMenu && autoHideScrollMenu
repeat: true
onTriggered: { showScrollMenu = false }
}
// ***** Optional Go Button
signal goButtonClicked()
function goButtonClickedFN(index){
goButtonClicked()
}
// ***** Item Select Button
// This one has a timer to make sure that the scroll menu is gone before the signal is given
signal itemSelected()
property bool itemSelectReady : false
function selectionClicked(index){
if ((index == 0) || ( scrollmenuArray[index - 1].charAt(0)== " ") ){ // skip for the scrollMenuTitle and categories
showScrollMenu = false
prevScrollmenuArray = scrollmenuArray.slice() // remember last input
} else {
if ( ( (alreadySelectedOnce) && ( selectedItemIndex == Math.max(0,index - 1) ) ) || ( selectedItemIndex == Math.max(0,index - 1) ) ) {
// clicked same item 2x OR clicked same item as previous time a selection was made
// Update output variables and text on button
selectedItemIndex = Math.max(0,index - 1)
selectedItem = scrollmenuArray[selectedItemIndex]
buttonText = selectedItem
itemSelectReady = true // start the timer below
showScrollMenu = false
prevScrollmenuArray = scrollmenuArray.slice() // remember last input
} else {
selectedItemIndex = Math.max(0,index - 1)
alreadySelectedOnce = true
}
}
}
Timer {
id: itemSelectedTimer
interval: 50
running: itemSelectReady
repeat: true
// signal to object that item is selected
onTriggered: { itemSelected(); itemSelectReady = false }
}
// *********************************************** Object implementation
// ***** Main Button
Rectangle {
id : mainButton
visible : ! showScrollMenu
width : goButton ? ( parent.width - buttonHeight ) : parent.width
height : parent.height
color : buttonColor
radius : buttonRadius
anchors {
top : parent.top
horizontalCenter : parent.horizontalCenter
}
border {
width : buttonBorderWidth
color : buttonBorderColor
}
Text {
text : buttonText
width : parent.width
anchors {
verticalCenter : parent.verticalCenter
horizontalCenter: parent.horizontalCenter
}
wrapMode : Text.WordWrap
horizontalAlignment : Text.AlignHCenter
color : buttonTextColor
font {
pixelSize : buttonPixelSize
family : fontFamily
bold : buttonTextBold
}
}
MouseArea {
anchors.fill : parent
onClicked : { mainButtonClicked() }
}
}
// ***** Optional Go Button
Rectangle {
id : appgoButton
visible : goButton && ( ! showScrollMenu )
width : buttonHeight
height : buttonHeight
color : buttonColor
radius : buttonRadius
anchors {
top : mainButton.top
left : mainButton.right
}
border {
width : buttonBorderWidth
color : buttonBorderColor
}
Text {
text : "Go"
width : parent.width
anchors {
verticalCenter : parent.verticalCenter
horizontalCenter: parent.horizontalCenter
}
wrapMode : Text.WordWrap
horizontalAlignment : Text.AlignHCenter
color : buttonTextColor
font {
pixelSize : buttonPixelSize
family : fontFamily
bold : buttonTextBold
}
}
MouseArea {
anchors.fill : parent
onClicked : { goButtonClickedFN() }
}
}
// ***** Item Select Button
Rectangle {
id : selection
visible : showScrollMenu
width : parent.width
height : itemHeight * ( showItems - 1)
color : "transparent" // see what's underneath when you pull the menu to far up or down :-)
anchors {
top : parent.top
horizontalCenter : parent.horizontalCenter
}
ListModel { id : scrollmenuModel }
GridView {
id : scrollmenuGrid
anchors.fill : parent
cellWidth : Math.max( itemWidth , buttonWidth / 2 + 2 ) // force 1 column of cells
cellHeight : itemHeight
model : scrollmenuModel
anchors {
horizontalCenter: parent.horizontalCenter
}
delegate:
Rectangle {
width : Math.max( itemWidth - buttonBorderWidth , buttonWidth / 2 + 2 ) // force 1 column of cells
height : itemHeight
radius : itemRadius
border {
width : itemBorderWidth
color : buttonBorderColor
}
anchors {
horizontalCenter : parent.horizontalCenter
}
gradient: Gradient {
GradientStop { position: 0.0; color: gradientColor1 }
GradientStop { position: ( (index == selectedItemIndex + 1 ) ) ? 0.0 : 1.0; color: gradientColor2 }
}
Text {
text : model.text
width : parent.width - 2
anchors {
verticalCenter : parent.verticalCenter
horizontalCenter: parent.horizontalCenter
}
font {
pixelSize : itemPixelSize
family : fontFamily
bold : itemTextBold
}
wrapMode : Text.WordWrap
horizontalAlignment : Text.AlignHCenter
color : itemTextColor
}
MouseArea {
anchors.fill : parent
onClicked : { hideScrollMenuTimer.stop() ; hideScrollMenuTimer.start() ; selectionClicked(index) }
onPositionChanged : { hideScrollMenuTimer.stop() ; hideScrollMenuTimer.start() }
}
}
}
}
}