File indexing completed on 2024-12-08 07:33:43

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 
0015     if (sourceModel()->data(sourceModel()->index(sourceRow, 0), filterRole()).toString().isEmpty()) {
0016         return false;
0017     }
0018 
0019     return (sourceModel()->data(sourceModel()->index(sourceRow, 0), filterRole()).toString().startsWith(m_filterText, Qt::CaseInsensitive)
0020             && !m_fullText.startsWith(sourceModel()->data(sourceModel()->index(sourceRow, 0), filterRole()).toString()))
0021         || (m_secondaryFilterRole != -1
0022             && sourceModel()
0023                    ->data(sourceModel()->index(sourceRow, 0), secondaryFilterRole())
0024                    .toString()
0025                    .startsWith(QStringView(m_filterText).sliced(1), Qt::CaseInsensitive));
0026 }
0027 
0028 bool CompletionProxyModel::lessThan(const QModelIndex &source_left, const QModelIndex &source_right) const
0029 {
0030     if (m_secondaryFilterRole == -1)
0031         return QSortFilterProxyModel::lessThan(source_left, source_right);
0032     bool left_primary = sourceModel()->data(source_left, filterRole()).toString().startsWith(m_filterText, Qt::CaseInsensitive);
0033     bool right_primary = sourceModel()->data(source_right, filterRole()).toString().startsWith(m_filterText, Qt::CaseInsensitive);
0034     if (left_primary != right_primary)
0035         return left_primary;
0036     return QSortFilterProxyModel::lessThan(source_left, source_right);
0037 }
0038 
0039 int CompletionProxyModel::secondaryFilterRole() const
0040 {
0041     return m_secondaryFilterRole;
0042 }
0043 
0044 void CompletionProxyModel::setSecondaryFilterRole(int role)
0045 {
0046     m_secondaryFilterRole = role;
0047 }
0048 
0049 QString CompletionProxyModel::filterText() const
0050 {
0051     return m_filterText;
0052 }
0053 
0054 void CompletionProxyModel::setFilterText(const QString &filterText)
0055 {
0056     m_filterText = filterText;
0057 }
0058 
0059 void CompletionProxyModel::setFullText(const QString &fullText)
0060 {
0061     m_fullText = fullText;
0062 }
0063 
0064 #include "moc_completionproxymodel.cpp"