KCMUtils

kpluginproxymodel.cpp
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
9KPluginProxyModel::KPluginProxyModel(QObject *parent)
11{
12 sort(0);
13 setCategorizedModel(true);
14}
15
16KPluginProxyModel::~KPluginProxyModel() = default;
17
18QString KPluginProxyModel::query() const
19{
20 return m_query;
21}
22
23void KPluginProxyModel::setQuery(const QString &query)
24{
25 if (m_query != query) {
26 m_query = query;
27 invalidate();
28 Q_EMIT queryChanged();
29 }
30}
31
32bool 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(sourceRow, 0);
39
40 const QString name = index.data(KPluginModel::NameRole).toString();
41
42 if (name.contains(m_query, Qt::CaseInsensitive)) {
43 return true;
44 }
45
46 const QString description = index.data(KPluginModel::DescriptionRole).toString();
47
48 if (description.contains(m_query, Qt::CaseInsensitive)) {
49 return true;
50 }
51
52 return false;
53}
54
55bool KPluginProxyModel::subSortLessThan(const QModelIndex &left, const QModelIndex &right) const
56{
57 if (left.data(KPluginModel::SortableRole).toBool() && right.data(KPluginModel::SortableRole).toBool()) {
58 return left.data(KPluginModel::NameRole).toString().compare(right.data(KPluginModel::NameRole).toString(), Qt::CaseInsensitive) < 0;
59 }
60 return 0;
61}
62
63int KPluginProxyModel::compareCategories(const QModelIndex &left, const QModelIndex &right) const
64{
65 const QStringList orderedCategoryLabels = m_model->getOrderedCategoryLabels();
66 const QString leftLabel = left.data(KCategorizedSortFilterProxyModel::CategorySortRole).toString();
67 const QString rightLabel = right.data(KCategorizedSortFilterProxyModel::CategorySortRole).toString();
68 // Preserve the order in which they were passed in the model from consumers
69 return orderedCategoryLabels.indexOf(leftLabel) - orderedCategoryLabels.indexOf(rightLabel);
70}
71
72#include "moc_kpluginproxymodel.cpp"
KSERVICE_EXPORT KService::List query(FilterFunc filterFunc)
QString name(StandardAction id)
QVariant data(int role) const const
Q_EMITQ_EMIT
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
bool isEmpty() const const
qsizetype indexOf(const QRegularExpression &re, qsizetype from) const const
CaseInsensitive
QTextStream & left(QTextStream &stream)
QTextStream & right(QTextStream &stream)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:15:20 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.