File indexing completed on 2024-05-19 04:53:42
0001 /* 0002 SPDX-FileCopyrightText: 2017 Nicolas Carion 0003 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0004 */ 0005 0006 #pragma once 0007 0008 #include <QSortFilterProxyModel> 0009 #include <memory> 0010 0011 /** @brief This class is used as a proxy model to filter an asset list based on given criterion (name, ...) 0012 */ 0013 class TreeItem; 0014 class AssetFilter : public QSortFilterProxyModel 0015 { 0016 Q_OBJECT 0017 0018 public: 0019 AssetFilter(QObject *parent = nullptr); 0020 0021 /** @brief Manage the name filter 0022 @param enabled whether to enable this filter 0023 @param pattern to match against effects' names 0024 */ 0025 void setFilterName(bool enabled, const QString &pattern); 0026 0027 /** @brief Returns true if the ModelIndex in the source model is visible after filtering 0028 */ 0029 bool isVisible(const QModelIndex &sourceIndex); 0030 0031 /** @brief If we are in favorite view, invalidate filter to refresh. Call this after a favorite has changed 0032 */ 0033 virtual void reloadFilterOnFavorite() = 0; 0034 0035 QVariantList getCategories(); 0036 Q_INVOKABLE QModelIndex getNextChild(const QModelIndex ¤t); 0037 Q_INVOKABLE QModelIndex getPreviousChild(const QModelIndex ¤t); 0038 Q_INVOKABLE QModelIndex firstVisibleItem(const QModelIndex ¤t); 0039 Q_INVOKABLE QModelIndex getCategory(int catRow); 0040 Q_INVOKABLE QModelIndex getModelIndex(const QModelIndex ¤t); 0041 Q_INVOKABLE QModelIndex getProxyIndex(const QModelIndex ¤t); 0042 0043 protected: 0044 bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override; 0045 bool lessThan(const QModelIndex &left, const QModelIndex &right) const override; 0046 /** @brief Given a TreeItem, check if the ID and Name (first and second) 0047 * columns contain the filtered name as a substring. 0048 * @return true if either ID or Name contains the filtered name as a 0049 * substring, false otherwise 0050 */ 0051 bool filterName(const std::shared_ptr<TreeItem> &item) const; 0052 /** @brief Returns a copy of the input string with any characters that are 0053 * not letters, numbers, or spaces removed. */ 0054 static QString normalizeText(const QString &text); 0055 /** @brief Apply all filter and returns true if the object should be kept after filtering */ 0056 virtual bool applyAll(std::shared_ptr<TreeItem> item) const; 0057 0058 bool m_name_enabled{false}; 0059 QString m_name_value; 0060 };