From 7f7a08b1be727bf8c402efcc6938c233162cc74a Mon Sep 17 00:00:00 2001 From: Jukka Helle Date: Tue, 21 Apr 2009 01:48:32 +0300 Subject: [PATCH] The InputLine is now multi-line. Control+Up/Down are now used to scroll the ChatView. Shift+Up/Down are used to manipulate the InputLine. Shift+Return lets you enter a new line. User now can always review pastes before sending to IRC. --- src/qtui/bufferwidget.cpp | 3 +- src/qtui/inputwidget.cpp | 2 + src/uisupport/inputline.cpp | 148 +++++++++++++++++++------------------------ 3 files changed, 68 insertions(+), 85 deletions(-) diff --git a/src/qtui/bufferwidget.cpp b/src/qtui/bufferwidget.cpp index e771e7d..db761b7 100644 --- a/src/qtui/bufferwidget.cpp +++ b/src/qtui/bufferwidget.cpp @@ -177,7 +177,7 @@ bool BufferWidget::eventFilter(QObject *watched, QEvent *event) { switch(keyEvent->key()) { case Qt::Key_Up: case Qt::Key_Down: - if(!(keyEvent->modifiers() & Qt::ShiftModifier)) + if(!(keyEvent->modifiers() & Qt::ControlModifier)) return false; case Qt::Key_PageUp: case Qt::Key_PageDown: @@ -186,4 +186,5 @@ bool BufferWidget::eventFilter(QObject *watched, QEvent *event) { default: return false; } + } diff --git a/src/qtui/inputwidget.cpp b/src/qtui/inputwidget.cpp index 737387f..226871a 100644 --- a/src/qtui/inputwidget.cpp +++ b/src/qtui/inputwidget.cpp @@ -35,6 +35,8 @@ InputWidget::InputWidget(QWidget *parent) _networkId(0) { ui.setupUi(this); + layout()->setContentsMargins(0, 0, 0, 0); + connect(ui.inputEdit, SIGNAL(sendText(QString)), this, SLOT(sendText(QString))); connect(ui.ownNick, SIGNAL(activated(QString)), this, SLOT(changeNick(QString))); setFocusProxy(ui.inputEdit); diff --git a/src/uisupport/inputline.cpp b/src/uisupport/inputline.cpp index 48ffa0a..2973faa 100644 --- a/src/uisupport/inputline.cpp +++ b/src/uisupport/inputline.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "bufferview.h" #include "graphicalui.h" @@ -40,9 +41,9 @@ InputLine::InputLine(QWidget *parent) tabCompleter(new TabCompleter(this)) { // Make the QTextEdit look like a QLineEdit -#if QT_VERSION >= 0x040500 - document()->setDocumentMargin(0); // new in Qt 4.5 and we really don't want it here -#endif +//#if QT_VERSION >= 0x040500 + document()->setDocumentMargin(leftMargin); // new in Qt 4.5 and we really don't want it here (except for Squider) +//#endif setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setAcceptRichText(false); @@ -68,12 +69,14 @@ 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; } @@ -115,40 +118,51 @@ void InputLine::keyPressEvent(QKeyEvent * event) { switch(event->key()) { case Qt::Key_Up: - event->accept(); + if(event->modifiers() & Qt::ShiftModifier) { + QTextEdit::keyPressEvent(event); + } else { + event->accept(); - addToHistory(text(), true); + addToHistory(text(), true); - if(idx > 0) { - idx--; - showHistoryEntry(); + if(idx > 0) { + idx--; + showHistoryEntry(); + } } break; case Qt::Key_Down: - event->accept(); - - addToHistory(text(), true); - - if(idx < history.count()) { - idx++; - if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1 - showHistoryEntry(); - else - resetLine(); // equals clear() in this case + if(event->modifiers() & Qt::ShiftModifier) { + QTextEdit::keyPressEvent(event); } else { - addToHistory(text()); - resetLine(); - } + event->accept(); + addToHistory(text(), true); + + if(idx < history.count()) { + idx++; + if(idx < history.count() || tempHistory.contains(idx)) // tempHistory might have an entry for idx == history.count() + 1 + showHistoryEntry(); + else + resetLine(); // equals clear() in this case + } else { + addToHistory(text()); + resetLine(); + } + } break; case Qt::Key_Return: case Qt::Key_Enter: case Qt::Key_Select: - event->accept(); - emit returnPressed(); + if(event->modifiers() & Qt::ShiftModifier) { + QTextEdit::keyPressEvent(event); + } else { + event->accept(); + emit returnPressed(); + } break; default: @@ -179,85 +193,51 @@ bool InputLine::addToHistory(const QString &text, bool temporary) { } void InputLine::on_returnPressed() { - if(!text().isEmpty()) { - addToHistory(text()); - emit sendText(text()); - resetLine(); - } -} - -void InputLine::on_textChanged(QString newText) { - QStringList lineSeparators; - lineSeparators << QString("\r\n") - << QString('\n') - << QString('\r'); - - QString lineSep; - foreach(QString separator, lineSeparators) { - if(newText.contains(separator)) { - lineSep = separator; - break; - } - } - - if(lineSep.isEmpty()) - return; - - QStringList lines = newText.split(lineSep, QString::SkipEmptyParts); - - if(lines.count() >= 4) { - QString msg = tr("Do you really want to paste %n lines?", "", lines.count()); - msg += "

"; - for(int i = 0; i < 3; i++) { - msg += lines[i].left(40); - if(lines[i].count() > 40) - 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); // Qt::Sheet is not ignored on other platforms as it should :/ -#endif - if(question.exec() == QMessageBox::No) - return; - } + QString newText = text(); + newText.replace(QString("\r\n"), QString('\n')); + newText.replace(QString('\r'), QString('\n')); + addToHistory(newText); + QStringList lines = newText.split(QString('\n'), QString::SkipEmptyParts); foreach(QString line, lines) { if(!line.isEmpty()) { - resetLine(); - insert(line); - emit returnPressed(); + emit sendText(line); } } -// if(newText.contains(lineSep)) { -// clear(); -// QString line = newText.section(lineSep, 0, 0); -// QString remainder = newText.section(lineSep, 1); -// insert(line); -// emit returnPressed(); -// insert(remainder); -// } + resetLine(); +} + +void InputLine::on_textChanged(QString newText) { + newText.replace(QString("\r\n"), QString('\n')); + newText.replace(QString('\r'), QString('\n')); + + QStringList lines = newText.split(QString('\n'), QString::KeepEmptyParts); + + int lineCount = lines.count(); + if(lineCount <= 0) lineCount = 1; + else if(lineCount > 8) lineCount = 8; + + if(lineCount > 1) { + setVerticalScrollBarPolicy(Qt::ScrollBarAsNeeded); + QFontMetrics fm(font()); + setMinimumSize(QSize(0, lineCount * fm.lineSpacing() + 2 * frameWidth() + 2 * leftMargin)); + } else { + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); + setMinimumSize(QSize(0, 0)); + } } 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 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); } -- 1.6.1.9.g97c34