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