Bring copyright headers into 2016
[quassel.git] / src / uisupport / multilineedit.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2016 by the Quassel Project                        *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #include <QApplication>
22 #include <QMessageBox>
23 #include <QScrollBar>
24
25 #ifdef HAVE_SONNET
26 #  include <Sonnet/SpellCheckDecorator>
27 #endif
28
29 #include "actioncollection.h"
30 #include "bufferview.h"
31 #include "graphicalui.h"
32 #include "multilineedit.h"
33 #include "tabcompleter.h"
34
35 const int leftMargin = 3;
36
37 MultiLineEdit::MultiLineEdit(QWidget *parent)
38     : MultiLineEditParent(parent),
39     _idx(0),
40     _mode(SingleLine),
41     _singleLine(true),
42     _minHeight(1),
43     _maxHeight(5),
44     _scrollBarsEnabled(true),
45     _pasteProtectionEnabled(true),
46     _emacsMode(false),
47     _completionSpace(0),
48     _lastDocumentHeight(-1)
49 {
50     document()->setDocumentMargin(0);
51
52     setAcceptRichText(false);
53 #ifdef HAVE_KDE
54     enableFindReplace(false);
55 #endif
56
57 #ifdef HAVE_SONNET
58     new Sonnet::SpellCheckDecorator(this);
59 #endif
60
61     setMode(SingleLine);
62     setLineWrapEnabled(false);
63     reset();
64
65     // Prevent QTextHtmlImporter::appendNodeText from eating whitespace
66     document()->setDefaultStyleSheet("span { white-space: pre-wrap; }");
67
68     connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
69
70     _mircColorMap["00"] = "#ffffff";
71     _mircColorMap["01"] = "#000000";
72     _mircColorMap["02"] = "#000080";
73     _mircColorMap["03"] = "#008000";
74     _mircColorMap["04"] = "#ff0000";
75     _mircColorMap["05"] = "#800000";
76     _mircColorMap["06"] = "#800080";
77     _mircColorMap["07"] = "#ffa500";
78     _mircColorMap["08"] = "#ffff00";
79     _mircColorMap["09"] = "#00ff00";
80     _mircColorMap["10"] = "#008080";
81     _mircColorMap["11"] = "#00ffff";
82     _mircColorMap["12"] = "#4169e1";
83     _mircColorMap["13"] = "#ff00ff";
84     _mircColorMap["14"] = "#808080";
85     _mircColorMap["15"] = "#c0c0c0";
86 }
87
88
89 MultiLineEdit::~MultiLineEdit()
90 {
91 }
92
93
94 void MultiLineEdit::setCustomFont(const QFont &font)
95 {
96     setFont(font);
97     updateSizeHint();
98 }
99
100
101 void MultiLineEdit::setMode(Mode mode)
102 {
103     if (mode == _mode)
104         return;
105
106     _mode = mode;
107 }
108
109
110 void MultiLineEdit::setLineWrapEnabled(bool enable)
111 {
112     setLineWrapMode(enable ? WidgetWidth : NoWrap);
113     updateSizeHint();
114 }
115
116
117 void MultiLineEdit::setMinHeight(int lines)
118 {
119     if (lines == _minHeight)
120         return;
121
122     _minHeight = lines;
123     updateSizeHint();
124 }
125
126
127 void MultiLineEdit::setMaxHeight(int lines)
128 {
129     if (lines == _maxHeight)
130         return;
131
132     _maxHeight = lines;
133     updateSizeHint();
134 }
135
136
137 void MultiLineEdit::setScrollBarsEnabled(bool enable)
138 {
139     if (_scrollBarsEnabled == enable)
140         return;
141
142     _scrollBarsEnabled = enable;
143     updateScrollBars();
144 }
145
146
147 void MultiLineEdit::updateScrollBars()
148 {
149     QFontMetrics fm(font());
150     int _maxPixelHeight = fm.lineSpacing() * _maxHeight;
151     if (_scrollBarsEnabled && document()->size().height() > _maxPixelHeight)
152         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
153     else
154         setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
155
156     if (!_scrollBarsEnabled || isSingleLine())
157         setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
158     else
159         setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
160 }
161
162
163 void MultiLineEdit::resizeEvent(QResizeEvent *event)
164 {
165     QTextEdit::resizeEvent(event);
166     updateSizeHint();
167     updateScrollBars();
168 }
169
170
171 void MultiLineEdit::updateSizeHint()
172 {
173     QFontMetrics fm(font());
174     int minPixelHeight = fm.lineSpacing() * _minHeight;
175     int maxPixelHeight = fm.lineSpacing() * _maxHeight;
176     int scrollBarHeight = horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0;
177
178     // use the style to determine a decent size
179     int h = qMin(qMax((int)document()->size().height() + scrollBarHeight, minPixelHeight), maxPixelHeight) + 2 * frameWidth();
180     QStyleOptionFrameV2 opt;
181     opt.initFrom(this);
182     opt.rect = QRect(0, 0, 100, h);
183     opt.lineWidth = lineWidth();
184     opt.midLineWidth = midLineWidth();
185     opt.state |= QStyle::State_Sunken;
186     QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
187     if (s != _sizeHint) {
188         _sizeHint = s;
189         updateGeometry();
190     }
191 }
192
193
194 QSize MultiLineEdit::sizeHint() const
195 {
196     if (!_sizeHint.isValid()) {
197         MultiLineEdit *that = const_cast<MultiLineEdit *>(this);
198         that->updateSizeHint();
199     }
200     return _sizeHint;
201 }
202
203
204 QSize MultiLineEdit::minimumSizeHint() const
205 {
206     return sizeHint();
207 }
208
209
210 void MultiLineEdit::setEmacsMode(bool enable)
211 {
212     _emacsMode = enable;
213 }
214
215
216 void MultiLineEdit::setSpellCheckEnabled(bool enable)
217 {
218 #ifdef HAVE_KDE
219     setCheckSpellingEnabled(enable);
220 #else
221     Q_UNUSED(enable)
222 #endif
223 }
224
225
226 void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *)
227 {
228     _pasteProtectionEnabled = enable;
229 }
230
231
232 void MultiLineEdit::historyMoveBack()
233 {
234     addToHistory(convertRichtextToMircCodes(), true);
235
236     if (_idx > 0) {
237         _idx--;
238         showHistoryEntry();
239     }
240 }
241
242
243 void MultiLineEdit::historyMoveForward()
244 {
245     addToHistory(convertRichtextToMircCodes(), true);
246
247     if (_idx < _history.count()) {
248         _idx++;
249         if (_idx < _history.count() || _tempHistory.contains(_idx)) // tempHistory might have an entry for idx == history.count() + 1
250             showHistoryEntry();
251         else
252             reset();        // equals clear() in this case
253     }
254     else {
255         addToHistory(convertRichtextToMircCodes());
256         reset();
257     }
258 }
259
260
261 bool MultiLineEdit::addToHistory(const QString &text, bool temporary)
262 {
263     if (text.isEmpty())
264         return false;
265
266     Q_ASSERT(0 <= _idx && _idx <= _history.count());
267
268     if (temporary) {
269         // if an entry of the history is changed, we remember it and show it again at this
270         // position until a line was actually sent
271         // sent lines get appended to the history
272         if (_history.isEmpty() || text != _history[_idx - (int)(_idx == _history.count())]) {
273             _tempHistory[_idx] = text;
274             return true;
275         }
276     }
277     else {
278         if (_history.isEmpty() || text != _history.last()) {
279             _history << text;
280             _tempHistory.clear();
281             return true;
282         }
283     }
284     return false;
285 }
286
287
288 bool MultiLineEdit::event(QEvent *e)
289 {
290     // We need to make sure that global shortcuts aren't eaten
291     if (e->type() == QEvent::ShortcutOverride) {
292         QKeyEvent *event = static_cast<QKeyEvent *>(e);
293         QKeySequence key = QKeySequence(event->key() | event->modifiers());
294         foreach(QAction *action, GraphicalUi::actionCollection()->actions()) {
295             if (action->shortcuts().contains(key)) {
296                 e->ignore();
297                 return false;
298             }
299         }
300     }
301
302     return MultiLineEditParent::event(e);
303 }
304
305
306 void MultiLineEdit::keyPressEvent(QKeyEvent *event)
307 {
308     if (event == QKeySequence::InsertLineSeparator) {
309         if (_mode == SingleLine) {
310             event->accept();
311             on_returnPressed();
312             return;
313         }
314         MultiLineEditParent::keyPressEvent(event);
315         return;
316     }
317
318     switch (event->key()) {
319     case Qt::Key_Up:
320         if (event->modifiers() & Qt::ShiftModifier)
321             break;
322         {
323             event->accept();
324             if (!(event->modifiers() & Qt::ControlModifier)) {
325                 int pos = textCursor().position();
326                 moveCursor(QTextCursor::Up);
327                 if (pos == textCursor().position()) // already on top line -> history
328                     historyMoveBack();
329             }
330             else
331                 historyMoveBack();
332             return;
333         }
334
335     case Qt::Key_Down:
336         if (event->modifiers() & Qt::ShiftModifier)
337             break;
338         {
339             event->accept();
340             if (!(event->modifiers() & Qt::ControlModifier)) {
341                 int pos = textCursor().position();
342                 moveCursor(QTextCursor::Down);
343                 if (pos == textCursor().position()) // already on bottom line -> history
344                     historyMoveForward();
345             }
346             else
347                 historyMoveForward();
348             return;
349         }
350
351     case Qt::Key_Return:
352     case Qt::Key_Enter:
353     case Qt::Key_Select:
354         event->accept();
355         on_returnPressed();
356         return;
357
358     // We don't want to have the tab key react even if no completer is installed
359     case Qt::Key_Tab:
360         event->accept();
361         return;
362
363     default:
364         ;
365     }
366
367     if (_emacsMode) {
368         if (event->modifiers() & Qt::ControlModifier) {
369             switch (event->key()) {
370             // move
371             case Qt::Key_A:
372                 moveCursor(QTextCursor::StartOfLine);
373                 return;
374             case Qt::Key_E:
375                 moveCursor(QTextCursor::EndOfLine);
376                 return;
377             case Qt::Key_F:
378                 moveCursor(QTextCursor::Right);
379                 return;
380             case Qt::Key_B:
381                 moveCursor(QTextCursor::Left);
382                 return;
383
384             // modify
385             case Qt::Key_Y:
386                 paste();
387                 return;
388             case Qt::Key_K:
389                 moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
390                 cut();
391                 return;
392
393             default:
394                 break;
395             }
396         }
397         else if (event->modifiers() & Qt::MetaModifier ||
398                  event->modifiers() & Qt::AltModifier)
399         {
400             switch (event->key()) {
401             case Qt::Key_Right:
402                 moveCursor(QTextCursor::WordRight);
403                 return;
404             case Qt::Key_Left:
405                 moveCursor(QTextCursor::WordLeft);
406                 return;
407             case Qt::Key_F:
408                 moveCursor(QTextCursor::WordRight);
409                 return;
410             case Qt::Key_B:
411                 moveCursor(QTextCursor::WordLeft);
412                 return;
413             case Qt::Key_Less:
414                 moveCursor(QTextCursor::Start);
415                 return;
416             case Qt::Key_Greater:
417                 moveCursor(QTextCursor::End);
418                 return;
419
420             // modify
421             case Qt::Key_D:
422                 moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
423                 cut();
424                 return;
425
426             case Qt::Key_U: // uppercase word
427                 moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
428                 textCursor().insertText(textCursor().selectedText().toUpper());
429                 return;
430
431             case Qt::Key_L: // lowercase word
432                 moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
433                 textCursor().insertText(textCursor().selectedText().toLower());
434                 return;
435
436             case Qt::Key_C:
437             {           // capitalize word
438                 moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
439                 QString const text = textCursor().selectedText();
440                 textCursor().insertText(text.left(1).toUpper() + text.mid(1).toLower());
441                 return;
442             }
443
444             case Qt::Key_T:
445             {           // transpose words
446                 moveCursor(QTextCursor::StartOfWord);
447                 moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
448                 QString const word1 = textCursor().selectedText();
449                 textCursor().clearSelection();
450                 moveCursor(QTextCursor::WordRight);
451                 moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
452                 QString const word2 = textCursor().selectedText();
453                 if (!word2.isEmpty() && !word1.isEmpty()) {
454                     textCursor().insertText(word1);
455                     moveCursor(QTextCursor::WordLeft);
456                     moveCursor(QTextCursor::WordLeft);
457                     moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
458                     textCursor().insertText(word2);
459                     moveCursor(QTextCursor::WordRight);
460                     moveCursor(QTextCursor::EndOfWord);
461                 }
462                 return;
463             }
464
465             default:
466                 break;
467             }
468         }
469     }
470
471 #ifdef HAVE_KDE
472     KTextEdit::keyPressEvent(event);
473 #else
474     QTextEdit::keyPressEvent(event);
475 #endif
476 }
477
478
479 QString MultiLineEdit::convertRichtextToMircCodes()
480 {
481     bool underline, bold, italic, color;
482     QString mircText, mircFgColor, mircBgColor;
483     QTextCursor cursor = textCursor();
484     QTextCursor peekcursor = textCursor();
485     cursor.movePosition(QTextCursor::Start);
486
487     underline = bold = italic = color = false;
488
489     while (cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor)) {
490         if (cursor.selectedText() == QString(QChar(QChar::LineSeparator))
491             || cursor.selectedText() == QString(QChar(QChar::ParagraphSeparator))) {
492             if (color) {
493                 color = false;
494                 mircText.append('\x03');
495             }
496             if (underline) {
497                 underline = false;
498                 mircText.append('\x1f');
499             }
500             if (italic) {
501                 italic = false;
502                 mircText.append('\x1d');
503             }
504             if (bold) {
505                 bold = false;
506                 mircText.append('\x02');
507             }
508             mircText.append('\n');
509         }
510         else {
511             if (!bold && cursor.charFormat().font().bold()) {
512                 bold = true;
513                 mircText.append('\x02');
514             }
515             if (!italic && cursor.charFormat().fontItalic()) {
516                 italic = true;
517                 mircText.append('\x1d');
518             }
519             if (!underline && cursor.charFormat().fontUnderline()) {
520                 underline = true;
521                 mircText.append('\x1f');
522             }
523             if (!color && (cursor.charFormat().foreground().isOpaque() || cursor.charFormat().background().isOpaque())) {
524                 color = true;
525                 mircText.append('\x03');
526                 mircFgColor = _mircColorMap.key(cursor.charFormat().foreground().color().name());
527                 mircBgColor = _mircColorMap.key(cursor.charFormat().background().color().name());
528
529                 if (mircFgColor.isEmpty()) {
530                     mircFgColor = "01"; //use black if the current foreground color can't be converted
531                 }
532
533                 mircText.append(mircFgColor);
534                 if (cursor.charFormat().background().isOpaque())
535                     mircText.append("," + mircBgColor);
536             }
537
538             mircText.append(cursor.selectedText());
539
540             peekcursor.setPosition(cursor.position());
541             peekcursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
542
543             if (mircCodesChanged(cursor, peekcursor)) {
544                 if (color) {
545                     color = false;
546                     mircText.append('\x03');
547                 }
548                 if (underline) {
549                     underline = false;
550                     mircText.append('\x1f');
551                 }
552                 if (italic) {
553                     italic = false;
554                     mircText.append('\x1d');
555                 }
556                 if (bold) {
557                     bold = false;
558                     mircText.append('\x02');
559                 }
560             }
561         }
562
563         cursor.clearSelection();
564     }
565
566     if (color)
567         mircText.append('\x03');
568
569     if (underline)
570         mircText.append('\x1f');
571
572     if (italic)
573         mircText.append('\x1d');
574
575     if (bold)
576         mircText.append('\x02');
577
578     return mircText;
579 }
580
581
582 bool MultiLineEdit::mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor)
583 {
584     bool changed = false;
585     if (cursor.charFormat().font().bold() != peekcursor.charFormat().font().bold())
586         changed = true;
587     if (cursor.charFormat().fontItalic() != peekcursor.charFormat().fontItalic())
588         changed = true;
589     if (cursor.charFormat().fontUnderline() != peekcursor.charFormat().fontUnderline())
590         changed = true;
591     if (cursor.charFormat().foreground().color() != peekcursor.charFormat().foreground().color())
592         changed = true;
593     if (cursor.charFormat().background().color() != peekcursor.charFormat().background().color())
594         changed = true;
595     return changed;
596 }
597
598
599 QString MultiLineEdit::convertMircCodesToHtml(const QString &text)
600 {
601     QStringList words;
602     QRegExp mircCode = QRegExp("(\ 2|\1d|\1f|\ 3)", Qt::CaseSensitive);
603
604     int posLeft = 0;
605     int posRight = 0;
606
607     for (;;) {
608         posRight = mircCode.indexIn(text, posLeft);
609
610         if (posRight < 0) {
611             words << text.mid(posLeft);
612             break; // no more mirc color codes
613         }
614
615         if (posLeft < posRight) {
616             words << text.mid(posLeft, posRight - posLeft);
617             posLeft = posRight;
618         }
619
620         posRight = text.indexOf(mircCode.cap(), posRight + 1);
621         words << text.mid(posLeft, posRight + 1 - posLeft);
622         posLeft = posRight + 1;
623     }
624
625     for (int i = 0; i < words.count(); i++) {
626         QString style;
627         if (words[i].contains('\x02')) {
628             style.append(" font-weight:600;");
629             words[i].replace('\x02', "");
630         }
631         if (words[i].contains('\x1d')) {
632             style.append(" font-style:italic;");
633             words[i].replace('\x1d', "");
634         }
635         if (words[i].contains('\x1f')) {
636             style.append(" text-decoration: underline;");
637             words[i].replace('\x1f', "");
638         }
639         if (words[i].contains('\x03')) {
640             int pos = words[i].indexOf('\x03');
641             int len = 3;
642             QString fg = words[i].mid(pos + 1, 2);
643             QString bg;
644             if (words[i][pos+3] == ',')
645                 bg = words[i].mid(pos+4, 2);
646
647             style.append(" color:");
648             style.append(_mircColorMap[fg]);
649             style.append(";");
650
651             if (!bg.isEmpty()) {
652                 style.append(" background-color:");
653                 style.append(_mircColorMap[bg]);
654                 style.append(";");
655                 len = 6;
656             }
657             words[i].replace(pos, len, "");
658             words[i].replace('\x03', "");
659         }
660         words[i].replace("&", "&amp;");
661         words[i].replace("<", "&lt;");
662         words[i].replace(">", "&gt;");
663         words[i].replace("\"", "&quot;");
664         if (style.isEmpty()) {
665             words[i] = "<span>" + words[i] + "</span>";
666         }
667         else {
668             words[i] = "<span style=\"" + style + "\">" + words[i] + "</span>";
669         }
670     }
671     return words.join("").replace("\n", "<br />");
672 }
673
674
675 void MultiLineEdit::on_returnPressed()
676 {
677     on_returnPressed(convertRichtextToMircCodes());
678 }
679
680
681 void MultiLineEdit::on_returnPressed(QString text)
682 {
683     if (_completionSpace && text.endsWith(" ")) {
684         text.chop(1);
685     }
686
687     if (!text.isEmpty()) {
688         foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) {
689             if (line.isEmpty())
690                 continue;
691             addToHistory(line);
692             emit textEntered(line);
693         }
694         reset();
695         _tempHistory.clear();
696     }
697     else {
698         emit noTextEntered();
699     }
700 }
701
702
703 void MultiLineEdit::on_textChanged()
704 {
705     _completionSpace = qMax(_completionSpace - 1, 0);
706
707     QString newText = text();
708     newText.replace("\r\n", "\n");
709     newText.replace('\r', '\n');
710     if (_mode == SingleLine) {
711         if (!pasteProtectionEnabled())
712             newText.replace('\n', ' ');
713         else if (newText.contains('\n')) {
714             QStringList lines = newText.split('\n', QString::SkipEmptyParts);
715             clear();
716
717             if (lines.count() >= 4) {
718                 QString msg = tr("Do you really want to paste %n line(s)?", "", lines.count());
719                 msg += "<p>";
720                 for (int i = 0; i < 4; i++) {
721 #if QT_VERSION < 0x050000
722                     msg += Qt::escape(lines[i].left(40));
723 #else
724                     msg += lines[i].left(40).toHtmlEscaped();
725 #endif
726                     if (lines[i].count() > 40)
727                         msg += "...";
728                     msg += "<br />";
729                 }
730                 msg += "...</p>";
731                 QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
732                 question.setDefaultButton(QMessageBox::No);
733 #ifdef Q_OS_MAC
734                 question.setWindowFlags(question.windowFlags() | Qt::Sheet);
735 #endif
736                 if (question.exec() != QMessageBox::Yes)
737                     return;
738             }
739
740             foreach(QString line, lines) {
741                 clear();
742                 insert(line);
743                 on_returnPressed();
744             }
745         }
746     }
747
748     _singleLine = (newText.indexOf('\n') < 0);
749
750     if (document()->size().height() != _lastDocumentHeight) {
751         _lastDocumentHeight = document()->size().height();
752         on_documentHeightChanged(_lastDocumentHeight);
753     }
754     updateSizeHint();
755     ensureCursorVisible();
756 }
757
758
759 void MultiLineEdit::on_documentHeightChanged(qreal)
760 {
761     updateScrollBars();
762 }
763
764
765 void MultiLineEdit::reset()
766 {
767     // every time the MultiLineEdit is cleared we also reset history index
768     _idx = _history.count();
769     clear();
770     QTextBlockFormat format = textCursor().blockFormat();
771     format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
772     textCursor().setBlockFormat(format);
773     updateScrollBars();
774 }
775
776
777 void MultiLineEdit::showHistoryEntry()
778 {
779     // if the user changed the history, display the changed line
780     setHtml(convertMircCodesToHtml(_tempHistory.contains(_idx) ? _tempHistory[_idx] : _history[_idx]));
781     QTextCursor cursor = textCursor();
782     QTextBlockFormat format = cursor.blockFormat();
783     format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
784     cursor.setBlockFormat(format);
785     cursor.movePosition(QTextCursor::End);
786     setTextCursor(cursor);
787     updateScrollBars();
788 }
789
790
791 void MultiLineEdit::addCompletionSpace()
792 {
793     // Inserting the space emits textChanged, which should not disable removal
794     _completionSpace = 2;
795     insertPlainText(" ");
796 }
797