diff options
author | Eike Ziller <eike.ziller@qt.io> | 2020-03-16 10:39:30 +0100 |
---|---|---|
committer | Eike Ziller <eike.ziller@qt.io> | 2020-03-16 14:48:36 +0000 |
commit | ba4a604d687dc45f3cc97f2a32e59516a9343871 (patch) | |
tree | 9921439f82be6bb9fba2fd919276b5c8f0897eb0 | |
parent | 2c3f31bc93e7d6bb27eac73264d0940f16012d41 (diff) |
Editors: Fix top/bottom split with cursor visible at end of document
It did not center on the text cursor.
- Fix lastVisibleBlockNumber() to return the last block if the editor is
scrolled to the bottom (and there potentially is no block located at the
very bottom edge of the editor widget).
- Fix comparison of block numbers (0-based) with line numbers (1-based)
Change-Id: I21405443bea3533e393a7cf320ded6d47f647949
Reviewed-by: David Schulz <david.schulz@qt.io>
-rw-r--r-- | src/plugins/texteditor/texteditor.cpp | 23 |
1 files changed, 15 insertions, 8 deletions
diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index a38fab49b0..b2f2bacb5f 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -3006,15 +3006,19 @@ bool TextEditorWidget::restoreState(const QByteArray &state) horizontalScrollBar()->setValue(hval); if (version >= 2) { - int firstBlock, lastBlock; - stream >> firstBlock; - stream >> lastBlock; + int originalFirstBlock, originalLastBlock; + stream >> originalFirstBlock; + stream >> originalLastBlock; // If current line was visible in the old state, make sure it is visible in the new state. // This can happen if the height of the editor changed in the meantime - if (firstBlock <= lineVal && lineVal <= lastBlock - && (lineVal < firstVisibleBlockNumber() || lastVisibleBlockNumber() <= lineVal)) { + const int lineBlock = lineVal - 1; // line is 1-based, blocks are 0-based + const bool originalCursorVisible = (originalFirstBlock <= lineBlock + && lineBlock <= originalLastBlock); + const int firstBlock = firstVisibleBlockNumber(); + const int lastBlock = lastVisibleBlockNumber(); + const bool cursorVisible = (firstBlock <= lineBlock && lineBlock <= lastBlock); + if (originalCursorVisible && !cursorVisible) centerCursor(); - } } d->saveCurrentCursorPositionForNavigation(); @@ -8456,8 +8460,11 @@ int TextEditorWidget::firstVisibleBlockNumber() const int TextEditorWidget::lastVisibleBlockNumber() const { QTextBlock block = blockForVerticalOffset(viewport()->height() - 1); - if (!block.isValid()) - block.previous(); + if (!block.isValid()) { + block = document()->lastBlock(); + while (block.isValid() && !block.isVisible()) + block = block.previous(); + } return block.isValid() ? block.blockNumber() : -1; } |