From 53dfc2a919fd03f5570cb106d71d3989a3f528d9 Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Tue, 13 Nov 2012 17:16:59 -0300 Subject: Created auxiliary functions. To allows access the model data from JavaScript; --- libqmenumodel/src/qmenumodel.cpp | 40 ++++++++++++++++++++++++++++++++++++++++ libqmenumodel/src/qmenumodel.h | 7 +++++++ 2 files changed, 47 insertions(+) diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp index f0c2274..a704d85 100644 --- a/libqmenumodel/src/qmenumodel.cpp +++ b/libqmenumodel/src/qmenumodel.cpp @@ -51,6 +51,10 @@ QMenuModel::QMenuModel(GMenuModel *other, QObject *parent) } setRoleNames(rolesNames); setMenuModel(other); + + QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged())); + QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged())); + QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged())); } /*! \internal */ @@ -59,6 +63,42 @@ QMenuModel::~QMenuModel() clearModel(); } +/*! + \qmlmethod QDBusMenuModel::get(int) + + Returns the item at index in the model. This allows the item data to be accessed from JavaScript: + + \b Note: methods should only be called after the Component has completed. +*/ + +QVariantMap QMenuModel::get(int row) const +{ + QVariantMap result; + int rowCountValue = rowCount(); + if ((rowCountValue > 0) && (row >= 0) && (row < rowCountValue)) { + QModelIndex i = index(row); + result.insert("action", data(i, Action)); + result.insert("label", data(i, Label)); + result.insert("linkSection", data(i, LinkSection)); + result.insert("linkSubMenu", data(i, LinkSubMenu)); + result.insert("extra", data(i, Extra)); + } + + return result; +} + +/*! + \qmlmethod QDBusMenuModel::count() + + The number of data entries in the model. + + \b Note: methods should only be called after the Component has completed. +*/ +int QMenuModel::count() const +{ + return rowCount(); +} + /*! \internal */ void QMenuModel::setMenuModel(GMenuModel *other) { diff --git a/libqmenumodel/src/qmenumodel.h b/libqmenumodel/src/qmenumodel.h index 22c30df..ac40db5 100644 --- a/libqmenumodel/src/qmenumodel.h +++ b/libqmenumodel/src/qmenumodel.h @@ -30,6 +30,7 @@ typedef struct _GMenuModel GMenuModel; class QMenuModel : public QAbstractListModel { Q_OBJECT + Q_PROPERTY(int count READ count NOTIFY countChanged) public: enum MenuRoles { @@ -42,11 +43,17 @@ public: ~QMenuModel(); + Q_INVOKABLE QVariantMap get(int row) const; + int count() const; + /* QAbstractItemModel */ QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const; QModelIndex parent (const QModelIndex &index) const; int rowCount(const QModelIndex &parent = QModelIndex()) const; +Q_SIGNALS: + void countChanged(); + protected: QMenuModel(GMenuModel *other=0, QObject *parent=0); void setMenuModel(GMenuModel *model); -- cgit v1.2.3 From 236259bc2ae2efc5ad2bac4a0eca76ebdb8293e8 Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Wed, 28 Nov 2012 20:19:14 -0300 Subject: Optimized get function code. --- libqmenumodel/src/qmenumodel.cpp | 17 ++++++++--------- libqmenumodel/src/qmenumodel.h | 2 +- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp index 5bf740d..7f4c383 100644 --- a/libqmenumodel/src/qmenumodel.cpp +++ b/libqmenumodel/src/qmenumodel.cpp @@ -65,16 +65,15 @@ QMenuModel::~QMenuModel() QVariantMap QMenuModel::get(int row) const { QVariantMap result; - int rowCountValue = rowCount(); - if ((rowCountValue > 0) && (row >= 0) && (row < rowCountValue)) { - QModelIndex i = index(row); - result.insert("action", data(i, Action)); - result.insert("label", data(i, Label)); - result.insert("linkSection", data(i, LinkSection)); - result.insert("linkSubMenu", data(i, LinkSubMenu)); - result.insert("extra", data(i, Extra)); - } + QModelIndex index = this->index(row); + if (index.isValid()) { + QMap data = itemData(index); + const QHash roleNames = this->roleNames(); + Q_FOREACH(int i, roleNames.keys()) { + result.insert(roleNames[i], data[i]); + } + } return result; } diff --git a/libqmenumodel/src/qmenumodel.h b/libqmenumodel/src/qmenumodel.h index f923658..9371bd8 100644 --- a/libqmenumodel/src/qmenumodel.h +++ b/libqmenumodel/src/qmenumodel.h @@ -45,7 +45,6 @@ public: ~QMenuModel(); Q_INVOKABLE QVariantMap get(int row) const; - int count() const; /* QAbstractItemModel */ QHash roleNames() const; @@ -70,6 +69,7 @@ private: QVariant getExtraProperties(const QModelIndex &index) const; QString parseExtraPropertyName(const QString &name) const; void clearModel(); + int count() const; static void onItemsChanged(GMenuModel *model, gint position, gint removed, gint added, gpointer data); }; -- cgit v1.2.3 From 08b143ccdf437ce2e52cdc22b66487d5ba3e22c5 Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Wed, 28 Nov 2012 20:19:42 -0300 Subject: Created unit test for get data function. --- tests/client/modeltest.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/tests/client/modeltest.cpp b/tests/client/modeltest.cpp index 8be92a6..3366177 100644 --- a/tests/client/modeltest.cpp +++ b/tests/client/modeltest.cpp @@ -260,6 +260,44 @@ private Q_SLOTS: delete model; } + + /* + * Test get function + */ + void testGetData() + { + // Make menu available + m_script.publishMenu(); + m_script.run(); + + // create a new model + QDBusMenuModel *model = new QDBusMenuModel(); + model->setBusType(DBusEnums::SessionBus); + model->setBusName(MENU_SERVICE_NAME); + model->setObjectPath(MENU_OBJECT_PATH); + model->start(); + + // Wait for dbus sync + QTest::qWait(500); + + // count + QCOMPARE(model->property("count").toInt(), model->rowCount()); + + QVariantMap data = model->get(0); + + QVERIFY(data.contains("action")); + QVERIFY(data.contains("extra")); + QVERIFY(data.contains("label")); + QVERIFY(data.contains("linkSection")); + QVERIFY(data.contains("linkSubMenu")); + + QCOMPARE(data["action"].toString(), QString("Menu0Act")); + + QVariantMap extra = data["extra"].toMap(); + QCOMPARE(extra.size(), 13); + QCOMPARE(extra["boolean"].toBool(), true); + + } }; QTEST_MAIN(ModelTest) -- cgit v1.2.3 From b3ac37876e6168cf91e3efedf7861709a5e1adef Mon Sep 17 00:00:00 2001 From: Renato Araujo Oliveira Filho Date: Thu, 29 Nov 2012 09:08:15 -0300 Subject: Used a shorter QObject::connect signature. --- libqmenumodel/src/qmenumodel.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/libqmenumodel/src/qmenumodel.cpp b/libqmenumodel/src/qmenumodel.cpp index 7f4c383..53dc966 100644 --- a/libqmenumodel/src/qmenumodel.cpp +++ b/libqmenumodel/src/qmenumodel.cpp @@ -43,9 +43,9 @@ QMenuModel::QMenuModel(GMenuModel *other, QObject *parent) { setMenuModel(other); - QObject::connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), this, SIGNAL(countChanged())); - QObject::connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), this, SIGNAL(countChanged())); - QObject::connect(this, SIGNAL(modelReset()), this, SIGNAL(countChanged())); + connect(this, SIGNAL(rowsInserted(const QModelIndex &, int, int)), SIGNAL(countChanged())); + connect(this, SIGNAL(rowsRemoved(const QModelIndex &, int, int)), SIGNAL(countChanged())); + connect(this, SIGNAL(modelReset()), SIGNAL(countChanged())); } /*! \internal */ -- cgit v1.2.3