File indexing completed on 2024-05-12 16:25:03

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #include "completionproxymodel.h"
0005 
0006 #include <QDebug>
0007 
0008 bool CompletionProxyModel::filterAcceptsRow(int sourceRow, const QModelIndex &sourceParent) const
0009 {
0010     Q_UNUSED(sourceParent);
0011     if (m_filterText.isEmpty()) {
0012         return false;
0013     }
0014     return (sourceModel()->data(sourceModel()->index(sourceRow, 0), filterRole()).toString().startsWith(m_filterText, Qt::CaseInsensitive)
0015             && !m_fullText.startsWith(sourceModel()->data(sourceModel()->index(sourceRow, 0), filterRole()).toString()))
0016         || (m_secondaryFilterRole != -1
0017             && sourceModel()
0018                    ->data(sourceModel()->index(sourceRow, 0), secondaryFilterRole())
0019                    .toString()
0020 #if QT_VERSION > QT_VERSION_CHECK(6, 0, 0)
0021                    .startsWith(QStringView(m_filterText).sliced(1), Qt::CaseInsensitive));
0022 #else
0023                    .startsWith(m_filterText.midRef(1), Qt::CaseInsensitive));
0024 #endif
0025 }
0026 
0027 bool CompletionProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
0028 {
0029     if (m_secondaryFilterRole == -1)
0030         return QSortFilterProxyModel::lessThan(source_left, source_right);
0031     bool left_primary = sourceModel()->data(source_left, filterRole()).toString().startsWith(m_filterText, Qt::CaseInsensitive);
0032     bool right_primary = sourceModel()->data(source_right, filterRole()).toString().startsWith(m_filterText, Qt::CaseInsensitive);
0033     if (left_primary != right_primary)
0034         return left_primary;
0035     return QSortFilterProxyModel::lessThan(source_left, source_right);
0036 }
0037 
0038 int CompletionProxyModel::secondaryFilterRole() const
0039 {
0040     return m_secondaryFilterRole;
0041 }
0042 
0043 void CompletionProxyModel::setSecondaryFilterRole(int role)
0044 {
0045     m_secondaryFilterRole = role;
0046 }
0047 
0048 QString CompletionProxyModel::filterText() const
0049 {
0050     return m_filterText;
0051 }
0052 
0053 void CompletionProxyModel::setFilterText(const QString &filterText)
0054 {
0055     m_filterText = filterText;
0056 }
0057 
0058 void CompletionProxyModel::setFullText(const QString &fullText)
0059 {
0060     m_fullText = fullText;
0061 }
0062 
0063 #include "moc_completionproxymodel.cpp"