From af52b65d6a6b4fe50deb074474c55817c669556f Mon Sep 17 00:00:00 2001 From: David Schulz Date: Wed, 26 Feb 2020 08:07:28 +0100 Subject: [PATCH 1/2] Editor: Fix self recursive update The update rect should be identical to the calculated line rect when repainting the current line highlight. And the offset must not be calculated into the line rect since we just want to repaint the complete viewport width. Fixes: QTCREATORBUG-23647 Change-Id: I0656f0fee4823c8ff55c70b2a4cd69f0183e141d Reviewed-by: Eike Ziller --- src/plugins/texteditor/texteditor.cpp | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index a48d58d1261..43e125e2fcd 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -4334,16 +4334,12 @@ void TextEditorWidgetPrivate::paintCurrentLineHighlight(const PaintEventData &da QRectF lineRect = data.block.layout()->lineForTextPosition(data.textCursor.positionInBlock()).rect(); lineRect.moveTop(lineRect.top() + blockRect.top()); lineRect.setLeft(0); - lineRect.setRight(data.viewportRect.width() - data.offset.x()); + lineRect.setRight(data.viewportRect.width()); QColor color = m_document->fontSettings().toTextCharFormat(C_CURRENT_LINE).background().color(); // set alpha, otherwise we cannot see block highlighting and find scope underneath color.setAlpha(128); - if (!data.eventRect.contains(lineRect.toRect())) { - QRect updateRect = data.eventRect; - updateRect.setLeft(0); - updateRect.setRight(data.viewportRect.width() - int(data.offset.x())); - q->viewport()->update(updateRect); - } + if (!data.eventRect.contains(lineRect.toRect())) + q->viewport()->update(lineRect.toRect()); painter.fillRect(lineRect, color); } From c912ee6862031b807fc82a76cc5ee95a800eb0bb Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Wed, 26 Feb 2020 13:04:30 +0100 Subject: [PATCH 2/2] TextEditor: Fix scrolling state after splitting And when restoring editors in other ways (like from a session). When splitting, the new editor was always scrolling to make the text cursor visible, even if it wasn't visible in the original. 1. open editor with long contents 2. scroll away from the text cursor, so the line isn't visible 3. split and wonder or 3. close and re-open Qt Creator, and restore the session Our code already does set the state of the new editor to the old one, but that state was destroyed by QPlainTextEdit::showEvent, which scrolls on first show if text was added to the document. So, wrap the showEvent with save/restoreState on first show. Change-Id: I95e0b4e963ebb33d13ce020affaf7fc7f94c9257 Reviewed-by: David Schulz --- src/plugins/texteditor/texteditor.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index 43e125e2fcd..6ff9a526070 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -651,6 +651,7 @@ public: MarginSettings m_marginSettings; // apply when making visible the first time, for the split case bool m_fontSettingsNeedsApply = true; + bool m_wasNotYetShown = true; BehaviorSettings m_behaviorSettings; int extraAreaSelectionAnchorBlockNumber = -1; @@ -7204,10 +7205,20 @@ void TextEditorWidget::encourageApply() void TextEditorWidget::showEvent(QShowEvent* e) { triggerPendingUpdates(); + // QPlainTextEdit::showEvent scrolls to make the cursor visible on first show + // which we don't want, since we restore previous states when + // opening editors, and when splitting/duplicating. + // So restore the previous state after that. + QByteArray state; + if (d->m_wasNotYetShown) + state = saveState(); QPlainTextEdit::showEvent(e); + if (d->m_wasNotYetShown) { + restoreState(state); + d->m_wasNotYetShown = false; + } } - void TextEditorWidgetPrivate::applyFontSettingsDelayed() { m_fontSettingsNeedsApply = true;