Add a new component ModelMenu

This is a wrapper round ContextMenu that allows one to populate
the menu items from a model

REVIEW: 114925
This commit is contained in:
David Edmundson 2014-01-09 13:24:19 +01:00
parent 41b1194aac
commit d200bd0d16
2 changed files with 84 additions and 0 deletions

View File

@ -0,0 +1,83 @@
/*
* Copyright 2014 David Edmundson <davidedmundson@kde.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import org.kde.plasma.components 2.0 as PlasmaComponents
import QtQuick 2.0
/**
* A ModelMenu creates a context menu with items populated from a model or a QList<QAction*>
* For standard item models, actions are created using the following model role names or properties
* display - a string contains the action name
* decoration - an icon to display
* separator - boolean that will add a seperator in the list
*
*
*
* Example code:
*
* @code
* ModelMenu {
* id: menu
* visualParent: someButton
* model: myModel
* }
*
* Button {
* id: someButton
* onClicked: menu.popup()
* }
* @endcode
*/
PlasmaComponents.ContextMenu {
id: menu
/**
* The model containing menu items
*/
property alias model: repeater.model
/**
* This signal is emitted when a menu item is clicked.
* The attached model properties for that menu item are passed as an argument
*/
signal clicked(QtObject model)
//ContextMenu cannot have child items, so in order to have ContextMenu as the root object of this item
//we create a new property which contains an item which can then load the child items
property Item _children : Item {
Repeater {
id: repeater
delegate: PlasmaComponents.MenuItem {
//for QList<QAction*> Repeater adds an attached property modelData
//for QAbstractItemModel* it doesn't. Not checking causes errors
text: typeof(modelData) != "undefined" ? modelData.text : model.display
icon: typeof(modelData) != "undefined" ? modelData.icon : model.decoration
separator: typeof(modelData) != "undefined" ? modelData.separator : model.separator
onClicked: {
menu.clicked(typeof(modelData) != "undefined" ? modelData : model)
}
Component.onCompleted: {
parent = menu
}
}
}
}
}

View File

@ -13,6 +13,7 @@ Dialog 2.0 Dialog.qml
Highlight 2.0 Highlight.qml
Label 2.0 Label.qml
ListItem 2.0 ListItem.qml
ModelContextMenu 2.0 ModelContextMenu.qml
Page 2.0 Page.qml
PageStack 2.0 PageStack.qml
ProgressBar 2.0 ProgressBar.qml