1 /*  This file is part of the Kate project.
2  *
3  *  SPDX-FileCopyrightText: 2012 Christoph Cullmann <cullmann@kde.org>
4  *  SPDX-FileCopyrightText: 2021 Waqar Ahmed        <waqar.17a@gmail.com>
5  *
6  *  SPDX-License-Identifier: LGPL-2.0-or-later
7  */
8 
9 #ifndef KATEPROJECTFILTERMODEL_H
10 #define KATEPROJECTFILTERMODEL_H
11 
12 #include <QDebug>
13 #include <QSortFilterProxyModel>
14 
15 #include <kfts_fuzzy_match.h>
16 
17 class KateProjectFilterProxyModel : public QSortFilterProxyModel
18 {
19 public:
20     KateProjectFilterProxyModel(QObject *parent = nullptr)
QSortFilterProxyModel(parent)21         : QSortFilterProxyModel(parent)
22     {
23     }
24 
setFilterString(const QString & string)25     void setFilterString(const QString &string)
26     {
27         m_pattern = string;
28         invalidateFilter();
29     }
30 
31 protected:
filterAcceptsRow(int sourceRow,const QModelIndex & sourceParent)32     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override
33     {
34         if (m_pattern.isEmpty()) {
35             return true;
36         }
37 
38         // If index is invalid(root index), return true
39         // The rowCount(invalidIndex) can be same as model->rowCount() and when
40         // we are recursively filtering, we get stuck on this index i.e.,
41         // trying to check its children again and again recursively.
42         auto index = sourceModel()->index(sourceRow, 0, sourceParent);
43         if (!index.isValid()) {
44             return true;
45         }
46 
47         int score = 0; // unused intentionally
48         QString file = index.data().toString();
49         return kfts::fuzzy_match(m_pattern, file, score);
50     }
51 
52 private:
53     QString m_pattern;
54 };
55 
56 #endif // KATEPROJECTFILTERMODEL_H
57