author | unc0rr |
Thu, 29 Jan 2015 23:36:58 +0300 | |
branch | qmlfrontend |
changeset 10754 | 8dd1cf1be5a2 |
parent 10108 | c68cf030eded |
child 11046 | 47a8c19ecb60 |
permissions | -rw-r--r-- |
6147 | 1 |
/* |
2 |
* Hedgewars, a free turn based strategy game |
|
3 |
* Copyright (c) 2006-2007 Igor Ulyanov <iulyanov@gmail.com> |
|
9998 | 4 |
* Copyright (c) 2004-2014 Andrey Korotaev <unC0Rr@gmail.com> |
6147 | 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
9998
diff
changeset
|
17 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
6147 | 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) |
6616
f77bb02b669f
astyle -C -S -L -N --style=allman --recursive "QTfrontend/*.cpp" "QTfrontend/*.h"
nemo
parents:
6218
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 |
{ |
|
56 |
m_cmds->append(commands); |
|
57 |
} |
|
58 |
||
59 |
||
60 |
void SmartLineEdit::removeCommands(const QStringList & commands) |
|
61 |
{ |
|
62 |
foreach (const QString & cmd, commands) |
|
63 |
{ |
|
64 |
m_cmds->removeAll(cmd); |
|
65 |
} |
|
66 |
} |
|
67 |
||
68 |
||
69 |
void SmartLineEdit::addNickname(const QString & name) |
|
70 |
{ |
|
6187 | 71 |
m_sorted_nicks->insert(name.toLower(), name); |
6147 | 72 |
m_nicks->append(name); |
73 |
} |
|
74 |
||
75 |
||
76 |
void SmartLineEdit::removeNickname(const QString & name) |
|
77 |
{ |
|
6187 | 78 |
m_sorted_nicks->remove(name.toLower()); |
6147 | 79 |
m_nicks->removeAll(name); |
6150
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 |
6149 | 86 |
m_cmds->clear(); |
6187 | 87 |
m_sorted_nicks->clear(); |
6149 | 88 |
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
|
89 |
resetAutoCompletionStatus(); |
6149 | 90 |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
91 |
// 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
|
92 |
HistoryLineEdit::reset(); |
6149 | 93 |
} |
94 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
95 |
|
6147 | 96 |
bool SmartLineEdit::event(QEvent * event) |
97 |
{ |
|
98 |
// we only want special treatment for key press events |
|
99 |
if (event->type() == QEvent::KeyPress) |
|
100 |
{ |
|
101 |
QKeyEvent * keyEvent = static_cast<QKeyEvent*>(event); |
|
102 |
||
103 |
// TAB key pressed and any useful chars in the matchMe -> let's process those |
|
104 |
if ((keyEvent->key() == Qt::Key_Tab) && (!text().trimmed().isEmpty())) |
|
105 |
{ |
|
106 |
keyPressEvent(keyEvent); |
|
107 |
if (event->isAccepted()) |
|
108 |
return true; |
|
109 |
} |
|
110 |
} |
|
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
|
111 |
return HistoryLineEdit::event(event); |
6147 | 112 |
} |
113 |
||
114 |
void SmartLineEdit::keyPressEvent(QKeyEvent * event) |
|
115 |
{ |
|
116 |
int key = event->key(); // retrieve pressed key |
|
117 |
||
118 |
// auto-complete on pressed TAB (except for whitespace-only contents) |
|
119 |
if ((key == Qt::Key_Tab) && (!text().trimmed().isEmpty())) |
|
120 |
{ |
|
121 |
autoComplete(); |
|
122 |
event->accept(); |
|
123 |
} |
|
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
|
124 |
// 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
|
125 |
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
|
126 |
{ |
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(); |
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 |
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
|
129 |
} |
6147 | 130 |
// otherwise forward keys to parent |
131 |
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
|
132 |
HistoryLineEdit::keyPressEvent(event); |
6147 | 133 |
} |
134 |
||
135 |
||
136 |
void SmartLineEdit::autoComplete() |
|
137 |
{ |
|
138 |
QString match = ""; |
|
139 |
bool isNick = false; |
|
140 |
QString matchMe = text(); |
|
141 |
QString prefix = ""; |
|
142 |
QString postfix = ""; |
|
143 |
bool isFirstWord; |
|
144 |
||
145 |
// we are trying to rematch, so use the data from earlier |
|
146 |
if (m_hasJustMatched) |
|
147 |
{ |
|
148 |
// restore values from earlier auto-completion |
|
149 |
matchMe = m_beforeMatch; |
|
150 |
prefix = m_prefix; |
|
151 |
postfix = m_postfix; |
|
152 |
isFirstWord = prefix.isEmpty(); |
|
153 |
} |
|
154 |
else |
|
155 |
{ |
|
156 |
m_cmds->sort(); |
|
6187 | 157 |
m_nicks = new QStringList(m_sorted_nicks->values()); |
6147 | 158 |
|
159 |
int cp = cursorPosition(); |
|
160 |
||
161 |
// cursor is not in or at end/beginning of word |
|
162 |
if ((cp = matchMe.length()) || (QString(matchMe.at(cp)).contains(m_whitespace))) |
|
163 |
if ((cp < 1) || (QString(matchMe.at(cp-1)).contains(m_whitespace))) |
|
164 |
return; |
|
165 |
||
166 |
// crop matchMe at cursor position |
|
167 |
prefix = matchMe.left (cp); |
|
168 |
postfix = matchMe.right(matchMe.length()-cp); |
|
169 |
||
170 |
matchMe = ""; |
|
171 |
||
172 |
||
173 |
// use the whole word the curser is on for matching |
|
174 |
int prefixLen = prefix.lastIndexOf(m_whitespace) + 1; |
|
175 |
int preWordLen = prefix.length() - prefixLen; |
|
176 |
int postWordLen = postfix.indexOf(m_whitespace); |
|
177 |
int postfixLen = 0; |
|
178 |
if (postWordLen < 0) |
|
179 |
postWordLen = postfix.length(); |
|
180 |
else |
|
181 |
postfixLen = postfix.length() - (postWordLen + 1); |
|
182 |
||
183 |
matchMe = prefix.right(preWordLen) + postfix.left(postWordLen); |
|
184 |
prefix = prefix.left(prefixLen); |
|
185 |
postfix = postfix.right(postfixLen); |
|
186 |
||
187 |
||
188 |
isFirstWord = prefix.isEmpty(); // true if first word |
|
189 |
} |
|
190 |
||
191 |
||
192 |
if (isFirstWord) |
|
193 |
{ |
|
194 |
// find matching commands |
|
195 |
foreach (const QString & cmd, *m_cmds) |
|
196 |
{ |
|
197 |
if (cmd.startsWith(matchMe, Qt::CaseInsensitive)) |
|
198 |
{ |
|
199 |
match = cmd; |
|
200 |
||
6197 | 201 |
// move match to end so next time new matches will be preferred |
6218
999215ca87d7
actually the those if(...); weren't supposed to be if statements in the first place, since the result is always true at that point of code anyway.
sheepluva
parents:
6217
diff
changeset
|
202 |
m_cmds->removeAll(cmd); |
999215ca87d7
actually the those if(...); weren't supposed to be if statements in the first place, since the result is always true at that point of code anyway.
sheepluva
parents:
6217
diff
changeset
|
203 |
m_cmds->append(cmd); |
6147 | 204 |
|
205 |
break; |
|
206 |
} |
|
207 |
} |
|
208 |
} |
|
209 |
||
210 |
if (match.isEmpty()) |
|
211 |
{ |
|
212 |
// find matching nicks |
|
213 |
foreach (const QString & nick, *m_nicks) |
|
214 |
{ |
|
215 |
if (nick.startsWith(matchMe, Qt::CaseInsensitive)) |
|
216 |
{ |
|
217 |
match = nick; |
|
218 |
isNick = true; |
|
219 |
||
220 |
// move match to end so next time new matches will be prefered |
|
6218
999215ca87d7
actually the those if(...); weren't supposed to be if statements in the first place, since the result is always true at that point of code anyway.
sheepluva
parents:
6217
diff
changeset
|
221 |
m_nicks->removeAll(nick); |
999215ca87d7
actually the those if(...); weren't supposed to be if statements in the first place, since the result is always true at that point of code anyway.
sheepluva
parents:
6217
diff
changeset
|
222 |
m_nicks->append(nick); |
6147 | 223 |
|
224 |
break; |
|
225 |
} |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
// we found a single match? |
|
230 |
if (!match.isEmpty()) |
|
231 |
{ |
|
232 |
// replace last word with match |
|
233 |
// and append ':' if a name at the beginning of the matchMe got completed |
|
234 |
// also add a space at the very end to ease further typing |
|
235 |
QString addAfter = ((isNick && isFirstWord)?": ":" "); |
|
236 |
match = prefix + match + addAfter; |
|
237 |
setText(match + postfix); |
|
238 |
||
239 |
setCursorPosition(match.length()); |
|
240 |
||
241 |
// save values for for the case a rematch is requested |
|
242 |
m_beforeMatch = matchMe; |
|
243 |
m_hasJustMatched = true; |
|
244 |
m_prefix = prefix; |
|
245 |
m_postfix = postfix; |
|
246 |
} |
|
247 |
} |
|
248 |
||
6149 | 249 |
void SmartLineEdit::resetAutoCompletionStatus() |
6147 | 250 |
{ |
251 |
m_beforeMatch = ""; |
|
252 |
m_hasJustMatched = false; |
|
253 |
m_prefix = ""; |
|
254 |
m_postfix = ""; |
|
255 |
} |
|
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
|
256 |