From cb26d890b9c0dd437da8a1187631153332790561 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Siret Date: Thu, 16 Feb 2017 19:07:25 +0100 Subject: [PATCH] Added 2 helper functions : get & roleForName Closes #24 --- qqmlsortfilterproxymodel.cpp | 20 +++++++++++++++++ qqmlsortfilterproxymodel.h | 5 +++++ tests/SortFilterProxyModel.pro | 3 ++- tests/tst_helpers.qml | 39 ++++++++++++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 tests/tst_helpers.qml diff --git a/qqmlsortfilterproxymodel.cpp b/qqmlsortfilterproxymodel.cpp index a31a100..73ad513 100644 --- a/qqmlsortfilterproxymodel.cpp +++ b/qqmlsortfilterproxymodel.cpp @@ -159,6 +159,26 @@ QVariant QQmlSortFilterProxyModel::sourceData(const QModelIndex &sourceIndex, in return sourceModel()->data(sourceIndex, role); } +int QQmlSortFilterProxyModel::roleForName(const QString& roleName) const +{ + return roleNames().key(roleName.toUtf8(), -1); +} + +QVariantMap QQmlSortFilterProxyModel::get(int row) const +{ + QVariantMap map; + QModelIndex modelIndex = index(row, 0); + QHash roles = roleNames(); + for (QHash::const_iterator it = roles.begin(); it != roles.end(); ++it) + map.insert(it.value(), data(modelIndex, it.key())); + return map; +} + +QVariant QQmlSortFilterProxyModel::get(int row, const QString& roleName) const +{ + return data(index(row, 0), roleForName(roleName)); +} + bool QQmlSortFilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex& source_parent) const { if (!m_completed) diff --git a/qqmlsortfilterproxymodel.h b/qqmlsortfilterproxymodel.h index 8364767..2396753 100644 --- a/qqmlsortfilterproxymodel.h +++ b/qqmlsortfilterproxymodel.h @@ -69,6 +69,11 @@ public: QVariant sourceData(const QModelIndex& sourceIndex, const QString& roleName) const; QVariant sourceData(const QModelIndex& sourceIndex, int role) const; + Q_INVOKABLE int roleForName(const QString& roleName) const; + + Q_INVOKABLE QVariantMap get(int row) const; + Q_INVOKABLE QVariant get(int row, const QString& roleName) const; + Q_SIGNALS: void countChanged(); diff --git a/tests/SortFilterProxyModel.pro b/tests/SortFilterProxyModel.pro index 7c23a69..c81e2bc 100644 --- a/tests/SortFilterProxyModel.pro +++ b/tests/SortFilterProxyModel.pro @@ -16,4 +16,5 @@ OTHER_FILES += \ tst_rangefilter.qml \ tst_indexfilter.qml \ tst_sourceroles.qml \ - tst_sorters.qml + tst_sorters.qml \ + tst_helpers.qml diff --git a/tests/tst_helpers.qml b/tests/tst_helpers.qml new file mode 100644 index 0000000..b04f56a --- /dev/null +++ b/tests/tst_helpers.qml @@ -0,0 +1,39 @@ +import QtQuick 2.0 +import QtQml 2.2 +import QtTest 1.1 +import SortFilterProxyModel 0.2 +import SortFilterProxyModel.Test 0.2 + +Item { + ListModel { + id: dataModel + ListElement { + firstName: "Tupac" + lastName: "Shakur" + } + ListElement { + firstName: "Charles" + lastName: "Aznavour" + } + } + SortFilterProxyModel { + id: testModel + sourceModel: dataModel + } + TestCase { + name: "Helper functions" + + function test_getWithRoleName() { + compare(testModel.get(0, "lastName"), "Shakur"); + } + + function test_getWithoutRoleName() { + compare(testModel.get(1), { firstName: "Charles", lastName: "Aznavour"}); + } + + function test_roleForName() { + compare(testModel.data(testModel.index(0, 0), testModel.roleForName("firstName")), "Tupac"); + compare(testModel.data(testModel.index(1, 0), testModel.roleForName("lastName")), "Aznavour"); + } + } +}