Verified Commit 6e9dcf4e authored by Carl Schwan's avatar Carl Schwan 🚴
Browse files

Implement sorting and filtering in contact group editor

BUG: 430789
parent 81764cbc
Pipeline #74825 canceled with stage
......@@ -149,6 +149,7 @@ void ContactGroupEditor::Private::itemChanged(const Item &item, const QSet<QByte
void ContactGroupEditor::Private::loadContactGroup(const KContacts::ContactGroup &group)
{
mGui.membersView->setSortingEnabled(false);
mGui.groupName->setText(group.name());
mGroupModel->loadContactGroup(group);
......@@ -161,6 +162,7 @@ void ContactGroupEditor::Private::loadContactGroup(const KContacts::ContactGroup
}
mGui.membersView->header()->resizeSections(QHeaderView::Stretch);
mGui.membersView->setSortingEnabled(true);
}
bool ContactGroupEditor::Private::storeContactGroup(KContacts::ContactGroup &group)
......@@ -208,7 +210,12 @@ ContactGroupEditor::ContactGroupEditor(Mode mode, QWidget *parent)
d->mGui.membersView->setEditTriggers(QAbstractItemView::AllEditTriggers);
d->mGroupModel = new ContactGroupModel(this);
d->mGui.membersView->setModel(d->mGroupModel);
auto proxyModel = new GroupFilterModel(this);
proxyModel->setSourceModel(d->mGroupModel);
connect(d->mGui.searchField, &QLineEdit::textChanged, this, [proxyModel](const QString &text) {
proxyModel->setFilterRegularExpression(text);
});
d->mGui.membersView->setModel(proxyModel);
d->mGui.membersView->setItemDelegate(new ContactGroupEditorDelegate(d->mGui.membersView, this));
if (mode == CreateMode) {
......
......@@ -48,6 +48,16 @@
</widget>
</item>
<item row="3" column="0" colspan="2">
<widget class="QLineEdit" name="searchField">
<property name="placeholderText">
<string>Search...</string>
</property>
<property name="clearButtonEnabled">
<bool>true</bool>
</property>
</widget>
</item>
<item row="4" column="0" colspan="2">
<widget class="QTreeView" name="membersView">
<property name="rootIsDecorated">
<bool>false</bool>
......
......@@ -448,4 +448,33 @@ bool ContactGroupModel::removeRows(int row, int count, const QModelIndex &parent
return true;
}
GroupFilterModel::GroupFilterModel(QObject *parent)
: QSortFilterProxyModel(parent)
{
setFilterCaseSensitivity(Qt::CaseInsensitive);
setFilterKeyColumn(-1);
}
bool GroupFilterModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
{
if (sourceRow == sourceModel()->rowCount() - 1) {
return true;
}
return QSortFilterProxyModel::filterAcceptsRow(sourceRow, sourceParent);
}
bool GroupFilterModel::lessThan(const QModelIndex &left, const QModelIndex &right) const
{
if (left.row() == sourceModel()->rowCount() - 1) {
return true;
}
if (right.row() == sourceModel()->rowCount() - 1) {
return false;
}
return QSortFilterProxyModel::lessThan(left, right);
}
#include "moc_contactgroupmodel_p.cpp"
......@@ -9,6 +9,7 @@
#pragma once
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include <kcontacts/contactgroup.h>
......@@ -44,5 +45,14 @@ private:
class Private;
Private *const d;
};
}
class GroupFilterModel : public QSortFilterProxyModel
{
public:
explicit GroupFilterModel(QObject *parent = nullptr);
bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
bool lessThan(const QModelIndex &left, const QModelIndex &right) const override;
};
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment