15 * You should have received a copy of the GNU General Public License |
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 |
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 |
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
18 */ |
18 */ |
19 |
19 |
20 #include <QStringList> |
|
21 |
|
22 #include "SmartLineEdit.h" |
20 #include "SmartLineEdit.h" |
23 |
21 |
24 SmartLineEdit::SmartLineEdit(QWidget * parent, int maxHistorySize) |
22 SmartLineEdit::SmartLineEdit(QWidget * parent, int maxHistorySize) |
25 : QLineEdit(parent) |
23 : HistoryLineEdit(parent, maxHistorySize) |
26 { |
24 { |
27 m_curHistEntryIdx = 0; |
|
28 m_maxHistorySize = maxHistorySize; |
|
29 |
|
30 m_whitespace = QRegExp("\\s"); |
25 m_whitespace = QRegExp("\\s"); |
31 |
26 |
32 m_cmds = new QStringList(); |
27 m_cmds = new QStringList(); |
33 m_nicks = new QStringList(); |
28 m_nicks = new QStringList(); |
34 |
|
35 m_history = new QStringList(); |
|
36 |
29 |
37 resetAutoCompletionStatus(); |
30 resetAutoCompletionStatus(); |
38 |
31 |
39 // reset autocompletion status when cursor is moved or content is changed |
32 // reset autocompletion status when cursor is moved or content is changed |
40 connect(this, SIGNAL(cursorPositionChanged(int, int)), |
33 connect(this, SIGNAL(cursorPositionChanged(int, int)), |
84 m_nicks->removeAll(name); |
77 m_nicks->removeAll(name); |
85 |
78 |
86 m_keywordMutex.unlock(); |
79 m_keywordMutex.unlock(); |
87 } |
80 } |
88 |
81 |
89 void SmartLineEdit::rememberCurrentText() |
82 |
90 { |
83 void SmartLineEdit::reset() |
91 m_historyMutex.lock(); |
|
92 |
|
93 rememberCurrentTextUnsynced(); |
|
94 |
|
95 m_historyMutex.unlock(); |
|
96 } |
|
97 |
|
98 void SmartLineEdit::rememberCurrentTextUnsynced() |
|
99 { |
|
100 QString newEntry = text(); |
|
101 |
|
102 // don't store whitespace-only/empty text |
|
103 if (newEntry.trimmed().isEmpty()) |
|
104 return; |
|
105 |
|
106 m_history->removeOne(newEntry); // no duplicates please |
|
107 m_history->append(newEntry); |
|
108 |
|
109 // do not keep more entries than allowed |
|
110 if (m_history->size() > m_maxHistorySize) |
|
111 m_history->removeFirst(); |
|
112 |
|
113 // we're looking at the latest entry |
|
114 m_curHistEntryIdx = m_history->size() - 1; |
|
115 } |
|
116 |
|
117 void SmartLineEdit::clear() |
|
118 { |
|
119 m_historyMutex.lock(); |
|
120 |
|
121 QLineEdit::clear(); |
|
122 m_curHistEntryIdx = m_history->size(); |
|
123 |
|
124 m_historyMutex.unlock(); |
|
125 } |
|
126 |
|
127 void SmartLineEdit::forgetEverything() |
|
128 { |
84 { |
129 // forget keywords |
85 // forget keywords |
130 m_keywordMutex.lock(); |
86 m_keywordMutex.lock(); |
131 |
87 |
132 m_cmds->clear(); |
88 m_cmds->clear(); |
133 m_nicks->clear(); |
89 m_nicks->clear(); |
|
90 resetAutoCompletionStatus(); |
134 |
91 |
135 m_keywordMutex.unlock(); |
92 m_keywordMutex.unlock(); |
136 |
93 |
137 // forget history |
94 // forget history |
138 m_historyMutex.lock(); |
95 HistoryLineEdit::reset(); |
139 |
96 } |
140 m_history->clear(); |
97 |
141 m_curHistEntryIdx = 0; |
|
142 |
|
143 m_historyMutex.unlock(); |
|
144 |
|
145 resetAutoCompletionStatus(); |
|
146 } |
|
147 |
|
148 void SmartLineEdit::navigateHistory(bool isGoingUp) |
|
149 { |
|
150 m_historyMutex.lock(); |
|
151 |
|
152 // save possible changes to new entry |
|
153 if ((m_curHistEntryIdx >= m_history->size() || |
|
154 (text() != m_history->at(m_curHistEntryIdx)))) |
|
155 { |
|
156 rememberCurrentTextUnsynced(); |
|
157 } |
|
158 |
|
159 if (isGoingUp) |
|
160 m_curHistEntryIdx--; |
|
161 else |
|
162 m_curHistEntryIdx++; |
|
163 |
|
164 // if Idx higher than valid range |
|
165 if (m_curHistEntryIdx >= m_history->size()) |
|
166 { |
|
167 QLineEdit::clear(); |
|
168 m_curHistEntryIdx = m_history->size(); |
|
169 } |
|
170 // if Idx in valid range |
|
171 else if (m_curHistEntryIdx >= 0) |
|
172 { |
|
173 setText(m_history->at(m_curHistEntryIdx)); |
|
174 } |
|
175 // if Idx below 0 |
|
176 else |
|
177 m_curHistEntryIdx = 0; |
|
178 |
|
179 |
|
180 m_historyMutex.unlock(); |
|
181 } |
|
182 |
98 |
183 bool SmartLineEdit::event(QEvent * event) |
99 bool SmartLineEdit::event(QEvent * event) |
184 { |
100 { |
185 // we only want special treatment for key press events |
101 // we only want special treatment for key press events |
186 if (event->type() == QEvent::KeyPress) |
102 if (event->type() == QEvent::KeyPress) |
206 if ((key == Qt::Key_Tab) && (!text().trimmed().isEmpty())) |
122 if ((key == Qt::Key_Tab) && (!text().trimmed().isEmpty())) |
207 { |
123 { |
208 autoComplete(); |
124 autoComplete(); |
209 event->accept(); |
125 event->accept(); |
210 } |
126 } |
211 // clear contents on pressed ESC, navigate history with arrow keys |
127 // clear contents on pressed ESC |
212 else if (event->modifiers() == Qt::NoModifier) |
128 else if ((event->modifiers() == Qt::NoModifier) && (key == Qt::Key_Escape)) |
213 switch (key) |
129 { |
214 { |
130 clear(); |
215 case Qt::Key_Escape: |
131 event->accept(); |
216 clear(); |
132 } |
217 event->accept(); |
|
218 break; |
|
219 |
|
220 case Qt::Key_Up: |
|
221 navigateHistory(true); |
|
222 event->accept(); |
|
223 break; |
|
224 |
|
225 case Qt::Key_Down: |
|
226 navigateHistory(false); |
|
227 event->accept(); |
|
228 break; |
|
229 |
|
230 default: |
|
231 QLineEdit::keyPressEvent(event); |
|
232 break; |
|
233 } |
|
234 // otherwise forward keys to parent |
133 // otherwise forward keys to parent |
235 else |
134 else |
236 QLineEdit::keyPressEvent(event); |
135 HistoryLineEdit::keyPressEvent(event); |
237 } |
136 } |
238 |
137 |
239 |
138 |
240 void SmartLineEdit::autoComplete() |
139 void SmartLineEdit::autoComplete() |
241 { |
140 { |