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