feat: Add JoinRole proxy role type

This commit is contained in:
Grecko 2017-09-16 16:53:11 +02:00
parent 48ac2bfcd7
commit 5b071c9526
4 changed files with 116 additions and 1 deletions

View File

@ -41,9 +41,52 @@ void ProxyRole::invalidate()
Q_EMIT invalidated();
}
QStringList JoinRole::roleNames() const
{
return m_roleNames;
}
void JoinRole::setRoleNames(const QStringList& roleNames)
{
if (m_roleNames == roleNames)
return;
m_roleNames = roleNames;
Q_EMIT roleNamesChanged();
invalidate();
}
QString JoinRole::separator() const
{
return m_separator;
}
void JoinRole::setSeparator(const QString& separator)
{
if (m_separator == separator)
return;
m_separator = separator;
Q_EMIT separatorChanged();
invalidate();
}
QVariant JoinRole::data(const QModelIndex &sourceIndex, const QQmlSortFilterProxyModel &proxyModel)
{
QString result;
for (const QString& roleName : m_roleNames)
result += proxyModel.sourceData(sourceIndex, roleName).toString() + m_separator;
if (!m_roleNames.isEmpty())
result.chop(m_separator.length());
return result;
}
void registerProxyRoleTypes() {
qmlRegisterUncreatableType<ProxyRole>("SortFilterProxyModel", 0, 2, "ProxyRole", "ProxyRole is an abstract class");
qmlRegisterType<JoinRole>("SortFilterProxyModel", 0, 2, "JoinRole");
}
Q_COREAPP_STARTUP_FUNCTION(registerProxyRoleTypes)

View File

@ -35,6 +35,32 @@ private:
QMutex m_mutex;
};
class JoinRole : public ProxyRole
{
Q_OBJECT
Q_PROPERTY(QStringList roleNames READ roleNames WRITE setRoleNames NOTIFY roleNamesChanged)
Q_PROPERTY(QString separator READ separator WRITE setSeparator NOTIFY separatorChanged)
public:
using ProxyRole::ProxyRole;
QStringList roleNames() const;
void setRoleNames(const QStringList& roleNames);
QString separator() const;
void setSeparator(const QString& separator);
Q_SIGNALS:
void roleNamesChanged();
void separatorChanged();
private:
QStringList m_roleNames;
QVariant data(const QModelIndex& sourceIndex, const QQmlSortFilterProxyModel& proxyModel) override;
QString m_separator = " ";
};
}
#endif // PROXYROLE_H

View File

@ -23,4 +23,5 @@ OTHER_FILES += \
tst_builtins.qml \
tst_rolesorter.qml \
tst_stringsorter.qml \
tst_proxyroles.qml
tst_proxyroles.qml \
tst_joinrole.qml

45
tests/tst_joinrole.qml Normal file
View File

@ -0,0 +1,45 @@
import QtQuick 2.0
import QtQml 2.2
import QtTest 1.1
import SortFilterProxyModel 0.2
import QtQml 2.2
Item {
ListModel {
id: listModel
ListElement { firstName: "Justin"; lastName: "Timberlake" }
}
SortFilterProxyModel {
id: testModel
sourceModel: listModel
proxyRoles: JoinRole {
id: joinRole
name: "fullName"
roleNames: ["firstName", "lastName"]
}
}
Instantiator {
id: instantiator
model: testModel
QtObject {
property string fullName: model.fullName
}
}
TestCase {
name: "JoinRole"
function test_joinRole() {
compare(instantiator.object.fullName, "Justin Timberlake");
listModel.setProperty(0, "lastName", "Bieber");
compare(instantiator.object.fullName, "Justin Bieber");
joinRole.roleNames = ["lastName", "firstName"];
compare(instantiator.object.fullName, "Bieber Justin");
joinRole.separator = " - ";
compare(instantiator.object.fullName, "Bieber - Justin");
}
}
}