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 |
/**
|
|
36 |
* A modification of QLineEdit that will attempt to auto-complete the current
|
|
37 |
* word with cursor when the TAB key is pressed.
|
|
38 |
* Additionally it will delete its contents when ESC is pressed.
|
|
39 |
* A Keyword can either be a command (if first word) or
|
|
40 |
* a nickname (completed if any word)
|
|
41 |
* @author sheepluva
|
|
42 |
* @since 0.9.17
|
|
43 |
*/
|
|
44 |
class SmartLineEdit : public QLineEdit
|
|
45 |
{
|
|
46 |
Q_OBJECT
|
|
47 |
|
|
48 |
public:
|
|
49 |
/**
|
|
50 |
* Class constructor.
|
|
51 |
*/
|
|
52 |
SmartLineEdit(QWidget * parent = 0);
|
|
53 |
|
|
54 |
/**
|
|
55 |
* Adds commands to the auto-completion feature.
|
|
56 |
* @param commands list of commands to be added.
|
|
57 |
*/
|
|
58 |
void addCommands(const QStringList & commands);
|
|
59 |
|
|
60 |
/**
|
|
61 |
* Adds a single nickname to the auto-completion feature.
|
|
62 |
* @param nickname name to be added.
|
|
63 |
*/
|
|
64 |
void addNickname(const QString & nickname);
|
|
65 |
|
|
66 |
/**
|
|
67 |
* Removes commands from the auto-completion feature.
|
|
68 |
* @param commands list of commands to be removed.
|
|
69 |
*/
|
|
70 |
void removeCommands(const QStringList & commands);
|
|
71 |
|
|
72 |
/**
|
|
73 |
* Removes a single nickname from the auto-completion feature.
|
|
74 |
* @param nickname name to be removed.
|
|
75 |
*/
|
|
76 |
void removeNickname(const QString & nickname);
|
|
77 |
|
6149
|
78 |
/**
|
|
79 |
* Forget all keywords and input history.
|
|
80 |
*/
|
|
81 |
void forgetEverything();
|
|
82 |
|
6147
|
83 |
|
|
84 |
protected:
|
|
85 |
/**
|
|
86 |
* Overrides method of parent class.
|
|
87 |
* Forward pressed TAB to parent class' method (for focus handling etc)
|
|
88 |
* only if line is empty.
|
|
89 |
* @param event the key event.
|
|
90 |
* @return returns true if the event was recognized.
|
|
91 |
*/
|
|
92 |
virtual bool event(QEvent * event);
|
|
93 |
|
|
94 |
/**
|
|
95 |
* Overrides method of parent class.
|
|
96 |
* Autocompletes if TAB is reported as pressed key in the key event,
|
|
97 |
* otherwise keys except for ESC (with no modifiers)
|
|
98 |
* are forwarded to parent method.
|
|
99 |
* ESC leads to the contents being cleared.
|
|
100 |
* @param event the key event.
|
|
101 |
*/
|
|
102 |
virtual void keyPressEvent(QKeyEvent * event);
|
|
103 |
|
|
104 |
|
|
105 |
private:
|
|
106 |
QRegExp m_whitespace; // regexp that matches a whitespace
|
|
107 |
|
|
108 |
QStringList * m_cmds; // list of recognized commands
|
|
109 |
QStringList * m_nicks; // list of recognized nicknames
|
|
110 |
|
|
111 |
// these variables contain information about the last replacement
|
|
112 |
// they get reset whenever cursor is moved or text is changed
|
|
113 |
|
|
114 |
QString m_beforeMatch; // the string that was just matched
|
|
115 |
bool m_hasJustMatched; // whether this widget just did an auto-completion
|
|
116 |
QString m_prefix; // prefix of the text replacement this widget just did
|
|
117 |
QString m_postfix; // postfix of the text replacement this widget just did
|
|
118 |
|
|
119 |
QMutex m_mutex; // make all the QStringList action thread-safe
|
|
120 |
|
|
121 |
/**
|
6149
|
122 |
* Autocompletes the contents based on the known commands and/or names.
|
6147
|
123 |
*/
|
|
124 |
void autoComplete();
|
|
125 |
|
|
126 |
|
|
127 |
private slots:
|
6149
|
128 |
/**
|
|
129 |
* Resets the information about the last match and text replacement.
|
|
130 |
*/
|
|
131 |
void resetAutoCompletionStatus();
|
6147
|
132 |
};
|
|
133 |
|
|
134 |
|
|
135 |
|
|
136 |
#endif // HEDGEWARS_SMARTLINEDIT
|