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 |
|
|
24 |
SmartLineEdit::SmartLineEdit(QWidget * parent)
|
|
25 |
: QLineEdit(parent)
|
|
26 |
{
|
|
27 |
m_whitespace = QRegExp("\\s");
|
|
28 |
|
|
29 |
m_cmds = new QStringList();
|
|
30 |
m_nicks = new QStringList();
|
|
31 |
|
6149
|
32 |
resetAutoCompletionStatus();
|
6147
|
33 |
|
6149
|
34 |
// reset autocompletion status when cursor is moved or content is changed
|
|
35 |
connect(this, SIGNAL(cursorPositionChanged(int, int)),
|
|
36 |
this, SLOT(resetAutoCompletionStatus()));
|
|
37 |
connect(this, SIGNAL(textChanged(const QString&)),
|
|
38 |
this, SLOT(resetAutoCompletionStatus()));
|
6147
|
39 |
}
|
|
40 |
|
|
41 |
|
|
42 |
void SmartLineEdit::addCommands(const QStringList & commands)
|
|
43 |
{
|
|
44 |
m_mutex.lock();
|
|
45 |
|
|
46 |
m_cmds->append(commands);
|
|
47 |
|
|
48 |
m_mutex.unlock();
|
|
49 |
}
|
|
50 |
|
|
51 |
|
|
52 |
void SmartLineEdit::removeCommands(const QStringList & commands)
|
|
53 |
{
|
|
54 |
m_mutex.lock();
|
|
55 |
|
|
56 |
foreach (const QString & cmd, commands)
|
|
57 |
{
|
|
58 |
m_cmds->removeAll(cmd);
|
|
59 |
}
|
|
60 |
|
|
61 |
m_mutex.unlock();
|
|
62 |
}
|
|
63 |
|
|
64 |
|
|
65 |
void SmartLineEdit::addNickname(const QString & name)
|
|
66 |
{
|
|
67 |
m_mutex.lock();
|
|
68 |
|
|
69 |
m_nicks->append(name);
|
|
70 |
|
|
71 |
m_mutex.unlock();
|
|
72 |
}
|
|
73 |
|
|
74 |
|
|
75 |
void SmartLineEdit::removeNickname(const QString & name)
|
|
76 |
{
|
|
77 |
m_mutex.lock();
|
|
78 |
|
|
79 |
m_nicks->removeAll(name);
|
|
80 |
|
|
81 |
m_mutex.unlock();
|
|
82 |
}
|
|
83 |
|
6149
|
84 |
void SmartLineEdit::forgetEverything()
|
|
85 |
{
|
|
86 |
m_mutex.lock();
|
|
87 |
|
|
88 |
m_cmds->clear();
|
|
89 |
m_nicks->clear();
|
|
90 |
|
|
91 |
m_mutex.unlock();
|
|
92 |
|
|
93 |
resetAutoCompletionStatus();
|
|
94 |
}
|
|
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 |
}
|
|
111 |
return QLineEdit::event(event);
|
|
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 |
}
|
|
124 |
// clear contents on pressed ESC
|
|
125 |
else if ((event->key() == Qt::Key_Escape) &&
|
|
126 |
(event->modifiers() == Qt::NoModifier)
|
|
127 |
)
|
|
128 |
clear();
|
|
129 |
// otherwise forward keys to parent
|
|
130 |
else
|
|
131 |
QLineEdit::keyPressEvent(event);
|
|
132 |
}
|
|
133 |
|
|
134 |
|
|
135 |
void SmartLineEdit::autoComplete()
|
|
136 |
{
|
|
137 |
QString match = "";
|
|
138 |
bool isNick = false;
|
|
139 |
QString matchMe = text();
|
|
140 |
QString prefix = "";
|
|
141 |
QString postfix = "";
|
|
142 |
bool isFirstWord;
|
|
143 |
|
|
144 |
// we are trying to rematch, so use the data from earlier
|
|
145 |
if (m_hasJustMatched)
|
|
146 |
{
|
|
147 |
// restore values from earlier auto-completion
|
|
148 |
matchMe = m_beforeMatch;
|
|
149 |
prefix = m_prefix;
|
|
150 |
postfix = m_postfix;
|
|
151 |
isFirstWord = prefix.isEmpty();
|
|
152 |
}
|
|
153 |
else
|
|
154 |
{
|
|
155 |
m_mutex.lock();
|
|
156 |
m_cmds->sort();
|
|
157 |
m_nicks->sort();
|
|
158 |
m_mutex.unlock();
|
|
159 |
|
|
160 |
int cp = cursorPosition();
|
|
161 |
|
|
162 |
// cursor is not in or at end/beginning of word
|
|
163 |
if ((cp = matchMe.length()) || (QString(matchMe.at(cp)).contains(m_whitespace)))
|
|
164 |
if ((cp < 1) || (QString(matchMe.at(cp-1)).contains(m_whitespace)))
|
|
165 |
return;
|
|
166 |
|
|
167 |
// crop matchMe at cursor position
|
|
168 |
prefix = matchMe.left (cp);
|
|
169 |
postfix = matchMe.right(matchMe.length()-cp);
|
|
170 |
|
|
171 |
matchMe = "";
|
|
172 |
|
|
173 |
|
|
174 |
// use the whole word the curser is on for matching
|
|
175 |
int prefixLen = prefix.lastIndexOf(m_whitespace) + 1;
|
|
176 |
int preWordLen = prefix.length() - prefixLen;
|
|
177 |
int postWordLen = postfix.indexOf(m_whitespace);
|
|
178 |
int postfixLen = 0;
|
|
179 |
if (postWordLen < 0)
|
|
180 |
postWordLen = postfix.length();
|
|
181 |
else
|
|
182 |
postfixLen = postfix.length() - (postWordLen + 1);
|
|
183 |
|
|
184 |
matchMe = prefix.right(preWordLen) + postfix.left(postWordLen);
|
|
185 |
prefix = prefix.left(prefixLen);
|
|
186 |
postfix = postfix.right(postfixLen);
|
|
187 |
|
|
188 |
|
|
189 |
isFirstWord = prefix.isEmpty(); // true if first word
|
|
190 |
}
|
|
191 |
|
|
192 |
|
|
193 |
m_mutex.lock();
|
|
194 |
|
|
195 |
if (isFirstWord)
|
|
196 |
{
|
|
197 |
// find matching commands
|
|
198 |
foreach (const QString & cmd, *m_cmds)
|
|
199 |
{
|
|
200 |
if (cmd.startsWith(matchMe, Qt::CaseInsensitive))
|
|
201 |
{
|
|
202 |
match = cmd;
|
|
203 |
|
|
204 |
// move match to end so next time new matches will be prefered
|
|
205 |
if (m_cmds->removeAll(cmd) > 0);
|
|
206 |
m_cmds->append(cmd);
|
|
207 |
|
|
208 |
break;
|
|
209 |
}
|
|
210 |
}
|
|
211 |
}
|
|
212 |
|
|
213 |
if (match.isEmpty())
|
|
214 |
{
|
|
215 |
// find matching nicks
|
|
216 |
foreach (const QString & nick, *m_nicks)
|
|
217 |
{
|
|
218 |
if (nick.startsWith(matchMe, Qt::CaseInsensitive))
|
|
219 |
{
|
|
220 |
match = nick;
|
|
221 |
isNick = true;
|
|
222 |
|
|
223 |
// move match to end so next time new matches will be prefered
|
|
224 |
if (m_nicks->removeAll(nick) > 0);
|
|
225 |
m_nicks->append(nick);
|
|
226 |
|
|
227 |
break;
|
|
228 |
}
|
|
229 |
}
|
|
230 |
}
|
|
231 |
|
|
232 |
m_mutex.unlock();
|
|
233 |
|
|
234 |
// we found a single match?
|
|
235 |
if (!match.isEmpty())
|
|
236 |
{
|
|
237 |
// replace last word with match
|
|
238 |
// and append ':' if a name at the beginning of the matchMe got completed
|
|
239 |
// also add a space at the very end to ease further typing
|
|
240 |
QString addAfter = ((isNick && isFirstWord)?": ":" ");
|
|
241 |
match = prefix + match + addAfter;
|
|
242 |
setText(match + postfix);
|
|
243 |
|
|
244 |
setCursorPosition(match.length());
|
|
245 |
|
|
246 |
// save values for for the case a rematch is requested
|
|
247 |
m_beforeMatch = matchMe;
|
|
248 |
m_hasJustMatched = true;
|
|
249 |
m_prefix = prefix;
|
|
250 |
m_postfix = postfix;
|
|
251 |
}
|
|
252 |
}
|
|
253 |
|
6149
|
254 |
void SmartLineEdit::resetAutoCompletionStatus()
|
6147
|
255 |
{
|
|
256 |
m_beforeMatch = "";
|
|
257 |
m_hasJustMatched = false;
|
|
258 |
m_prefix = "";
|
|
259 |
m_postfix = "";
|
|
260 |
}
|