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