2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-11-24 17:35:56 +03:00
|
|
|
#include "CompletionProxyModel.h"
|
|
|
|
|
|
|
|
#include <QRegularExpression>
|
|
|
|
|
|
|
|
#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);
|
|
|
|
QChar splitPoints(' ');
|
|
|
|
|
|
|
|
// insert all the full texts
|
|
|
|
for (int i = 0; i < sourceModel()->rowCount(); i++) {
|
|
|
|
if (static_cast<size_t>(i) < max_completions_)
|
|
|
|
mapping.push_back(i);
|
|
|
|
|
|
|
|
auto string1 = sourceModel()
|
|
|
|
->data(sourceModel()->index(i, 0), CompletionModel::SearchRole)
|
|
|
|
.toString()
|
|
|
|
.toLower();
|
|
|
|
if (!string1.isEmpty())
|
|
|
|
trie_.insert(string1.toUcs4(), i);
|
|
|
|
|
|
|
|
auto string2 = sourceModel()
|
|
|
|
->data(sourceModel()->index(i, 0), CompletionModel::SearchRole2)
|
|
|
|
.toString()
|
|
|
|
.toLower();
|
|
|
|
if (!string2.isEmpty())
|
|
|
|
trie_.insert(string2.toUcs4(), i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert the partial matches
|
|
|
|
for (int i = 0; i < sourceModel()->rowCount(); i++) {
|
|
|
|
auto string1 = sourceModel()
|
|
|
|
->data(sourceModel()->index(i, 0), CompletionModel::SearchRole)
|
|
|
|
.toString()
|
|
|
|
.toLower();
|
|
|
|
|
|
|
|
for (const auto &e : string1.splitRef(splitPoints)) {
|
|
|
|
if (!e.isEmpty()) // NOTE(Nico): Use Qt::SkipEmptyParts in Qt 5.14
|
|
|
|
trie_.insert(e.toUcs4(), i);
|
2021-03-14 03:24:26 +03:00
|
|
|
}
|
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
auto string2 = sourceModel()
|
|
|
|
->data(sourceModel()->index(i, 0), CompletionModel::SearchRole2)
|
|
|
|
.toString()
|
|
|
|
.toLower();
|
2020-11-24 17:35:56 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (!string2.isEmpty()) {
|
|
|
|
for (const auto &e : string2.splitRef(splitPoints)) {
|
|
|
|
if (!e.isEmpty()) // NOTE(Nico): Use Qt::SkipEmptyParts in Qt 5.14
|
|
|
|
trie_.insert(e.toUcs4(), i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|