diff options
author | Olivier Tilloy <olivier.tilloy@canonical.com> | 2012-10-05 13:26:10 +0000 |
---|---|---|
committer | Tarmac <> | 2012-10-05 13:26:10 +0000 |
commit | c3fbab270f60fc374bae50e787e0b2046f127a07 (patch) | |
tree | 61d0eae8f1c1e3d23fb6559cb19e920b167b187c /examples/render-menumodel.qml | |
parent | 7961150ad1eaa1b3bc62d5215e274a7b58b0ef72 (diff) | |
parent | 696fba50395807c344325af11e71ec74cf370c3d (diff) | |
download | qmenumodel-c3fbab270f60fc374bae50e787e0b2046f127a07.tar.gz qmenumodel-c3fbab270f60fc374bae50e787e0b2046f127a07.tar.bz2 qmenumodel-c3fbab270f60fc374bae50e787e0b2046f127a07.zip |
Tweaks to the structure of the code, and added example code.. Approved by Renato Araujo Oliveira Filho, jenkins.
Diffstat (limited to 'examples/render-menumodel.qml')
-rw-r--r-- | examples/render-menumodel.qml | 136 |
1 files changed, 136 insertions, 0 deletions
diff --git a/examples/render-menumodel.qml b/examples/render-menumodel.qml new file mode 100644 index 0000000..45daf0d --- /dev/null +++ b/examples/render-menumodel.qml @@ -0,0 +1,136 @@ +// This example QML application renders a menu model exposed on the session bus +// under the well-known name com.canonical.testmenu and at the object path +// /com/canonical/testmenu. + +import QtQuick 1.1 +import QMenuModel 0.1 + +Item { + id: container + width: 300 + height: 300 + + QDBusMenuModel { + id: menuModel + busType: DBus.SessionBus + busName: "com.canonical.testmenu" + objectPath: "/com/canonical/testmenu" + onStatusChanged: console.log("status of menu model changed to", status) + } + + ListView { + id: view + property variant __back: [] + anchors.left: parent.left + anchors.right:parent.right + anchors.top: parent.top + anchors.bottom: backbutton.top + anchors.margins: 10 + spacing: 3 + model: menuModel + Component.onCompleted: menuModel.start() + delegate: Rectangle { + width: parent.width + height: 30 + radius: 3 + color: { + if (linkSubMenu == null) return "lightgrey" + if (delegatearea.containsMouse) return "steelblue" + return "lightsteelblue" + } + Text { + anchors.fill: parent + anchors.margins: 5 + verticalAlignment: Text.AlignVCenter + color: (linkSubMenu == null) ? "grey" : "black" + text: { + if (linkSubMenu == null) return "%1 (%2)".arg(label).arg(action) + else return "submenu" + } + } + MouseArea { + id: delegatearea + anchors.fill: parent + hoverEnabled: true + onClicked: { + if (linkSubMenu == null) return + var newback = view.__back + newback.push(view.model) + view.__back = newback + view.model = linkSubMenu + } + onEntered: { + var text = "" + for (var prop in extra) { + text += "%1 = %2\n".arg(prop).arg(extra[prop].toString()) + } + if (text != "") { + tooltip.text = text + tooltip.visible = true + } + } + onExited: { + tooltip.visible = false + } + onPositionChanged: { + var pos = container.mapFromItem(delegatearea, mouse.x, mouse.y) + tooltip.x = pos.x + 10 + tooltip.y = pos.y + 10 + } + } + } + function goback() { + var newback = view.__back + model = newback[newback.length - 1] + newback.pop() + view.__back = newback + } + } + + Rectangle { + id: backbutton + anchors.left: parent.left + anchors.right: parent.right + anchors.bottom: parent.bottom + anchors.margins: 5 + radius: 4 + height: 50 + color: (mousearea.enabled && mousearea.containsMouse) ? "steelblue" : "lightsteelblue" + Text { + anchors.fill: parent + horizontalAlignment: Text.AlignHCenter + verticalAlignment: Text.AlignVCenter + text: "back" + color: mousearea.enabled ? "black" : "grey" + } + MouseArea { + id: mousearea + anchors.fill: parent + enabled: view.__back.length > 0 + hoverEnabled: true + onClicked: { + view.goback() + } + } + } + + Rectangle { + id: tooltip + property alias text: t.text + visible: false + width: t.width + 10 + height: t.height + 10 + radius: 4 + color: "lightyellow" + Text { + id: t + height: paintedHeight + width: paintedWidth + x: 5 + y: 5 + anchors.margins: 4 + font.pixelSize: 12 + } + } +} + |