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