Make multiline input configurable
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 26 Aug 2009 10:39:25 +0000 (12:39 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Wed, 26 Aug 2009 10:40:42 +0000 (12:40 +0200)
You can now switch this off to get a single line with paste protection as it used to be.
This makes mostly sense for people using a Qt style with buggy layouting, screwing up dynamic
resize.

src/qtui/inputwidget.cpp
src/qtui/inputwidget.h
src/qtui/settingspages/inputwidgetsettingspage.ui
src/uisupport/multilineedit.cpp
src/uisupport/multilineedit.h

index 11e3ec3..22e0602 100644 (file)
@@ -54,6 +54,7 @@ InputWidget::InputWidget(QWidget *parent)
   ui.inputEdit->setMinHeight(1);
   ui.inputEdit->setMaxHeight(5);
   ui.inputEdit->setMode(MultiLineEdit::MultiLine);
+  ui.inputEdit->setPasteProtectionEnabled(true);
 
   new TabCompleter(ui.inputEdit);
 
@@ -74,8 +75,11 @@ InputWidget::InputWidget(QWidget *parent)
   s.notify("MaxNumLines", this, SLOT(setMaxLines(QVariant)));
   setMaxLines(s.value("MaxNumLines", 5));
 
-  s.notify("EnableScrollBars", this, SLOT(setEnableScrollBars(QVariant)));
-  setEnableScrollBars(s.value("EnableScrollBars", true));
+  s.notify("EnableScrollBars", this, SLOT(setScrollBarsEnabled(QVariant)));
+  setScrollBarsEnabled(s.value("EnableScrollBars", true));
+
+  s.notify("EnableMultiLine", this, SLOT(setMultiLineEnabled(QVariant)));
+  setMultiLineEnabled(s.value("EnableMultiLine", true));
 
   ActionCollection *coll = QtUi::actionCollection();
 
@@ -107,10 +111,14 @@ void InputWidget::setMaxLines(const QVariant &v) {
   ui.inputEdit->setMaxHeight(v.toInt());
 }
 
-void InputWidget::setEnableScrollBars(const QVariant &v) {
+void InputWidget::setScrollBarsEnabled(const QVariant &v) {
   ui.inputEdit->setScrollBarsEnabled(v.toBool());
 }
 
+void InputWidget::setMultiLineEnabled(const QVariant &v) {
+  ui.inputEdit->setMode(v.toBool()? MultiLineEdit::MultiLine : MultiLineEdit::SingleLine);
+}
+
 bool InputWidget::eventFilter(QObject *watched, QEvent *event) {
   if(event->type() != QEvent::KeyPress)
     return false;
index 7ba9046..6e78509 100644 (file)
@@ -55,7 +55,8 @@ private slots:
   void setEnableSpellCheck(const QVariant &);
   void setShowNickSelector(const QVariant &);
   void setMaxLines(const QVariant &);
-  void setEnableScrollBars(const QVariant &);
+  void setMultiLineEnabled(const QVariant &);
+  void setScrollBarsEnabled(const QVariant &);
 
   void sendText(const QString &text) const;
   void changeNick(const QString &newNick) const;
index 92aea26..a4dedc4 100644 (file)
      <property name="title">
       <string>Multi-Line Editing</string>
      </property>
+     <property name="checkable">
+      <bool>true</bool>
+     </property>
+     <property name="settingsKey" stdset="0">
+      <string>EnableMultiLine</string>
+     </property>
+     <property name="defaultValue" stdset="0">
+      <bool>true</bool>
+     </property>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout_2">
index 96fd43f..699d4ec 100644 (file)
@@ -166,6 +166,10 @@ void MultiLineEdit::setWordWrapEnabled(bool enable) {
   updateSizeHint();
 }
 
+void MultiLineEdit::setPasteProtectionEnabled(bool enable, QWidget *) {
+  _pasteProtectionEnabled = enable;
+}
+
 void MultiLineEdit::historyMoveBack() {
   addToHistory(text(), true);
 
@@ -293,8 +297,12 @@ void MultiLineEdit::keyPressEvent(QKeyEvent *event) {
 }
 
 void MultiLineEdit::on_returnPressed() {
-  if(!text().isEmpty()) {
-    foreach(const QString &line, text().split('\n', QString::SkipEmptyParts)) {
+  on_returnPressed(text());
+}
+
+void MultiLineEdit::on_returnPressed(const QString & text) {
+  if(!text.isEmpty()) {
+    foreach(const QString &line, text.split('\n', QString::SkipEmptyParts)) {
       if(line.isEmpty())
         continue;
       addToHistory(line);
@@ -309,8 +317,39 @@ void MultiLineEdit::on_textChanged() {
   QString newText = text();
   newText.replace("\r\n", "\n");
   newText.replace('\r', '\n');
-  if(_mode == SingleLine)
-    newText.replace('\n', ' ');
+  if(_mode == SingleLine) {
+    if(!pasteProtectionEnabled())
+      newText.replace('\n', ' ');
+    else if(newText.contains('\n')) {
+      QStringList lines = newText.split('\n', QString::SkipEmptyParts);
+      clear();
+
+      if(lines.count() >= 4) {
+        QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
+        msg += "<p>";
+        for(int i = 0; i < 4; i++) {
+          msg += Qt::escape(lines[i].left(40));
+          if(lines[i].count() > 40)
+            msg += "...";
+          msg += "<br />";
+        }
+        msg += "...</p>";
+        QMessageBox question(QMessageBox::NoIcon, tr("Paste Protection"), msg, QMessageBox::Yes|QMessageBox::No);
+        question.setDefaultButton(QMessageBox::No);
+#ifdef Q_WS_MAC
+        question.setWindowFlags(question.windowFlags() | Qt::Sheet);
+#endif
+        if(question.exec() != QMessageBox::Yes)
+          return;
+      }
+
+      foreach(QString line, lines) {
+        clear();
+        insert(line);
+        on_returnPressed();
+      }
+    }
+  }
 
   _singleLine = (newText.indexOf('\n') < 0);
 
index 0a527b9..d12dfe6 100644 (file)
@@ -58,7 +58,9 @@ public:
   inline void insert(const QString &newText) { insertPlainText(newText); }
   inline void backspace() { keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier)); }
   inline bool hasSelectedText() { return textCursor().hasSelection(); }
+
   inline bool isSingleLine() const { return _singleLine; }
+  inline bool pasteProtectionEnabled() const { return _pasteProtectionEnabled; }
 
   virtual QSize sizeHint() const;
   virtual QSize minimumSizeHint() const;
@@ -69,6 +71,7 @@ public slots:
   void setMaxHeight(int numLines);
   void setScrollBarsEnabled(bool enable = true);
   void setSpellCheckEnabled(bool enable = true);
+  void setPasteProtectionEnabled(bool enable = true, QWidget *msgBoxParent = 0);
 
   // Note: Enabling wrap will make isSingleLine() not work correctly, so only use this if minHeight() > 1!
   void setWordWrapEnabled(bool enable = true);
@@ -82,6 +85,7 @@ protected:
 
 private slots:
   void on_returnPressed();
+  void on_returnPressed(const QString &text);
   void on_textChanged();
   void on_documentHeightChanged(qreal height);
 
@@ -98,6 +102,7 @@ private:
   int _minHeight;
   int _maxHeight;
   bool _scrollBarsEnabled;
+  bool _pasteProtectionEnabled;
 
   QSize _sizeHint;
   qreal _lastDocumentHeight;