14eecf23b0f822c42ae95f3bf26381adb03f870b
[quassel.git] / src / uisupport / multilineedit.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2010 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  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 #include <QApplication>
22 #include <QMenu>
23 #include <QMessageBox>
24 #include <QScrollBar>
25
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   :
35 #ifdef HAVE_KDE
36     KTextEdit(parent),
37 #else
38     QTextEdit(parent),
39 #endif
40     _idx(0),
41     _mode(SingleLine),
42     _singleLine(true),
43     _minHeight(1),
44     _maxHeight(5),
45     _scrollBarsEnabled(true),
46     _pasteProtectionEnabled(true),
47     _lastDocumentHeight(-1)
48 {
49 #if QT_VERSION >= 0x040500
50   document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here
51 #endif
52
53   setAcceptRichText(false);
54 #ifdef HAVE_KDE
55   enableFindReplace(false);
56 #endif
57
58   setMode(SingleLine);
59   setWordWrapEnabled(false);
60   reset();
61
62   connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
63
64   _mircColorMap["00"] = "#ffffff";
65   _mircColorMap["01"] = "#000000";
66   _mircColorMap["02"] = "#000080";
67   _mircColorMap["03"] = "#008000";
68   _mircColorMap["04"] = "#ff0000";
69   _mircColorMap["05"] = "#800000";
70   _mircColorMap["06"] = "#800080";
71   _mircColorMap["07"] = "#ffa500";
72   _mircColorMap["08"] = "#ffff00";
73   _mircColorMap["09"] = "#00ff00";
74   _mircColorMap["10"] = "#008080";
75   _mircColorMap["11"] = "#00ffff";
76   _mircColorMap["12"] = "#4169e1";
77   _mircColorMap["13"] = "#ff00ff";
78   _mircColorMap["14"] = "#808080";
79   _mircColorMap["15"] = "#c0c0c0";
80
81 }
82
83 MultiLineEdit::~MultiLineEdit() {
84 }
85
86 void MultiLineEdit::setCustomFont(const QFont &font) {
87   setFont(font);
88   updateSizeHint();
89 }
90
91 void MultiLineEdit::setMode(Mode mode) {
92   if(mode == _mode)
93     return;
94
95   _mode = mode;
96 }
97
98 void MultiLineEdit::setMinHeight(int lines) {
99   if(lines == _minHeight)
100     return;
101
102   _minHeight = lines;
103   updateSizeHint();
104 }
105
106 void MultiLineEdit::setMaxHeight(int lines) {
107   if(lines == _maxHeight)
108     return;
109
110   _maxHeight = lines;
111   updateSizeHint();
112 }
113
114 void MultiLineEdit::setScrollBarsEnabled(bool enable) {
115   if(_scrollBarsEnabled == enable)
116     return;
117
118   _scrollBarsEnabled = enable;
119   updateScrollBars();
120 }
121
122 void MultiLineEdit::updateScrollBars() {
123   QFontMetrics fm(font());
124   int _maxPixelHeight = fm.lineSpacing() * _maxHeight;
125   if(_scrollBarsEnabled && document()->size().height() > _maxPixelHeight)
126     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
127   else
128     setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
129
130   if(!_scrollBarsEnabled || isSingleLine())
131     setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
132   else
133     setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
134 }
135
136 void MultiLineEdit::resizeEvent(QResizeEvent *event) {
137   QTextEdit::resizeEvent(event);
138   updateSizeHint();
139   updateScrollBars();
140 }
141
142 void MultiLineEdit::updateSizeHint() {
143   QFontMetrics fm(font());
144   int minPixelHeight = fm.lineSpacing() * _minHeight;
145   int maxPixelHeight = fm.lineSpacing() * _maxHeight;
146   int scrollBarHeight = horizontalScrollBar()->isVisible() ? horizontalScrollBar()->height() : 0;
147
148   // use the style to determine a decent size
149   int h = qMin(qMax((int)document()->size().height() + scrollBarHeight, minPixelHeight), maxPixelHeight) + 2 * frameWidth();
150   QStyleOptionFrameV2 opt;
151   opt.initFrom(this);
152   opt.rect = QRect(0, 0, 100, h);
153   opt.lineWidth = lineWidth();
154   opt.midLineWidth = midLineWidth();
155   opt.state |= QStyle::State_Sunken;
156   QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
157   if(s != _sizeHint) {
158     _sizeHint = s;
159     updateGeometry();
160   }
161 }
162
163 QSize MultiLineEdit::sizeHint() const {
164   if(!_sizeHint.isValid()) {
165     MultiLineEdit *that = const_cast<MultiLineEdit *>(this);
166     that->updateSizeHint();
167   }
168   return _sizeHint;
169 }
170
171 QSize MultiLineEdit::minimumSizeHint() const {
172   return sizeHint();
173 }
174
175 void MultiLineEdit::setSpellCheckEnabled(bool enable) {
176 #ifdef HAVE_KDE
177   setCheckSpellingEnabled(enable);
178 #else
179   Q_UNUSED(enable)
180 #endif
181 }
182
183 void MultiLineEdit::setWordWrapEnabled(bool enable) {
184   setLineWrapMode(enable? WidgetWidth : NoWrap);
185   updateSizeHint();
186 }
187
188 void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *) {
189   _pasteProtectionEnabled = enable;
190 }
191
192 void MultiLineEdit::historyMoveBack() {
193   addToHistory(convertRichtextToMircCodes(), true);
194
195   if(_idx > 0) {
196     _idx--;
197     showHistoryEntry();
198   }
199 }
200
201 void MultiLineEdit::historyMoveForward() {
202   addToHistory(convertRichtextToMircCodes(), true);
203
204   if(_idx < _history.count()) {
205     _idx++;
206     if(_idx < _history.count() || _tempHistory.contains(_idx)) // tempHistory might have an entry for idx == history.count() + 1
207       showHistoryEntry();
208     else
209       reset();              // equals clear() in this case
210   } else {
211     addToHistory(convertRichtextToMircCodes());
212     reset();
213   }
214 }
215
216 bool MultiLineEdit::addToHistory(const QString &text, bool temporary) {
217   if(text.isEmpty())
218     return false;
219
220   Q_ASSERT(0 <= _idx && _idx <= _history.count());
221
222   if(temporary) {
223     // if an entry of the history is changed, we remember it and show it again at this
224     // position until a line was actually sent
225     // sent lines get appended to the history
226     if(_history.isEmpty() || text != _history[_idx - (int)(_idx == _history.count())]) {
227       _tempHistory[_idx] = text;
228       return true;
229     }
230   } else {
231     if(_history.isEmpty() || text != _history.last()) {
232       _history << text;
233       _tempHistory.clear();
234       return true;
235     }
236   }
237   return false;
238 }
239
240 void MultiLineEdit::keyPressEvent(QKeyEvent *event) {
241   // Workaround the fact that Qt < 4.5 doesn't know InsertLineSeparator yet
242 #if QT_VERSION >= 0x040500
243   if(event == QKeySequence::InsertLineSeparator) {
244 #else
245
246 # ifdef Q_WS_MAC
247   if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::META) {
248 # else
249   if((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::SHIFT) {
250 # endif
251 #endif
252
253     if(_mode == SingleLine) {
254       event->accept();
255       on_returnPressed();
256       return;
257     }
258 #ifdef HAVE_KDE
259     KTextEdit::keyPressEvent(event);
260 #else
261     QTextEdit::keyPressEvent(event);
262 #endif
263     return;
264   }
265
266   switch(event->key()) {
267   case Qt::Key_Up:
268     if(event->modifiers() & Qt::ShiftModifier)
269       break;
270     {
271       event->accept();
272       if(!(event->modifiers() & Qt::ControlModifier)) {
273         int pos = textCursor().position();
274         moveCursor(QTextCursor::Up);
275         if(pos == textCursor().position()) // already on top line -> history
276           historyMoveBack();
277       } else
278         historyMoveBack();
279       return;
280     }
281
282   case Qt::Key_Down:
283     if(event->modifiers() & Qt::ShiftModifier)
284       break;
285     {
286       event->accept();
287       if(!(event->modifiers() & Qt::ControlModifier)) {
288         int pos = textCursor().position();
289         moveCursor(QTextCursor::Down);
290         if(pos == textCursor().position()) // already on bottom line -> history
291           historyMoveForward();
292       } else
293         historyMoveForward();
294       return;
295     }
296
297   case Qt::Key_Return:
298   case Qt::Key_Enter:
299   case Qt::Key_Select:
300     event->accept();
301     on_returnPressed();
302     return;
303
304   // We don't want to have the tab key react even if no completer is installed
305   case Qt::Key_Tab:
306     event->accept();
307     return;
308
309   default:
310     ;
311   }
312
313
314 #ifdef HAVE_KDE
315   KTextEdit::keyPressEvent(event);
316 #else
317   QTextEdit::keyPressEvent(event);
318 #endif
319 }
320
321 QString MultiLineEdit::convertRichtextToMircCodes() {
322   bool underline, bold, italic, color;
323   QString mircText, mircFgColor, mircBgColor;
324   QTextCursor cursor = textCursor();
325   QTextCursor peekcursor = textCursor();
326   cursor.movePosition(QTextCursor::Start);
327
328   underline = bold = italic = color = false;
329
330   while (cursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor)) {
331
332     if (cursor.selectedText() == QString(QChar(QChar::LineSeparator))
333       || cursor.selectedText() == QString(QChar(QChar::ParagraphSeparator))) {
334       if (color) {
335         color = false;
336         mircText.append('\x03');
337       }
338       if (underline) {
339         underline = false;
340         mircText.append('\x1f');
341       }
342       if (italic) {
343         italic = false;
344         mircText.append('\x1d');
345       }
346       if (bold) {
347         bold = false;
348         mircText.append('\x02');
349       }
350       mircText.append('\n');
351     }
352     else {
353       if (!bold && cursor.charFormat().font().bold()) {
354         bold = true;
355         mircText.append('\x02');
356       }
357       if (!italic && cursor.charFormat().fontItalic()) {
358         italic = true;
359         mircText.append('\x1d');
360       }
361       if (!underline && cursor.charFormat().fontUnderline()) {
362         underline = true;
363         mircText.append('\x1f');
364       }
365       if (!color && (cursor.charFormat().foreground().isOpaque() || cursor.charFormat().background().isOpaque())) {
366         color = true;
367         mircText.append('\x03');
368         mircFgColor = _mircColorMap.key(cursor.charFormat().foreground().color().name());
369         mircBgColor = _mircColorMap.key(cursor.charFormat().background().color().name());
370
371         if (mircFgColor.isEmpty()) {
372             mircFgColor = "01"; //use black if the current foreground color can't be converted
373         }
374
375         mircText.append(mircFgColor);
376         if (cursor.charFormat().background().isOpaque())
377           mircText.append("," + mircBgColor);
378       }
379
380       mircText.append(cursor.selectedText());
381
382       peekcursor.setPosition(cursor.position());
383       peekcursor.movePosition(QTextCursor::NextCharacter, QTextCursor::KeepAnchor);
384
385       if (mircCodesChanged(cursor, peekcursor)) {
386         if (color) {
387           color = false;
388           mircText.append('\x03');
389         }
390         if (underline) {
391           underline = false;
392           mircText.append('\x1f');
393         }
394         if (italic) {
395           italic = false;
396           mircText.append('\x1d');
397         }
398         if (bold) {
399           bold = false;
400           mircText.append('\x02');
401         }
402       }
403     }
404
405     cursor.clearSelection();
406   }
407   if (color) {
408     color = false;
409     mircText.append('\x03');
410   }
411   if (underline) {
412     underline = false;
413     mircText.append('\x1f');
414   }
415   if (italic) {
416     italic = false;
417     mircText.append('\x1d');
418   }
419   if (bold) {
420     bold = false;
421     mircText.append('\x02');
422   }
423
424   return mircText;
425 }
426
427 bool MultiLineEdit::mircCodesChanged(QTextCursor &cursor, QTextCursor &peekcursor) {
428   bool changed = false;
429   if (cursor.charFormat().font().bold() != peekcursor.charFormat().font().bold())
430     changed = true;
431   if (cursor.charFormat().fontItalic() != peekcursor.charFormat().fontItalic())
432     changed = true;
433   if (cursor.charFormat().fontUnderline() != peekcursor.charFormat().fontUnderline())
434     changed = true;
435   if (cursor.charFormat().foreground().color() != peekcursor.charFormat().foreground().color())
436     changed = true;
437   if (cursor.charFormat().background().color() != peekcursor.charFormat().background().color())
438     changed = true;
439   return changed;
440 }
441
442 QString MultiLineEdit::convertMircCodesToHtml(const QString &text) {
443   QStringList words;
444   QRegExp mircCode = QRegExp("(\ 2|\1d|\1f|\ 3)", Qt::CaseSensitive);
445
446   int posLeft = 0;
447   int posRight = 0;
448
449   for(;;) {
450     posRight = mircCode.indexIn(text, posLeft);
451
452     if(posRight < 0) {
453       words << text.mid(posLeft);
454       break; // no more mirc color codes
455     }
456
457     if (posLeft < posRight) {
458       words << text.mid(posLeft, posRight - posLeft);
459       posLeft = posRight;
460     }
461
462     posRight = text.indexOf(mircCode.cap(), posRight + 1);
463     words << text.mid(posLeft, posRight + 1 - posLeft);
464     posLeft = posRight + 1;
465   }
466
467   for (int i = 0; i < words.count(); i++) {
468       QString style;
469       if (words[i].contains('\x02')) {
470         style.append(" font-weight:600;");
471         words[i].replace('\x02',"");
472       }
473       if (words[i].contains('\x1d')) {
474         style.append(" font-style:italic;");
475         words[i].replace('\x1d',"");
476       }
477       if (words[i].contains('\x1f')) {
478         style.append(" text-decoration: underline;");
479         words[i].replace('\x1f',"");
480       }
481       if (words[i].contains('\x03')) {
482         int pos = words[i].indexOf('\x03');
483         int len = 3;
484         QString fg = words[i].mid(pos + 1,2);
485         QString bg;
486         if (words[i][pos+3] == ',')
487           bg = words[i].mid(pos+4,2);
488
489         style.append(" color:");
490         style.append(_mircColorMap[fg]);
491         style.append(";");
492
493         if (!bg.isEmpty()) {
494           style.append(" background-color:");
495           style.append(_mircColorMap[bg]);
496           style.append(";");
497           len = 6;
498         }
499         words[i].replace(pos, len, "");
500         words[i].replace('\x03',"");
501       }
502       words[i].replace("&","&amp;");
503       words[i].replace("<", "&lt;");
504       words[i].replace(">", "&gt;");
505       words[i].replace("\"", "&quot;");
506       if (style.isEmpty()) {
507         words[i] = "<span>" + words[i] + "</span>";
508       }
509       else {
510         words[i] = "<span style=\"" + style + "\">" + words[i] + "</span>";
511       }
512   }
513   return words.join("").replace("\n","<br />");
514 }
515
516 void MultiLineEdit::on_returnPressed() {
517   on_returnPressed(convertRichtextToMircCodes());
518 }
519
520 void MultiLineEdit::on_returnPressed(const QString & text) {
521   if(!text.isEmpty()) {
522     foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) {
523       if(line.isEmpty())
524         continue;
525       addToHistory(line);
526       emit textEntered(line);
527     }
528     reset();
529     _tempHistory.clear();
530   } else {
531     emit noTextEntered();
532   }
533 }
534
535 void MultiLineEdit::on_textChanged() {
536   QString newText = text();
537   newText.replace("\r\n", "\n");
538   newText.replace('\r', '\n');
539   if(_mode == SingleLine) {
540     if(!pasteProtectionEnabled())
541       newText.replace('\n', ' ');
542     else if(newText.contains('\n')) {
543       QStringList lines = newText.split('\n', QString::SkipEmptyParts);
544       clear();
545
546       if(lines.count() >= 4) {
547         QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
548         msg += "<p>";
549         for(int i = 0; i < 4; i++) {
550           msg += Qt::escape(lines[i].left(40));
551           if(lines[i].count() > 40)
552             msg += "...";
553           msg += "<br />";
554         }
555         msg += "...</p>";
556         QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
557         question.setDefaultButton(QMessageBox::No);
558 #ifdef Q_WS_MAC
559         question.setWindowFlags(question.windowFlags() | Qt::Sheet);
560 #endif
561         if(question.exec() != QMessageBox::Yes)
562           return;
563       }
564
565       foreach(QString line, lines) {
566         clear();
567         insert(line);
568         on_returnPressed();
569       }
570     }
571   }
572
573   _singleLine = (newText.indexOf('\n') < 0);
574
575   if(document()->size().height() != _lastDocumentHeight) {
576     _lastDocumentHeight = document()->size().height();
577     on_documentHeightChanged(_lastDocumentHeight);
578   }
579   updateSizeHint();
580   ensureCursorVisible();
581 }
582
583 void MultiLineEdit::on_documentHeightChanged(qreal) {
584   updateScrollBars();
585 }
586
587 void MultiLineEdit::reset() {
588   // every time the MultiLineEdit is cleared we also reset history index
589   _idx = _history.count();
590   clear();
591   QTextBlockFormat format = textCursor().blockFormat();
592   format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
593   textCursor().setBlockFormat(format);
594   updateScrollBars();
595 }
596
597 void MultiLineEdit::showHistoryEntry() {
598   // if the user changed the history, display the changed line
599   setHtml(convertMircCodesToHtml(_tempHistory.contains(_idx) ? _tempHistory[_idx] : _history[_idx]));
600   QTextCursor cursor = textCursor();
601   QTextBlockFormat format = cursor.blockFormat();
602   format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
603   cursor.setBlockFormat(format);
604   cursor.movePosition(QTextCursor::End);
605   setTextCursor(cursor);
606   updateScrollBars();
607 }