|
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 #include <QStringList> |
|
21 |
|
22 #include "SmartLineEdit.h" |
|
23 |
|
24 HistoryLineEdit::HistoryLineEdit(QWidget * parent, int maxHistorySize) |
|
25 : QLineEdit(parent) |
|
26 { |
|
27 m_curHistEntryIdx = 0; |
|
28 m_maxHistorySize = maxHistorySize; |
|
29 m_history = new QStringList(); |
|
30 } |
|
31 |
|
32 |
|
33 void HistoryLineEdit::rememberCurrentText() |
|
34 { |
|
35 m_historyMutex.lock(); |
|
36 |
|
37 rememberCurrentTextUnsynced(); |
|
38 |
|
39 m_historyMutex.unlock(); |
|
40 } |
|
41 |
|
42 |
|
43 void HistoryLineEdit::rememberCurrentTextUnsynced() |
|
44 { |
|
45 QString newEntry = text(); |
|
46 |
|
47 // don't store whitespace-only/empty text |
|
48 if (newEntry.trimmed().isEmpty()) |
|
49 return; |
|
50 |
|
51 m_history->removeOne(newEntry); // no duplicates please |
|
52 m_history->append(newEntry); |
|
53 |
|
54 // do not keep more entries than allowed |
|
55 if (m_history->size() > m_maxHistorySize) |
|
56 m_history->removeFirst(); |
|
57 |
|
58 // we're looking at the latest entry |
|
59 m_curHistEntryIdx = m_history->size() - 1; |
|
60 } |
|
61 |
|
62 |
|
63 void HistoryLineEdit::clear() |
|
64 { |
|
65 m_historyMutex.lock(); |
|
66 |
|
67 QLineEdit::clear(); |
|
68 m_curHistEntryIdx = m_history->size(); |
|
69 |
|
70 m_historyMutex.unlock(); |
|
71 } |
|
72 |
|
73 |
|
74 void HistoryLineEdit::reset() |
|
75 { |
|
76 // forget history |
|
77 m_historyMutex.lock(); |
|
78 |
|
79 m_history->clear(); |
|
80 m_curHistEntryIdx = 0; |
|
81 |
|
82 m_historyMutex.unlock(); |
|
83 } |
|
84 |
|
85 |
|
86 void HistoryLineEdit::navigateHistory(bool isGoingUp) |
|
87 { |
|
88 m_historyMutex.lock(); |
|
89 |
|
90 // save possible changes to new entry |
|
91 if ((m_curHistEntryIdx >= m_history->size() || |
|
92 (text() != m_history->at(m_curHistEntryIdx)))) |
|
93 { |
|
94 rememberCurrentTextUnsynced(); |
|
95 } |
|
96 |
|
97 if (isGoingUp) |
|
98 m_curHistEntryIdx--; |
|
99 else |
|
100 m_curHistEntryIdx++; |
|
101 |
|
102 // if Idx higher than valid range |
|
103 if (m_curHistEntryIdx >= m_history->size()) |
|
104 { |
|
105 QLineEdit::clear(); |
|
106 m_curHistEntryIdx = m_history->size(); |
|
107 } |
|
108 // if Idx in valid range |
|
109 else if (m_curHistEntryIdx >= 0) |
|
110 { |
|
111 setText(m_history->at(m_curHistEntryIdx)); |
|
112 } |
|
113 // if Idx below 0 |
|
114 else |
|
115 m_curHistEntryIdx = 0; |
|
116 |
|
117 |
|
118 m_historyMutex.unlock(); |
|
119 } |
|
120 |
|
121 |
|
122 void HistoryLineEdit::keyPressEvent(QKeyEvent * event) |
|
123 { |
|
124 int key = event->key(); // retrieve pressed key |
|
125 |
|
126 // navigate history with arrow keys |
|
127 if (event->modifiers() == Qt::NoModifier) |
|
128 switch (key) |
|
129 { |
|
130 case Qt::Key_Escape: |
|
131 clear(); |
|
132 event->accept(); |
|
133 break; |
|
134 |
|
135 case Qt::Key_Up: |
|
136 navigateHistory(true); |
|
137 event->accept(); |
|
138 break; |
|
139 |
|
140 case Qt::Key_Down: |
|
141 navigateHistory(false); |
|
142 event->accept(); |
|
143 break; |
|
144 |
|
145 default: |
|
146 QLineEdit::keyPressEvent(event); |
|
147 break; |
|
148 } |
|
149 // otherwise forward keys to parent |
|
150 else |
|
151 QLineEdit::keyPressEvent(event); |
|
152 } |
|
153 |