| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "CompletionProxyModel.h" |
| 6 | |
| 7 | #include <QRegularExpression> |
| 8 | #include <QTextBoundaryFinder> |
| 9 | |
| 10 | #include "CompletionModelRoles.h" |
| 11 | #include "Logging.h" |
| 12 | |
| 13 | CompletionProxyModel::CompletionProxyModel(QAbstractItemModel *model, |
| 14 | int max_mistakes, |
| 15 | size_t max_completions, |
| 16 | QObject *parent) |
| 17 | : QAbstractProxyModel(parent) |
| 18 | , maxMistakes_(max_mistakes) |
| 19 | , max_completions_(max_completions) |
| 20 | { |
| 21 | setSourceModel(model); |
| 22 | |
| 23 | auto insertParts = [this](const QString &str, int id) { |
| 24 | QTextBoundaryFinder finder(QTextBoundaryFinder::BoundaryType::Word, str); |
| 25 | finder.toStart(); |
| 26 | do { |
| 27 | auto start = finder.position(); |
| 28 | finder.toNextBoundary(); |
| 29 | auto end = finder.position(); |
| 30 | |
| 31 | auto ref = QStringView(str).mid(start, end - start).trimmed(); |
| 32 | if (!ref.isEmpty()) |
| 33 | trie_.insert<ElementRank::second>(ref.toUcs4(), id); |
| 34 | } while (finder.position() < str.size()); |
| 35 | }; |
| 36 | |
| 37 | const auto start_at = std::chrono::steady_clock::now(); |
| 38 | |
| 39 | // insert full texts and partial matches |
| 40 | for (int i = 0; i < sourceModel()->rowCount(); i++) { |
| 41 | // full texts are ranked first and partial matches second |
| 42 | // that way when searching full texts will be first in result list |
| 43 | |
| 44 | auto string1 = sourceModel() |
| 45 | ->data(sourceModel()->index(i, 0), CompletionModel::SearchRole) |
| 46 | .toString() |
| 47 | .toCaseFolded(); |
| 48 | if (!string1.isEmpty()) { |
| 49 | trie_.insert<ElementRank::first>(string1.toUcs4(), i); |
| 50 | insertParts(string1, i); |
| 51 | } |
| 52 | |
| 53 | auto string2 = sourceModel() |
| 54 | ->data(sourceModel()->index(i, 0), CompletionModel::SearchRole2) |
| 55 | .toString() |
| 56 | .toCaseFolded(); |
| 57 | if (!string2.isEmpty()) { |
| 58 | trie_.insert<ElementRank::first>(string2.toUcs4(), i); |
| 59 | insertParts(string2, i); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | const auto end_at = std::chrono::steady_clock::now(); |
| 64 | const auto build_time = std::chrono::duration<double, std::milli>(end_at - start_at); |
| 65 | nhlog::ui()->debug("CompletionProxyModel: build trie: {} ms", build_time.count()); |
| 66 | |
| 67 | // initialize default mapping |
| 68 | mapping.resize(std::min(max_completions_, static_cast<size_t>(model->rowCount()))); |
| 69 | std::iota(mapping.begin(), mapping.end(), 0); |
| 70 | |
| 71 | connect( |
| 72 | this, |
| 73 | &CompletionProxyModel::newSearchString, |
| 74 | this, |
| 75 | [this](const QString &s) { |
| 76 | searchString_ = s.toCaseFolded(); |
| 77 | invalidate(); |
| 78 | }, |
| 79 | Qt::QueuedConnection); |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | CompletionProxyModel::invalidate() |
| 84 | { |
| 85 | auto key = searchString_.toUcs4(); |
| 86 | beginResetModel(); |
| 87 | if (!key.empty()) // return default model data, if no search string |
| 88 | mapping = trie_.search(key, max_completions_, maxMistakes_); |
| 89 | endResetModel(); |
| 90 | } |
| 91 | |
| 92 | QHash<int, QByteArray> |
| 93 | CompletionProxyModel::roleNames() const |
| 94 | { |
| 95 | return this->sourceModel()->roleNames(); |
| 96 | } |
| 97 | |
| 98 | int |
| 99 | CompletionProxyModel::rowCount(const QModelIndex &) const |
| 100 | { |
| 101 | if (searchString_.isEmpty()) |
| 102 | return std::min( |
| 103 | static_cast<int>(std::min<size_t>(max_completions_, std::numeric_limits<int>::max())), |
| 104 | sourceModel()->rowCount()); |
| 105 | else |
| 106 | return (int)mapping.size(); |
| 107 | } |
| 108 | |
| 109 | QModelIndex |
| 110 | CompletionProxyModel::mapFromSource(const QModelIndex &sourceIndex) const |
| 111 | { |
| 112 | // return default model data, if no search string |
| 113 | if (searchString_.isEmpty()) { |
| 114 | return index(sourceIndex.row(), 0); |
| 115 | } |
| 116 | |
| 117 | for (int i = 0; i < (int)mapping.size(); i++) { |
| 118 | if (mapping[i] == sourceIndex.row()) { |
| 119 | return index(i, 0); |
| 120 | } |
| 121 | } |
| 122 | return QModelIndex(); |
| 123 | } |
| 124 | |
| 125 | QModelIndex |
| 126 | CompletionProxyModel::mapToSource(const QModelIndex &proxyIndex) const |
| 127 | { |
| 128 | auto row = proxyIndex.row(); |
| 129 | |
| 130 | // return default model data, if no search string |
| 131 | if (searchString_.isEmpty()) { |
| 132 | return index(row, 0); |
| 133 | } |
| 134 | |
| 135 | if (row < 0 || row >= (int)mapping.size()) |
| 136 | return QModelIndex(); |
| 137 | |
| 138 | return sourceModel()->index(mapping[row], 0); |
| 139 | } |
| 140 | |
| 141 | QModelIndex |
| 142 | CompletionProxyModel::index(int row, int column, const QModelIndex &) const |
| 143 | { |
| 144 | return createIndex(row, column); |
| 145 | } |
| 146 | |
| 147 | QModelIndex |
| 148 | CompletionProxyModel::parent(const QModelIndex &) const |
| 149 | { |
| 150 | return QModelIndex{}; |
| 151 | } |
| 152 | int |
| 153 | CompletionProxyModel::columnCount(const QModelIndex &) const |
| 154 | { |
| 155 | return sourceModel()->columnCount(); |
| 156 | } |
| 157 | |
| 158 | QVariant |
| 159 | CompletionProxyModel::completionAt(int i) const |
| 160 | { |
| 161 | if (i >= 0 && i < rowCount()) |
| 162 | return data(index(i, 0), CompletionModel::CompletionRole); |
| 163 | else |
| 164 | return {}; |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | CompletionProxyModel::setSearchString(const QString &s) |
| 169 | { |
| 170 | emit newSearchString(s); |
| 171 | } |
| 172 | |
| 173 | #include "moc_CompletionProxyModel.cpp" |