Plasma-framework

ModelContextMenu.qml
1/*
2 SPDX-FileCopyrightText: 2014 David Edmundson <davidedmundson@kde.org>
3 SPDX-FileCopyrightText: 2017 Kai Uwe Broulik <kde@privat.broulik.de>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8import org.kde.plasma.extras as PlasmaExtras
9
10import QtQuick
11import QtQml
12
13/**
14 * A ModelContextMenu creates a context menu with items populated from a model or a QList<QAction*>.
15 * For standard item models, actions are created using the following model role names or properties:
16 * @li @c display - a string contains the action name
17 * @li @c decoration - an icon to display
18 * @li @c separator - boolean that will add a separator in the list
19 *
20 *
21 *
22 * Example code:
23 *
24 * @code
25 * ModelContextMenu {
26 * id: menu
27 * visualParent: someButton
28 * model: myModel
29 * }
30 *
31 * Button {
32 * id: someButton
33 * onClicked: menu.popup()
34 * }
35 * @endcode
36 */
37
38PlasmaExtras.Menu {
39 id: menu
40
41 /**
42 * The model containing menu items
43 */
44 property alias model: instantiator.model
45
46 /**
47 * This signal is emitted when a menu item is clicked.
48 * The attached model properties for that menu item are passed as an argument
49 */
50 signal clicked(var model)
51
52 //ContextMenu cannot have child items, so in order to have ContextMenu as the root object of this item
53 //we create a new property which contains an item which can then load the child items
54 property Instantiator _children: Instantiator {
55 id: instantiator
56 delegate: PlasmaExtras.MenuItem {
57 //for QList<QAction*> Repeater adds an attached property modelData
58 //for QAbstractItemModel* it doesn't. Not checking causes errors
59 text: (typeof(modelData) != "undefined" ? modelData.text : model.display) || ""
60 icon: typeof(modelData) != "undefined" ? modelData.icon : model.decoration
61 separator: (typeof(modelData) != "undefined" ? modelData.separator : model.separator === true) || false
62 section: (typeof(modelData) != "undefined" ? modelData.section : model.section === true) || false
63 onClicked: {
64 menu.clicked(typeof(modelData) != "undefined" ? modelData : model)
65 }
66 }
67
68 onObjectAdded: (index, object) => menu.addMenuItem(object, null)
69 onObjectRemoved: (index, object) => menu.removeMenuItem(object)
70 }
71}
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:54:11 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.