cmake: avoid de-duplication of user's CXXFLAGS
[quassel.git] / src / uisupport / multilineedit.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2020 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, strikethrough;
467     QString mircText, mircFgColor, mircBgColor;
468     QTextCursor cursor = textCursor();
469     QTextCursor peekcursor = textCursor();
470     cursor.movePosition(QTextCursor::Start);
471
472     underline = bold = italic = color = strikethrough = 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             if (strikethrough) {
494                 strikethrough = false;
495                 mircText.append('\x1E');
496             }
497             mircText.append('\n');
498         }
499         else {
500             if (!bold && cursor.charFormat().font().bold()) {
501                 bold = true;
502                 mircText.append('\x02');
503             }
504             if (!italic && cursor.charFormat().fontItalic()) {
505                 italic = true;
506                 mircText.append('\x1d');
507             }
508             if (!underline && cursor.charFormat().fontUnderline()) {
509                 underline = true;
510                 mircText.append('\x1f');
511             }
512             if (!strikethrough && cursor.charFormat().fontStrikeOut()) {
513                 strikethrough = true;
514                 mircText.append('\x1E');
515             }
516             if (!color && (cursor.charFormat().foreground().isOpaque() || cursor.charFormat().background().isOpaque())) {
517                 color = true;
518                 mircText.append('\x03');
519                 mircFgColor = _mircColorMap.key(cursor.charFormat().foreground().color().name());
520                 mircBgColor = _mircColorMap.key(cursor.charFormat().background().color().name());
521
522                 if (mircFgColor.isEmpty()) {
523                     mircFgColor = "01";  // use black if the current foreground color can't be converted
524                 }
525
526                 mircText.append(mircFgColor);
527                 if (cursor.charFormat().background().isOpaque())
528                     mircText.append("," + mircBgColor);
529             }
530
531             mircText.append(cursor.selectedText());
532
533             peekcursor.setPosition(cursor.position());
534             peekcursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
535
536             if (mircCodesChanged(cursor, peekcursor)) {
537                 if (color) {
538                     color = false;
539                     mircText.append('\x03');
540                 }
541                 if (underline) {
542                     underline = false;
543                     mircText.append('\x1f');
544                 }
545                 if (italic) {
546                     italic = false;
547                     mircText.append('\x1d');
548                 }
549                 if (bold) {
550                     bold = false;
551                     mircText.append('\x02');
552                 }
553                 if (strikethrough) {
554                     strikethrough = false;
555                     mircText.append('\x1E');
556                 }
557             }
558         }
559
560         cursor.clearSelection();
561     }
562
563     if (color)
564         mircText.append('\x03');
565
566     if (underline)
567         mircText.append('\x1f');
568
569     if (italic)
570         mircText.append('\x1d');
571
572     if (bold)
573         mircText.append('\x02');
574
575     if (strikethrough)
576         mircText.append('\x1E');
577
578     return mircText;
579 }
580
581 bool MultiLineEdit::mircCodesChanged(QTextCursor& cursor, QTextCursor& peekcursor)
582 {
583     bool changed = false;
584     if (cursor.charFormat().font().bold() != peekcursor.charFormat().font().bold())
585         changed = true;
586     if (cursor.charFormat().fontItalic() != peekcursor.charFormat().fontItalic())
587         changed = true;
588     if (cursor.charFormat().fontUnderline() != peekcursor.charFormat().fontUnderline())
589         changed = true;
590     if (cursor.charFormat().fontStrikeOut() != peekcursor.charFormat().fontStrikeOut())
591         changed = true;
592     if (cursor.charFormat().foreground().color() != peekcursor.charFormat().foreground().color())
593         changed = true;
594     if (cursor.charFormat().background().color() != peekcursor.charFormat().background().color())
595         changed = true;
596     return changed;
597 }
598
599 QString MultiLineEdit::convertMircCodesToHtml(const QString& text)
600 {
601     QStringList words;
602     QRegExp mircCode = QRegExp("(\x02|\x1d|\x1f|\x03|\x1E)", Qt::CaseSensitive);
603
604     int posLeft = 0;
605     int posRight = 0;
606
607     for (;;) {
608         posRight = mircCode.indexIn(text, posLeft);
609
610         if (posRight < 0) {
611             words << text.mid(posLeft);
612             break;  // no more mirc color codes
613         }
614
615         if (posLeft < posRight) {
616             words << text.mid(posLeft, posRight - posLeft);
617             posLeft = posRight;
618         }
619
620         posRight = text.indexOf(mircCode.cap(), posRight + 1);
621         if (posRight == -1) {
622             words << text.mid(posLeft);
623             break;  // unclosed color code; can't process
624         }
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('\x1E')) {
644             style.append(" text-decoration: line-through;");
645             words[i].replace('\x1E', "");
646         }
647         if (words[i].contains('\x03')) {
648             int pos = words[i].indexOf('\x03');
649             int len = 3;
650             QString fg = words[i].mid(pos + 1, 2);
651             QString bg;
652             if (words[i][pos + 3] == ',')
653                 bg = words[i].mid(pos + 4, 2);
654
655             style.append(" color:");
656             style.append(_mircColorMap[fg]);
657             style.append(";");
658
659             if (!bg.isEmpty()) {
660                 style.append(" background-color:");
661                 style.append(_mircColorMap[bg]);
662                 style.append(";");
663                 len = 6;
664             }
665             words[i].replace(pos, len, "");
666             words[i].replace('\x03', "");
667         }
668         words[i].replace("&", "&amp;");
669         words[i].replace("<", "&lt;");
670         words[i].replace(">", "&gt;");
671         words[i].replace("\"", "&quot;");
672         if (style.isEmpty()) {
673             words[i] = "<span>" + words[i] + "</span>";
674         }
675         else {
676             words[i] = "<span style=\"" + style + "\">" + words[i] + "</span>";
677         }
678     }
679     return words.join("").replace("\n", "<br />");
680 }
681
682 void MultiLineEdit::on_returnPressed()
683 {
684     on_returnPressed(convertRichtextToMircCodes());
685 }
686
687 void MultiLineEdit::on_returnPressed(QString text)
688 {
689     if (_completionSpace && text.endsWith(" ")) {
690         text.chop(1);
691     }
692
693     if (!text.isEmpty()) {
694         foreach (const QString& line, text.split('\n', QString::SkipEmptyParts)) {
695             if (line.isEmpty())
696                 continue;
697             addToHistory(line);
698             emit textEntered(line);
699         }
700         reset();
701         _tempHistory.clear();
702     }
703     else {
704         emit noTextEntered();
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                     msg += lines[i].left(40).toHtmlEscaped();
727                     if (lines[i].count() > 40)
728                         msg += "...";
729                     msg += "<br />";
730                 }
731                 msg += "...</p>";
732                 QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes | QMessageBox::No);
733                 question.setDefaultButton(QMessageBox::No);
734 #ifdef Q_OS_MAC
735                 question.setWindowFlags(question.windowFlags() | Qt::Sheet);
736 #endif
737                 if (question.exec() != QMessageBox::Yes)
738                     return;
739             }
740
741             foreach (QString line, lines) {
742                 clear();
743                 insert(line);
744                 on_returnPressed();
745             }
746         }
747     }
748
749     _singleLine = (newText.indexOf('\n') < 0);
750
751     if (document()->size().height() != _lastDocumentHeight) {
752         _lastDocumentHeight = document()->size().height();
753         on_documentHeightChanged(_lastDocumentHeight);
754     }
755     updateSizeHint();
756     ensureCursorVisible();
757 }
758
759 void MultiLineEdit::on_documentHeightChanged(qreal)
760 {
761     updateScrollBars();
762 }
763
764 void MultiLineEdit::reset()
765 {
766     // every time the MultiLineEdit is cleared we also reset history index
767     _idx = _history.count();
768     clear();
769     QTextBlockFormat format = textCursor().blockFormat();
770     format.setLeftMargin(leftMargin);  // we want a little space between the frame and the contents
771     textCursor().setBlockFormat(format);
772     updateScrollBars();
773 }
774
775 void MultiLineEdit::showHistoryEntry()
776 {
777     // if the user changed the history, display the changed line
778     setHtml(convertMircCodesToHtml(_tempHistory.contains(_idx) ? _tempHistory[_idx] : _history[_idx]));
779     QTextCursor cursor = textCursor();
780     QTextBlockFormat format = cursor.blockFormat();
781     format.setLeftMargin(leftMargin);  // we want a little space between the frame and the contents
782     cursor.setBlockFormat(format);
783     cursor.movePosition(QTextCursor::End);
784     setTextCursor(cursor);
785     updateScrollBars();
786 }
787
788 void MultiLineEdit::addCompletionSpace()
789 {
790     // Inserting the space emits textChanged, which should not disable removal
791     _completionSpace = 2;
792     insertPlainText(" ");
793 }