2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
2022-01-01 06:57:53 +03:00
|
|
|
// SPDX-FileCopyrightText: 2022 Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-24 17:35:56 +03:00
|
|
|
#include "CompletionProxyModel.h"
|
|
|
|
|
|
|
|
#include <QRegularExpression>
|
2022-01-31 23:36:22 +03:00
|
|
|
#include <QTextBoundaryFinder>
|
2020-11-24 17:35:56 +03:00
|
|
|
|
|
|
|
#include "CompletionModelRoles.h"
|
|
|
|
#include "Logging.h"
|
|
|
|
#include "Utils.h"
|
|
|
|
|
2021-03-06 21:48:24 +03:00
|
|
|
CompletionProxyModel::CompletionProxyModel(QAbstractItemModel *model,
|
|
|
|
int max_mistakes,
|
2021-04-18 21:21:03 +03:00
|
|
|
size_t max_completions,
|
2021-03-06 21:48:24 +03:00
|
|
|
QObject *parent)
|
2020-11-24 17:35:56 +03:00
|
|
|
: QAbstractProxyModel(parent)
|
2021-03-06 21:48:24 +03:00
|
|
|
, maxMistakes_(max_mistakes)
|
2021-04-18 21:21:03 +03:00
|
|
|
, max_completions_(max_completions)
|
2020-11-24 17:35:56 +03:00
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
setSourceModel(model);
|
|
|
|
|
2022-07-23 00:52:11 +03:00
|
|
|
auto insertParts = [this](const QString &str, int id) {
|
|
|
|
QTextBoundaryFinder finder(QTextBoundaryFinder::BoundaryType::Word, str);
|
|
|
|
finder.toStart();
|
|
|
|
do {
|
|
|
|
auto start = finder.position();
|
|
|
|
finder.toNextBoundary();
|
|
|
|
auto end = finder.position();
|
|
|
|
|
|
|
|
auto ref = str.midRef(start, end - start).trimmed();
|
|
|
|
if (!ref.isEmpty())
|
|
|
|
trie_.insert<ElementRank::second>(ref.toUcs4(), id);
|
|
|
|
} while (finder.position() < str.size());
|
|
|
|
};
|
|
|
|
|
|
|
|
const auto start_at = std::chrono::steady_clock::now();
|
|
|
|
|
|
|
|
// insert full texts and partial matches
|
2021-09-18 01:22:33 +03:00
|
|
|
for (int i = 0; i < sourceModel()->rowCount(); i++) {
|
2022-07-23 00:52:11 +03:00
|
|
|
// full texts are ranked first and partial matches second
|
|
|
|
// that way when searching full texts will be first in result list
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
auto string1 = sourceModel()
|
|
|
|
->data(sourceModel()->index(i, 0), CompletionModel::SearchRole)
|
|
|
|
.toString()
|
|
|
|
.toLower();
|
2022-07-23 00:52:11 +03:00
|
|
|
if (!string1.isEmpty()) {
|
|
|
|
trie_.insert<ElementRank::first>(string1.toUcs4(), i);
|
|
|
|
insertParts(string1, i);
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
auto string2 = sourceModel()
|
|
|
|
->data(sourceModel()->index(i, 0), CompletionModel::SearchRole2)
|
|
|
|
.toString()
|
|
|
|
.toLower();
|
2022-07-23 00:52:11 +03:00
|
|
|
if (!string2.isEmpty()) {
|
|
|
|
trie_.insert<ElementRank::first>(string2.toUcs4(), i);
|
|
|
|
insertParts(string2, i);
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
|
2022-07-23 00:52:11 +03:00
|
|
|
const auto end_at = std::chrono::steady_clock::now();
|
|
|
|
const auto build_time = std::chrono::duration<double, std::milli>(end_at - start_at);
|
|
|
|
nhlog::ui()->debug("CompletionProxyModel: build trie: {} ms", build_time.count());
|
2021-09-18 01:22:33 +03:00
|
|
|
|
2022-07-23 00:52:11 +03:00
|
|
|
// initialize default mapping
|
|
|
|
mapping.resize(std::min(max_completions_, static_cast<size_t>(model->rowCount())));
|
|
|
|
std::iota(mapping.begin(), mapping.end(), 0);
|
2021-09-18 01:22:33 +03:00
|
|
|
|
|
|
|
connect(
|
|
|
|
this,
|
|
|
|
&CompletionProxyModel::newSearchString,
|
|
|
|
this,
|
|
|
|
[this](QString s) {
|
|
|
|
searchString_ = s.toLower();
|
|
|
|
invalidate();
|
|
|
|
},
|
|
|
|
Qt::QueuedConnection);
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CompletionProxyModel::invalidate()
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto key = searchString_.toUcs4();
|
|
|
|
beginResetModel();
|
|
|
|
if (!key.empty()) // return default model data, if no search string
|
|
|
|
mapping = trie_.search(key, max_completions_, maxMistakes_);
|
|
|
|
endResetModel();
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
CompletionProxyModel::roleNames() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return this->sourceModel()->roleNames();
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
CompletionProxyModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
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();
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
CompletionProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
// 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);
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return QModelIndex();
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
CompletionProxyModel::mapToSource(const QModelIndex &proxyIndex) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
auto row = proxyIndex.row();
|
2021-04-18 21:21:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
// return default model data, if no search string
|
|
|
|
if (searchString_.isEmpty()) {
|
|
|
|
return index(row, 0);
|
|
|
|
}
|
2021-04-18 21:21:03 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (row < 0 || row >= (int)mapping.size())
|
|
|
|
return QModelIndex();
|
2020-11-24 17:35:56 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
return sourceModel()->index(mapping[row], 0);
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
CompletionProxyModel::index(int row, int column, const QModelIndex &) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return createIndex(row, column);
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
CompletionProxyModel::parent(const QModelIndex &) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return QModelIndex{};
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
int
|
|
|
|
CompletionProxyModel::columnCount(const QModelIndex &) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return sourceModel()->columnCount();
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
CompletionProxyModel::completionAt(int i) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (i >= 0 && i < rowCount())
|
|
|
|
return data(index(i, 0), CompletionModel::CompletionRole);
|
|
|
|
else
|
|
|
|
return {};
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CompletionProxyModel::setSearchString(QString s)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
emit newSearchString(s);
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|