From 445945df9d3ee9fff836d1c76b6a1d7316668775 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Thu, 4 Oct 2012 13:40:02 +0200 Subject: Add examples. --- examples/README | 11 ++++ examples/exportactiongroup.py | 57 ++++++++++++++++++ examples/exportmenu.py | 63 +++++++++++++++++++ examples/render-menumodel.qml | 136 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 267 insertions(+) create mode 100644 examples/README create mode 100755 examples/exportactiongroup.py create mode 100755 examples/exportmenu.py create mode 100644 examples/render-menumodel.qml diff --git a/examples/README b/examples/README new file mode 100644 index 0000000..945da8b --- /dev/null +++ b/examples/README @@ -0,0 +1,11 @@ +This directory contains examples that demonstrate how to use the QMenuModel +QML binding in applications. + +To export an example menu model on the bus, run 'exportmenu.py'. + +To render this menu in a QML application, run 'render-menumodel.qml' in +qmlviewer. You will need to inform qmlviewer of the location of the QMenuModel +if it’s not installed system-wide, e.g.: + + qmlviewer -I src examples/render-menumodel.qml + diff --git a/examples/exportactiongroup.py b/examples/exportactiongroup.py new file mode 100755 index 0000000..f69416e --- /dev/null +++ b/examples/exportactiongroup.py @@ -0,0 +1,57 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +""" +This example script exports an action group on the session bus under the name +com.canonical.testactiongroup and at the object path +/com/canonical/testactiongroup. +""" + +import sys + +from gi.repository import Gio +from gi.repository import GLib + + +BUS_NAME = 'com.canonical.testactiongroup' +BUS_OBJECT_PATH = '/com/canonical/testactiongroup' + + +def action_activated(action, data): + name = action.get_name() + if action.get_state_type() is None: + print 'stateless action activated: %s' % name + else: + print 'stateful action activated: %s (current state: %s)' % + (name, action.get_state()) + + +if __name__ == '__main__': + bus = Gio.bus_get_sync(Gio.BusType.SESSION, None) + # Claim well-known bus name and ensure only one instance of self is running + # at any given time. + # http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-names + proxy = Gio.DBusProxy.new_sync(bus, 0, None, + 'org.freedesktop.DBus', + '/org/freedesktop/DBus', + 'org.freedesktop.DBus', None) + result = proxy.RequestName('(su)', BUS_NAME, 0x4) + if result != 1 : + print >> sys.stderr, ("Name '%s' is already owned on the session bus." + "Aborting.") % BUS_NAME + sys.exit(1) + + group = Gio.SimpleActionGroup() + foo = Gio.SimpleAction.new('foo', None) + group.insert(foo) + bar = Gio.SimpleAction.new_stateful('bar', None, GLib.Variant.new_boolean(False)) + group.insert(bar) + bleh = Gio.SimpleAction.new_stateful('bleh', None, GLib.Variant.new_string('bleh')) + group.insert(bleh) + for name in group.list_actions(): + action = group.lookup_action(name) + action.connect('activate', action_activated) + bus.export_action_group(BUS_OBJECT_PATH, group) + + GLib.MainLoop().run() + diff --git a/examples/exportmenu.py b/examples/exportmenu.py new file mode 100755 index 0000000..831f995 --- /dev/null +++ b/examples/exportmenu.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python +# -*- encoding: utf-8 -*- + +""" +This example script exports a menu model on the session bus under the name +com.canonical.testmenu and at the object path /com/canonical/testmenu. +The menu model contains items that have attributes with custom values, as well +as sub-menus. +""" + +import sys + +from gi.repository import Gio +from gi.repository import GLib + + +BUS_NAME = 'com.canonical.testmenu' +BUS_OBJECT_PATH = '/com/canonical/testmenu' + + +if __name__ == '__main__': + bus = Gio.bus_get_sync(Gio.BusType.SESSION, None) + # Claim well-known bus name and ensure only one instance of self is running + # at any given time. + # http://dbus.freedesktop.org/doc/dbus-specification.html#message-bus-names + proxy = Gio.DBusProxy.new_sync(bus, 0, None, + 'org.freedesktop.DBus', + '/org/freedesktop/DBus', + 'org.freedesktop.DBus', None) + result = proxy.RequestName('(su)', BUS_NAME, 0x4) + if result != 1 : + print >> sys.stderr, ("Name '%s' is already owned on the session bus." + "Aborting.") % BUS_NAME + sys.exit(1) + + menu = Gio.Menu() + foo = Gio.MenuItem.new('foo', 'app.foo') + foo.set_attribute_value('x-additionaltext', + GLib.Variant.new_string('lorem ipsum')) + foo.set_attribute_value('x-enabled', GLib.Variant.new_boolean(True)) + menu.append_item(foo) + bar = Gio.MenuItem.new('bar', 'app.bar') + bar.set_attribute_value('x-defaultvalue', + GLib.Variant.new_string('Hello World!')) + bar.set_attribute_value('x-canonical-currentvalue', + GLib.Variant.new_string('awesome')) + bar.set_attribute_value('x-velocity', GLib.Variant.new_uint64(83374)) + menu.append_item(bar) + menu.append('bleh', 'app.bleh') + submenu = Gio.Menu() + submenu.append('submenu A', 'app.suba') + submenu2 = Gio.Menu() + submenu2.append('submenu2 A', 'app.sub2a') + submenu2.append('submenu2 B', 'app.sub2b') + submenu2.append('submenu2 C', 'app.sub2c') + submenu.append_submenu('submenu submenu', submenu2) + submenu.append('submenu C', 'app.subc') + menu.append_submenu('submenu', submenu) + menu.append('baz', 'app.baz') + bus.export_menu_model(BUS_OBJECT_PATH, menu) + + GLib.MainLoop().run() + diff --git a/examples/render-menumodel.qml b/examples/render-menumodel.qml new file mode 100644 index 0000000..0128b37 --- /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 // uninstalled, run qmlviewer with "-I ../src" + +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 + //enabled: linkSubMenu != null + hoverEnabled: true + onClicked: { + 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 + } + } +} + -- cgit v1.2.3