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);
60 connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
63 MultiLineEdit::~MultiLineEdit() {
66 void MultiLineEdit::setCustomFont(const QFont &font) {
71 void MultiLineEdit::setMode(Mode mode) {
78 void MultiLineEdit::setMinHeight(int lines) {
79 if(lines == _minHeight)
86 void MultiLineEdit::setMaxHeight(int lines) {
87 if(lines == _maxHeight)
94 void MultiLineEdit::enableScrollBars(bool enable) {
95 if(_scrollBarsEnabled == enable)
98 _scrollBarsEnabled = enable;
102 void MultiLineEdit::updateScrollBars() {
103 QFontMetrics fm(font());
104 int _maxPixelHeight = fm.lineSpacing() * _maxHeight;
105 if(_scrollBarsEnabled && document()->size().height() > _maxPixelHeight)
106 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
108 setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
110 if(!_scrollBarsEnabled || isSingleLine())
111 setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
113 setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
116 void MultiLineEdit::resizeEvent(QResizeEvent *event) {
118 QTextEdit::resizeEvent(event);
121 void MultiLineEdit::computeSizeHint() {
122 QFontMetrics fm(font());
123 int minPixelHeight = fm.lineSpacing() * _minHeight;
124 int maxPixelHeight = fm.lineSpacing() * _maxHeight;
125 int scrollBarHeight = horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0;
127 // use the style to determine a decent size
128 int h = qMin(qMax((int)document()->size().height() + scrollBarHeight, minPixelHeight), maxPixelHeight) + 2 * frameWidth();
129 QStyleOptionFrameV2 opt;
131 opt.rect = QRect(0, 0, 100, h);
132 opt.lineWidth = lineWidth();
133 opt.midLineWidth = midLineWidth();
134 opt.state |= QStyle::State_Sunken;
135 QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
142 QSize MultiLineEdit::sizeHint() const {
143 if(!_sizeHint.isValid()) {
144 MultiLineEdit *that = const_cast<MultiLineEdit *>(this);
145 that->computeSizeHint();
150 QSize MultiLineEdit::minimumSizeHint() const {
154 void MultiLineEdit::enableSpellCheck(bool enable) {
156 setCheckSpellingEnabled(enable);
162 void MultiLineEdit::historyMoveBack() {
163 addToHistory(text(), true);
171 void MultiLineEdit::historyMoveForward() {
172 addToHistory(text(), true);
174 if(idx < history.count()) {
176 if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1
179 reset(); // equals clear() in this case
181 addToHistory(text());
186 bool MultiLineEdit::addToHistory(const QString &text, bool temporary) {
190 Q_ASSERT(0 <= idx && idx <= history.count());
193 // if an entry of the history is changed, we remember it and show it again at this
194 // position until a line was actually sent
195 // sent lines get appended to the history
196 if(history.isEmpty() || text != history[idx - (int)(idx == history.count())]) {
197 tempHistory[idx] = text;
201 if(history.isEmpty() || text != history.last()) {
210 void MultiLineEdit::keyPressEvent(QKeyEvent *event) {
211 // Workaround the fact that Qt < 4.5 doesn't know InsertLineSeparator yet
212 #if QT_VERSION >= 0x040500
213 if(event == QKeySequence::InsertLineSeparator) {
217 if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::META) {
219 if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::SHIFT) {
223 if(_mode == SingleLine)
226 KTextEdit::keyPressEvent(event);
228 QTextEdit::keyPressEvent(event);
233 switch(event->key()) {
235 if(event->modifiers() & Qt::ShiftModifier)
239 if(!(event->modifiers() & Qt::ControlModifier)) {
240 int pos = textCursor().position();
241 moveCursor(QTextCursor::Up);
242 if(pos == textCursor().position()) // already on top line -> history
250 if(event->modifiers() & Qt::ShiftModifier)
254 if(!(event->modifiers() & Qt::ControlModifier)) {
255 int pos = textCursor().position();
256 moveCursor(QTextCursor::Down);
257 if(pos == textCursor().position()) // already on bottom line -> history
258 historyMoveForward();
260 historyMoveForward();
271 // We don't want to have the tab key react even if no completer is installed
282 KTextEdit::keyPressEvent(event);
284 QTextEdit::keyPressEvent(event);
288 void MultiLineEdit::on_returnPressed() {
289 if(!text().isEmpty()) {
290 foreach(const QString &line, text().split('\n', QString::SkipEmptyParts)) {
294 emit textEntered(line);
301 void MultiLineEdit::on_textChanged() {
302 QString newText = text();
303 newText.replace("\r\n", "\n");
304 newText.replace('\r', '\n');
305 if(_mode == SingleLine)
306 newText.replace('\n', ' ');
308 _singleLine = (newText.indexOf('\n') < 0);
310 if(document()->size().height() != _lastDocumentHeight) {
311 _lastDocumentHeight = document()->size().height();
312 on_documentHeightChanged(_lastDocumentHeight);
317 void MultiLineEdit::on_documentHeightChanged(qreal) {
321 void MultiLineEdit::reset() {
322 // every time the MultiLineEdit is cleared we also reset history index
323 idx = history.count();
325 QTextBlockFormat format = textCursor().blockFormat();
326 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
327 textCursor().setBlockFormat(format);
331 void MultiLineEdit::showHistoryEntry() {
332 // if the user changed the history, display the changed line
333 setPlainText(tempHistory.contains(idx) ? tempHistory[idx] : history[idx]);
334 QTextCursor cursor = textCursor();
335 QTextBlockFormat format = cursor.blockFormat();
336 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
337 cursor.setBlockFormat(format);
338 cursor.movePosition(QTextCursor::End);
339 setTextCursor(cursor);