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