Added 2 helper functions : get & roleForName

Closes #24
This commit is contained in:
Pierre-Yves Siret 2017-02-16 19:07:25 +01:00
parent 56f78c7037
commit cb26d890b9
4 changed files with 66 additions and 1 deletions

View File

@ -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<int, QByteArray> roles = roleNames();
for (QHash<int, QByteArray>::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)

View File

@ -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();

View File

@ -16,4 +16,5 @@ OTHER_FILES += \
tst_rangefilter.qml \
tst_indexfilter.qml \
tst_sourceroles.qml \
tst_sorters.qml
tst_sorters.qml \
tst_helpers.qml

39
tests/tst_helpers.qml Normal file
View File

@ -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");
}
}
}