KTextEditor

codecompletionmodel.cpp
1 /*
2  SPDX-FileCopyrightText: 2005-2006 Hamish Rodda <[email protected]>
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 
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(row, column, (void *)nullptr);
48 }
49 
51 {
53 
54  for (int i = CompletionRole; i <= AccessibilityAccept; ++i) {
55  QVariant v = data(index, i);
56  if (v.isValid()) {
57  ret.insert(i, v);
58  }
59  }
60 
61  return ret;
62 }
63 
65 {
66  return QModelIndex();
67 }
68 
69 void CodeCompletionModel::setRowCount(int rowCount)
70 {
71  d->rowCount = rowCount;
72 }
73 
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 
91 {
92  view->document()->replaceText(word, data(index.sibling(index.row(), Name)).toString());
93 }
94 
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(this, hasGroups);
105  }
106 }
virtual Document * document() const =0
Get the view&#39;s document, that means the view is a view of the returned document.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
Reimplemented from QAbstractItemModel::rowCount().
virtual QMap< int, QVariant > itemData(const QModelIndex &index) const const
virtual void completionInvoked(KTextEditor::View *view, const KTextEditor::Range &range, InvocationType invocationType)
This function is responsible to generating / updating the list of current completions.
QMap< int, QVariant > itemData(const QModelIndex &index) const override
Reimplemented from QAbstractItemModel::itemData().
bool hasGroups() const
This function returns true if the model needs grouping, otherwise false.
virtual void executeCompletionItem(KTextEditor::View *view, const Range &word, const QModelIndex &index) const
This function is responsible for inserting a selected completion into the view.
bool isValid() const const
int row() const const
The KTextEditor namespace contains all the public API that is required to use the KTextEditor compone...
Definition: katetextblock.h:22
int columnCount(const QModelIndex &parent=QModelIndex()) const override
Reimplemented from QAbstractItemModel::columnCount().
An object representing a section of text, from one Cursor to another.
QString toString() const
Returns the cursor position as string in the format "start-line:start-column,endl-line:end-column".
QModelIndex sibling(int row, int column) const const
bool isValid() const const
QMap::iterator insert(const Key &key, const T &value)
QModelIndex index(int row, int column, const QModelIndex &parent=QModelIndex()) const override
Reimplemented from QAbstractItemModel::index().
A text widget with KXMLGUIClient that represents a Document.
Definition: view.h:146
QObject * parent() const const
virtual bool replaceText(const Range &range, const QString &text, bool block=false)
Replace text from range with specified text.
Definition: document.cpp:90
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Thu Dec 2 2021 22:51:18 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.