Warning, file /plasma/plasma-workspace/kcms/desktoptheme/filterproxymodel.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002     SPDX-FileCopyrightText: 2019 Kai Uwe Broulik <kde@privat.broulik.de>
0003     SPDX-FileCopyrightText: 2019 David Redondo <kde@david-redondo.de>
0004 
0005     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0006 */
0007 
0008 #include "filterproxymodel.h"
0009 
0010 #include "themesmodel.h"
0011 
0012 FilterProxyModel::FilterProxyModel(QObject *parent)
0013     : QSortFilterProxyModel(parent)
0014 {
0015 }
0016 
0017 FilterProxyModel::~FilterProxyModel() = default;
0018 
0019 QString FilterProxyModel::selectedTheme() const
0020 {
0021     return m_selectedTheme;
0022 }
0023 
0024 void FilterProxyModel::setSelectedTheme(const QString &pluginName)
0025 {
0026     if (m_selectedTheme == pluginName) {
0027         return;
0028     }
0029 
0030     const bool firstTime = m_selectedTheme.isNull();
0031     m_selectedTheme = pluginName;
0032 
0033     if (!firstTime) {
0034         Q_EMIT selectedThemeChanged();
0035     }
0036     Q_EMIT selectedThemeIndexChanged();
0037 }
0038 
0039 int FilterProxyModel::selectedThemeIndex() const
0040 {
0041     // We must search in the source model and then map the index to our proxy model.
0042     const auto results = sourceModel()->match(sourceModel()->index(0, 0), ThemesModel::PluginNameRole, m_selectedTheme, 1, Qt::MatchExactly);
0043 
0044     if (results.count() == 1) {
0045         const QModelIndex result = mapFromSource(results.first());
0046         if (result.isValid()) {
0047             return result.row();
0048         }
0049     }
0050 
0051     return -1;
0052 }
0053 
0054 QString FilterProxyModel::query() const
0055 {
0056     return m_query;
0057 }
0058 
0059 void FilterProxyModel::setQuery(const QString &query)
0060 {
0061     if (m_query != query) {
0062         const int oldIndex = selectedThemeIndex();
0063 
0064         m_query = query;
0065         invalidateFilter();
0066 
0067         Q_EMIT queryChanged();
0068 
0069         if (selectedThemeIndex() != oldIndex) {
0070             Q_EMIT selectedThemeIndexChanged();
0071         }
0072     }
0073 }
0074 
0075 FilterProxyModel::ThemeFilter FilterProxyModel::filter() const
0076 {
0077     return m_filter;
0078 }
0079 
0080 void FilterProxyModel::setFilter(ThemeFilter filter)
0081 {
0082     if (m_filter != filter) {
0083         const int oldIndex = selectedThemeIndex();
0084 
0085         m_filter = filter;
0086         invalidateFilter();
0087 
0088         Q_EMIT filterChanged();
0089 
0090         if (selectedThemeIndex() != oldIndex) {
0091             Q_EMIT selectedThemeIndexChanged();
0092         }
0093     }
0094 }
0095 
0096 bool FilterProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0097 {
0098     const QModelIndex idx = sourceModel()->index(sourceRow, 0, sourceParent);
0099 
0100     if (!m_query.isEmpty()) {
0101         if (!idx.data(Qt::DisplayRole).toString().contains(m_query, Qt::CaseInsensitive)
0102             && !idx.data(ThemesModel::PluginNameRole).toString().contains(m_query, Qt::CaseInsensitive)) {
0103             return false;
0104         }
0105     }
0106 
0107     const auto type = idx.data(ThemesModel::ColorTypeRole).value<ThemesModel::ColorType>();
0108     switch (m_filter) {
0109     case AllThemes:
0110         return true;
0111     case LightThemes:
0112         return type == ThemesModel::LightTheme;
0113     case DarkThemes:
0114         return type == ThemesModel::DarkTheme;
0115     case ThemesFollowingColors:
0116         return type == ThemesModel::FollowsColorTheme;
0117     }
0118 
0119     return true;
0120 }