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 if(event == QKeySequence::InsertLineSeparator) {
205 if(_mode == SingleLine)
208 KTextEdit::keyPressEvent(event);
210 QTextEdit::keyPressEvent(event);
215 switch(event->key()) {
218 if(!(event->modifiers() & Qt::ControlModifier)) {
219 int pos = textCursor().position();
220 moveCursor(QTextCursor::Up);
221 if(pos == textCursor().position()) // already on top line -> history
230 if(!(event->modifiers() & Qt::ControlModifier)) {
231 int pos = textCursor().position();
232 moveCursor(QTextCursor::Down);
233 if(pos == textCursor().position()) // already on bottom line -> history
234 historyMoveForward();
236 historyMoveForward();
247 // We don't want to have the tab key react if no completer is installed
254 KTextEdit::keyPressEvent(event);
256 QTextEdit::keyPressEvent(event);
261 void MultiLineEdit::on_returnPressed() {
262 if(!text().isEmpty()) {
263 foreach(const QString &line, text().split('\n', QString::SkipEmptyParts)) {
267 emit textEntered(line);
274 void MultiLineEdit::on_textChanged() {
275 QString newText = text();
276 newText.replace("\r\n", "\n");
277 newText.replace('\r', '\n');
278 if(_mode == SingleLine)
279 newText.replace('\n', ' ');
281 if(document()->size().height() != _lastDocumentHeight) {
282 _lastDocumentHeight = document()->size().height();
283 on_documentHeightChanged(_lastDocumentHeight);
288 void MultiLineEdit::on_documentHeightChanged(qreal) {
292 void MultiLineEdit::reset() {
293 // every time the MultiLineEdit is cleared we also reset history index
294 idx = history.count();
296 QTextBlockFormat format = textCursor().blockFormat();
297 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
298 textCursor().setBlockFormat(format);
301 void MultiLineEdit::showHistoryEntry() {
302 // if the user changed the history, display the changed line
303 setPlainText(tempHistory.contains(idx) ? tempHistory[idx] : history[idx]);
304 QTextCursor cursor = textCursor();
305 QTextBlockFormat format = cursor.blockFormat();
306 format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
307 cursor.setBlockFormat(format);
308 cursor.movePosition(QTextCursor::End);
309 setTextCursor(cursor);