summaryrefslogtreecommitdiffstats
path: root/src/CompletionProxyModel.cpp
blob: fa5b3c2d25b820d14f7803ebd7be0bda567ef4cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "CompletionProxyModel.h"

#include <QRegularExpression>

#include "CompletionModelRoles.h"
#include "Logging.h"
#include "Utils.h"

CompletionProxyModel::CompletionProxyModel(QAbstractItemModel *model, QObject *parent)
  : QAbstractProxyModel(parent)
{
        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);

                for (const auto &e : string1.split(splitPoints)) {
                        if (!e.isEmpty()) // NOTE(Nico): Use Qt::SkipEmptyParts in Qt 5.14
                                trie_.insert(e.toUcs4(), i);
                }

                auto string2 = sourceModel()
                                 ->data(sourceModel()->index(i, 0), CompletionModel::SearchRole2)
                                 .toString()
                                 .toLower();

                if (!string2.isEmpty()) {
                        trie_.insert(string2.toUcs4(), i);
                        for (const auto &e : string2.split(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) {
                  s.remove(":");
                  s.remove("@");
                  searchString = s.toLower();
                  invalidate();
          },
          Qt::QueuedConnection);
}

void
CompletionProxyModel::invalidate()
{
        auto key = searchString.toUcs4();
        beginResetModel();
        mapping = trie_.search(key, 7);
        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);
}