File indexing completed on 2024-05-05 05:01:21

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QSortFilterProxyModel>
0007 
0008 /**
0009  * @class CompletionProxyModel
0010  *
0011  * A filter model to sort and filter completion results.
0012  *
0013  * This model is designed to work with multiple source models depending upon the
0014  * completion type.
0015  *
0016  * A model value will be shown if its primary or secondary role values start with
0017  * the filter text. The exception is if the full text perfectly matches
0018  * the primary filter role value in which case the completion ends (i.e. the filter
0019  * will return no results).
0020  *
0021  * @note The filter is primarily design to work with strings, therefore make sure
0022  *       that the source model roles that are to be filtered are strings.
0023  */
0024 class CompletionProxyModel : public QSortFilterProxyModel
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     /**
0030      * @brief Wether a row should be shown or not.
0031      *
0032      * @sa QSortFilterProxyModel::filterAcceptsRow
0033      */
0034     bool filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const override;
0035 
0036     /**
0037      * @brief Returns true if the value of source_left is less than source_right.
0038      *
0039      * @sa QSortFilterProxyModel::lessThan
0040      */
0041     bool lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const override;
0042 
0043     /**
0044      * @brief Get the current secondary filter role.
0045      */
0046     int secondaryFilterRole() const;
0047 
0048     /**
0049      * @brief Set the secondary filter role.
0050      *
0051      * Refer to the source model for what value corresponds to what role.
0052      */
0053     void setSecondaryFilterRole(int role);
0054 
0055     /**
0056      * @brief Get the current text being used to filter the source model.
0057      */
0058     QString filterText() const;
0059 
0060     /**
0061      * @brief Set the text to be used to filter the source model.
0062      */
0063     void setFilterText(const QString &filterText);
0064 
0065     /**
0066      * @brief Set the full text in the chatbar after the completion start.
0067      *
0068      * This is used to automatically end the completion if the user replicated the
0069      * primary filter role value perfectly.
0070      */
0071     void setFullText(const QString &fullText);
0072 
0073 private:
0074     int m_secondaryFilterRole = -1;
0075     QString m_filterText;
0076     QString m_fullText;
0077 };