1 | /* |
---|---|
2 | SPDX-FileCopyrightText: 2021 Alexander Lohnau <alexander.lohnau@gmx.de> |
3 | SPDX-License-Identifier: LGPL-2.0-or-later |
4 | */ |
5 | |
6 | #include "kpluginproxymodel.h" |
7 | #include "kpluginmodel.h" |
8 | |
9 | KPluginProxyModel::KPluginProxyModel(QObject *parent) |
10 | : KCategorizedSortFilterProxyModel(parent) |
11 | { |
12 | sort(column: 0); |
13 | setCategorizedModel(true); |
14 | } |
15 | |
16 | KPluginProxyModel::~KPluginProxyModel() = default; |
17 | |
18 | QString KPluginProxyModel::query() const |
19 | { |
20 | return m_query; |
21 | } |
22 | |
23 | void KPluginProxyModel::setQuery(const QString &query) |
24 | { |
25 | if (m_query != query) { |
26 | m_query = query; |
27 | invalidate(); |
28 | Q_EMIT queryChanged(); |
29 | } |
30 | } |
31 | |
32 | bool KPluginProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex & /*sourceParent*/) const |
33 | { |
34 | if (m_query.isEmpty()) { |
35 | return true; |
36 | } |
37 | |
38 | const QModelIndex index = sourceModel()->index(row: sourceRow, column: 0); |
39 | |
40 | const QString name = index.data(arole: KPluginModel::NameRole).toString(); |
41 | |
42 | if (name.contains(s: m_query, cs: Qt::CaseInsensitive)) { |
43 | return true; |
44 | } |
45 | |
46 | const QString description = index.data(arole: KPluginModel::DescriptionRole).toString(); |
47 | |
48 | if (description.contains(s: m_query, cs: Qt::CaseInsensitive)) { |
49 | return true; |
50 | } |
51 | |
52 | return false; |
53 | } |
54 | |
55 | bool KPluginProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const |
56 | { |
57 | if (left.data(arole: KPluginModel::SortableRole).toBool() && right.data(arole: KPluginModel::SortableRole).toBool()) { |
58 | return left.data(arole: KPluginModel::NameRole).toString().compare(s: right.data(arole: KPluginModel::NameRole).toString(), cs: Qt::CaseInsensitive) < 0; |
59 | } |
60 | return 0; |
61 | } |
62 | |
63 | int KPluginProxyModel::compareCategories(const QModelIndex &left, const QModelIndex &right) const |
64 | { |
65 | const QStringList orderedCategoryLabels = m_model->getOrderedCategoryLabels(); |
66 | const QString leftLabel = left.data(arole: KCategorizedSortFilterProxyModel::CategorySortRole).toString(); |
67 | const QString rightLabel = right.data(arole: KCategorizedSortFilterProxyModel::CategorySortRole).toString(); |
68 | // Preserve the order in which they were passed in the model from consumers |
69 | return orderedCategoryLabels.indexOf(str: leftLabel) - orderedCategoryLabels.indexOf(str: rightLabel); |
70 | } |
71 | |
72 | #include "moc_kpluginproxymodel.cpp" |
73 |