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