From 914118e3b22cdd91191aea8568130f831db0cb8d Mon Sep 17 00:00:00 2001
From: Manuel Nickschas
Date: Fri, 17 Apr 2009 14:03:03 +0200
Subject: [PATCH] Revamp InputLine
* 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 | 1 +
src/qtui/ui/inputwidget.ui | 61 +++++++++++++++------------
src/uisupport/inputline.cpp | 84 ++++++++++++++++++++-----------------
src/uisupport/inputline.h | 39 +++++++++--------
4 files changed, 101 insertions(+), 84 deletions(-)
diff --git a/src/qtui/bufferwidget.cpp b/src/qtui/bufferwidget.cpp
index 1c727e42..e771e7d3 100644
--- a/src/qtui/bufferwidget.cpp
+++ b/src/qtui/bufferwidget.cpp
@@ -20,6 +20,7 @@
#include
#include
+#include
#include
#include "action.h"
diff --git a/src/qtui/ui/inputwidget.ui b/src/qtui/ui/inputwidget.ui
index bb8f5027..349465cf 100644
--- a/src/qtui/ui/inputwidget.ui
+++ b/src/qtui/ui/inputwidget.ui
@@ -1,48 +1,57 @@
-
+
+
InputWidget
-
-
+
+
0
0
- 696
- 28
+ 759
+ 202
-
-
+
+
0
0
-
+
Form
-
-
- 0
-
-
- 0
-
-
- 0
-
-
- 0
-
+
-
-
-
- -
-
+
+
-
+
+
+ -
+
+
+
+ 0
+ 0
+
+
+
+ Qt::ScrollBarAlwaysOff
+
+
+ Qt::ScrollBarAlwaysOff
+
+
+ QTextEdit::NoWrap
+
+
+
+
InputLine
- QLineEdit
+ QTextEdit
diff --git a/src/uisupport/inputline.cpp b/src/uisupport/inputline.cpp
index 6cd4fcbf..48ffa0a3 100644
--- a/src/uisupport/inputline.cpp
+++ b/src/uisupport/inputline.cpp
@@ -18,35 +18,41 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
+#include
+#include
+#include
+
#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 += "
";
}
msg += "...
";
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
}
diff --git a/src/uisupport/inputline.h b/src/uisupport/inputline.h
index 66742b63..9917ffa2 100644
--- a/src/uisupport/inputline.h
+++ b/src/uisupport/inputline.h
@@ -18,10 +18,11 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
-#ifndef _INPUTLINE_H_
-#define _INPUTLINE_H_
+#ifndef INPUTLINE_H_
+#define INPUTLINE_H_
-#include
+#include
+#include
#ifdef HAVE_KDE
#include
@@ -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;
--
2.20.1