summary refs log tree commit diff
path: root/src/CompletionProxyModel.h
blob: ba28e84c6c4db5966d3140aeab854f60f3810b04 (plain) (blame)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#pragma once

// Class for showing a limited amount of completions at a time

#include <QAbstractProxyModel>

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

template<typename Key, typename Value>
struct trie
{
        std::vector<Value> values;
        std::map<Key, trie> next;

        void insert(const QVector<Key> &keys, const Value &v)
        {
                auto t = this;
                for (const auto k : keys) {
                        t = &t->next[k];
                }

                t->values.push_back(v);
        }

        std::vector<Value> valuesAndSubvalues(size_t limit = -1) const
        {
                std::vector<Value> ret;
                if (limit < 200)
                        ret.reserve(limit);

                for (const auto &v : values) {
                        if (ret.size() >= limit)
                                return ret;
                        else
                                ret.push_back(v);
                }

                for (const auto &[k, t] : next) {
                        (void)k;
                        if (ret.size() >= limit)
                                return ret;
                        else {
                                auto temp = t.valuesAndSubvalues(limit - ret.size());
                                for (auto &&v : temp) {
                                        if (ret.size() >= limit)
                                                return ret;

                                        if (std::find(ret.begin(), ret.end(), v) == ret.end()) {
                                                ret.push_back(std::move(v));
                                        }
                                }
                        }
                }

                return ret;
        }

        std::vector<Value> search(const QVector<Key> &keys,
                                  size_t limit,
                                  size_t max_distance = 2) const
        {
                std::vector<Value> ret;
                if (!limit)
                        return ret;

                auto append = [&ret, limit](std::vector<Value> &&in) {
                        for (auto &&v : in) {
                                if (ret.size() >= limit)
                                        return;

                                if (std::find(ret.begin(), ret.end(), v) == ret.end()) {
                                        ret.push_back(std::move(v));
                                }
                        }
                };

                {
                        auto t = this;
                        int i  = 0;
                        for (; i < (int)keys.size(); i++) {
                                if (auto e = t->next.find(keys[i]); e != t->next.end()) {
                                        t = &e->second;
                                } else {
                                        t = nullptr;
                                        break;
                                }
                        }

                        if (t) {
                                ret = t->valuesAndSubvalues(limit);
                        }
                }

                if (max_distance && keys.size() < static_cast<int>(limit) && keys.size() > 1) {
                        max_distance -= 1;

                        // swap chars case
                        if (keys.size() >= 2) {
                                auto t = this;
                                for (int i = 1; i >= 0; i--) {
                                        if (auto e = t->next.find(keys[i]); e != t->next.end()) {
                                                t = &e->second;
                                        } else {
                                                t = nullptr;
                                                break;
                                        }
                                }

                                if (t) {
                                        append(t->search(
                                          keys.mid(2), (limit - ret.size()) * 2, max_distance));
                                }
                        }

                        // delete character case
                        append(this->search(keys.mid(1), (limit - ret.size()) * 2, max_distance));

                        // substitute and insert cases
                        for (const auto &[k, t] : this->next) {
                                if (k == keys[0] || ret.size() >= limit)
                                        break;

                                // substitute
                                append(this->search(keys.mid(1), limit - ret.size(), max_distance));

                                if (ret.size() >= limit)
                                        break;

                                // insert
                                append(this->search(keys, limit - ret.size(), max_distance));
                        }
                }

                return ret;
        }
};

class CompletionProxyModel : public QAbstractProxyModel
{
        Q_OBJECT

public:
        CompletionProxyModel(QAbstractItemModel *model, QObject *parent = nullptr)
          : QAbstractProxyModel(parent)
        {
                setSourceModel(model);

                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);

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

                        if (!string2.isEmpty())
                                trie_.insert(string2.toUcs4(), i);
                }

                connect(
                  this,
                  &CompletionProxyModel::newSearchString,
                  this,
                  [this](QString s) {
                          s.remove(":");
                          s.remove("@");
                          searchString = s.toLower();
                          invalidate();
                  },
                  Qt::QueuedConnection);
        }

        void 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> roleNames() const override
        {
                return this->sourceModel()->roleNames();
        }

        int rowCount(const QModelIndex &parent = QModelIndex()) const override
        {
                (void)parent;
                return (int)mapping.size();
        }

        QModelIndex mapFromSource(const QModelIndex &sourceIndex) const override
        {
                for (int i = 0; i < (int)mapping.size(); i++) {
                        if (mapping[i] == sourceIndex.row()) {
                                return index(i, 0);
                        }
                }
                return QModelIndex();
        }

        QModelIndex mapToSource(const QModelIndex &proxyIndex) const override
        {
                auto row = proxyIndex.row();
                if (row < 0 || row >= (int)mapping.size())
                        return QModelIndex();

                return sourceModel()->index(mapping[row], 0);
        }

        QModelIndex index(int row,
                          int column,
                          const QModelIndex &parent = QModelIndex()) const override
        {
                (void)parent;
                return createIndex(row, column);
        }

        QModelIndex parent(const QModelIndex &) const override { return QModelIndex{}; }
        int columnCount(const QModelIndex &) const override { return sourceModel()->columnCount(); }

public slots:
        QVariant completionAt(int i) const
        {
                if (i >= 0 && i < rowCount())
                        return data(index(i, 0), CompletionModel::CompletionRole);
                else
                        return {};
        }

        void setSearchString(QString s) { emit newSearchString(s); }

signals:
        void newSearchString(QString);

private:
        QString searchString;
        trie<uint, int> trie_;
        std::vector<int> mapping;
};