AusweisApp2/src/qml/ProviderCategoryFilterModel...

178 lines
3.8 KiB
C++
Raw Normal View History

2017-12-20 14:54:05 +01:00
/*!
2018-03-28 15:10:51 +02:00
* \copyright Copyright (c) 2015-2018 Governikus GmbH & Co. KG, Germany
2017-12-20 14:54:05 +01:00
*/
2017-07-03 09:33:28 +02:00
#include "ProviderCategoryFilterModel.h"
using namespace governikus;
2017-12-20 14:54:05 +01:00
namespace
{
const QStringList& getCategories()
{
static QStringList cats({QStringLiteral("citizen"), QStringLiteral("insurance"), QStringLiteral("finance"), QStringLiteral("other")});
return cats;
}
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
}
2017-07-03 09:33:28 +02:00
QString ProviderCategoryFilterModel::getSearchString() const
{
return mSearchString;
}
void ProviderCategoryFilterModel::updateSearchString(const QString& pSearchString)
{
const QString& newSearchString = pSearchString.trimmed();
if (mSearchString != newSearchString)
{
mSearchString = newSearchString;
invalidateFilter();
Q_EMIT fireCriteriaChanged();
}
}
QStringList ProviderCategoryFilterModel::getSelectedCategories() const
{
return mSelectedCategories.toList();
}
int ProviderCategoryFilterModel::getAdditionalResultCount() const
{
int results = 0;
2017-12-20 14:54:05 +01:00
for (const QString& p : getCategories())
2017-07-03 09:33:28 +02:00
{
results += matchesForExcludedCategory(p);
}
return results;
}
int ProviderCategoryFilterModel::matchesForExcludedCategory(const QString& pCategory) const
{
if (mSearchString.isEmpty() || mSelectedCategories.isEmpty() || mSelectedCategories.contains(pCategory))
{
return 0;
}
QAbstractItemModel* const model = sourceModel();
const int count = model->rowCount();
int matchCount = 0;
for (int sourceRow = 0; sourceRow < count; ++sourceRow)
{
const QModelIndex idx = model->index(sourceRow, 0, QModelIndex());
const QString dt = model->data(idx, Qt::DisplayRole).toString();
if (!dt.contains(mSearchString, Qt::CaseInsensitive))
{
continue;
}
if (pCategory.toLower() == model->data(idx, ProviderModel::CATEGORY).toString().toLower())
{
matchCount++;
}
}
return matchCount;
}
bool ProviderCategoryFilterModel::filterAcceptsRow(int pSourceRow, const QModelIndex& pSourceParent) const
{
QAbstractItemModel* const model = sourceModel();
Q_ASSERT(model != nullptr);
const QModelIndex idx = model->index(pSourceRow, 0, pSourceParent);
if (!mSearchString.isEmpty())
{
const QString dt = model->data(idx, Qt::DisplayRole).toString();
if (!dt.contains(mSearchString, Qt::CaseInsensitive))
{
return false;
}
}
return mSelectedCategories.isEmpty() || mSelectedCategories.contains(QStringLiteral("all")) ||
mSelectedCategories.contains(model->data(idx, ProviderModel::CATEGORY).toString().toLower());
}
2017-12-20 14:54:05 +01:00
ProviderCategoryFilterModel::ProviderCategoryFilterModel() :
mProviderModel()
2017-07-03 09:33:28 +02:00
{
2017-12-20 14:54:05 +01:00
QSortFilterProxyModel::setSourceModel(&mProviderModel);
2017-07-03 09:33:28 +02:00
2017-12-20 14:54:05 +01:00
QSortFilterProxyModel::sort(0);
2017-07-03 09:33:28 +02:00
sortByCategoryFirst(false);
setSortCaseSensitivity(Qt::CaseInsensitive);
}
ProviderCategoryFilterModel::~ProviderCategoryFilterModel()
{
}
void ProviderCategoryFilterModel::sortByCategoryFirst(bool pEnabled)
{
setSortRole(pEnabled ? ProviderModel::SORT_ROLE : ProviderModel::SHORTNAME);
}
void ProviderCategoryFilterModel::setCategorySelection(const QString& pCategory)
{
mSelectedCategories.clear();
if (!pCategory.isEmpty())
{
mSelectedCategories.insert(pCategory.toLower());
}
invalidateFilter();
Q_EMIT fireCriteriaChanged();
}
void ProviderCategoryFilterModel::updateCategorySelection(const QString& pCategory, bool pSelected)
{
const int categoryCount = mSelectedCategories.count();
if (pSelected)
{
mSelectedCategories.insert(pCategory.toLower());
}
else
{
mSelectedCategories.remove(pCategory.toLower());
}
if (mSelectedCategories.count() != categoryCount)
{
invalidateFilter();
Q_EMIT fireCriteriaChanged();
}
}
void ProviderCategoryFilterModel::addAdditionalResultCategories()
{
bool needUpdate = false;
2017-12-20 14:54:05 +01:00
for (const QString& p : getCategories())
2017-07-03 09:33:28 +02:00
{
if (matchesForExcludedCategory(p) > 0)
{
needUpdate = true;
mSelectedCategories += p;
}
}
if (needUpdate)
{
invalidateFilter();
Q_EMIT fireCriteriaChanged();
}
}