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,
|
|
|
|
QObject *parent)
|
2020-11-24 17:35:56 +03:00
|
|
|
: QAbstractProxyModel(parent)
|
2021-03-06 21:48:24 +03:00
|
|
|
, maxMistakes_(max_mistakes)
|
2020-11-24 17:35:56 +03:00
|
|
|
{
|
|
|
|
setSourceModel(model);
|
|
|
|
QRegularExpression splitPoints("\\s+|-");
|
|
|
|
|
|
|
|
for (int i = 0; i < sourceModel()->rowCount(); i++) {
|
|
|
|
if (i < 7)
|
|
|
|
mapping.push_back(i);
|
|
|
|
|
|
|
|
auto string1 = sourceModel()
|
|
|
|
->data(sourceModel()->index(i, 0), CompletionModel::SearchRole)
|
|
|
|
.toString()
|
|
|
|
.toLower();
|
|
|
|
trie_.insert(string1.toUcs4(), i);
|
|
|
|
|
2020-11-27 01:59:14 +03:00
|
|
|
for (const auto &e : string1.split(splitPoints)) {
|
|
|
|
if (!e.isEmpty()) // NOTE(Nico): Use Qt::SkipEmptyParts in Qt 5.14
|
|
|
|
trie_.insert(e.toUcs4(), i);
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto string2 = sourceModel()
|
|
|
|
->data(sourceModel()->index(i, 0), CompletionModel::SearchRole2)
|
|
|
|
.toString()
|
|
|
|
.toLower();
|
|
|
|
|
|
|
|
if (!string2.isEmpty()) {
|
|
|
|
trie_.insert(string2.toUcs4(), i);
|
2020-11-27 01:59:14 +03:00
|
|
|
for (const auto &e : string2.split(splitPoints)) {
|
|
|
|
if (!e.isEmpty()) // NOTE(Nico): Use Qt::SkipEmptyParts in Qt 5.14
|
|
|
|
trie_.insert(e.toUcs4(), i);
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-05 17:26:07 +03:00
|
|
|
connect(
|
|
|
|
this,
|
|
|
|
&CompletionProxyModel::newSearchString,
|
|
|
|
this,
|
|
|
|
[this](QString s) {
|
|
|
|
s.remove(":");
|
|
|
|
s.remove("@");
|
|
|
|
searchString = s.toLower();
|
|
|
|
invalidate();
|
|
|
|
},
|
|
|
|
Qt::QueuedConnection);
|
2020-11-24 17:35:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CompletionProxyModel::invalidate()
|
|
|
|
{
|
|
|
|
auto key = searchString.toUcs4();
|
|
|
|
beginResetModel();
|
2021-03-06 21:48:24 +03:00
|
|
|
mapping = trie_.search(key, 7, maxMistakes_);
|
2020-11-24 17:35:56 +03:00
|
|
|
endResetModel();
|
|
|
|
|
|
|
|
std::string temp;
|
|
|
|
for (auto v : mapping) {
|
|
|
|
temp += std::to_string(v) + ", ";
|
|
|
|
}
|
|
|
|
nhlog::ui()->debug("mapping: {}", temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
CompletionProxyModel::roleNames() const
|
|
|
|
{
|
|
|
|
return this->sourceModel()->roleNames();
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
CompletionProxyModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return (int)mapping.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
CompletionProxyModel::mapFromSource(const QModelIndex &sourceIndex) const
|
|
|
|
{
|
|
|
|
for (int i = 0; i < (int)mapping.size(); i++) {
|
|
|
|
if (mapping[i] == sourceIndex.row()) {
|
|
|
|
return index(i, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return QModelIndex();
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
CompletionProxyModel::mapToSource(const QModelIndex &proxyIndex) const
|
|
|
|
{
|
|
|
|
auto row = proxyIndex.row();
|
|
|
|
if (row < 0 || row >= (int)mapping.size())
|
|
|
|
return QModelIndex();
|
|
|
|
|
|
|
|
return sourceModel()->index(mapping[row], 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
CompletionProxyModel::index(int row, int column, const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return createIndex(row, column);
|
|
|
|
}
|
|
|
|
|
|
|
|
QModelIndex
|
|
|
|
CompletionProxyModel::parent(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return QModelIndex{};
|
|
|
|
}
|
|
|
|
int
|
|
|
|
CompletionProxyModel::columnCount(const QModelIndex &) const
|
|
|
|
{
|
|
|
|
return sourceModel()->columnCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
CompletionProxyModel::completionAt(int i) const
|
|
|
|
{
|
|
|
|
if (i >= 0 && i < rowCount())
|
|
|
|
return data(index(i, 0), CompletionModel::CompletionRole);
|
|
|
|
else
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CompletionProxyModel::setSearchString(QString s)
|
|
|
|
{
|
|
|
|
emit newSearchString(s);
|
|
|
|
}
|