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