-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScriptEdit.cpp
More file actions
164 lines (138 loc) · 4.94 KB
/
ScriptEdit.cpp
File metadata and controls
164 lines (138 loc) · 4.94 KB
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
#include <QMetaProperty>
#include <QMetaObject>
#include <QScrollBar>
#include "ScriptEdit.h"
ScriptEdit::ScriptEdit(QWidget *parent)
: QTextEdit(parent), m_completer(nullptr), m_completerModel(nullptr)
{
m_completer = new QCompleter(this);
m_completerModel = new QStringListModel(m_completer);
m_completer->setModel(m_completerModel);
m_completer->setCaseSensitivity(Qt::CaseInsensitive);
m_completer->setWrapAround(false);
m_completer->setWidget(this);
m_completer->setCompletionMode(QCompleter::PopupCompletion);
QObject::connect(m_completer, SIGNAL(activated(QString)), this, SLOT(insertCompletion(QString)));
}
ScriptEdit::~ScriptEdit()
{
}
QCompleter *ScriptEdit::completer() const
{
return m_completer;
}
void ScriptEdit::insertCompletion(const QString& completion)
{
if (m_completer->widget() != this)
return;
QTextCursor tc = textCursor();
tc.select(QTextCursor::WordUnderCursor);
tc.insertText(completion);
setTextCursor(tc);
}
QString ScriptEdit::textUnderCursor() const
{
QTextCursor tc = textCursor();
tc.select(QTextCursor::BlockUnderCursor);
return tc.selectedText();
}
void ScriptEdit::focusInEvent(QFocusEvent *e)
{
if (m_completer)
m_completer->setWidget(this);
QTextEdit::focusInEvent(e);
}
void ScriptEdit::keyPressEvent(QKeyEvent *e)
{
if (m_completer && m_completer->popup()->isVisible())
{
// The following keys are forwarded by the completer to the widget
switch (e->key()) {
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Escape:
case Qt::Key_Tab:
case Qt::Key_Backtab:
e->ignore();
return; // let the completer do default behavior
default:
break;
}
}
bool isShortcut = ((e->modifiers() & Qt::ControlModifier) && e->key() == Qt::Key_Space); // CTRL+E
if (!m_completer || !isShortcut) // do not process the shortcut when we have a completer
QTextEdit::keyPressEvent(e);
const bool ctrlOrShift = e->modifiers() & (Qt::ControlModifier | Qt::ShiftModifier);
if (!m_completer || (ctrlOrShift && e->text().isEmpty()))
return;
static QString eow(" ~!@#$%^&*_+{}|:\"<>?,/;'[]()\\-="); // end of word
bool hasModifier = (e->modifiers() != Qt::NoModifier) && !ctrlOrShift;
QString completionPrefix = textUnderCursor();
int pos = textCursor().positionInBlock();
bool complete = false;
// Check autocomplete
if (completionPrefix.size() >= 6 && pos >= 6 && completionPrefix.contains("spell."))
{
// Remove all after first right whitespace about cursor position
for (qint32 i = pos; i < completionPrefix.size(); ++i)
{
if (completionPrefix.at(i) == ' ')
{
completionPrefix.remove(i, completionPrefix.size() - i);
break;
}
}
// Remove all before first dot about cursor position
// and contains 'spell.'
for (qint32 i = pos - 1; i != -1; --i)
{
if (completionPrefix.at(i) == '.')
{
if (completionPrefix.mid(i - 5, 6).startsWith("spell."))
{
completionPrefix.remove(0, i + 1);
complete = true;
break;
}
}
}
}
if (!isShortcut && e->key() != Qt::Key_Period && (hasModifier || e->text().isEmpty() || !complete || eow.contains(e->text().right(1))))
{
m_completer->popup()->hide();
return;
}
if (completionPrefix != m_completer->completionPrefix())
{
m_completer->setCompletionPrefix(completionPrefix);
m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
}
if (completionPrefix == m_completer->currentCompletion())
{
m_completer->popup()->hide();
return;
}
QRect cr = cursorRect();
cr.setWidth(m_completer->popup()->sizeHintForColumn(0) + m_completer->popup()->verticalScrollBar()->sizeHint().width());
m_completer->complete(cr);
}
void ScriptEdit::setupCompleter(QObject* metaSpell)
{
QSet<QString> fields;
qint32 propertyCount = metaSpell->metaObject()->propertyCount();
qint32 methodCount = metaSpell->metaObject()->methodCount();
for (qint32 i = 1; i < propertyCount; ++i)
fields << metaSpell->metaObject()->property(i).name();
for (qint32 i = 0; i < methodCount; ++i)
{
QString methodName = metaSpell->metaObject()->method(i).methodSignature();
if (methodName.contains("(uchar)"))
fields << methodName.replace("(uchar)", "(index)");
else if (methodName.contains("(uint)"))
fields << methodName.replace("(uint)", "(id)");
else if (methodName.contains("(qulonglong)"))
fields << methodName.replace("(qulonglong)", "(flags)");
}
m_completerModel->setStringList(fields.toList());
m_completerModel->sort(0);
}