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