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 |
|
|
78 |
|
|
79 |
protected:
|
|
80 |
/**
|
|
81 |
* Overrides method of parent class.
|
|
82 |
* Forward pressed TAB to parent class' method (for focus handling etc)
|
|
83 |
* only if line is empty.
|
|
84 |
* @param event the key event.
|
|
85 |
* @return returns true if the event was recognized.
|
|
86 |
*/
|
|
87 |
virtual bool event(QEvent * event);
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Overrides method of parent class.
|
|
91 |
* Autocompletes if TAB is reported as pressed key in the key event,
|
|
92 |
* otherwise keys except for ESC (with no modifiers)
|
|
93 |
* are forwarded to parent method.
|
|
94 |
* ESC leads to the contents being cleared.
|
|
95 |
* @param event the key event.
|
|
96 |
*/
|
|
97 |
virtual void keyPressEvent(QKeyEvent * event);
|
|
98 |
|
|
99 |
|
|
100 |
private:
|
|
101 |
QRegExp m_whitespace; // regexp that matches a whitespace
|
|
102 |
|
|
103 |
QStringList * m_cmds; // list of recognized commands
|
|
104 |
QStringList * m_nicks; // list of recognized nicknames
|
|
105 |
|
|
106 |
// these variables contain information about the last replacement
|
|
107 |
// they get reset whenever cursor is moved or text is changed
|
|
108 |
|
|
109 |
QString m_beforeMatch; // the string that was just matched
|
|
110 |
bool m_hasJustMatched; // whether this widget just did an auto-completion
|
|
111 |
QString m_prefix; // prefix of the text replacement this widget just did
|
|
112 |
QString m_postfix; // postfix of the text replacement this widget just did
|
|
113 |
|
|
114 |
QMutex m_mutex; // make all the QStringList action thread-safe
|
|
115 |
|
|
116 |
/**
|
|
117 |
* Autocompletes the contents based on the known commands and/or names
|
|
118 |
*/
|
|
119 |
void autoComplete();
|
|
120 |
|
|
121 |
|
|
122 |
private slots:
|
|
123 |
// resets the information about the last match and text replacement
|
|
124 |
void reset();
|
|
125 |
};
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
#endif // HEDGEWARS_SMARTLINEDIT
|