Warning, file /plasma/plasma-workspace/kcms/colors/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 
0004     SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005 */
0006 
0007 #include "filterproxymodel.h"
0008 
0009 #include "colorsmodel.h"
0010 
0011 FilterProxyModel::FilterProxyModel(QObject *parent)
0012     : QSortFilterProxyModel(parent)
0013 {
0014 }
0015 
0016 FilterProxyModel::~FilterProxyModel() = default;
0017 
0018 QString FilterProxyModel::selectedScheme() const
0019 {
0020     return m_selectedScheme;
0021 }
0022 
0023 void FilterProxyModel::setSelectedScheme(const QString &scheme)
0024 {
0025     if (m_selectedScheme == scheme) {
0026         return;
0027     }
0028 
0029     m_selectedScheme = scheme;
0030 
0031     Q_EMIT selectedSchemeChanged();
0032     Q_EMIT selectedSchemeIndexChanged();
0033 }
0034 
0035 int FilterProxyModel::selectedSchemeIndex() const
0036 {
0037     // We must search in the source model and then map the index to our proxy model.
0038     const auto results = sourceModel()->match(sourceModel()->index(0, 0), ColorsModel::SchemeNameRole, m_selectedScheme, 1, Qt::MatchExactly);
0039 
0040     if (results.count() == 1) {
0041         const QModelIndex result = mapFromSource(results.first());
0042         if (result.isValid()) {
0043             return result.row();
0044         }
0045     }
0046 
0047     return -1;
0048 }
0049 
0050 QString FilterProxyModel::query() const
0051 {
0052     return m_query;
0053 }
0054 
0055 void FilterProxyModel::setQuery(const QString &query)
0056 {
0057     if (m_query != query) {
0058         const int oldIndex = selectedSchemeIndex();
0059 
0060         m_query = query;
0061         invalidateFilter();
0062 
0063         Q_EMIT queryChanged();
0064 
0065         if (selectedSchemeIndex() != oldIndex) {
0066             Q_EMIT selectedSchemeIndexChanged();
0067         }
0068     }
0069 }
0070 
0071 KCMColors::SchemeFilter FilterProxyModel::filter() const
0072 {
0073     return m_filter;
0074 }
0075 
0076 void FilterProxyModel::setFilter(KCMColors::SchemeFilter filter)
0077 {
0078     if (m_filter != filter) {
0079         const int oldIndex = selectedSchemeIndex();
0080 
0081         m_filter = filter;
0082         invalidateFilter();
0083 
0084         Q_EMIT filterChanged();
0085 
0086         if (selectedSchemeIndex() != oldIndex) {
0087             Q_EMIT selectedSchemeIndexChanged();
0088         }
0089     }
0090 }
0091 
0092 bool FilterProxyModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0093 {
0094     const QModelIndex idx = sourceModel()->index(source_row, 0, source_parent);
0095 
0096     if (!m_query.isEmpty()) {
0097         if (!idx.data(Qt::DisplayRole).toString().contains(m_query, Qt::CaseInsensitive)
0098             && !idx.data(ColorsModel::SchemeNameRole).toString().contains(m_query, Qt::CaseInsensitive)) {
0099             return false;
0100         }
0101     }
0102 
0103     if (m_filter != KCMColors::AllSchemes) {
0104         const QPalette palette = idx.data(ColorsModel::PaletteRole).value<QPalette>();
0105 
0106         const int windowBackgroundGray = qGray(palette.window().color().rgb());
0107 
0108         if (m_filter == KCMColors::DarkSchemes) {
0109             return windowBackgroundGray < 192;
0110         } else if (m_filter == KCMColors::LightSchemes) {
0111             return windowBackgroundGray >= 192;
0112         }
0113     }
0114 
0115     return true;
0116 }
0117 
0118 #include "moc_filterproxymodel.cpp"