1 /***************************************************************************
2 * Copyright (C) 2005/06 by the Quassel Project *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
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. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
21 #include <QApplication>
23 #include <QMessageBox>
26 #include "bufferview.h"
27 #include "graphicalui.h"
28 #include "multilineedit.h"
29 #include "tabcompleter.h"
31 const int leftMargin = 3;
33 MultiLineEdit::MultiLineEdit(QWidget *parent)
45 _scrollBarsEnabled(true),
46 _lastDocumentHeight(-1)
48 #if QT_VERSION >= 0x040500
49 document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here
52 setAcceptRichText(false);
54 enableFindReplace(false);
58 setWordWrapEnabled(false);
61 connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
64 MultiLineEdit::~MultiLineEdit() {
67 void MultiLineEdit::setCustomFont(const QFont &font) {
72 void MultiLineEdit::setMode(Mode mode) {
79 void MultiLineEdit::setMinHeight(int lines) {
80 if(lines == _minHeight)
87 void MultiLineEdit::setMaxHeight(int lines) {
88 if(lines == _maxHeight)
95 void MultiLineEdit::setScrollBarsEnabled(bool enable) {
96 if(_scrollBarsEnabled == enable)
99 _scrollBarsEnabled = enable;
103 void MultiLineEdit::updateScrollBars() {
104 QFontMetrics fm(font());
105 int _maxPixelHeight = fm.lineSpacing() * _maxHeight;
106 if(_scrollBarsEnabled && document()->size().height() > _maxPixelHeight)
107 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
109 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
111 if(!_scrollBarsEnabled || isSingleLine())
112 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
114 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
117 void MultiLineEdit::resizeEvent(QResizeEvent *event) {
118 QTextEdit::resizeEvent(event);
123 void MultiLineEdit::updateSizeHint() {
124 QFontMetrics fm(font());
125 int minPixelHeight = fm.lineSpacing() * _minHeight;
126 int maxPixelHeight = fm.lineSpacing() * _maxHeight;
127 int scrollBarHeight = horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0;
129 // use the style to determine a decent size
130 int h = qMin(qMax((int)document()->size().height() + scrollBarHeight, minPixelHeight), maxPixelHeight) + 2 * frameWidth();
131 QStyleOptionFrameV2 opt;
133 opt.rect = QRect(0, 0, 100, h);
134 opt.lineWidth = lineWidth();
135 opt.midLineWidth = midLineWidth();
136 opt.state |= QStyle::State_Sunken;
137 QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
144 QSize MultiLineEdit::sizeHint() const {
145 if(!_sizeHint.isValid()) {
146 MultiLineEdit *that = const_cast<MultiLineEdit *>(this);
147 that->updateSizeHint();
152 QSize MultiLineEdit::minimumSizeHint() const {
156 void MultiLineEdit::setSpellCheckEnabled(bool enable) {
158 setCheckSpellingEnabled(enable);
164 void MultiLineEdit::setWordWrapEnabled(bool enable) {
165 setLineWrapMode(enable? WidgetWidth : NoWrap);
169 void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *) {
170 _pasteProtectionEnabled = enable;
173 void MultiLineEdit::historyMoveBack() {
174 addToHistory(text(), true);
182 void MultiLineEdit::historyMoveForward() {
183 addToHistory(text(), true);
185 if(idx < history.count()) {
187 if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
190 reset(); // equals clear() in this case
192 addToHistory(text());
197 bool MultiLineEdit::addToHistory(const QString &text, bool temporary) {
201 Q_ASSERT(0 <= idx && idx <= history.count());
204 // if an entry of the history is changed, we remember it and show it again at this
205 // position until a line was actually sent
206 // sent lines get appended to the history
207 if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
208 tempHistory[idx] = text;
212 if(history.isEmpty() || text != history.last()) {
221 void MultiLineEdit::keyPressEvent(QKeyEvent *event) {
222 // Workaround the fact that Qt < 4.5 doesn't know InsertLineSeparator yet
223 #if QT_VERSION >= 0x040500
224 if(event == QKeySequence::InsertLineSeparator) {
228 if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::META) {
230 if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::SHIFT) {
234 if(_mode == SingleLine)
237 KTextEdit::keyPressEvent(event);
239 QTextEdit::keyPressEvent(event);
244 switch(event->key()) {
246 if(event->modifiers() & Qt::ShiftModifier)
250 if(!(event->modifiers() & Qt::ControlModifier)) {
251 int pos = textCursor().position();
252 moveCursor(QTextCursor::Up);
253 if(pos == textCursor().position()) // already on top line -> history
261 if(event->modifiers() & Qt::ShiftModifier)
265 if(!(event->modifiers() & Qt::ControlModifier)) {
266 int pos = textCursor().position();
267 moveCursor(QTextCursor::Down);
268 if(pos == textCursor().position()) // already on bottom line -> history
269 historyMoveForward();
271 historyMoveForward();
282 // We don't want to have the tab key react even if no completer is installed
293 KTextEdit::keyPressEvent(event);
295 QTextEdit::keyPressEvent(event);
299 void MultiLineEdit::on_returnPressed() {
300 on_returnPressed(text());
303 void MultiLineEdit::on_returnPressed(const QString & text) {
304 if(!text.isEmpty()) {
305 foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) {
309 emit textEntered(line);
316 void MultiLineEdit::on_textChanged() {
317 QString newText = text();
318 newText.replace("\r\n", "\n");
319 newText.replace('\r', '\n');
320 if(_mode == SingleLine) {
321 if(!pasteProtectionEnabled())
322 newText.replace('\n', ' ');
323 else if(newText.contains('\n')) {
324 QStringList lines = newText.split('\n', QString::SkipEmptyParts);
327 if(lines.count() >= 4) {
328 QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
330 for(int i = 0; i < 4; i++) {
331 msg += Qt::escape(lines[i].left(40));
332 if(lines[i].count() > 40)
337 QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
338 question.setDefaultButton(QMessageBox::No);
340 question.setWindowFlags(question.windowFlags() | Qt::Sheet);
342 if(question.exec() != QMessageBox::Yes)
346 foreach(QString line, lines) {
354 _singleLine = (newText.indexOf('\n') < 0);
356 if(document()->size().height() != _lastDocumentHeight) {
357 _lastDocumentHeight = document()->size().height();
358 on_documentHeightChanged(_lastDocumentHeight);
361 ensureCursorVisible();
364 void MultiLineEdit::on_documentHeightChanged(qreal) {
368 void MultiLineEdit::reset() {
369 // every time the MultiLineEdit is cleared we also reset history index
370 idx = history.count();
372 QTextBlockFormat format = textCursor().blockFormat();
373 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
374 textCursor().setBlockFormat(format);
378 void MultiLineEdit::showHistoryEntry() {
379 // if the user changed the history, display the changed line
380 setPlainText(tempHistory.contains(idx) ? tempHistory[idx] : history[idx]);
381 QTextCursor cursor = textCursor();
382 QTextBlockFormat format = cursor.blockFormat();
383 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
384 cursor.setBlockFormat(format);
385 cursor.movePosition(QTextCursor::End);
386 setTextCursor(cursor);