From 244ec0549fc57adb462276bbc5a3581487ae9e70 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Thu, 4 Oct 2012 12:12:13 +0200 Subject: Removed leftover examples infrastructure. --- examples/CMakeLists.txt | 4 ---- examples/run-example.sh.in | 1 - 2 files changed, 5 deletions(-) delete mode 100644 examples/CMakeLists.txt delete mode 100755 examples/run-example.sh.in (limited to 'examples') diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt deleted file mode 100644 index fda5683..0000000 --- a/examples/CMakeLists.txt +++ /dev/null @@ -1,4 +0,0 @@ -project(examples) - -configure_file("${CMAKE_CURRENT_SOURCE_DIR}/run-example.sh.in" - "${CMAKE_CURRENT_BINARY_DIR}/run-example.sh" @ONLY) diff --git a/examples/run-example.sh.in b/examples/run-example.sh.in deleted file mode 100755 index 4843f49..0000000 --- a/examples/run-example.sh.in +++ /dev/null @@ -1 +0,0 @@ -gdb --args qmlviewer -I @src_BINARY_DIR@ $1 -- cgit v1.2.3 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 (limited to 'examples') 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 From 3fbfb27d55e0d1e4359a295d2dff42984497fa13 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Thu, 4 Oct 2012 17:15:27 +0200 Subject: Updated examples/README to reflect name changes. --- examples/README | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples') diff --git a/examples/README b/examples/README index 945da8b..c2516e3 100644 --- a/examples/README +++ b/examples/README @@ -7,5 +7,5 @@ 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 + qmlviewer -I libqmenumodel examples/render-menumodel.qml -- cgit v1.2.3 From 957046e27eafa963e828b2c9b80d8d4bc6a36084 Mon Sep 17 00:00:00 2001 From: Olivier Tilloy Date: Thu, 4 Oct 2012 17:16:14 +0200 Subject: Ignore clicks on menu items that do not have a submenu. --- examples/render-menumodel.qml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/render-menumodel.qml b/examples/render-menumodel.qml index 0128b37..45daf0d 100644 --- a/examples/render-menumodel.qml +++ b/examples/render-menumodel.qml @@ -3,7 +3,7 @@ // /com/canonical/testmenu. import QtQuick 1.1 -import QMenuModel 0.1 // uninstalled, run qmlviewer with "-I ../src" +import QMenuModel 0.1 Item { id: container @@ -51,9 +51,9 @@ Item { MouseArea { id: delegatearea anchors.fill: parent - //enabled: linkSubMenu != null hoverEnabled: true onClicked: { + if (linkSubMenu == null) return var newback = view.__back newback.push(view.model) view.__back = newback -- cgit v1.2.3