-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommandsender.cpp
155 lines (137 loc) · 4.14 KB
/
commandsender.cpp
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
#include "commandsender.h"
CommandSender::CommandSender(QWidget *parent) : QLineEdit(parent)
{
m_cursorStart = this->cursorPosition();
m_cursorEnd = this->cursorPosition();
m_previousCommandIndex = -1;
}
void CommandSender::InitCompleter(const QStringList &stringList)
{
if (m_completer)
{
delete m_completer;
}
m_completer = new QCompleter(stringList, this);
m_completer->setWidget(this);
m_completer->setCompletionMode(QCompleter::PopupCompletion);
m_completer->setCaseSensitivity(Qt::CaseInsensitive);
connect(m_completer, QOverload<const QString &>::of(&QCompleter::activated), this, &CommandSender::insertCompletion);
}
void CommandSender::SaveCommand()
{
// Remember last used commands, up to 10
m_previousCommandIndex = -1;
m_previousCommands.push_front(this->text());
if (m_previousCommands.size() > 10)
{
m_previousCommands.pop_back();
}
}
void CommandSender::keyPressEvent(QKeyEvent *e)
{
if (!m_completer)
{
QLineEdit::keyPressEvent(e);
return;
}
bool completerVisible = m_completer->popup()->isVisible();
if (completerVisible)
{
// 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;
}
}
else
{
m_cursorStart = this->cursorPosition();
}
// Process key press
QLineEdit::keyPressEvent(e);
// Show previous commands
if (this->cursorPosition() == 0 && (e->key() == Qt::Key_Up || e->key() == Qt::Key_Down))
{
int index = m_previousCommandIndex;
if (e->key() == Qt::Key_Up)
{
m_previousCommandIndex++;
}
else if (e->key() == Qt::Key_Down)
{
m_previousCommandIndex--;
}
m_previousCommandIndex = qBound(-1, m_previousCommandIndex, m_previousCommands.size() - 1);
if (index == 0 && m_previousCommandIndex == -1)
{
this->clear();
}
else if (m_previousCommandIndex != -1)
{
this->setText(m_previousCommands[m_previousCommandIndex]);
this->setCursorPosition(0);
}
}
if (this->text().isEmpty())
{
m_previousCommandIndex = -1;
}
PopUpCompleter();
}
void CommandSender::mousePressEvent(QMouseEvent *e)
{
QLineEdit::mousePressEvent(e);
PopUpCompleter();
}
void CommandSender::insertCompletion(const QString &completion)
{
if (m_completer->widget() != this)
{
return;
}
QString text = this->text();
text.remove(m_cursorStart, m_cursorEnd - m_cursorStart);
bool addComma = (m_cursorStart == text.size());
text.insert(m_cursorStart, completion + (addComma ? "," : ""));
this->setText(text);
this->setCursorPosition(m_cursorStart + completion.length() + (addComma ? 1 : 0));
}
void CommandSender::PopUpCompleter()
{
// Force popup if edit is empty and every 2nd comma
QString const text = this->text();
QString const textLeft = text.left(this->cursorPosition());
if (textLeft.count(',') % 2 == 0 && this->cursorPosition() != 0)
{
m_cursorStart = textLeft.lastIndexOf(',') + 1;
m_cursorEnd = text.indexOf(',', this->cursorPosition());
if (m_cursorEnd == -1)
{
m_cursorEnd = text.size();
}
}
else
{
m_completer->popup()->hide();
return;
}
QString completionPrefix;
if (m_cursorEnd > m_cursorStart)
{
completionPrefix = text.mid(m_cursorStart, m_cursorEnd - m_cursorStart);
//qDebug() << completionPrefix;
}
m_completer->setCompletionPrefix(completionPrefix);
m_completer->popup()->setCurrentIndex(m_completer->completionModel()->index(0, 0));
QRect cr = cursorRect();
cr.setWidth(m_completer->popup()->sizeHintForColumn(0) + m_completer->popup()->verticalScrollBar()->sizeHint().width());
m_completer->complete(cr); // popup it up!
}