author | sheepluva |
Wed, 19 Oct 2011 02:10:27 +0200 | |
changeset 6150 | 1d98752c1fba |
parent 6149 | 0b92341adb6a |
child 6151 | 9fd5b70acb1a |
permissions | -rw-r--r-- |
6147 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com> |
|
4 |
* Copyright (c) 2007-2011 Andrey Korotaev <unC0Rr@gmail.com> |
|
5 |
* |
|
6 |
* This program is free software; you can redistribute it and/or modify |
|
7 |
* it under the terms of the GNU General Public License as published by |
|
8 |
* the Free Software Foundation; version 2 of the License |
|
9 |
* |
|
10 |
* This program is distributed in the hope that it will be useful, |
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
* GNU General Public License for more details. |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License |
|
16 |
* along with this program; if not, write to the Free Software |
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
|
18 |
*/ |
|
19 |
||
20 |
#include <QStringList> |
|
21 |
||
22 |
#include "SmartLineEdit.h" |
|
23 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
24 |
SmartLineEdit::SmartLineEdit(QWidget * parent, int maxHistorySize) |
6147 | 25 |
: QLineEdit(parent) |
26 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
27 |
m_curHistEntryIdx = 0; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
28 |
m_maxHistorySize = maxHistorySize; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
29 |
|
6147 | 30 |
m_whitespace = QRegExp("\\s"); |
31 |
||
32 |
m_cmds = new QStringList(); |
|
33 |
m_nicks = new QStringList(); |
|
34 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
35 |
m_history = new QStringList(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
36 |
|
6149 | 37 |
resetAutoCompletionStatus(); |
6147 | 38 |
|
6149 | 39 |
// reset autocompletion status when cursor is moved or content is changed |
40 |
connect(this, SIGNAL(cursorPositionChanged(int, int)), |
|
41 |
this, SLOT(resetAutoCompletionStatus())); |
|
42 |
connect(this, SIGNAL(textChanged(const QString&)), |
|
43 |
this, SLOT(resetAutoCompletionStatus())); |
|
6147 | 44 |
} |
45 |
||
46 |
||
47 |
void SmartLineEdit::addCommands(const QStringList & commands) |
|
48 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
49 |
m_keywordMutex.lock(); |
6147 | 50 |
|
51 |
m_cmds->append(commands); |
|
52 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
53 |
m_keywordMutex.unlock(); |
6147 | 54 |
} |
55 |
||
56 |
||
57 |
void SmartLineEdit::removeCommands(const QStringList & commands) |
|
58 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
59 |
m_keywordMutex.lock(); |
6147 | 60 |
|
61 |
foreach (const QString & cmd, commands) |
|
62 |
{ |
|
63 |
m_cmds->removeAll(cmd); |
|
64 |
} |
|
65 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
66 |
m_keywordMutex.unlock(); |
6147 | 67 |
} |
68 |
||
69 |
||
70 |
void SmartLineEdit::addNickname(const QString & name) |
|
71 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
72 |
m_keywordMutex.lock(); |
6147 | 73 |
|
74 |
m_nicks->append(name); |
|
75 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
76 |
m_keywordMutex.unlock(); |
6147 | 77 |
} |
78 |
||
79 |
||
80 |
void SmartLineEdit::removeNickname(const QString & name) |
|
81 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
82 |
m_keywordMutex.lock(); |
6147 | 83 |
|
84 |
m_nicks->removeAll(name); |
|
85 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
86 |
m_keywordMutex.unlock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
87 |
} |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
88 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
89 |
void SmartLineEdit::rememberCurrentText() |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
90 |
{ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
91 |
m_historyMutex.lock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
92 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
93 |
rememberCurrentTextUnsynced(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
94 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
95 |
m_historyMutex.unlock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
96 |
} |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
97 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
98 |
void SmartLineEdit::rememberCurrentTextUnsynced() |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
99 |
{ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
100 |
QString newEntry = text(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
101 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
102 |
// don't store whitespace-only/empty text |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
103 |
if (newEntry.trimmed().isEmpty()) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
104 |
return; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
105 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
106 |
m_history->removeOne(newEntry); // no duplicates please |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
107 |
m_history->append(newEntry); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
108 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
109 |
// do not keep more entries than allowed |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
110 |
if (m_history->size() > m_maxHistorySize) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
111 |
m_history->removeFirst(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
112 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
113 |
// we're looking at the latest entry |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
114 |
m_curHistEntryIdx = m_history->size() - 1; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
115 |
} |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
116 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
117 |
void SmartLineEdit::clear() |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
118 |
{ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
119 |
m_historyMutex.lock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
120 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
121 |
QLineEdit::clear(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
122 |
m_curHistEntryIdx = m_history->size(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
123 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
124 |
m_historyMutex.unlock(); |
6147 | 125 |
} |
126 |
||
6149 | 127 |
void SmartLineEdit::forgetEverything() |
128 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
129 |
// forget keywords |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
130 |
m_keywordMutex.lock(); |
6149 | 131 |
|
132 |
m_cmds->clear(); |
|
133 |
m_nicks->clear(); |
|
134 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
135 |
m_keywordMutex.unlock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
136 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
137 |
// forget history |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
138 |
m_historyMutex.lock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
139 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
140 |
m_history->clear(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
141 |
m_curHistEntryIdx = 0; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
142 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
143 |
m_historyMutex.unlock(); |
6149 | 144 |
|
145 |
resetAutoCompletionStatus(); |
|
146 |
} |
|
147 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
148 |
void SmartLineEdit::navigateHistory(bool isGoingUp) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
149 |
{ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
150 |
m_historyMutex.lock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
151 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
152 |
// save possible changes to new entry |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
153 |
if ((m_curHistEntryIdx >= m_history->size() || |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
154 |
(text() != m_history->at(m_curHistEntryIdx)))) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
155 |
{ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
156 |
rememberCurrentTextUnsynced(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
157 |
} |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
158 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
159 |
if (isGoingUp) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
160 |
m_curHistEntryIdx--; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
161 |
else |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
162 |
m_curHistEntryIdx++; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
163 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
164 |
// if Idx higher than valid range |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
165 |
if (m_curHistEntryIdx >= m_history->size()) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
166 |
{ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
167 |
QLineEdit::clear(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
168 |
m_curHistEntryIdx = m_history->size(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
169 |
} |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
170 |
// if Idx in valid range |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
171 |
else if (m_curHistEntryIdx >= 0) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
172 |
{ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
173 |
setText(m_history->at(m_curHistEntryIdx)); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
174 |
} |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
175 |
// if Idx below 0 |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
176 |
else |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
177 |
m_curHistEntryIdx = 0; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
178 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
179 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
180 |
m_historyMutex.unlock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
181 |
} |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
182 |
|
6147 | 183 |
bool SmartLineEdit::event(QEvent * event) |
184 |
{ |
|
185 |
// we only want special treatment for key press events |
|
186 |
if (event->type() == QEvent::KeyPress) |
|
187 |
{ |
|
188 |
QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event); |
|
189 |
||
190 |
// TAB key pressed and any useful chars in the matchMe -> let's process those |
|
191 |
if ((keyEvent->key() == Qt::Key_Tab) && (!text().trimmed().isEmpty())) |
|
192 |
{ |
|
193 |
keyPressEvent(keyEvent); |
|
194 |
if (event->isAccepted()) |
|
195 |
return true; |
|
196 |
} |
|
197 |
} |
|
198 |
return QLineEdit::event(event); |
|
199 |
} |
|
200 |
||
201 |
void SmartLineEdit::keyPressEvent(QKeyEvent * event) |
|
202 |
{ |
|
203 |
int key = event->key(); // retrieve pressed key |
|
204 |
||
205 |
// auto-complete on pressed TAB (except for whitespace-only contents) |
|
206 |
if ((key == Qt::Key_Tab) && (!text().trimmed().isEmpty())) |
|
207 |
{ |
|
208 |
autoComplete(); |
|
209 |
event->accept(); |
|
210 |
} |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
211 |
// clear contents on pressed ESC, navigate history with arrow keys |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
212 |
else if (event->modifiers() == Qt::NoModifier) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
213 |
switch (key) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
214 |
{ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
215 |
case Qt::Key_Escape: |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
216 |
clear(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
217 |
event->accept(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
218 |
break; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
219 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
220 |
case Qt::Key_Up: |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
221 |
navigateHistory(true); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
222 |
event->accept(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
223 |
break; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
224 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
225 |
case Qt::Key_Down: |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
226 |
navigateHistory(false); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
227 |
event->accept(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
228 |
break; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
229 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
230 |
default: |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
231 |
QLineEdit::keyPressEvent(event); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
232 |
break; |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
233 |
} |
6147 | 234 |
// otherwise forward keys to parent |
235 |
else |
|
236 |
QLineEdit::keyPressEvent(event); |
|
237 |
} |
|
238 |
||
239 |
||
240 |
void SmartLineEdit::autoComplete() |
|
241 |
{ |
|
242 |
QString match = ""; |
|
243 |
bool isNick = false; |
|
244 |
QString matchMe = text(); |
|
245 |
QString prefix = ""; |
|
246 |
QString postfix = ""; |
|
247 |
bool isFirstWord; |
|
248 |
||
249 |
// we are trying to rematch, so use the data from earlier |
|
250 |
if (m_hasJustMatched) |
|
251 |
{ |
|
252 |
// restore values from earlier auto-completion |
|
253 |
matchMe = m_beforeMatch; |
|
254 |
prefix = m_prefix; |
|
255 |
postfix = m_postfix; |
|
256 |
isFirstWord = prefix.isEmpty(); |
|
257 |
} |
|
258 |
else |
|
259 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
260 |
m_keywordMutex.lock(); |
6147 | 261 |
m_cmds->sort(); |
262 |
m_nicks->sort(); |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
263 |
m_keywordMutex.unlock(); |
6147 | 264 |
|
265 |
int cp = cursorPosition(); |
|
266 |
||
267 |
// cursor is not in or at end/beginning of word |
|
268 |
if ((cp = matchMe.length()) || (QString(matchMe.at(cp)).contains(m_whitespace))) |
|
269 |
if ((cp < 1) || (QString(matchMe.at(cp-1)).contains(m_whitespace))) |
|
270 |
return; |
|
271 |
||
272 |
// crop matchMe at cursor position |
|
273 |
prefix = matchMe.left (cp); |
|
274 |
postfix = matchMe.right(matchMe.length()-cp); |
|
275 |
||
276 |
matchMe = ""; |
|
277 |
||
278 |
||
279 |
// use the whole word the curser is on for matching |
|
280 |
int prefixLen = prefix.lastIndexOf(m_whitespace) + 1; |
|
281 |
int preWordLen = prefix.length() - prefixLen; |
|
282 |
int postWordLen = postfix.indexOf(m_whitespace); |
|
283 |
int postfixLen = 0; |
|
284 |
if (postWordLen < 0) |
|
285 |
postWordLen = postfix.length(); |
|
286 |
else |
|
287 |
postfixLen = postfix.length() - (postWordLen + 1); |
|
288 |
||
289 |
matchMe = prefix.right(preWordLen) + postfix.left(postWordLen); |
|
290 |
prefix = prefix.left(prefixLen); |
|
291 |
postfix = postfix.right(postfixLen); |
|
292 |
||
293 |
||
294 |
isFirstWord = prefix.isEmpty(); // true if first word |
|
295 |
} |
|
296 |
||
297 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
298 |
m_keywordMutex.lock(); |
6147 | 299 |
|
300 |
if (isFirstWord) |
|
301 |
{ |
|
302 |
// find matching commands |
|
303 |
foreach (const QString & cmd, *m_cmds) |
|
304 |
{ |
|
305 |
if (cmd.startsWith(matchMe, Qt::CaseInsensitive)) |
|
306 |
{ |
|
307 |
match = cmd; |
|
308 |
||
309 |
// move match to end so next time new matches will be prefered |
|
310 |
if (m_cmds->removeAll(cmd) > 0); |
|
311 |
m_cmds->append(cmd); |
|
312 |
||
313 |
break; |
|
314 |
} |
|
315 |
} |
|
316 |
} |
|
317 |
||
318 |
if (match.isEmpty()) |
|
319 |
{ |
|
320 |
// find matching nicks |
|
321 |
foreach (const QString & nick, *m_nicks) |
|
322 |
{ |
|
323 |
if (nick.startsWith(matchMe, Qt::CaseInsensitive)) |
|
324 |
{ |
|
325 |
match = nick; |
|
326 |
isNick = true; |
|
327 |
||
328 |
// move match to end so next time new matches will be prefered |
|
329 |
if (m_nicks->removeAll(nick) > 0); |
|
330 |
m_nicks->append(nick); |
|
331 |
||
332 |
break; |
|
333 |
} |
|
334 |
} |
|
335 |
} |
|
336 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
337 |
m_keywordMutex.unlock(); |
6147 | 338 |
|
339 |
// we found a single match? |
|
340 |
if (!match.isEmpty()) |
|
341 |
{ |
|
342 |
// replace last word with match |
|
343 |
// and append ':' if a name at the beginning of the matchMe got completed |
|
344 |
// also add a space at the very end to ease further typing |
|
345 |
QString addAfter = ((isNick && isFirstWord)?": ":" "); |
|
346 |
match = prefix + match + addAfter; |
|
347 |
setText(match + postfix); |
|
348 |
||
349 |
setCursorPosition(match.length()); |
|
350 |
||
351 |
// save values for for the case a rematch is requested |
|
352 |
m_beforeMatch = matchMe; |
|
353 |
m_hasJustMatched = true; |
|
354 |
m_prefix = prefix; |
|
355 |
m_postfix = postfix; |
|
356 |
} |
|
357 |
} |
|
358 |
||
6149 | 359 |
void SmartLineEdit::resetAutoCompletionStatus() |
6147 | 360 |
{ |
361 |
m_beforeMatch = ""; |
|
362 |
m_hasJustMatched = false; |
|
363 |
m_prefix = ""; |
|
364 |
m_postfix = ""; |
|
365 |
} |