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)
42 _wrapMode(QTextOption::NoWrap),
46 _scrollBarsEnabled(true),
47 _lastDocumentHeight(-1)
49 #if QT_VERSION >= 0x040500
50 document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here
53 setAcceptRichText(false);
54 setWordWrapMode(QTextOption::NoWrap);
56 enableFindReplace(false);
59 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
60 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
66 connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
69 MultiLineEdit::~MultiLineEdit() {
72 void MultiLineEdit::setCustomFont(const QFont &font) {
77 void MultiLineEdit::setMode(Mode mode) {
84 void MultiLineEdit::setWrapMode(QTextOption::WrapMode wrapMode) {
85 if(_wrapMode == wrapMode)
89 setWordWrapMode(wrapMode);
92 void MultiLineEdit::setMinHeight(int lines) {
93 if(lines == _minHeight)
100 void MultiLineEdit::setMaxHeight(int lines) {
101 if(lines == _maxHeight)
108 void MultiLineEdit::enableScrollBars(bool enable) {
109 if(_scrollBarsEnabled == enable)
112 _scrollBarsEnabled = enable;
113 if(enable && numLines() > 1) {
114 // the vertical scrollbar must be enabled/disabled manually;
115 // ScrollBarAsNeeded leads to flicker because of the dynamic widget resize
116 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
118 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
123 void MultiLineEdit::updateScrollBars() {
124 QFontMetrics fm(font());
125 int _maxPixelHeight = fm.lineSpacing() * _maxHeight;
126 if(_scrollBarsEnabled && document()->size().height() > _maxPixelHeight)
127 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
129 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
132 void MultiLineEdit::resizeEvent(QResizeEvent *event) {
134 QTextEdit::resizeEvent(event);
137 QSize MultiLineEdit::sizeHint() const {
138 QFontMetrics fm(font());
139 int _minPixelHeight = fm.lineSpacing() * _minHeight;
140 int _maxPixelHeight = fm.lineSpacing() * _maxHeight;
142 // use the style to determine a decent size
143 int h = qMin(qMax((int)document()->size().height(), _minPixelHeight), _maxPixelHeight) + 2 * frameWidth();
144 QStyleOptionFrameV2 opt;
146 opt.rect = QRect(0, 0, 100, h);
147 opt.lineWidth = lineWidth();
148 opt.midLineWidth = midLineWidth();
149 opt.state |= QStyle::State_Sunken;
150 QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
154 QSize MultiLineEdit::minimumSizeHint() const {
158 void MultiLineEdit::historyMoveBack() {
159 addToHistory(text(), true);
167 void MultiLineEdit::historyMoveForward() {
168 addToHistory(text(), true);
170 if(idx < history.count()) {
172 if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
175 reset(); // equals clear() in this case
179 bool MultiLineEdit::addToHistory(const QString &text, bool temporary) {
183 Q_ASSERT(0 <= idx && idx <= history.count());
186 // if an entry of the history is changed, we remember it and show it again at this
187 // position until a line was actually sent
188 // sent lines get appended to the history
189 if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
190 tempHistory[idx] = text;
194 if(history.isEmpty() || text != history.last()) {
203 void MultiLineEdit::keyPressEvent(QKeyEvent *event) {
204 // Workaround the fact that Qt < 4.5 doesn't know InsertLineSeparator yet
205 #if QT_VERSION >= 0x040500
206 if(event == QKeySequence::InsertLineSeparator) {
210 if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::META) {
212 if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::SHIFT) {
216 if(_mode == SingleLine)
219 KTextEdit::keyPressEvent(event);
221 QTextEdit::keyPressEvent(event);
226 switch(event->key()) {
229 if(!(event->modifiers() & Qt::ControlModifier)) {
230 int pos = textCursor().position();
231 moveCursor(QTextCursor::Up);
232 if(pos == textCursor().position()) // already on top line -> history
241 if(!(event->modifiers() & Qt::ControlModifier)) {
242 int pos = textCursor().position();
243 moveCursor(QTextCursor::Down);
244 if(pos == textCursor().position()) // already on bottom line -> history
245 historyMoveForward();
247 historyMoveForward();
258 // We don't want to have the tab key react if no completer is installed
265 KTextEdit::keyPressEvent(event);
267 QTextEdit::keyPressEvent(event);
272 void MultiLineEdit::on_returnPressed() {
273 if(!text().isEmpty()) {
274 foreach(const QString &line, text().split('\n', QString::SkipEmptyParts)) {
278 emit textEntered(line);
285 void MultiLineEdit::on_textChanged() {
286 QString newText = text();
287 newText.replace("\r\n", "\n");
288 newText.replace('\r', '\n');
289 if(_mode == SingleLine)
290 newText.replace('\n', ' ');
292 if(document()->size().height() != _lastDocumentHeight) {
293 _lastDocumentHeight = document()->size().height();
294 on_documentHeightChanged(_lastDocumentHeight);
299 void MultiLineEdit::on_documentHeightChanged(qreal) {
303 void MultiLineEdit::reset() {
304 // every time the MultiLineEdit is cleared we also reset history index
305 idx = history.count();
307 QTextBlockFormat format = textCursor().blockFormat();
308 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
309 textCursor().setBlockFormat(format);
312 void MultiLineEdit::showHistoryEntry() {
313 // if the user changed the history, display the changed line
314 setPlainText(tempHistory.contains(idx) ? tempHistory[idx] : history[idx]);
315 QTextCursor cursor = textCursor();
316 QTextBlockFormat format = cursor.blockFormat();
317 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
318 cursor.setBlockFormat(format);
319 cursor.movePosition(QTextCursor::End);
320 setTextCursor(cursor);