author | sheepluva |
Wed, 19 Oct 2011 02:10:27 +0200 | |
changeset 6150 | 1d98752c1fba |
parent 6149 | 0b92341adb6a |
child 6151 | 9fd5b70acb1a |
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 |
#ifndef HEDGEWARS_SMARTLINEDIT |
|
21 |
#define HEDGEWARS_SMARTLINEDIT |
|
22 |
||
23 |
#include <QLineEdit> |
|
24 |
#include <QStringList> |
|
25 |
#include <QString> |
|
26 |
||
27 |
#include <QEvent> |
|
28 |
#include <QKeyEvent> |
|
29 |
||
30 |
#include <QMutex> |
|
31 |
#include <QRegExp> |
|
32 |
||
33 |
class QLineEdit; |
|
34 |
||
35 |
/** |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
36 |
* A modification of QLineEdit that features: |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
37 |
* + Auto-completion for word under cursor when the TAB key is pressed. |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
38 |
* + ESC key clears text. |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
39 |
* + History of previous contents, re-selectable using the arrow keys. |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
40 |
* |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
41 |
* Note: |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
42 |
* * A Keyword can either be a command (if first word) or |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
43 |
* a nickname (completed regardless of position in text). |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
44 |
* * Public methods for accessing keywords and history are thread-safe. |
6147 | 45 |
* @author sheepluva |
46 |
* @since 0.9.17 |
|
47 |
*/ |
|
48 |
class SmartLineEdit : public QLineEdit |
|
49 |
{ |
|
50 |
Q_OBJECT |
|
51 |
||
52 |
public: |
|
53 |
/** |
|
54 |
* Class constructor. |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
55 |
* @param parent parent QWidget. |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
56 |
* @param maxHistorySize maximum amount of history entries kept. |
6147 | 57 |
*/ |
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
58 |
SmartLineEdit(QWidget * parent = 0, int maxHistorySize = 64); |
6147 | 59 |
|
60 |
/** |
|
61 |
* Adds commands to the auto-completion feature. |
|
62 |
* @param commands list of commands to be added. |
|
63 |
*/ |
|
64 |
void addCommands(const QStringList & commands); |
|
65 |
||
66 |
/** |
|
67 |
* Adds a single nickname to the auto-completion feature. |
|
68 |
* @param nickname name to be added. |
|
69 |
*/ |
|
70 |
void addNickname(const QString & nickname); |
|
71 |
||
72 |
/** |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
73 |
* Appends current text to history. |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
74 |
*/ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
75 |
void rememberCurrentText(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
76 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
77 |
/** |
6147 | 78 |
* Removes commands from the auto-completion feature. |
79 |
* @param commands list of commands to be removed. |
|
80 |
*/ |
|
81 |
void removeCommands(const QStringList & commands); |
|
82 |
||
83 |
/** |
|
84 |
* Removes a single nickname from the auto-completion feature. |
|
85 |
* @param nickname name to be removed. |
|
86 |
*/ |
|
87 |
void removeNickname(const QString & nickname); |
|
88 |
||
6149 | 89 |
/** |
90 |
* Forget all keywords and input history. |
|
91 |
*/ |
|
92 |
void forgetEverything(); |
|
93 |
||
6147 | 94 |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
95 |
public slots: |
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 |
* Clears the contents. |
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 |
void clear(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
100 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
101 |
|
6147 | 102 |
protected: |
103 |
/** |
|
104 |
* Overrides method of parent class. |
|
105 |
* Forward pressed TAB to parent class' method (for focus handling etc) |
|
106 |
* only if line is empty. |
|
107 |
* @param event the key event. |
|
108 |
* @return returns true if the event was recognized. |
|
109 |
*/ |
|
110 |
virtual bool event(QEvent * event); |
|
111 |
||
112 |
/** |
|
113 |
* Overrides method of parent class. |
|
114 |
* Autocompletes if TAB is reported as pressed key in the key event, |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
115 |
* otherwise keys except for ESC and Up/Down (with no modifiers) |
6147 | 116 |
* are forwarded to parent method. |
117 |
* ESC leads to the contents being cleared. |
|
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
118 |
* Arrow keys are used for navigating the history. |
6147 | 119 |
* @param event the key event. |
120 |
*/ |
|
121 |
virtual void keyPressEvent(QKeyEvent * event); |
|
122 |
||
123 |
||
124 |
private: |
|
125 |
QRegExp m_whitespace; // regexp that matches a whitespace |
|
126 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
127 |
int m_maxHistorySize; // the maximum allowed size for the history |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
128 |
int m_curHistEntryIdx; // the index of the currently used entry or -1 |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
129 |
|
6147 | 130 |
QStringList * m_cmds; // list of recognized commands |
131 |
QStringList * m_nicks; // list of recognized nicknames |
|
132 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
133 |
QStringList * m_history; // history of previous inputs |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
134 |
|
6147 | 135 |
// these variables contain information about the last replacement |
136 |
// they get reset whenever cursor is moved or text is changed |
|
137 |
||
138 |
QString m_beforeMatch; // the string that was just matched |
|
139 |
bool m_hasJustMatched; // whether this widget just did an auto-completion |
|
140 |
QString m_prefix; // prefix of the text replacement this widget just did |
|
141 |
QString m_postfix; // postfix of the text replacement this widget just did |
|
142 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
143 |
QMutex m_keywordMutex; // make keyword QStringList action thread-safe |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
144 |
QMutex m_historyMutex; // make history QStringList action thread-safe |
6147 | 145 |
|
146 |
/** |
|
6149 | 147 |
* Autocompletes the contents based on the known commands and/or names. |
6147 | 148 |
*/ |
149 |
void autoComplete(); |
|
150 |
||
6150
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
151 |
/** |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
152 |
* Navigates content history in the desired direction. |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
153 |
* Note: no wrap-around on purpose (so that holding down/up will get the |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
154 |
* the user to the respective end rather than into an endless cycle :P) |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
155 |
* @param isGoingUp true: next older entry, false: next more recent entry. |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
156 |
*/ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
157 |
void navigateHistory(bool isGoingUp); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
158 |
|
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
159 |
/** |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
160 |
* Appends current text to history, without Mutex. |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
161 |
*/ |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
162 |
void rememberCurrentTextUnsynced(); |
1d98752c1fba
frontend chat input history, use arrow keys UP/DOWN
sheepluva
parents:
6149
diff
changeset
|
163 |
|
6147 | 164 |
|
165 |
private slots: |
|
6149 | 166 |
/** |
167 |
* Resets the information about the last match and text replacement. |
|
168 |
*/ |
|
169 |
void resetAutoCompletionStatus(); |
|
6147 | 170 |
}; |
171 |
||
172 |
||
173 |
||
174 |
#endif // HEDGEWARS_SMARTLINEDIT |