Commit bfb21a63 authored by Max von Buelow's avatar Max von Buelow Committed by Jonathan Marten
Browse files

Implement tag name search

Makes tag name search also compatible to plain tag names.
See akonadi-contacts!17
parent 7306c0d7
Pipeline #174788 passed with stage
in 4 minutes and 6 seconds
......@@ -26,9 +26,28 @@ class CategoryFilterProxyModelPrivate : public QObject
public:
CategoryFilterProxyModelPrivate(CategoryFilterProxyModel *parent);
QList<Tag::Id> filterIdList;
QList<Tag> filterTagList;
bool filterEnabled = false;
bool containsId(Akonadi::Tag::Id id) const
{
for (const Tag &tag : filterTagList) {
if (tag.id() == id) {
return true; // a category matches filter
}
}
return false;
}
bool containsName(const QString &cat) const
{
for (const Tag &tag : filterTagList) {
if (tag.name() == cat) {
return true; // a category matches filter
}
}
return false;
}
private:
CategoryFilterProxyModel *const q_ptr;
};
......@@ -58,11 +77,11 @@ bool CategoryFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &pare
if (!d->filterEnabled) {
return true; // filter not enabled
}
if (d->filterIdList.isEmpty()) {
if (d->filterTagList.isEmpty()) {
return false; // nothing accepted
}
// all accepted
if (d->filterIdList.at(0) == CategorySelectWidget::FilterAll) {
if (d->filterTagList.at(0).id() == CategorySelectWidget::FilterAll) {
return true;
}
......@@ -80,31 +99,36 @@ bool CategoryFilterProxyModel::filterAcceptsRow(int row, const QModelIndex &pare
if (idx >= 0) {
++validCategories;
Tag::Id id = cat.midRef(idx + 5).toInt();
if (d->filterIdList.contains(id)) {
if (d->containsId(id)) {
return true; // a category matches filter
}
}
} else {
++validCategories;
if (d->containsName(cat)) {
return true; // a category matches filter
}
}
}
if (validCategories > 0) {
return false; // categorised but no match
} else {
return d->filterIdList.contains(CategorySelectWidget::FilterUntagged);
return d->containsId(CategorySelectWidget::FilterUntagged);
}
} else if (item.hasPayload<KContacts::ContactGroup>()) { // a contact group item
return d->filterIdList.contains(CategorySelectWidget::FilterGroups);
return d->containsId(CategorySelectWidget::FilterGroups);
}
return true; // not a recognised item
}
void CategoryFilterProxyModel::setFilterCategories(const QList<Akonadi::Tag::Id> &idList)
void CategoryFilterProxyModel::setFilterCategories(const QList<Akonadi::Tag> &tagList)
{
Q_D(CategoryFilterProxyModel);
if (idList != d->filterIdList) {
d->filterIdList = idList;
if (tagList != d->filterTagList) {
d->filterTagList = tagList;
invalidateFilter();
}
}
......
......@@ -47,7 +47,7 @@ public Q_SLOTS:
* which are to be accepted by the filter.
* @see CategorySelectModel::filterChanged
**/
void setFilterCategories(const QList<Akonadi::Tag::Id> &idList);
void setFilterCategories(const QList<Akonadi::Tag> &idList);
/**
* Enable or disable the filter.
......
......@@ -38,7 +38,7 @@ public:
void init();
Q_REQUIRED_RESULT QStandardItemModel *itemModel() const;
void selectAll(Qt::CheckState state) const;
Q_REQUIRED_RESULT QList<Akonadi::Tag::Id> filterTags() const;
Q_REQUIRED_RESULT QList<Akonadi::Tag> filterTags() const;
public Q_SLOTS:
void slotSelectAll();
......@@ -234,17 +234,18 @@ void CategorySelectWidgetPrivate::slotCheckedItemsTimer()
Q_EMIT q->filterChanged(filterTags());
}
QList<Akonadi::Tag::Id> CategorySelectWidgetPrivate::filterTags() const
QList<Akonadi::Tag> CategorySelectWidgetPrivate::filterTags() const
{
QList<Tag::Id> filter;
QList<Tag> filter;
bool allOn = true;
for (int row = 0; row < itemModel()->rowCount(); ++row) {
const QStandardItem *it = itemModel()->item(row);
Q_ASSERT(it != nullptr);
if (it->checkState() == Qt::Checked) {
Tag::Id id = it->data(FILTER_ROLE).toInt();
if (id != 0) {
filter.append(id);
Tag tag(it->data(FILTER_ROLE).toInt());
tag.setName(it->text());
if (tag.id() != 0) {
filter.append(tag);
}
} else {
allOn = false;
......@@ -253,7 +254,7 @@ QList<Akonadi::Tag::Id> CategorySelectWidgetPrivate::filterTags() const
if (allOn) {
filter.clear();
filter.append(CategorySelectWidget::FilterAll);
filter.append(Tag(CategorySelectWidget::FilterAll));
}
// qCDebug(KADDRESSBOOK_LOG) << "filter" << filter;
......@@ -270,7 +271,7 @@ CategorySelectWidget::CategorySelectWidget(QWidget *parent)
CategorySelectWidget::~CategorySelectWidget() = default;
QList<Akonadi::Tag::Id> CategorySelectWidget::filterTags() const
QList<Akonadi::Tag> CategorySelectWidget::filterTags() const
{
Q_D(const CategorySelectWidget);
return d->filterTags();
......
......@@ -54,7 +54,7 @@ public:
*
* @see filterChanged
**/
QList<Akonadi::Tag::Id> filterTags() const;
QList<Akonadi::Tag> filterTags() const;
Q_SIGNALS:
/**
......@@ -65,7 +65,7 @@ Q_SIGNALS:
*
* @see CategorySelectModel::filterChanged
**/
void filterChanged(const QList<Akonadi::Tag::Id> &idList);
void filterChanged(const QList<Akonadi::Tag> &tagList);
private:
std::unique_ptr<CategorySelectWidgetPrivate> const d_ptr;
......
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