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