From cb6f0dfb7a08bf05db2d613e356b9c923e6488b3 Mon Sep 17 00:00:00 2001 From: mae <qt-info@nokia.com> Date: Wed, 16 Sep 2009 12:13:15 +0200 Subject: [PATCH] Re-fix output window. Scrolling was messed up when long paragraphs with line wrap were appended, also appendOutput and appendOutputInline ended up doing almost the same thing. It might be that appendOutput is called in some places where appendOutputInline would be more adequate. The code now tries to called QPlainTextEdit::appendPlainText() whenever it can because of its update optimizations when it comes to enforcing maximum block count. We want application output to be processed fast, even if the maximum block count was reached! --- src/plugins/projectexplorer/outputwindow.cpp | 55 +++++++++++--------- src/plugins/projectexplorer/outputwindow.h | 1 + 2 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/plugins/projectexplorer/outputwindow.cpp b/src/plugins/projectexplorer/outputwindow.cpp index 0efdab5ee44..0c29756f370 100644 --- a/src/plugins/projectexplorer/outputwindow.cpp +++ b/src/plugins/projectexplorer/outputwindow.cpp @@ -340,6 +340,8 @@ bool OutputPane::canNavigate() OutputWindow::OutputWindow(QWidget *parent) : QPlainTextEdit(parent) { + m_enforceNewline = false; + setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); //setCenterOnScroll(false); setWindowTitle(tr("Application Output Window")); @@ -392,14 +394,13 @@ OutputWindow::~OutputWindow() void OutputWindow::appendOutput(const QString &out) { + QString s = out; + m_enforceNewline = true; // make appendOutputInline put in a newline next time + if (s.endsWith(QLatin1Char('\n'))) { + s.chop(1); + } setMaximumBlockCount(MaxBlockCount); - moveCursor(QTextCursor::End); - if (out.endsWith('\n')) - insertPlainText(out.right(out.length()-1)); - else - insertPlainText(out); - // insert newline and get automatic scroll behavior - appendPlainText(""); // makes sure that there's an newline in front + appendPlainText(out); enableUndoRedo(); } @@ -408,26 +409,32 @@ void OutputWindow::appendOutputInline(const QString &out) { setMaximumBlockCount(MaxBlockCount); - int newline = out.indexOf(QLatin1Char('\n')); - if (newline < 0) { + int newline = -1; + bool enforceNewline = m_enforceNewline; + m_enforceNewline = false; + + if (!enforceNewline) { + newline = out.indexOf(QLatin1Char('\n')); moveCursor(QTextCursor::End); - insertPlainText(out); // doesn't insert additional '\n' like appendPlainText - }else{ - int lastnewline = out.lastIndexOf(QLatin1Char('\n')); - // make sure that we use appendPlainText to add the last newline - // in the string, so we get automatic scrolling - // and work around the fact that appendPlainText also ensures - // a newline in front of the appended text - if (lastnewline > 0) { - moveCursor(QTextCursor::End); - insertPlainText(out.left(lastnewline)); - } - appendPlainText(""); // add the newline - if (lastnewline < out.length()-1) { // newline is not last character - moveCursor(QTextCursor::End); - insertPlainText(out.mid(lastnewline+1)); + bool atBottom = (blockBoundingRect(document()->lastBlock()).bottom() + contentOffset().y() + <= viewport()->rect().bottom()); + insertPlainText(newline < 0 ? out : out.left(newline)); // doesn't enforce new paragraph like appendPlainText + if (atBottom) + verticalScrollBar()->setValue(verticalScrollBar()->maximum()); + } + + QString s = out.mid(newline+1); + if (s.isEmpty()) { + m_enforceNewline = true; + } else { + if (s.endsWith(QLatin1Char('\n'))) { + m_enforceNewline = true; + qDebug() << "CHOP"; + s.chop(1); } + appendPlainText(s); } + enableUndoRedo(); } diff --git a/src/plugins/projectexplorer/outputwindow.h b/src/plugins/projectexplorer/outputwindow.h index 76657d4843f..6820f5076e0 100644 --- a/src/plugins/projectexplorer/outputwindow.h +++ b/src/plugins/projectexplorer/outputwindow.h @@ -129,6 +129,7 @@ public: private: Core::BaseContext *m_outputWindowContext; void enableUndoRedo(); + bool m_enforceNewline; }; #if 0 -- GitLab