Warning, file /plasma/plasma-sdk/cuttlefish/src/sortfiltermodel.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: 2020 David Redondo <kde@david-redondo.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "sortfiltermodel.h"
0008 #include "iconmodel.h"
0009 
0010 using namespace CuttleFish;
0011 
0012 SortFilterModel::SortFilterModel(QObject *parent)
0013     : QSortFilterProxyModel(parent)
0014 {
0015     setSortRole(IconModel::IconName);
0016     setSortCaseSensitivity(Qt::CaseInsensitive);
0017 }
0018 
0019 bool SortFilterModel::filterAcceptsRow(int source_row, const QModelIndex &source_parent) const
0020 {
0021     const QModelIndex sourceIndex = sourceModel()->index(source_row, 0, source_parent);
0022     if (!(m_category.isEmpty() || m_category == QLatin1String("all")) && (m_category != sourceIndex.data(IconModel::Category))) {
0023         return false;
0024     }
0025     if (!m_filter.isEmpty()) {
0026         return sourceIndex.data(IconModel::IconName).toString().contains(m_filter, Qt::CaseInsensitive);
0027     }
0028     return true;
0029 }
0030 
0031 int SortFilterModel::currentIndex()
0032 {
0033     const QModelIndex index = mapFromSource(m_currentSourceIndex);
0034     if (index.isValid()) {
0035         return index.row();
0036     }
0037     m_currentSourceIndex = mapToSource(this->index(0, 0));
0038     return 0;
0039 }
0040 
0041 void SortFilterModel::setCurrentIndex(int index)
0042 {
0043     if (mapFromSource(m_currentSourceIndex).row() != index) {
0044         m_currentSourceIndex = mapToSource(this->index(index, 0));
0045         emit currentIndexChanged();
0046     }
0047 }
0048 
0049 QString SortFilterModel::category() const
0050 {
0051     return m_category;
0052 }
0053 
0054 void SortFilterModel::setCategory(const QString &category)
0055 {
0056     if (category == m_category) {
0057         return;
0058     }
0059     int oldIndex = currentIndex();
0060     m_category = category;
0061     invalidateFilter();
0062     emit categoryChanged();
0063     if (currentIndex() != oldIndex) {
0064         emit currentIndexChanged();
0065     }
0066 }
0067 
0068 QString SortFilterModel::filter() const
0069 {
0070     return m_filter;
0071 }
0072 
0073 void SortFilterModel::setFilter(const QString &filter)
0074 {
0075     if (filter == m_filter) {
0076         return;
0077     }
0078     int oldIndex = currentIndex();
0079     m_filter = filter;
0080     invalidateFilter();
0081     emit filterChanged();
0082     if (currentIndex() != oldIndex) {
0083         emit currentIndexChanged();
0084     }
0085 }