author | sheepluva |
Thu, 20 Oct 2011 02:56:58 +0200 | |
changeset 6157 | 9c474b8a8b5e |
parent 6151 | 9fd5b70acb1a |
child 6168 | 6f301dac12ff |
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 "SmartLineEdit.h" |
|
21 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
22 |
SmartLineEdit::SmartLineEdit(QWidget * parent, int maxHistorySize) |
6151
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
23 |
: HistoryLineEdit(parent, maxHistorySize) |
6147 | 24 |
{ |
25 |
m_whitespace = QRegExp("\\s"); |
|
26 |
||
27 |
m_cmds = new QStringList(); |
|
28 |
m_nicks = new QStringList(); |
|
29 |
||
6149 | 30 |
resetAutoCompletionStatus(); |
6147 | 31 |
|
6149 | 32 |
// reset autocompletion status when cursor is moved or content is changed |
33 |
connect(this, SIGNAL(cursorPositionChanged(int, int)), |
|
34 |
this, SLOT(resetAutoCompletionStatus())); |
|
35 |
connect(this, SIGNAL(textChanged(const QString&)), |
|
36 |
this, SLOT(resetAutoCompletionStatus())); |
|
6147 | 37 |
} |
38 |
||
39 |
||
40 |
void SmartLineEdit::addCommands(const QStringList & commands) |
|
41 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
42 |
m_keywordMutex.lock(); |
6147 | 43 |
|
44 |
m_cmds->append(commands); |
|
45 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
46 |
m_keywordMutex.unlock(); |
6147 | 47 |
} |
48 |
||
49 |
||
50 |
void SmartLineEdit::removeCommands(const QStringList & commands) |
|
51 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
52 |
m_keywordMutex.lock(); |
6147 | 53 |
|
54 |
foreach (const QString & cmd, commands) |
|
55 |
{ |
|
56 |
m_cmds->removeAll(cmd); |
|
57 |
} |
|
58 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
59 |
m_keywordMutex.unlock(); |
6147 | 60 |
} |
61 |
||
62 |
||
63 |
void SmartLineEdit::addNickname(const QString & name) |
|
64 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
65 |
m_keywordMutex.lock(); |
6147 | 66 |
|
67 |
m_nicks->append(name); |
|
68 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
69 |
m_keywordMutex.unlock(); |
6147 | 70 |
} |
71 |
||
72 |
||
73 |
void SmartLineEdit::removeNickname(const QString & name) |
|
74 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
75 |
m_keywordMutex.lock(); |
6147 | 76 |
|
77 |
m_nicks->removeAll(name); |
|
78 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
79 |
m_keywordMutex.unlock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
80 |
} |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
81 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
82 |
|
6151
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
83 |
void SmartLineEdit::reset() |
6149 | 84 |
{ |
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
85 |
// forget keywords |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
86 |
m_keywordMutex.lock(); |
6149 | 87 |
|
88 |
m_cmds->clear(); |
|
89 |
m_nicks->clear(); |
|
6151
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
90 |
resetAutoCompletionStatus(); |
6149 | 91 |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
92 |
m_keywordMutex.unlock(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
93 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
94 |
// forget history |
6151
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
95 |
HistoryLineEdit::reset(); |
6149 | 96 |
} |
97 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
98 |
|
6147 | 99 |
bool SmartLineEdit::event(QEvent * event) |
100 |
{ |
|
101 |
// we only want special treatment for key press events |
|
102 |
if (event->type() == QEvent::KeyPress) |
|
103 |
{ |
|
104 |
QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event); |
|
105 |
||
106 |
// TAB key pressed and any useful chars in the matchMe -> let's process those |
|
107 |
if ((keyEvent->key() == Qt::Key_Tab) && (!text().trimmed().isEmpty())) |
|
108 |
{ |
|
109 |
keyPressEvent(keyEvent); |
|
110 |
if (event->isAccepted()) |
|
111 |
return true; |
|
112 |
} |
|
113 |
} |
|
6151
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
114 |
return HistoryLineEdit::event(event); |
6147 | 115 |
} |
116 |
||
117 |
void SmartLineEdit::keyPressEvent(QKeyEvent * event) |
|
118 |
{ |
|
119 |
int key = event->key(); // retrieve pressed key |
|
120 |
||
121 |
// auto-complete on pressed TAB (except for whitespace-only contents) |
|
122 |
if ((key == Qt::Key_Tab) && (!text().trimmed().isEmpty())) |
|
123 |
{ |
|
124 |
autoComplete(); |
|
125 |
event->accept(); |
|
126 |
} |
|
6151
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
127 |
// clear contents on pressed ESC |
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
128 |
else if ((event->modifiers() == Qt::NoModifier) && (key == Qt::Key_Escape)) |
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
129 |
{ |
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
130 |
clear(); |
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
131 |
event->accept(); |
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
132 |
} |
6147 | 133 |
// otherwise forward keys to parent |
134 |
else |
|
6151
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
135 |
HistoryLineEdit::keyPressEvent(event); |
6147 | 136 |
} |
137 |
||
138 |
||
139 |
void SmartLineEdit::autoComplete() |
|
140 |
{ |
|
141 |
QString match = ""; |
|
142 |
bool isNick = false; |
|
143 |
QString matchMe = text(); |
|
144 |
QString prefix = ""; |
|
145 |
QString postfix = ""; |
|
146 |
bool isFirstWord; |
|
147 |
||
148 |
// we are trying to rematch, so use the data from earlier |
|
149 |
if (m_hasJustMatched) |
|
150 |
{ |
|
151 |
// restore values from earlier auto-completion |
|
152 |
matchMe = m_beforeMatch; |
|
153 |
prefix = m_prefix; |
|
154 |
postfix = m_postfix; |
|
155 |
isFirstWord = prefix.isEmpty(); |
|
156 |
} |
|
157 |
else |
|
158 |
{ |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
159 |
m_keywordMutex.lock(); |
6147 | 160 |
m_cmds->sort(); |
161 |
m_nicks->sort(); |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
162 |
m_keywordMutex.unlock(); |
6147 | 163 |
|
164 |
int cp = cursorPosition(); |
|
165 |
||
166 |
// cursor is not in or at end/beginning of word |
|
167 |
if ((cp = matchMe.length()) || (QString(matchMe.at(cp)).contains(m_whitespace))) |
|
168 |
if ((cp < 1) || (QString(matchMe.at(cp-1)).contains(m_whitespace))) |
|
169 |
return; |
|
170 |
||
171 |
// crop matchMe at cursor position |
|
172 |
prefix = matchMe.left (cp); |
|
173 |
postfix = matchMe.right(matchMe.length()-cp); |
|
174 |
||
175 |
matchMe = ""; |
|
176 |
||
177 |
||
178 |
// use the whole word the curser is on for matching |
|
179 |
int prefixLen = prefix.lastIndexOf(m_whitespace) + 1; |
|
180 |
int preWordLen = prefix.length() - prefixLen; |
|
181 |
int postWordLen = postfix.indexOf(m_whitespace); |
|
182 |
int postfixLen = 0; |
|
183 |
if (postWordLen < 0) |
|
184 |
postWordLen = postfix.length(); |
|
185 |
else |
|
186 |
postfixLen = postfix.length() - (postWordLen + 1); |
|
187 |
||
188 |
matchMe = prefix.right(preWordLen) + postfix.left(postWordLen); |
|
189 |
prefix = prefix.left(prefixLen); |
|
190 |
postfix = postfix.right(postfixLen); |
|
191 |
||
192 |
||
193 |
isFirstWord = prefix.isEmpty(); // true if first word |
|
194 |
} |
|
195 |
||
196 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
197 |
m_keywordMutex.lock(); |
6147 | 198 |
|
199 |
if (isFirstWord) |
|
200 |
{ |
|
201 |
// find matching commands |
|
202 |
foreach (const QString & cmd, *m_cmds) |
|
203 |
{ |
|
204 |
if (cmd.startsWith(matchMe, Qt::CaseInsensitive)) |
|
205 |
{ |
|
206 |
match = cmd; |
|
207 |
||
208 |
// move match to end so next time new matches will be prefered |
|
209 |
if (m_cmds->removeAll(cmd) > 0); |
|
210 |
m_cmds->append(cmd); |
|
211 |
||
212 |
break; |
|
213 |
} |
|
214 |
} |
|
215 |
} |
|
216 |
||
217 |
if (match.isEmpty()) |
|
218 |
{ |
|
219 |
// find matching nicks |
|
220 |
foreach (const QString & nick, *m_nicks) |
|
221 |
{ |
|
222 |
if (nick.startsWith(matchMe, Qt::CaseInsensitive)) |
|
223 |
{ |
|
224 |
match = nick; |
|
225 |
isNick = true; |
|
226 |
||
227 |
// move match to end so next time new matches will be prefered |
|
228 |
if (m_nicks->removeAll(nick) > 0); |
|
229 |
m_nicks->append(nick); |
|
230 |
||
231 |
break; |
|
232 |
} |
|
233 |
} |
|
234 |
} |
|
235 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
236 |
m_keywordMutex.unlock(); |
6147 | 237 |
|
238 |
// we found a single match? |
|
239 |
if (!match.isEmpty()) |
|
240 |
{ |
|
241 |
// replace last word with match |
|
242 |
// and append ':' if a name at the beginning of the matchMe got completed |
|
243 |
// also add a space at the very end to ease further typing |
|
244 |
QString addAfter = ((isNick && isFirstWord)?": ":" "); |
|
245 |
match = prefix + match + addAfter; |
|
246 |
setText(match + postfix); |
|
247 |
||
248 |
setCursorPosition(match.length()); |
|
249 |
||
250 |
// save values for for the case a rematch is requested |
|
251 |
m_beforeMatch = matchMe; |
|
252 |
m_hasJustMatched = true; |
|
253 |
m_prefix = prefix; |
|
254 |
m_postfix = postfix; |
|
255 |
} |
|
256 |
} |
|
257 |
||
6149 | 258 |
void SmartLineEdit::resetAutoCompletionStatus() |
6147 | 259 |
{ |
260 |
m_beforeMatch = ""; |
|
261 |
m_hasJustMatched = false; |
|
262 |
m_prefix = ""; |
|
263 |
m_postfix = ""; |
|
264 |
} |
|
6151
9fd5b70acb1a
give the room name edit box a history of previous room. however I hate that box from the bottom of my heart, it shall dieeeee... later...
sheepluva
parents:
6150
diff
changeset
|
265 |