Fix multiline textedit not expanding on OS X
[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     QWidget *widget = this;
187 #ifdef Q_OS_MAC
188     widget = 0;
189 #endif
190     QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), widget);
191     if (s != _sizeHint) {
192         _sizeHint = s;
193         updateGeometry();
194     }
195 }
196
197
198 QSize MultiLineEdit::sizeHint() const
199 {
200     if (!_sizeHint.isValid()) {
201         MultiLineEdit *that = const_cast<MultiLineEdit *>(this);
202         that->updateSizeHint();
203     }
204     return _sizeHint;
205 }
206
207
208 QSize MultiLineEdit::minimumSizeHint() const
209 {
210     return sizeHint();
211 }
212
213
214 void MultiLineEdit::setEmacsMode(bool enable)
215 {
216     _emacsMode = enable;
217 }
218
219
220 void MultiLineEdit::setSpellCheckEnabled(bool enable)
221 {
222 #ifdef HAVE_KDE
223     setCheckSpellingEnabled(enable);
224 #else
225     Q_UNUSED(enable)
226 #endif
227 }
228
229
230 void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *)
231 {
232     _pasteProtectionEnabled = enable;
233 }
234
235
236 void MultiLineEdit::historyMoveBack()
237 {
238     addToHistory(convertRichtextToMircCodes(), true);
239
240     if (_idx > 0) {
241         _idx--;
242         showHistoryEntry();
243     }
244 }
245
246
247 void MultiLineEdit::historyMoveForward()
248 {
249     addToHistory(convertRichtextToMircCodes(), true);
250
251     if (_idx < _history.count()) {
252         _idx++;
253         if (_idx < _history.count() || _tempHistory.contains(_idx)) // tempHistory might have an entry for idx == history.count() + 1
254             showHistoryEntry();
255         else
256             reset();        // equals clear() in this case
257     }
258     else {
259         addToHistory(convertRichtextToMircCodes());
260         reset();
261     }
262 }
263
264
265 bool MultiLineEdit::addToHistory(const QString &text, bool temporary)
266 {
267     if (text.isEmpty())
268         return false;
269
270     Q_ASSERT(0 <= _idx && _idx <= _history.count());
271
272     if (temporary) {
273         // if an entry of the history is changed, we remember it and show it again at this
274         // position until a line was actually sent
275         // sent lines get appended to the history
276         if (_history.isEmpty() || text != _history[_idx - (int)(_idx == _history.count())]) {
277             _tempHistory[_idx] = text;
278             return true;
279         }
280     }
281     else {
282         if (_history.isEmpty() || text != _history.last()) {
283             _history << text;
284             _tempHistory.clear();
285             return true;
286         }
287     }
288     return false;
289 }
290
291
292 bool MultiLineEdit::event(QEvent *e)
293 {
294     // We need to make sure that global shortcuts aren't eaten
295     if (e->type() == QEvent::ShortcutOverride) {
296         QKeyEvent *event = static_cast<QKeyEvent *>(e);
297         QKeySequence key = QKeySequence(event->key() | event->modifiers());
298         foreach(QAction *action, GraphicalUi::actionCollection()->actions()) {
299             if (action->shortcuts().contains(key)) {
300                 e->ignore();
301                 return false;
302             }
303         }
304     }
305
306     return MultiLineEditParent::event(e);
307 }
308
309
310 void MultiLineEdit::keyPressEvent(QKeyEvent *event)
311 {
312     if (event == QKeySequence::InsertLineSeparator) {
313         if (_mode == SingleLine) {
314             event->accept();
315             on_returnPressed();
316             return;
317         }
318         MultiLineEditParent::keyPressEvent(event);
319         return;
320     }
321
322     switch (event->key()) {
323     case Qt::Key_Up:
324         if (event->modifiers() & Qt::ShiftModifier)
325             break;
326         {
327             event->accept();
328             if (!(event->modifiers() & Qt::ControlModifier)) {
329                 int pos = textCursor().position();
330                 moveCursor(QTextCursor::Up);
331                 if (pos == textCursor().position()) // already on top line -> history
332                     historyMoveBack();
333             }
334             else
335                 historyMoveBack();
336             return;
337         }
338
339     case Qt::Key_Down:
340         if (event->modifiers() & Qt::ShiftModifier)
341             break;
342         {
343             event->accept();
344             if (!(event->modifiers() & Qt::ControlModifier)) {
345                 int pos = textCursor().position();
346                 moveCursor(QTextCursor::Down);
347                 if (pos == textCursor().position()) // already on bottom line -> history
348                     historyMoveForward();
349             }
350             else
351                 historyMoveForward();
352             return;
353         }
354
355     case Qt::Key_Return:
356     case Qt::Key_Enter:
357     case Qt::Key_Select:
358         event->accept();
359         on_returnPressed();
360         return;
361
362     // We don't want to have the tab key react even if no completer is installed
363     case Qt::Key_Tab:
364         event->accept();
365         return;
366
367     default:
368         ;
369     }
370
371     if (_emacsMode) {
372         if (event->modifiers() & Qt::ControlModifier) {
373             switch (event->key()) {
374             // move
375             case Qt::Key_A:
376                 moveCursor(QTextCursor::StartOfLine);
377                 return;
378             case Qt::Key_E:
379                 moveCursor(QTextCursor::EndOfLine);
380                 return;
381             case Qt::Key_F:
382                 moveCursor(QTextCursor::Right);
383                 return;
384             case Qt::Key_B:
385                 moveCursor(QTextCursor::Left);
386                 return;
387
388             // modify
389             case Qt::Key_Y:
390                 paste();
391                 return;
392             case Qt::Key_K:
393                 moveCursor(QTextCursor::EndOfLine, QTextCursor::KeepAnchor);
394                 cut();
395                 return;
396
397             default:
398                 break;
399             }
400         }
401         else if (event->modifiers() & Qt::MetaModifier ||
402                  event->modifiers() & Qt::AltModifier)
403         {
404             switch (event->key()) {
405             case Qt::Key_Right:
406                 moveCursor(QTextCursor::WordRight);
407                 return;
408             case Qt::Key_Left:
409                 moveCursor(QTextCursor::WordLeft);
410                 return;
411             case Qt::Key_F:
412                 moveCursor(QTextCursor::WordRight);
413                 return;
414             case Qt::Key_B:
415                 moveCursor(QTextCursor::WordLeft);
416                 return;
417             case Qt::Key_Less:
418                 moveCursor(QTextCursor::Start);
419                 return;
420             case Qt::Key_Greater:
421                 moveCursor(QTextCursor::End);
422                 return;
423
424             // modify
425             case Qt::Key_D:
426                 moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
427                 cut();
428                 return;
429
430             case Qt::Key_U: // uppercase word
431                 moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
432                 textCursor().insertText(textCursor().selectedText().toUpper());
433                 return;
434
435             case Qt::Key_L: // lowercase word
436                 moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
437                 textCursor().insertText(textCursor().selectedText().toLower());
438                 return;
439
440             case Qt::Key_C:
441             {           // capitalize word
442                 moveCursor(QTextCursor::WordRight, QTextCursor::KeepAnchor);
443                 QString const text = textCursor().selectedText();
444                 textCursor().insertText(text.left(1).toUpper() + text.mid(1).toLower());
445                 return;
446             }
447
448             case Qt::Key_T:
449             {           // transpose words
450                 moveCursor(QTextCursor::StartOfWord);
451                 moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
452                 QString const word1 = textCursor().selectedText();
453                 textCursor().clearSelection();
454                 moveCursor(QTextCursor::WordRight);
455                 moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
456                 QString const word2 = textCursor().selectedText();
457                 if (!word2.isEmpty() && !word1.isEmpty()) {
458                     textCursor().insertText(word1);
459                     moveCursor(QTextCursor::WordLeft);
460                     moveCursor(QTextCursor::WordLeft);
461                     moveCursor(QTextCursor::EndOfWord, QTextCursor::KeepAnchor);
462                     textCursor().insertText(word2);
463                     moveCursor(QTextCursor::WordRight);
464                     moveCursor(QTextCursor::EndOfWord);
465                 }
466                 return;
467             }
468
469             default:
470                 break;
471             }
472         }
473     }
474
475 #ifdef HAVE_KDE
476     KTextEdit::keyPressEvent(event);
477 #else
478     QTextEdit::keyPressEvent(event);
479 #endif
480 }
481
482
483 QString MultiLineEdit::convertRichtextToMircCodes()
484 {
485     bool underline, bold, italic, color;
486     QString mircText, mircFgColor, mircBgColor;
487     QTextCursor cursor = textCursor();
488     QTextCursor peekcursor = textCursor();
489     cursor.movePosition(QTextCursor::Start);
490
491     underline = bold = italic = color = false;
492
493     while (cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor)) {
494         if (cursor.selectedText() == QString(QChar(QChar::LineSeparator))
495             || cursor.selectedText() == QString(QChar(QChar::ParagraphSeparator))) {
496             if (color) {
497                 color = false;
498                 mircText.append('\x03');
499             }
500             if (underline) {
501                 underline = false;
502                 mircText.append('\x1f');
503             }
504             if (italic) {
505                 italic = false;
506                 mircText.append('\x1d');
507             }
508             if (bold) {
509                 bold = false;
510                 mircText.append('\x02');
511             }
512             mircText.append('\n');
513         }
514         else {
515             if (!bold && cursor.charFormat().font().bold()) {
516                 bold = true;
517                 mircText.append('\x02');
518             }
519             if (!italic && cursor.charFormat().fontItalic()) {
520                 italic = true;
521                 mircText.append('\x1d');
522             }
523             if (!underline && cursor.charFormat().fontUnderline()) {
524                 underline = true;
525                 mircText.append('\x1f');
526             }
527             if (!color && (cursor.charFormat().foreground().isOpaque() || cursor.charFormat().background().isOpaque())) {
528                 color = true;
529                 mircText.append('\x03');
530                 mircFgColor = _mircColorMap.key(cursor.charFormat().foreground().color().name());
531                 mircBgColor = _mircColorMap.key(cursor.charFormat().background().color().name());
532
533                 if (mircFgColor.isEmpty()) {
534                     mircFgColor = "01"; //use black if the current foreground color can't be converted
535                 }
536
537                 mircText.append(mircFgColor);
538                 if (cursor.charFormat().background().isOpaque())
539                     mircText.append("," + mircBgColor);
540             }
541
542             mircText.append(cursor.selectedText());
543
544             peekcursor.setPosition(cursor.position());
545             peekcursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
546
547             if (mircCodesChanged(cursor, peekcursor)) {
548                 if (color) {
549                     color = false;
550                     mircText.append('\x03');
551                 }
552                 if (underline) {
553                     underline = false;
554                     mircText.append('\x1f');
555                 }
556                 if (italic) {
557                     italic = false;
558                     mircText.append('\x1d');
559                 }
560                 if (bold) {
561                     bold = false;
562                     mircText.append('\x02');
563                 }
564             }
565         }
566
567         cursor.clearSelection();
568     }
569
570     if (color)
571         mircText.append('\x03');
572
573     if (underline)
574         mircText.append('\x1f');
575
576     if (italic)
577         mircText.append('\x1d');
578
579     if (bold)
580         mircText.append('\x02');
581
582     return mircText;
583 }
584
585
586 bool MultiLineEdit::mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor)
587 {
588     bool changed = false;
589     if (cursor.charFormat().font().bold() != peekcursor.charFormat().font().bold())
590         changed = true;
591     if (cursor.charFormat().fontItalic() != peekcursor.charFormat().fontItalic())
592         changed = true;
593     if (cursor.charFormat().fontUnderline() != peekcursor.charFormat().fontUnderline())
594         changed = true;
595     if (cursor.charFormat().foreground().color() != peekcursor.charFormat().foreground().color())
596         changed = true;
597     if (cursor.charFormat().background().color() != peekcursor.charFormat().background().color())
598         changed = true;
599     return changed;
600 }
601
602
603 QString MultiLineEdit::convertMircCodesToHtml(const QString &text)
604 {
605     QStringList words;
606     QRegExp mircCode = QRegExp("(\ 2|\1d|\1f|\ 3)", Qt::CaseSensitive);
607
608     int posLeft = 0;
609     int posRight = 0;
610
611     for (;;) {
612         posRight = mircCode.indexIn(text, posLeft);
613
614         if (posRight < 0) {
615             words << text.mid(posLeft);
616             break; // no more mirc color codes
617         }
618
619         if (posLeft < posRight) {
620             words << text.mid(posLeft, posRight - posLeft);
621             posLeft = posRight;
622         }
623
624         posRight = text.indexOf(mircCode.cap(), posRight + 1);
625         words << text.mid(posLeft, posRight + 1 - posLeft);
626         posLeft = posRight + 1;
627     }
628
629     for (int i = 0; i < words.count(); i++) {
630         QString style;
631         if (words[i].contains('\x02')) {
632             style.append(" font-weight:600;");
633             words[i].replace('\x02', "");
634         }
635         if (words[i].contains('\x1d')) {
636             style.append(" font-style:italic;");
637             words[i].replace('\x1d', "");
638         }
639         if (words[i].contains('\x1f')) {
640             style.append(" text-decoration: underline;");
641             words[i].replace('\x1f', "");
642         }
643         if (words[i].contains('\x03')) {
644             int pos = words[i].indexOf('\x03');
645             int len = 3;
646             QString fg = words[i].mid(pos + 1, 2);
647             QString bg;
648             if (words[i][pos+3] == ',')
649                 bg = words[i].mid(pos+4, 2);
650
651             style.append(" color:");
652             style.append(_mircColorMap[fg]);
653             style.append(";");
654
655             if (!bg.isEmpty()) {
656                 style.append(" background-color:");
657                 style.append(_mircColorMap[bg]);
658                 style.append(";");
659                 len = 6;
660             }
661             words[i].replace(pos, len, "");
662             words[i].replace('\x03', "");
663         }
664         words[i].replace("&", "&amp;");
665         words[i].replace("<", "&lt;");
666         words[i].replace(">", "&gt;");
667         words[i].replace("\"", "&quot;");
668         if (style.isEmpty()) {
669             words[i] = "<span>" + words[i] + "</span>";
670         }
671         else {
672             words[i] = "<span style=\"" + style + "\">" + words[i] + "</span>";
673         }
674     }
675     return words.join("").replace("\n", "<br />");
676 }
677
678
679 void MultiLineEdit::on_returnPressed()
680 {
681     on_returnPressed(convertRichtextToMircCodes());
682 }
683
684
685 void MultiLineEdit::on_returnPressed(QString text)
686 {
687     if (_completionSpace && text.endsWith(" ")) {
688         text.chop(1);
689     }
690
691     if (!text.isEmpty()) {
692         foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) {
693             if (line.isEmpty())
694                 continue;
695             addToHistory(line);
696             emit textEntered(line);
697         }
698         reset();
699         _tempHistory.clear();
700     }
701     else {
702         emit noTextEntered();
703     }
704 }
705
706
707 void MultiLineEdit::on_textChanged()
708 {
709     _completionSpace = qMax(_completionSpace - 1, 0);
710
711     QString newText = text();
712     newText.replace("\r\n", "\n");
713     newText.replace('\r', '\n');
714     if (_mode == SingleLine) {
715         if (!pasteProtectionEnabled())
716             newText.replace('\n', ' ');
717         else if (newText.contains('\n')) {
718             QStringList lines = newText.split('\n', QString::SkipEmptyParts);
719             clear();
720
721             if (lines.count() >= 4) {
722                 QString msg = tr("Do you really want to paste %n line(s)?", "", lines.count());
723                 msg += "<p>";
724                 for (int i = 0; i < 4; i++) {
725 #if QT_VERSION < 0x050000
726                     msg += Qt::escape(lines[i].left(40));
727 #else
728                     msg += lines[i].left(40).toHtmlEscaped();
729 #endif
730                     if (lines[i].count() > 40)
731                         msg += "...";
732                     msg += "<br />";
733                 }
734                 msg += "...</p>";
735                 QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
736                 question.setDefaultButton(QMessageBox::No);
737 #ifdef Q_OS_MAC
738                 question.setWindowFlags(question.windowFlags() | Qt::Sheet);
739 #endif
740                 if (question.exec() != QMessageBox::Yes)
741                     return;
742             }
743
744             foreach(QString line, lines) {
745                 clear();
746                 insert(line);
747                 on_returnPressed();
748             }
749         }
750     }
751
752     _singleLine = (newText.indexOf('\n') < 0);
753
754     if (document()->size().height() != _lastDocumentHeight) {
755         _lastDocumentHeight = document()->size().height();
756         on_documentHeightChanged(_lastDocumentHeight);
757     }
758     updateSizeHint();
759     ensureCursorVisible();
760 }
761
762
763 void MultiLineEdit::on_documentHeightChanged(qreal)
764 {
765     updateScrollBars();
766 }
767
768
769 void MultiLineEdit::reset()
770 {
771     // every time the MultiLineEdit is cleared we also reset history index
772     _idx = _history.count();
773     clear();
774     QTextBlockFormat format = textCursor().blockFormat();
775     format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
776     textCursor().setBlockFormat(format);
777     updateScrollBars();
778 }
779
780
781 void MultiLineEdit::showHistoryEntry()
782 {
783     // if the user changed the history, display the changed line
784     setHtml(convertMircCodesToHtml(_tempHistory.contains(_idx) ? _tempHistory[_idx] : _history[_idx]));
785     QTextCursor cursor = textCursor();
786     QTextBlockFormat format = cursor.blockFormat();
787     format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
788     cursor.setBlockFormat(format);
789     cursor.movePosition(QTextCursor::End);
790     setTextCursor(cursor);
791     updateScrollBars();
792 }
793
794
795 void MultiLineEdit::addCompletionSpace()
796 {
797     // Inserting the space emits textChanged, which should not disable removal
798     _completionSpace = 2;
799     insertPlainText(" ");
800 }
801