diff options
Diffstat (limited to 'src/CompletionProxyModel.cpp')
-rw-r--r-- | src/CompletionProxyModel.cpp | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/src/CompletionProxyModel.cpp b/src/CompletionProxyModel.cpp index 2341c292..412708a2 100644 --- a/src/CompletionProxyModel.cpp +++ b/src/CompletionProxyModel.cpp @@ -12,16 +12,18 @@ CompletionProxyModel::CompletionProxyModel(QAbstractItemModel *model, int max_mistakes, + size_t max_completions, QObject *parent) : QAbstractProxyModel(parent) , maxMistakes_(max_mistakes) + , max_completions_(max_completions) { setSourceModel(model); QRegularExpression splitPoints("\\s+|-"); // insert all the full texts for (int i = 0; i < sourceModel()->rowCount(); i++) { - if (i < 7) + if (static_cast<size_t>(i) < max_completions_) mapping.push_back(i); auto string1 = sourceModel() @@ -82,14 +84,9 @@ CompletionProxyModel::invalidate() { auto key = searchString_.toUcs4(); beginResetModel(); - mapping = trie_.search(key, 7, maxMistakes_); + if (!key.empty()) // return default model data, if no search string + mapping = trie_.search(key, max_completions_, maxMistakes_); endResetModel(); - - std::string temp; - for (auto v : mapping) { - temp += std::to_string(v) + ", "; - } - nhlog::ui()->debug("mapping: {}", temp); } QHash<int, QByteArray> @@ -101,12 +98,22 @@ CompletionProxyModel::roleNames() const int CompletionProxyModel::rowCount(const QModelIndex &) const { - return (int)mapping.size(); + if (searchString_.isEmpty()) + return std::min(static_cast<int>(std::min<size_t>(max_completions_, + std::numeric_limits<int>::max())), + sourceModel()->rowCount()); + else + return (int)mapping.size(); } QModelIndex CompletionProxyModel::mapFromSource(const QModelIndex &sourceIndex) const { + // return default model data, if no search string + if (searchString_.isEmpty()) { + return index(sourceIndex.row(), 0); + } + for (int i = 0; i < (int)mapping.size(); i++) { if (mapping[i] == sourceIndex.row()) { return index(i, 0); @@ -119,6 +126,12 @@ QModelIndex CompletionProxyModel::mapToSource(const QModelIndex &proxyIndex) const { auto row = proxyIndex.row(); + + // return default model data, if no search string + if (searchString_.isEmpty()) { + return index(row, 0); + } + if (row < 0 || row >= (int)mapping.size()) return QModelIndex(); |