Changeset View
Changeset View
Standalone View
Standalone View
kcms/kded/filterproxymodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2020 Kai Uwe Broulik <kde@privat.broulik.de> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "filterproxymodel.h" | ||||
22 | | ||||
23 | #include "modulesmodel.h" | ||||
24 | | ||||
25 | FilterProxyModel::FilterProxyModel(QObject *parent) : QSortFilterProxyModel(parent) | ||||
26 | { | ||||
27 | | ||||
28 | } | ||||
29 | | ||||
30 | FilterProxyModel::~FilterProxyModel() = default; | ||||
31 | | ||||
32 | QString FilterProxyModel::query() const | ||||
33 | { | ||||
34 | return m_query; | ||||
35 | } | ||||
36 | | ||||
37 | void FilterProxyModel::setQuery(const QString &query) | ||||
38 | { | ||||
39 | if (m_query != query) { | ||||
40 | m_query = query; | ||||
41 | invalidateFilter(); | ||||
42 | emit queryChanged(); | ||||
43 | } | ||||
44 | } | ||||
45 | | ||||
46 | KDEDConfig::ModuleStatus FilterProxyModel::statusFilter() const | ||||
47 | { | ||||
48 | return m_statusFilter; | ||||
49 | } | ||||
50 | | ||||
51 | void FilterProxyModel::setStatusFilter(KDEDConfig::ModuleStatus statusFilter) | ||||
52 | { | ||||
53 | if (m_statusFilter != statusFilter) { | ||||
54 | m_statusFilter = statusFilter; | ||||
55 | invalidateFilter(); | ||||
56 | emit statusFilterChanged(); | ||||
57 | } | ||||
58 | } | ||||
59 | | ||||
60 | bool FilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const | ||||
61 | { | ||||
62 | const QModelIndex idx = sourceModel()->index(source_row, 0, source_parent); | ||||
63 | | ||||
64 | if (!m_query.isEmpty()) { | ||||
65 | if (!idx.data(Qt::DisplayRole).toString().contains(m_query, Qt::CaseInsensitive) | ||||
66 | && !idx.data(ModulesModel::ModuleNameRole).toString().contains(m_query, Qt::CaseInsensitive)) { | ||||
67 | return false; | ||||
68 | } | ||||
69 | } | ||||
70 | | ||||
71 | if (m_statusFilter != KDEDConfig::UnknownStatus) { | ||||
72 | const auto status = static_cast<KDEDConfig::ModuleStatus>(idx.data(ModulesModel::StatusRole).toInt()); | ||||
73 | if (m_statusFilter != status) { | ||||
74 | return false; | ||||
75 | } | ||||
76 | } | ||||
77 | | ||||
78 | return true; | ||||
79 | } |