21 |
21 |
#include <QApplication>
|
22 |
22 |
#include <QMenu>
|
23 |
23 |
#include <QMessageBox>
|
|
24 |
#include <QLayout>
|
24 |
25 |
|
25 |
26 |
#include "bufferview.h"
|
26 |
27 |
#include "graphicalui.h"
|
... | ... | |
40 |
41 |
tabCompleter(new TabCompleter(this))
|
41 |
42 |
{
|
42 |
43 |
// Make the QTextEdit look like a QLineEdit
|
43 |
|
#if QT_VERSION >= 0x040500
|
44 |
|
document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here
|
45 |
|
#endif
|
|
44 |
//#if QT_VERSION >= 0x040500
|
|
45 |
document()->setDocumentMargin(leftMargin); // new in Qt 4.5 and we really don't want it here (except for Squider)
|
|
46 |
//#endif
|
46 |
47 |
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
47 |
48 |
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
48 |
49 |
setAcceptRichText(false);
|
... | ... | |
68 |
69 |
// use the style to determine a decent size
|
69 |
70 |
QFontMetrics fm(font());
|
70 |
71 |
int h = fm.lineSpacing() + 2 * frameWidth();
|
|
72 |
|
71 |
73 |
QStyleOptionFrameV2 opt;
|
72 |
74 |
opt.initFrom(this);
|
73 |
75 |
opt.rect = QRect(0, 0, 100, h);
|
74 |
76 |
opt.lineWidth = lineWidth();
|
75 |
77 |
opt.midLineWidth = midLineWidth();
|
76 |
78 |
opt.state |= QStyle::State_Sunken;
|
|
79 |
|
77 |
80 |
QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
|
78 |
81 |
return s;
|
79 |
82 |
}
|
... | ... | |
115 |
118 |
|
116 |
119 |
switch(event->key()) {
|
117 |
120 |
case Qt::Key_Up:
|
118 |
|
event->accept();
|
|
121 |
if(event->modifiers() & Qt::ShiftModifier) {
|
|
122 |
QTextEdit::keyPressEvent(event);
|
|
123 |
} else {
|
|
124 |
event->accept();
|
119 |
125 |
|
120 |
|
addToHistory(text(), true);
|
|
126 |
addToHistory(text(), true);
|
121 |
127 |
|
122 |
|
if(idx > 0) {
|
123 |
|
idx--;
|
124 |
|
showHistoryEntry();
|
|
128 |
if(idx > 0) {
|
|
129 |
idx--;
|
|
130 |
showHistoryEntry();
|
|
131 |
}
|
125 |
132 |
}
|
126 |
133 |
|
127 |
134 |
break;
|
128 |
135 |
|
129 |
136 |
case Qt::Key_Down:
|
130 |
|
event->accept();
|
131 |
|
|
132 |
|
addToHistory(text(), true);
|
133 |
|
|
134 |
|
if(idx < history.count()) {
|
135 |
|
idx++;
|
136 |
|
if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
|
137 |
|
showHistoryEntry();
|
138 |
|
else
|
139 |
|
resetLine(); // equals clear() in this case
|
|
137 |
if(event->modifiers() & Qt::ShiftModifier) {
|
|
138 |
QTextEdit::keyPressEvent(event);
|
140 |
139 |
} else {
|
141 |
|
addToHistory(text());
|
142 |
|
resetLine();
|
143 |
|
}
|
|
140 |
event->accept();
|
144 |
141 |
|
|
142 |
addToHistory(text(), true);
|
|
143 |
|
|
144 |
if(idx < history.count()) {
|
|
145 |
idx++;
|
|
146 |
if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
|
|
147 |
showHistoryEntry();
|
|
148 |
else
|
|
149 |
resetLine(); // equals clear() in this case
|
|
150 |
} else {
|
|
151 |
addToHistory(text());
|
|
152 |
resetLine();
|
|
153 |
}
|
|
154 |
}
|
145 |
155 |
break;
|
146 |
156 |
|
147 |
157 |
case Qt::Key_Return:
|
148 |
158 |
case Qt::Key_Enter:
|
149 |
159 |
case Qt::Key_Select:
|
150 |
|
event->accept();
|
151 |
|
emit returnPressed();
|
|
160 |
if(event->modifiers() & Qt::ShiftModifier) {
|
|
161 |
QTextEdit::keyPressEvent(event);
|
|
162 |
} else {
|
|
163 |
event->accept();
|
|
164 |
emit returnPressed();
|
|
165 |
}
|
152 |
166 |
break;
|
153 |
167 |
|
154 |
168 |
default:
|
... | ... | |
179 |
193 |
}
|
180 |
194 |
|
181 |
195 |
void InputLine::on_returnPressed() {
|
182 |
|
if(!text().isEmpty()) {
|
183 |
|
addToHistory(text());
|
184 |
|
emit sendText(text());
|
185 |
|
resetLine();
|
186 |
|
}
|
187 |
|
}
|
188 |
|
|
189 |
|
void InputLine::on_textChanged(QString newText) {
|
190 |
|
QStringList lineSeparators;
|
191 |
|
lineSeparators << QString("\r\n")
|
192 |
|
<< QString('\n')
|
193 |
|
<< QString('\r');
|
194 |
|
|
195 |
|
QString lineSep;
|
196 |
|
foreach(QString separator, lineSeparators) {
|
197 |
|
if(newText.contains(separator)) {
|
198 |
|
lineSep = separator;
|
199 |
|
break;
|
200 |
|
}
|
201 |
|
}
|
202 |
|
|
203 |
|
if(lineSep.isEmpty())
|
204 |
|
return;
|
205 |
|
|
206 |
|
QStringList lines = newText.split(lineSep, QString::SkipEmptyParts);
|
207 |
|
|
208 |
|
if(lines.count() >= 4) {
|
209 |
|
QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
|
210 |
|
msg += "<p>";
|
211 |
|
for(int i = 0; i < 3; i++) {
|
212 |
|
msg += lines[i].left(40);
|
213 |
|
if(lines[i].count() > 40)
|
214 |
|
msg += "...";
|
215 |
|
msg += "<br />";
|
216 |
|
}
|
217 |
|
msg += "...</p>";
|
218 |
|
QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
|
219 |
|
question.setDefaultButton(QMessageBox::No);
|
220 |
|
#ifdef Q_WS_MAC
|
221 |
|
question.setWindowFlags(question.windowFlags() | Qt::Sheet); // Qt::Sheet is not ignored on other platforms as it should :/
|
222 |
|
#endif
|
223 |
|
if(question.exec() == QMessageBox::No)
|
224 |
|
return;
|
225 |
|
}
|
|
196 |
QString newText = text();
|
|
197 |
newText.replace(QString("\r\n"), QString('\n'));
|
|
198 |
newText.replace(QString('\r'), QString('\n'));
|
226 |
199 |
|
|
200 |
addToHistory(newText);
|
|
201 |
QStringList lines = newText.split(QString('\n'), QString::SkipEmptyParts);
|
227 |
202 |
foreach(QString line, lines) {
|
228 |
203 |
if(!line.isEmpty()) {
|
229 |
|
resetLine();
|
230 |
|
insert(line);
|
231 |
|
emit returnPressed();
|
|
204 |
emit sendText(line);
|
232 |
205 |
}
|
233 |
206 |
}
|
234 |
207 |
|
235 |
|
// if(newText.contains(lineSep)) {
|
236 |
|
// clear();
|
237 |
|
// QString line = newText.section(lineSep, 0, 0);
|
238 |
|
// QString remainder = newText.section(lineSep, 1);
|
239 |
|
// insert(line);
|
240 |
|
// emit returnPressed();
|
241 |
|
// insert(remainder);
|
242 |
|
// }
|
|
208 |
resetLine();
|
|
209 |
}
|
|
210 |
|
|
211 |
void InputLine::on_textChanged(QString newText) {
|
|
212 |
newText.replace(QString("\r\n"), QString('\n'));
|
|
213 |
newText.replace(QString('\r'), QString('\n'));
|
|
214 |
|
|
215 |
QStringList lines = newText.split(QString('\n'), QString::KeepEmptyParts);
|
|
216 |
|
|
217 |
int lineCount = lines.count();
|
|
218 |
if(lineCount <= 0) lineCount = 1;
|
|
219 |
else if(lineCount > 8) lineCount = 8;
|
|
220 |
|
|
221 |
if(lineCount > 1) {
|
|
222 |
setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded);
|
|
223 |
QFontMetrics fm(font());
|
|
224 |
setMinimumSize(QSize(0, lineCount * fm.lineSpacing() + 2 * frameWidth() + 2 * leftMargin));
|
|
225 |
} else {
|
|
226 |
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
|
|
227 |
setMinimumSize(QSize(0, 0));
|
|
228 |
}
|
243 |
229 |
}
|
244 |
230 |
|
245 |
231 |
void InputLine::resetLine() {
|
246 |
232 |
// every time the InputLine is cleared we also reset history index
|
247 |
233 |
idx = history.count();
|
248 |
234 |
clear();
|
249 |
|
QTextBlockFormat format = textCursor().blockFormat();
|
250 |
|
format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
|
251 |
|
textCursor().setBlockFormat(format);
|
252 |
235 |
}
|
253 |
236 |
|
254 |
237 |
void InputLine::showHistoryEntry() {
|
255 |
238 |
// if the user changed the history, display the changed line
|
256 |
239 |
setPlainText(tempHistory.contains(idx) ? tempHistory[idx] : history[idx]);
|
257 |
240 |
QTextCursor cursor = textCursor();
|
258 |
|
QTextBlockFormat format = cursor.blockFormat();
|
259 |
|
format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
|
260 |
|
cursor.setBlockFormat(format);
|
261 |
241 |
cursor.movePosition(QTextCursor::End);
|
262 |
242 |
setTextCursor(cursor);
|
263 |
243 |
}
|
264 |
|
-
|