File indexing completed on 2024-12-22 03:41:45

0001 /*
0002     SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de>
0003     SPDX-License-Identifier: LGPL-2.0-or-later
0004 */
0005 
0006 #include "kpluginproxymodel.h"
0007 #include "kpluginmodel.h"
0008 
0009 KPluginProxyModel::KPluginProxyModel(QObject *parent)
0010     : KCategorizedSortFilterProxyModel(parent)
0011 {
0012     sort(0);
0013     setCategorizedModel(true);
0014 }
0015 
0016 KPluginProxyModel::~KPluginProxyModel() = default;
0017 
0018 QString KPluginProxyModel::query() const
0019 {
0020     return m_query;
0021 }
0022 
0023 void KPluginProxyModel::setQuery(const QString &query)
0024 {
0025     if (m_query != query) {
0026         m_query = query;
0027         invalidate();
0028         Q_EMIT queryChanged();
0029     }
0030 }
0031 
0032 bool KPluginProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & /*sourceParent*/) const
0033 {
0034     if (m_query.isEmpty()) {
0035         return true;
0036     }
0037 
0038     const QModelIndex index = sourceModel()->index(sourceRow, 0);
0039 
0040     const QString name = index.data(KPluginModel::NameRole).toString();
0041 
0042     if (name.contains(m_query, Qt::CaseInsensitive)) {
0043         return true;
0044     }
0045 
0046     const QString description = index.data(KPluginModel::DescriptionRole).toString();
0047 
0048     if (description.contains(m_query, Qt::CaseInsensitive)) {
0049         return true;
0050     }
0051 
0052     return false;
0053 }
0054 
0055 bool KPluginProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
0056 {
0057     if (left.data(KPluginModel::SortableRole).toBool() && right.data(KPluginModel::SortableRole).toBool()) {
0058         return left.data(KPluginModel::NameRole).toString().compare(right.data(KPluginModel::NameRole).toString(), Qt::CaseInsensitive) < 0;
0059     }
0060     return 0;
0061 }
0062 
0063 int KPluginProxyModel::compareCategories(const QModelIndex &left, const QModelIndex &right) const
0064 {
0065     const QStringList orderedCategoryLabels = m_model->getOrderedCategoryLabels();
0066     const QString leftLabel = left.data(KCategorizedSortFilterProxyModel::CategorySortRole).toString();
0067     const QString rightLabel = right.data(KCategorizedSortFilterProxyModel::CategorySortRole).toString();
0068     // Preserve the order in which they were passed in the model from consumers
0069     return orderedCategoryLabels.indexOf(leftLabel) - orderedCategoryLabels.indexOf(rightLabel);
0070 }
0071 
0072 #include "moc_kpluginproxymodel.cpp"