Bug 138956 - KDE 5: low level API to control scrolling
Summary: KDE 5: low level API to control scrolling
Status: RESOLVED FIXED
Alias: None
Product: frameworks-ktexteditor
Classification: Frameworks and Libraries
Component: general (other bugs)
Version First Reported In: unspecified
Platform: Compiled Sources Linux
: NOR wishlist
Target Milestone: ---
Assignee: KWrite Developers
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-12-18 12:09 UTC by Thomas Friedrichsmeier
Modified: 2022-01-12 15:19 UTC (History)
3 users (show)

See Also:
Latest Commit:
Version Fixed/Implemented In:
Sentry Crash Report:


Attachments
Untested patch (2.13 KB, patch)
2011-09-22 09:19 UTC, Thomas Friedrichsmeier
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Friedrichsmeier 2006-12-18 12:09:47 UTC
Version:            (using KDE Devel)
Installed from:    Compiled sources

Summary:
- API to get the current x/y scrolling position of the view (in pixels)
- API to set a new x/y scrolling position of the view (in pixels)
- and / or, API to set cursor position without scrolling
- probably something for KDE4

Rationale:
I wrote a mail about this to kwrite-devel a while ago (http://lists.kde.org/?l=kwrite-devel&m=116109849704307&w=2). Copying the contents to this bug report:

In RKWard, we're (mis-)using a KatePart to provide a console emulation with 
syntax highlighting (R language). One thing we'd like to do in this scenario 
is to prevent the cursor from ever leaving the bottom-most line. We can catch 
all KeyUp-presses, but for mouse presses things are a little more 
complicated. After all, the user should be able to select text anywhere in 
the console, only the cursor should not move in this case.

What I tried to do was to simply make sure the cursor is set back to the last 
line after each mouse-button release (using setCursorPosition ()). This works 
in principle, but has the side effect of always scrolling back down to the 
last line, even if (e.g.) some text was selected near the top of the 
document.

Is there a way to set the cursor position without scrolling the view? 
Alternatively, is there a way to determine the current scrolling position of 
a Kate::View, so I can save that position before moving the cursor, and 
restore it afterwards?
Comment 1 Dominik Haumann 2011-09-19 19:03:45 UTC
Thomas, if you still need that, it should be done for KDE 5. What functions do you need exaclty?
KTextEditor::View::scrollUp(), pageUp(), toTop() or something like that?
Comment 2 Thomas Friedrichsmeier 2011-09-20 07:41:39 UTC
Hi Dominik,

this is not a high priority, but yes, this would still be nice to have. Basically, the use case that I have, goes like this:

   QPoint saved_pos = KTextEditor::View::currentScrollPosition();
   // do something which may affect scrolling position, implicitly
   KTextEditor::View::setScrollPosition(saved_pos);

This is assuming pixel based positions.

However, thinking about it, scrolling could also be based on the cursor position (at the top-left corner of the viewport). This would be almost as good for my use-case, and would conceivably come in handy in many other situations, as well. I.e.:

/** Return the cursor position at the top left of the view. */
KTextEditor::Cursor KTextEditor::View::currentScrollPosition();
/** Scroll the view so that pos is visible at the top left corner of the view, without changing the active cursor position.
@note: If the given position is near the bottom / right of the document, the actual scrolling position may be adjusted to the top / left. However, it is guaranteed that the given cursor position will be inside the visible range, after scrolling. */
KTextEditor::View::setScrollPosition(const KTextEditor::Cursor &pos);
Comment 3 Dominik Haumann 2011-09-20 16:49:12 UTC
How to interpret columns in the position is clear for fixed fonts, but not for non-fixed fonts.

If it's only about lines, simple ints for the lines would be enough. 

If you build Kate according to http://www.kate-editor.org/get-it/, you can run your RKWard with ./run.sh rkward (or the correct app name), and the new Kate Part will be loaded.

This way, you can very easily provide a patch that works for you, if you want.
Comment 4 Thomas Friedrichsmeier 2011-09-22 09:19:54 UTC
Created attachment 63849 [details]
Untested patch

At this moment, kate git does not compile for me due to an unrelated problem. Therefore, the patch I'm attaching is entirely untested. But if my guesses aren't entirely wrong, then it's actually quite simple, since KateViewInternal already provides this functionality.

Pulling KateViewInternal::makeVisible() into the public API (perhaps as KTextEditor::View::ensurePositionVisible()) might also be worth while.
Comment 5 Christoph Cullmann 2014-09-14 10:53:40 UTC
In KF5 we can very easy add that functions, if they really do the job, can somebody test that?
Comment 6 Lothar 2019-03-13 20:41:26 UTC
From the initial posting, I guess this is related to BUG: 399014 BUG: 306745 or better, it may fixed by the Patch https://phabricator.kde.org/D17857
Comment 7 Thomas Friedrichsmeier 2019-03-13 21:22:05 UTC
Frankly, I'm not sure, I understand that PR.

However, to clarify, my (somewhat esoteric) usecase:
1) User selects something near the top of the document, implicitly changing cursor position.
2) I need to set the cursor back to the very end of the document (not editing anything, though).

I would like to have a way to accomplish step 2 *without* apparent scrolling, i.e. the view should remain scrolled where ever it was scrolled to by the user. It should not jump to make last line visible.
Comment 8 Lothar 2019-03-14 06:23:47 UTC
OK. Then I may misunderstood something. 

You mentioned "using setCursorPosition()" and I had a change noted there https://phabricator.kde.org/D17857#425268
Not sure that this effect your wish. Can you try it?

Since 2006 there may have many thing changed. Perhaps is something new in stock to solve you problem(?)
Comment 9 Waqar Ahmed 2022-01-12 15:19:52 UTC
Since the request is about "low-level" api, please have a look at the class "KateScrollBarRestorer" in the patch here: https://invent.kde.org/utilities/kate/-/merge_requests/556