Revamp InputLine
authorManuel Nickschas <sputnick@quassel-irc.org>
Fri, 17 Apr 2009 12:03:03 +0000 (14:03 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Fri, 17 Apr 2009 12:07:56 +0000 (14:07 +0200)
* Use {Q|K}TextEdit rather than QLineEdit on all platforms - we need this for fancy stuff to come
* Replaced hacky hardcoded height calculation by proper QStyle-based information, should finally
  fix the jumpy input line everywhere, the oversized widget in Oxygen, and other issues
* Use sizeHint() rather than setting a maximumHeight() to make the input line work in layouts
* Cleanups

src/qtui/bufferwidget.cpp
src/qtui/ui/inputwidget.ui
src/uisupport/inputline.cpp
src/uisupport/inputline.h

index 1c727e4..e771e7d 100644 (file)
@@ -20,6 +20,7 @@
 
 #include <QLayout>
 #include <QKeyEvent>
+#include <QMenu>
 #include <QScrollBar>
 
 #include "action.h"
index bb8f502..349465c 100644 (file)
@@ -1,48 +1,57 @@
-<ui version="4.0" >
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
  <class>InputWidget</class>
- <widget class="QWidget" name="InputWidget" >
-  <property name="geometry" >
+ <widget class="QWidget" name="InputWidget">
+  <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>696</width>
-    <height>28</height>
+    <width>759</width>
+    <height>202</height>
    </rect>
   </property>
-  <property name="sizePolicy" >
-   <sizepolicy vsizetype="Fixed" hsizetype="Preferred" >
+  <property name="sizePolicy">
+   <sizepolicy hsizetype="Preferred" vsizetype="Fixed">
     <horstretch>0</horstretch>
     <verstretch>0</verstretch>
    </sizepolicy>
   </property>
-  <property name="windowTitle" >
+  <property name="windowTitle">
    <string>Form</string>
   </property>
-  <layout class="QHBoxLayout" >
-   <property name="leftMargin" >
-    <number>0</number>
-   </property>
-   <property name="topMargin" >
-    <number>0</number>
-   </property>
-   <property name="rightMargin" >
-    <number>0</number>
-   </property>
-   <property name="bottomMargin" >
-    <number>0</number>
-   </property>
+  <layout class="QVBoxLayout" name="verticalLayout">
    <item>
-    <widget class="QComboBox" name="ownNick" />
-   </item>
-   <item>
-    <widget class="InputLine" name="inputEdit" />
+    <layout class="QHBoxLayout" name="horizontalLayout">
+     <item>
+      <widget class="QComboBox" name="ownNick"/>
+     </item>
+     <item>
+      <widget class="InputLine" name="inputEdit">
+       <property name="sizePolicy">
+        <sizepolicy hsizetype="Expanding" vsizetype="Fixed">
+         <horstretch>0</horstretch>
+         <verstretch>0</verstretch>
+        </sizepolicy>
+       </property>
+       <property name="verticalScrollBarPolicy">
+        <enum>Qt::ScrollBarAlwaysOff</enum>
+       </property>
+       <property name="horizontalScrollBarPolicy">
+        <enum>Qt::ScrollBarAlwaysOff</enum>
+       </property>
+       <property name="lineWrapMode">
+        <enum>QTextEdit::NoWrap</enum>
+       </property>
+      </widget>
+     </item>
+    </layout>
    </item>
   </layout>
  </widget>
  <customwidgets>
   <customwidget>
    <class>InputLine</class>
-   <extends>QLineEdit</extends>
+   <extends>QTextEdit</extends>
    <header>inputline.h</header>
   </customwidget>
  </customwidgets>
index 6cd4fcb..48ffa0a 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include <QApplication>
+#include <QMenu>
+#include <QMessageBox>
+
 #include "bufferview.h"
 #include "graphicalui.h"
 #include "inputline.h"
 #include "tabcompleter.h"
 
+const int leftMargin = 3;
+
 InputLine::InputLine(QWidget *parent)
   :
 #ifdef HAVE_KDE
     KTextEdit(parent),
 #else
-    QLineEdit(parent),
+    QTextEdit(parent),
 #endif
     idx(0),
     tabCompleter(new TabCompleter(this))
 {
-#ifdef HAVE_KDE
-//This is done to make the KTextEdit look like a lineedit
+  // Make the QTextEdit look like a QLineEdit
 #if QT_VERSION >= 0x040500
-  document()->setDocumentMargin(0);
+  document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here
 #endif
-  setMaximumHeight(document()->size().toSize().height());
   setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
   setAcceptRichText(false);
   setLineWrapMode(NoWrap);
+#ifdef HAVE_KDE
   enableFindReplace(false);
-  connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
 #endif
+  resetLine();
 
+  connect(this, SIGNAL(textChanged()), this, SLOT(on_textChanged()));
   connect(this, SIGNAL(returnPressed()), this, SLOT(on_returnPressed()));
   connect(this, SIGNAL(textChanged(QString)), this, SLOT(on_textChanged(QString)));
 }
@@ -56,9 +62,24 @@ InputLine::~InputLine() {
 
 void InputLine::setCustomFont(const QFont &font) {
   setFont(font);
-#ifdef HAVE_KDE
-  setMaximumHeight(document()->size().toSize().height() + 2*frameWidth());
-#endif
+}
+
+QSize InputLine::sizeHint() const {
+  // use the style to determine a decent size
+  QFontMetrics fm(font());
+  int h = fm.lineSpacing() + 2 * frameWidth();
+  QStyleOptionFrameV2 opt;
+  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);
+  return s;
+}
+
+QSize InputLine::minimumSizeHint() const {
+  return sizeHint();
 }
 
 bool InputLine::eventFilter(QObject *watched, QEvent *event) {
@@ -83,8 +104,6 @@ bool InputLine::eventFilter(QObject *watched, QEvent *event) {
 }
 
 void InputLine::keyPressEvent(QKeyEvent * event) {
-
-#ifdef HAVE_KDE
   if(event->matches(QKeySequence::Find)) {
     QAction *act = GraphicalUi::actionCollection()->action("ToggleSearchBar");
     if(act) {
@@ -93,7 +112,6 @@ void InputLine::keyPressEvent(QKeyEvent * event) {
       return;
     }
   }
-#endif
 
   switch(event->key()) {
   case Qt::Key_Up:
@@ -126,26 +144,15 @@ void InputLine::keyPressEvent(QKeyEvent * event) {
 
     break;
 
-  case Qt::Key_Select:         // for Qtopia
-    emit returnPressed();
-    break;
-
-#ifdef HAVE_KDE
-//Since this is a ktextedit, we don't have this signal "natively"
   case Qt::Key_Return:
   case Qt::Key_Enter:
+  case Qt::Key_Select:
     event->accept();
     emit returnPressed();
     break;
 
-#endif
-
   default:
-#ifdef HAVE_KDE
-    KTextEdit::keyPressEvent(event);
-#else
-    QLineEdit::keyPressEvent(event);
-#endif
+    QTextEdit::keyPressEvent(event);
   }
 }
 
@@ -182,8 +189,8 @@ void InputLine::on_returnPressed() {
 void InputLine::on_textChanged(QString newText) {
   QStringList lineSeparators;
   lineSeparators << QString("\r\n")
-                << QString('\n')
-                << QString('\r');
+                 << QString('\n')
+                 << QString('\r');
 
   QString lineSep;
   foreach(QString separator, lineSeparators) {
@@ -196,8 +203,7 @@ void InputLine::on_textChanged(QString newText) {
   if(lineSep.isEmpty())
     return;
 
-  QStringList lines = newText.split(lineSep);
-  clear();
+  QStringList lines = newText.split(lineSep, QString::SkipEmptyParts);
 
   if(lines.count() >= 4) {
     QString msg = tr("Do you really want to paste %n lines?", "", lines.count());
@@ -205,14 +211,14 @@ void InputLine::on_textChanged(QString newText) {
     for(int i = 0; i < 3; i++) {
       msg += lines[i].left(40);
       if(lines[i].count() > 40)
-       msg += "...";
+        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);
+    question.setWindowFlags(question.windowFlags() | Qt::Sheet); // Qt::Sheet is not ignored on other platforms as it should :/
 #endif
     if(question.exec() == QMessageBox::No)
       return;
@@ -220,11 +226,12 @@ void InputLine::on_textChanged(QString newText) {
 
   foreach(QString line, lines) {
     if(!line.isEmpty()) {
-      clear();
+      resetLine();
       insert(line);
       emit returnPressed();
     }
   }
+
 //   if(newText.contains(lineSep)) {
 //     clear();
 //     QString line = newText.section(lineSep, 0, 0);
@@ -239,17 +246,18 @@ void InputLine::resetLine() {
   // every time the InputLine is cleared we also reset history index
   idx = history.count();
   clear();
+  QTextBlockFormat format = textCursor().blockFormat();
+  format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
+  textCursor().setBlockFormat(format);
 }
 
 void InputLine::showHistoryEntry() {
   // if the user changed the history, display the changed line
-  QString text = tempHistory.contains(idx) ? tempHistory[idx] : history[idx];
-#ifdef HAVE_KDE
-  setPlainText(text);
+  setPlainText(tempHistory.contains(idx) ? tempHistory[idx] : history[idx]);
   QTextCursor cursor = textCursor();
+  QTextBlockFormat format = cursor.blockFormat();
+  format.setLeftMargin(leftMargin); // we want a little space between the frame and the contents
+  cursor.setBlockFormat(format);
   cursor.movePosition(QTextCursor::End);
   setTextCursor(cursor);
-#else
-  setText(text);
-#endif
 }
index 66742b6..9917ffa 100644 (file)
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
-#ifndef _INPUTLINE_H_
-#define _INPUTLINE_H_
+#ifndef INPUTLINE_H_
+#define INPUTLINE_H_
 
-#include <QtGui>
+#include <QKeyEvent>
+#include <QTextEdit>
 
 #ifdef HAVE_KDE
 #include <KDE/KTextEdit>
@@ -33,7 +34,7 @@ class InputLine : public
 #ifdef HAVE_KDE
                   KTextEdit
 #else
-                  QLineEdit
+                  QTextEdit
 #endif
 {
   Q_OBJECT
@@ -44,37 +45,35 @@ public:
 
   void setCustomFont(const QFont &); // should be used instead setFont(), so we can set our size correctly
 
-#ifdef HAVE_KDE
-//Compatibility methods with the rest of the classes which expects this to be a QLineEdit
-  QString text() { return toPlainText(); };
-  int cursorPosition() { return textCursor().position(); };
-  void insert(const QString &newText) { insertPlainText(newText); };
-  void backspace() { keyPressEvent(new QKeyEvent(QEvent::KeyPress, Qt::Key_Backspace, Qt::NoModifier));  };
-  bool hasSelectedText() { return textCursor().hasSelection(); };
-#endif
+  // Compatibility methods with the rest of the classes which still expect this to be a QLineEdit
+  inline QString text() { return toPlainText(); }
+  inline int cursorPosition() { return textCursor().position(); }
+  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(); }
+
+  virtual QSize sizeHint() const;
+  virtual QSize minimumSizeHint() const;
 
 protected:
-  //    virtual bool event(QEvent *);
   virtual void keyPressEvent(QKeyEvent * event);
   virtual bool eventFilter(QObject *watched, QEvent *event);
 
 private slots:
   void on_returnPressed();
   void on_textChanged(QString newText);
-#ifdef HAVE_KDE
-//Needed to emulate the signal that QLineEdit has
-  void on_textChanged() { emit textChanged(toPlainText()); };
-#endif
+
+  // Needed to emulate the signal that QLineEdit has
+  inline void on_textChanged() { emit textChanged(toPlainText()); };
 
   bool addToHistory(const QString &text, bool temporary = false);
 
 signals:
   void sendText(QString text);
-#ifdef HAVE_KDE
-//KTextEdit does not provide this signal, so we manually emit it in keyPressEvent()
+
+  // QTextEdit does not provide this signal, so we manually emit it in keyPressEvent()
   void returnPressed();
   void textChanged(QString newText);
-#endif
 
 private:
   QStringList history;