File indexing completed on 2024-04-28 05:49:07

0001 /*  This file is part of the Kate project.
0002  *
0003  *  SPDX-FileCopyrightText: 2012 Christoph Cullmann <cullmann@kde.org>
0004  *  SPDX-FileCopyrightText: 2021 Waqar Ahmed        <waqar.17a@gmail.com>
0005  *
0006  *  SPDX-License-Identifier: LGPL-2.0-or-later
0007  */
0008 
0009 #pragma once
0010 
0011 #include <QSortFilterProxyModel>
0012 
0013 #include <KFuzzyMatcher>
0014 
0015 class KateProjectFilterProxyModel : public QSortFilterProxyModel
0016 {
0017 public:
0018     KateProjectFilterProxyModel(QObject *parent = nullptr)
0019         : QSortFilterProxyModel(parent)
0020     {
0021     }
0022 
0023     void setFilterString(const QString &string)
0024     {
0025         m_pattern = string;
0026         invalidateFilter();
0027     }
0028 
0029 protected:
0030     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
0031     {
0032         if (m_pattern.isEmpty()) {
0033             return true;
0034         }
0035 
0036         // If index is invalid(root index), return true
0037         // The rowCount(invalidIndex) can be same as model->rowCount() and when
0038         // we are recursively filtering, we get stuck on this index i.e.,
0039         // trying to check its children again and again recursively.
0040         auto index = sourceModel()->index(sourceRow, 0, sourceParent);
0041         if (!index.isValid()) {
0042             return true;
0043         }
0044 
0045         const QString file = index.data().toString();
0046         return KFuzzyMatcher::matchSimple(m_pattern, file);
0047     }
0048 
0049 private:
0050     QString m_pattern;
0051 };