Revamp InputLine
[quassel.git] / src / uisupport / inputline.cpp
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
 }