aboutsummaryrefslogtreecommitdiff
path: root/examples/info-menumodel.cpp
diff options
context:
space:
mode:
authorOlivier Tilloy <olivier.tilloy@canonical.com>2012-10-15 12:40:44 +0000
committerTarmac <>2012-10-15 12:40:44 +0000
commit5999e7a42962140b6479995d77a178186b6b2fcc (patch)
tree3927d59c3a5edf7362bb4de937ae11fe7291b029 /examples/info-menumodel.cpp
parent46b559bd4b2229332c1858939be765939fc6fb05 (diff)
parent4366f70b6d6f5f1788353dd74df15e616365d25b (diff)
downloadqmenumodel-5999e7a42962140b6479995d77a178186b6b2fcc.tar.gz
qmenumodel-5999e7a42962140b6479995d77a178186b6b2fcc.tar.bz2
qmenumodel-5999e7a42962140b6479995d77a178186b6b2fcc.zip
Expose a C++ library, and separate into 3 binary packages:
- libqmenumodel0: C++ library - libqmenumodel-dev: development headers for the C++ library - qmenumodel-qml: QML module. Approved by Renato Araujo Oliveira Filho, jenkins.
Diffstat (limited to 'examples/info-menumodel.cpp')
-rw-r--r--examples/info-menumodel.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/examples/info-menumodel.cpp b/examples/info-menumodel.cpp
new file mode 100644
index 0000000..d370731
--- /dev/null
+++ b/examples/info-menumodel.cpp
@@ -0,0 +1,87 @@
+/*
+ * Example executable that links against libqmenumodel to monitor
+ * and print information about a menu model exported on D-Bus.
+ */
+
+// QMenuModel
+#include "qmenumodel/qdbusmenumodel.h"
+
+// stdlib
+#include <iostream>
+
+// Qt
+#include <QtCore/QCoreApplication>
+#include <QtCore/QObject>
+
+
+#define BUS_NAME "com.canonical.testmenu"
+#define BUS_OBJECT_PATH "/com/canonical/testmenu"
+
+
+class MenuModelMonitor : public QDBusMenuModel
+{
+ Q_OBJECT
+
+public:
+ MenuModelMonitor(QObject* parent=0)
+ : QDBusMenuModel(parent)
+ {
+ setProperty("busType", DBusEnums::SessionBus);
+ setProperty("busName", BUS_NAME);
+ setProperty("objectPath", BUS_OBJECT_PATH);
+ QObject::connect(this, SIGNAL(statusChanged(DBusEnums::ConnectionStatus)),
+ SLOT(onStatusChanged(DBusEnums::ConnectionStatus)));
+ QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+ SLOT(onModelChanged()));
+ QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
+ SLOT(onModelChanged()));
+ std::cout << "Monitoring menu model "
+ << property("objectPath").toString().toUtf8().constData()
+ << " on the well-known name "
+ << property("busName").toString().toUtf8().constData()
+ << std::endl;
+ }
+
+private Q_SLOTS:
+ void onStatusChanged(DBusEnums::ConnectionStatus status)
+ {
+ std::cout << "Status of menu model changed to " << status << std::endl;
+ if (status == DBusEnums::Connected) {
+ printModel(this);
+ }
+ }
+
+ void onModelChanged()
+ {
+ printModel(this);
+ }
+
+private:
+ void printModel(QMenuModel* model, int indent=0)
+ {
+ int count = model->rowCount();
+ for (int i = 0; i < count; ++i) {
+ QModelIndex index = model->index(i, 0);
+ QString label = model->data(index, QMenuModel::Label).toString();
+ QVariant submenu = model->data(index, QMenuModel::LinkSubMenu);
+ for (int j = 0; j < indent * 2; ++j) std::cout << " ";
+ std::cout << " > " << label.toUtf8().constData() << std::endl;
+ if (submenu.isValid()) {
+ printModel(qobject_cast<QMenuModel*>(submenu.value<QObject*>()), indent + 1);
+ }
+ }
+ }
+};
+
+#include "info-menumodel.moc"
+
+
+int main(int argc, char** argv)
+{
+ MenuModelMonitor monitor;
+ monitor.start();
+ int result = QCoreApplication(argc, argv).exec();
+ monitor.stop();
+ return result;
+}
+