Semi-yearly copyright bump
[quassel.git] / src / uisupport / multilineedit.cpp
index c56b03c..3c625c5 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2014 by the Quassel Project                        *
+ *   Copyright (C) 2005-2018 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -41,11 +41,10 @@ MultiLineEdit::MultiLineEdit(QWidget *parent)
     _scrollBarsEnabled(true),
     _pasteProtectionEnabled(true),
     _emacsMode(false),
+    _completionSpace(0),
     _lastDocumentHeight(-1)
 {
-#if QT_VERSION >= 0x040500
-    document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here
-#endif
+    document()->setDocumentMargin(0);
 
     setAcceptRichText(false);
 #ifdef HAVE_KDE
@@ -56,6 +55,9 @@ MultiLineEdit::MultiLineEdit(QWidget *parent)
     setLineWrapEnabled(false);
     reset();
 
+    // Prevent QTextHtmlImporter::appendNodeText from eating whitespace
+    document()->setDefaultStyleSheet("span { white-space: pre-wrap; }");
+
     connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
 
     _mircColorMap["00"] = "#ffffff";
@@ -81,6 +83,14 @@ MultiLineEdit::~MultiLineEdit()
 {
 }
 
+#if defined HAVE_KF5 || defined HAVE_KDE4
+void MultiLineEdit::createHighlighter()
+{
+    KTextEdit::createHighlighter();
+    if (highlighter())
+        highlighter()->setAutomatic(false);
+}
+#endif
 
 void MultiLineEdit::setCustomFont(const QFont &font)
 {
@@ -168,13 +178,21 @@ void MultiLineEdit::updateSizeHint()
 
     // use the style to determine a decent size
     int h = qMin(qMax((int)document()->size().height() + scrollBarHeight, minPixelHeight), maxPixelHeight) + 2 * frameWidth();
+#if QT_VERSION < 0x050000
     QStyleOptionFrameV2 opt;
+#else
+    QStyleOptionFrame opt;
+#endif
     opt.initFrom(this);
     opt.rect = QRect(0, 0, 100, h);
     opt.lineWidth = lineWidth();
     opt.midLineWidth = midLineWidth();
     opt.state |= QStyle::State_Sunken;
-    QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), this);
+    QWidget *widget = this;
+#ifdef Q_OS_MAC
+    widget = 0;
+#endif
+    QSize s = style()->sizeFromContents(QStyle::CT_LineEdit, &opt, QSize(100, h).expandedTo(QApplication::globalStrut()), widget);
     if (s != _sizeHint) {
         _sizeHint = s;
         updateGeometry();
@@ -296,18 +314,7 @@ bool MultiLineEdit::event(QEvent *e)
 
 void MultiLineEdit::keyPressEvent(QKeyEvent *event)
 {
-    // Workaround the fact that Qt < 4.5 doesn't know InsertLineSeparator yet
-#if QT_VERSION >= 0x040500
     if (event == QKeySequence::InsertLineSeparator) {
-#else
-
-# ifdef Q_OS_MAC
-    if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::META) {
-# else
-    if ((event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) && event->modifiers() & Qt::SHIFT) {
-# endif
-#endif
-
         if (_mode == SingleLine) {
             event->accept();
             on_returnPressed();
@@ -564,22 +571,18 @@ QString MultiLineEdit::convertRichtextToMircCodes()
 
         cursor.clearSelection();
     }
-    if (color) {
-        color = false;
+
+    if (color)
         mircText.append('\x03');
-    }
-    if (underline) {
-        underline = false;
+
+    if (underline)
         mircText.append('\x1f');
-    }
-    if (italic) {
-        italic = false;
+
+    if (italic)
         mircText.append('\x1d');
-    }
-    if (bold) {
-        bold = false;
+
+    if (bold)
         mircText.append('\x02');
-    }
 
     return mircText;
 }
@@ -684,8 +687,12 @@ void MultiLineEdit::on_returnPressed()
 }
 
 
-void MultiLineEdit::on_returnPressed(const QString &text)
+void MultiLineEdit::on_returnPressed(QString text)
 {
+    if (_completionSpace && text.endsWith(" ")) {
+        text.chop(1);
+    }
+
     if (!text.isEmpty()) {
         foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) {
             if (line.isEmpty())
@@ -704,6 +711,8 @@ void MultiLineEdit::on_returnPressed(const QString &text)
 
 void MultiLineEdit::on_textChanged()
 {
+    _completionSpace = qMax(_completionSpace - 1, 0);
+
     QString newText = text();
     newText.replace("\r\n", "\n");
     newText.replace('\r', '\n');
@@ -718,7 +727,11 @@ void MultiLineEdit::on_textChanged()
                 QString msg = tr("Do you really want to paste %n line(s)?", "", lines.count());
                 msg += "<p>";
                 for (int i = 0; i < 4; i++) {
+#if QT_VERSION < 0x050000
                     msg += Qt::escape(lines[i].left(40));
+#else
+                    msg += lines[i].left(40).toHtmlEscaped();
+#endif
                     if (lines[i].count() > 40)
                         msg += "...";
                     msg += "<br />";
@@ -782,3 +795,12 @@ void MultiLineEdit::showHistoryEntry()
     setTextCursor(cursor);
     updateScrollBars();
 }
+
+
+void MultiLineEdit::addCompletionSpace()
+{
+    // Inserting the space emits textChanged, which should not disable removal
+    _completionSpace = 2;
+    insertPlainText(" ");
+}
+