File: Main.qml

package info (click to toggle)
qt6-declarative 6.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 308,920 kB
  • sloc: cpp: 775,911; javascript: 514,247; xml: 10,855; python: 2,806; ansic: 2,253; java: 810; sh: 262; makefile: 41; php: 27
file content (474 lines) | stat: -rw-r--r-- 15,410 bytes parent folder | download
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
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only

import QtCore
import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Dialogs

ApplicationWindow {
    id: window
    width: 800
    height: 600
    visible: true
    title: qsTr("Menus - style: %1").arg(currentStyle)

    required property string currentStyle

    Shortcut {
        sequence: "Ctrl+Q"
        onActivated: Qt.quit()
    }

    Settings {
        id: settings

        property alias windowX: window.x
        property alias windowY: window.y
    }

    menuBar: MenuBar {
        visible: menuBarVisibleSwitch.checked

        Menu {
            id: fileMenu
            objectName: "file"
            title: qsTr("&File")
            popupType: popupTypeCombo.popupType()
            ContextAction { text: qsTr("&New...") }
            ContextMenuItem { text: "menuItem" }
            ContextAction { text: qsTr("&Open...") }
            ContextAction { text: qsTr("&Save") }
            ContextAction { text: qsTr("Save &As...") }
            Menu {
                title: qsTr("Sub...")
                ContextAction { text: qsTr("Sub action 1") }
                ContextAction { text: qsTr("Sub action 2") }
                Menu {
                    title: qsTr("SubSub...")
                    ContextAction { text: qsTr("SubSub action 1") }
                    ContextAction { text: qsTr("SubSub action 2") }
                }
            }
            MenuSeparator { }
            ContextAction {
                text: qsTr("&Quit")
                // This is needed for macOS since it takes priority over the Shortcut.
                onTriggered: Qt.quit()
            }
            Action {
                text: qsTr("Remove menu")
                onTriggered: menuBar.removeMenu(fileMenu)
            }
        }
        Menu {
            id: editMenu
            objectName: "edit"
            title: qsTr("&Edit")
            popupType: popupTypeCombo.popupType()
            ContextAction {
                id: cutAction
                text: qsTr("Cut")
                enabled: textArea.selectedText.length > 0
            }
            ContextAction {
                text: qsTr("Copy")
                enabled: textArea.selectedText.length > 0
            }
            ContextAction {
                text: qsTr("Paste")
                enabled: textArea.activeFocus
            }

            MenuSeparator {}

            Action {
                text: qsTr("Checkable menu")
                checkable: true
                checked: true
            }
            Action {
                text: qsTr("Remove menu")
                onTriggered: menuBar.removeMenu(editMenu)
            }
            Menu {
                id: editSubMenu
                title: qsTr("Find / Replace")
                Action { text: qsTr("&Find") }
            }

            MenuSeparator {}

            ContextAction {
                text: qsTr("Dummy Action")
                shortcut: "Ctrl+I"
            }
        }
        MenuBarItem {
            id: explicitMenuBarItem
            menu: Menu {
                id: menuBarItemMenu
                objectName: "MenuBarItem"
                title: "MenuBarItem"
                popupType: popupTypeCombo.popupType()
                ContextAction { text: qsTr("Action") }
                Action {
                    text: qsTr("Remove menu")
                    onTriggered: menuBar.removeMenu(menuBarItemMenu)
                }
            }
        }
    }

    Component {
        id: extraMenuComp
        Menu {
            id: extraMenu
            objectName: "Extra"
            title: qsTr("&Extra")
            ContextAction { text: qsTr("&Trigger") }
            Action {
                text: qsTr("Remove Extra menu")
                onTriggered: menuBar.removeMenu(extraMenu)
            }
        }
    }

    ColumnLayout {
        anchors.fill: parent

        Label {
            text: qsTr("Right click on the window background to open a context menu. "
               + "Right click on the TextArea to access its edit context menu.\n\n"
               + "Things to check:\n\n"
               + "- Do the menu items trigger their actions (check console for output)?\n"
               + "- Do checkable menu items work?\n"
               + "- Do the Edit menu items (in the MenuBar menu and edit context menu)"
               + " work as expected with the TextArea?\n"
               + "  - Are they enabled/disabled as expected?\n"
               + "  - Does the TextArea keep focus after interacting with the Edit menu items?\n"
               + "- Does adding and removing menu items work?\n"
               + "- Do the menus in the MenuBar work?\n"
               + "- Can you add and remove menus from the MenuBar?\n"
               + "- Do shortcuts work?")
            verticalAlignment: Text.AlignVCenter
            wrapMode: Text.Wrap

            Layout.alignment: Qt.AlignHCenter
            Layout.preferredWidth: window.width * 0.5
            Layout.fillHeight: true
        }

        GroupBox {
            title: qsTr("Context menu")

            Layout.fillWidth: true

            ColumnLayout {
                anchors.fill: parent

                RowLayout {
                    Label {
                        text: qsTr("Popup type")
                    }

                    ComboBox {
                        id: popupTypeCombo
                        model: ["Item", "Window", "Native"]
                        onCurrentIndexChanged: CppSettings.popupType = currentIndex
                        currentIndex: CppSettings.popupType

                        function popupType() {
                            if (currentText === "Window")
                                return Popup.Window
                            else if (currentText === "Native")
                                return Popup.Native
                            else
                                return Popup.Item
                        }
                    }
                }

                Row {
                    Button {
                        text: qsTr("Add action")
                        onClicked: backgroundContextMenu.appendAction()
                    }
                    Button {
                        text: qsTr("Remove action")
                        onClicked: backgroundContextMenu.removeLastAction()
                    }

                    Button {
                        text: qsTr("Add sub-menu action")
                        onClicked: subMenu.appendAction()
                    }
                    Button {
                        text: qsTr("Remove sub-menu action")
                        onClicked: subMenu.removeLastAction()
                    }
                }
                Row {
                    Switch {
                        text: qsTr("Don't use native menu windows")
                        checked: CppSettings.dontUseNativeMenuWindows
                        onClicked: CppSettings.dontUseNativeMenuWindows = checked
                    }
                }
            }
        }

        TextArea {
            id: textArea
            text: qsTr("Dummy TextArea to test disabled menu items")

            Layout.fillWidth: true
            Layout.minimumHeight: 100

            TapHandler {
                objectName: "textAreaMouseTapHandler"
                acceptedButtons: Qt.RightButton
                onPressedChanged: if (pressed) editContextMenu.popup()
            }
            TapHandler {
                objectName: "textAreaTouchTapHandler"
                acceptedDevices: PointerDevice.TouchScreen
                onLongPressed: editContextMenu.popup()
            }
        }

        Component {
            id: menuBarItemComp
            MenuBarItem {
            }
        }

        MessageDialog {
            id: restartNeededDialog
            buttons: MessageDialog.Ok
            text: "Your current changes requires a restart to take effect!"
        }

        GroupBox {
            title: qsTr("MenuBar")

            Layout.fillWidth: true

            ColumnLayout {
                anchors.fill: parent

                Row {
                    Switch {
                        text: qsTr("Don't use native menu bar")
                        checked: CppSettings.dontUseNativeMenuBar

                        onClicked: {
                            CppSettings.dontUseNativeMenuBar = checked
                            restartNeededDialog.open()
                        }
                    }
                    Switch {
                        id: menuBarVisibleSwitch
                        text: qsTr("MenuBar visible")
                        checked: true
                    }
                }
                Row {
                    Button {
                        text: "Append menu"
                        onClicked: {
                            let menu = extraMenuComp.createObject(menuBar, { title: "Extra " + menuBar.count })
                            menuBar.addMenu(menu)
                        }
                    }
                    Button {
                        text: "Prepend menu"
                        onClicked: {
                            let menu = extraMenuComp.createObject(menuBar, { title: "Extra " + menuBar.count })
                            menuBar.insertMenu(0, menu)
                        }
                    }
                    Button {
                        text: qsTr("Add file menu")
                        onClicked: menuBar.addMenu(fileMenu)
                    }
                    Button {
                        text: "Change labels"
                        onClicked: {
                            fileMenu.title = "File changed"
                            cutAction.text = "Cut changed"
                        }
                    }
                    Button {
                        text: "toggle delegate"
                        onClicked: menuBar.delegate = menuBar.delegate ? null : menuBarItemComp
                    }
                    Switch {
                        text: "MenuBarItem visible"
                        checked: true
                        onCheckedChanged: explicitMenuBarItem.visible = checked
                    }
                }
            }
        }
    }

    TapHandler {
        objectName: "backgroundTouchTapHandler"
        acceptedDevices: PointerDevice.TouchScreen
        onLongPressed: backgroundContextMenu.popup()
    }

    Component {
        id: actionComponent

        Action {}
    }

    component ContextAction: Action {
        onCheckedChanged: (checked) => print("checked of \"" + text + "\" changed to " + checked)
        onTriggered: print("triggered \"" + text + "\"")
    }

    component ContextMenuItem: MenuItem {
        onCheckedChanged: print("checked of \"" + text + "\" changed to " + checked)
        onTriggered: print("triggered \"" + text + "\"")
    }

    contentItem.ContextMenu.menu: Menu {
        id: backgroundContextMenu
        objectName: "backgroundContextMenu"
        popupType: popupTypeCombo.popupType()

        function appendAction() {
            let action = actionComponent.createObject(null, { text: qsTr("Extra context menu item") })
            backgroundContextMenu.addAction(action)
        }

        function removeLastAction() {
            // TODO: Can't use count here because it's 0: it uses contentModel->count(), but native menu items
            // are not Qt Quick items, so we either need to document that you should use contentData.count
            // or add an "actions" property. The problem with contentData is that it could contain
            // non-Action objects. Another potential issue is that "It is not re-ordered when items are inserted or moved",
            // making it unreliable as a general purpose container of actions if users add or remove them dynamically.
            backgroundContextMenu.removeAction(backgroundContextMenu.actionAt(backgroundContextMenu.contentData.length - 1))
        }

        ContextAction {
            text: qsTr("Context menu item")
            shortcut: "A"
        }
        ContextMenuItem {
            text: qsTr("Checkable context menu item")
            checkable: true
        }
        ContextAction {
            text: qsTr("Checked context menu item")
            checkable: true
            checked: true
            shortcut: "C"
        }
        ContextAction {
            text: qsTr("Disabled context menu item")
            enabled: false
            shortcut: "D"
        }
        ContextAction {
            text: qsTr("Checked and disabled context menu item")
            checkable: true
            checked: true
            enabled: false
            shortcut: "E"
        }

        MenuSeparator {}

        ContextAction {
            text: qsTr("Context menu item with icon (name)")
            icon.name: "mail-send"
        }

        ContextAction {
            text: qsTr("Context menu item with icon (source)")
            icon.source: "qrc:/qt/qml/Menus/icons/warning.png"
        }

        ContextAction {
            text: qsTr("Context menu item with disabled icon (source)")
            icon.source: "qrc:/qt/qml/Menus/icons/warning.png"
            enabled: false
        }

        MenuSeparator {}

        Menu {
            id: subMenu
            title: qsTr("Sub-menu")
            objectName: title
            popupType: popupTypeCombo.popupType()

            function appendAction() {
                let action = actionComponent.createObject(null, { text: qsTr("Extra sub-menu item") })
                subMenu.addAction(action)
            }

            function removeLastAction() {
                subMenu.removeAction(subMenu.actionAt(subMenu.contentData.length - 1))
            }

            ContextAction {
                text: qsTr("Sub-menu item")
            }
            ContextAction {
                text: qsTr("Checkable sub-menu item")
                checkable: true
                shortcut: "G"
            }
            ContextAction {
                text: qsTr("Checked sub-menu item")
                checkable: true
                checked: true
            }

            MenuSeparator {}

            ContextAction {
                text: qsTr("Disabled sub-menu item")
                enabled: false
                shortcut: "I"
            }
            ContextAction {
                text: qsTr("Checked and disabled sub-menu item")
                checkable: true
                checked: true
                enabled: false
                shortcut: "J"
            }
            Menu {
                title: qsTr("SubSub...")
                ContextAction { text: qsTr("SubSub action 1") }
                ContextAction { text: qsTr("SubSub action 2") }
            }
        }
    }

    Menu {
        id: editContextMenu
        objectName: "editContextMenu"

        ContextAction {
            text: qsTr("Cut")
            enabled: textArea.selectedText.length > 0
        }
        ContextAction {
            text: qsTr("Copy")
            enabled: textArea.selectedText.length > 0
        }
        ContextAction {
            text: qsTr("Paste")
            enabled: textArea.activeFocus
        }
    }
}