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