| 1 | /* |
| 2 | SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <rodda@kde.org> |
| 3 | |
| 4 | SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 | */ |
| 6 | |
| 7 | #include "codecompletionmodel.h" |
| 8 | |
| 9 | #include "document.h" |
| 10 | #include "view.h" |
| 11 | |
| 12 | using namespace KTextEditor; |
| 13 | |
| 14 | class KTextEditor::CodeCompletionModelPrivate |
| 15 | { |
| 16 | public: |
| 17 | CodeCompletionModelPrivate() |
| 18 | { |
| 19 | } |
| 20 | |
| 21 | int rowCount = 0; |
| 22 | bool hasGroups = false; |
| 23 | }; |
| 24 | |
| 25 | CodeCompletionModel::CodeCompletionModel(QObject *parent) |
| 26 | : QAbstractItemModel(parent) |
| 27 | , d(new CodeCompletionModelPrivate) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | CodeCompletionModel::~CodeCompletionModel() |
| 32 | { |
| 33 | delete d; |
| 34 | } |
| 35 | |
| 36 | int CodeCompletionModel::columnCount(const QModelIndex &) const |
| 37 | { |
| 38 | return ColumnCount; |
| 39 | } |
| 40 | |
| 41 | QModelIndex CodeCompletionModel::index(int row, int column, const QModelIndex &parent) const |
| 42 | { |
| 43 | if (row < 0 || row >= d->rowCount || column < 0 || column >= ColumnCount || parent.isValid()) { |
| 44 | return QModelIndex(); |
| 45 | } |
| 46 | |
| 47 | return createIndex(arow: row, acolumn: column, adata: (void *)nullptr); |
| 48 | } |
| 49 | |
| 50 | QMap<int, QVariant> CodeCompletionModel::itemData(const QModelIndex &index) const |
| 51 | { |
| 52 | QMap<int, QVariant> ret = QAbstractItemModel::itemData(index); |
| 53 | |
| 54 | for (int i = CompletionRole; i <= AccessibilityAccept; ++i) { |
| 55 | QVariant v = data(index, role: i); |
| 56 | if (v.isValid()) { |
| 57 | ret.insert(key: i, value: v); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | return ret; |
| 62 | } |
| 63 | |
| 64 | QModelIndex CodeCompletionModel::parent(const QModelIndex &) const |
| 65 | { |
| 66 | return QModelIndex(); |
| 67 | } |
| 68 | |
| 69 | void CodeCompletionModel::setRowCount(int rowCount) |
| 70 | { |
| 71 | d->rowCount = rowCount; |
| 72 | } |
| 73 | |
| 74 | int CodeCompletionModel::rowCount(const QModelIndex &parent) const |
| 75 | { |
| 76 | if (parent.isValid()) { |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | return d->rowCount; |
| 81 | } |
| 82 | |
| 83 | void CodeCompletionModel::completionInvoked(KTextEditor::View *view, const Range &range, InvocationType invocationType) |
| 84 | { |
| 85 | Q_UNUSED(view) |
| 86 | Q_UNUSED(range) |
| 87 | Q_UNUSED(invocationType) |
| 88 | } |
| 89 | |
| 90 | void CodeCompletionModel::executeCompletionItem(KTextEditor::View *view, const Range &word, const QModelIndex &index) const |
| 91 | { |
| 92 | view->document()->replaceText(range: word, text: data(index: index.sibling(arow: index.row(), acolumn: Name)).toString()); |
| 93 | } |
| 94 | |
| 95 | bool CodeCompletionModel::hasGroups() const |
| 96 | { |
| 97 | return d->hasGroups; |
| 98 | } |
| 99 | |
| 100 | void CodeCompletionModel::setHasGroups(bool hasGroups) |
| 101 | { |
| 102 | if (d->hasGroups != hasGroups) { |
| 103 | d->hasGroups = hasGroups; |
| 104 | Q_EMIT hasGroupsChanged(model: this, hasGroups); |
| 105 | } |
| 106 | } |
| 107 | |