Bug 462864

Summary: Mouse wheel does not work properly in viewer
Product: [Applications] kphotoalbum Reporter: Andreas Schleth <schleth_es>
Component: ViewerAssignee: KPhotoAlbum Bugs <kphotoalbum-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: johannes, tl
Priority: NOR    
Version First Reported In: GIT master   
Target Milestone: ---   
Platform: openSUSE   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description Andreas Schleth 2022-12-10 18:56:21 UTC
SUMMARY
Mouse wheel only goes backwards in time when viewing images in the viewer.

STEPS TO REPRODUCE
1. find a selection of a few images in the thumbnail browser ("kphotoalbum --demo" works fine)
2. double click on one in the middle of the selection to start the viewer
3. try the pg-up/pg-down arrow keys: they work as intended.
4. now, try the mouse wheel: turning forwards and backwards a few clicks

OBSERVED RESULT
5. each turn increment will switch to an older image regardless of direction
6. after you turned long enough you will get stuck on the first image of the series (with the demo this is "new wave") and the mouse wheel will not change anything anymore. (pg-down still works)

EXPECTED RESULT
Mouse wheel forwards should work as pg-up, wheel backwards should work as pg-down.
This bug was introduced somewhere in 5.9 development. In 5.8.1 the function is still OK.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: 
OpenSuse Leap 15.4
KPA: v5.9.1-103-g00d997eb (also in: v5.9.1-102-ga2fa6493)
KDE Frameworks Version 5.90.0 
Qt Version 5.15.2 (built against 5.15.2)
Comment 1 Johannes Zarl-Zierl 2022-12-11 00:04:03 UTC
I can't reproduce this so far. My best guess is that this was introduced by commit 5505780551caee4e0a93d8ae1a7e7bf9fe7cf7c4.

Do you, by any chance, have a mouse wheel that can scroll sideways?
Comment 2 Tobias Leupold 2022-12-11 11:21:28 UTC
I tried this with both my real mouse and my Trackman (trackball with mouse wheel emulation). Both work as expected …
Comment 3 Andreas Schleth 2022-12-11 16:08:14 UTC
Oh my, this is one of those ... 

My mouse is just a regular Logitech 2 buttons and a wheel acting as middle button. 
As I had a dead rodent recently, I even tried another mouse to the same effect (some Chinese brand), same effect.
Same on a different machine (same system, but older cpu) and with a right-handed mouse.

But I will check with my laptop which runs Ubuntu...
... well it runs 20.04 ... which is too old for the current build system (again!).

So I will report back when I have updated that machine to something newer (hoping that the update will not break all the Tuxedo drivers).
Comment 4 Andreas Schleth 2022-12-11 17:45:00 UTC
Is there newer info which packages to install to build with Ubuntu?

The data in https://community.kde.org/KPhotoAlbum/build_instructions are a bit dated and do not work for me.
Comment 5 Andreas Schleth 2022-12-11 18:07:52 UTC
OK giyf... I found the missing package, updated the install instructions :-) and built the thing on Ubuntu 22.04 and ...

... it works as expected (by the devs, not by me :-). The mouse wheel works forwards and backwards.

Thus, this is a bug not to be found with Ubuntu 22.04 but on openSuse Leap 15.4.
My Ubuntu has KDE 5.92.0 Qt 5.15.3 and KPA is at 5.9.1-104-... Kernel is 5.15.0
My Suse is at KDE 5.90.0 Qt 5.15.2 Kernel 5.14.12 - so just a tiny bit behind on everything.

Perhaps I need to find the commit that changed the behaviour... so I am going to brush up my git know-how.
Comment 6 Andreas Schleth 2022-12-11 19:23:26 UTC
So, I did some git bisect starting with a good version from April 2022 and the bug first appears in:

5505780551caee4e0a93d8ae1a7e7bf9fe7cf7c4 is the first bad commit
commit 5505780551caee4e0a93d8ae1a7e7bf9fe7cf7c4
Author: Johannes Zarl-Zierl <johannes@zarl-zierl.at>
Date:   Mon Oct 3 23:46:01 2022 +0200

    WIP: Port away from deprecated Qt APIs.
    
    This commit is marked work in progress because I have disabled two
    deprecation warnings for QMap::insertMulti so that I can enable
    QT_DISABLE_DEPRECATED_BEFORE for Qt 5.15.

Does this help?
Comment 7 Johannes Zarl-Zierl 2022-12-11 21:50:25 UTC
Hi Andreas,

Am Sonntag, 11. Dezember 2022, 20:23:26 CET schrieben Sie:
> So, I did some git bisect starting with a good version from April 2022 and
> the bug first appears in:
> 
> 5505780551caee4e0a93d8ae1a7e7bf9fe7cf7c4 is the first bad commit

Ok, so my hunch was correct. 

> Does this help?

I'm not sure. The porting instructions from the old to the new API are a bit 
sparse on the details.
My working assumption to the bug is that on this particular system your mouse 
wheel for some reason is emitting a horizontal scroll instead of a vertical 
scroll (the old API did not differentiate between the two).

For the viewerwidget it makes sense to allow horizontal scrolling anyways, so 
I'll just add that.

Please keep an eye out for similar problems with any other widgets in 
KPhtotoAlbum on your OpenSuse systems...

Cheers,
  Johannes

P.S.: Btw. thanks for updating the wiki page! You really know how to make a 
dev's day better ;-)
Comment 8 Johannes Zarl-Zierl 2022-12-11 21:54:15 UTC
Git commit a833229b0c04516067bc6e490fbca4d7c370f0c7 by Johannes Zarl-Zierl.
Committed on 11/12/2022 at 21:52.
Pushed by johanneszarl into branch 'master'.

Allow horizontal scrolling in ViewerWidget

M  +1    -1    Viewer/ViewerWidget.cpp

https://invent.kde.org/graphics/kphotoalbum/commit/a833229b0c04516067bc6e490fbca4d7c370f0c7
Comment 9 Johannes Zarl-Zierl 2022-12-11 22:09:09 UTC
Git commit d908ac10be06b629ad63902971994f876dbc86cf by Johannes Zarl-Zierl.
Committed on 11/12/2022 at 22:06.
Pushed by johanneszarl into branch 'master'.

Properly allow horizontal scrolling in ViewerWidget

Improving upon a833229b, I took a look at the implementation of the
deprecated method QWheelEvent::delta() and used the same logic to
determine scroll orientation.

M  +3    -1    Viewer/ViewerWidget.cpp

https://invent.kde.org/graphics/kphotoalbum/commit/d908ac10be06b629ad63902971994f876dbc86cf
Comment 10 Andreas Schleth 2022-12-11 23:13:39 UTC
Hi Johannes,

unfortunately the bug is still there in v5.9.1-106-gd908ac10 ...
But at least we know where to look for it.

So I added some diagnostic print:

    std::cout << "check deltax" << pxDelta.x() << "   deltay" << pxDelta.y() << "\n";

Which recorded on one forward and backward increment:

 check deltax0   deltay0
 check deltax0   deltay0

The effect on screen was the older next picture for each event. 
Then I did some digging in the docs and found in https://doc.qt.io/qt-5/qwheelevent.html :
"There are two ways to read the wheel event delta: angleDelta() returns the deltas in wheel degrees. These values are always provided. pixelDelta() returns the deltas in screen pixels, and is available on platforms that have high-resolution trackpads, such as macOS."

Improving my test output to:
    const auto anDelta = event->angleDelta();
    std::cout << "check deltax " << pxDelta.x() << "   deltay " << pxDelta.y() << "\n";
    std::cout << "check anglex " << anDelta.x() << "   angley " << anDelta.y() << "\n";
produced:
check deltax 0   deltay 0
check anglex 0   angley 120
check deltax 0   deltay 0
check anglex 0   angley -120

So, my suggestion would be to use the angleDelta instead of the pixelDelta like this:
...
    const auto anDelta = event->angleDelta();
    const bool isHorizontal = (qAbs(anDelta.x()) > qAbs(anDelta.y()));
    if ((!isHorizontal && anDelta.y() < 0) || (isHorizontal && anDelta.x() < 0)) {
...
I did the same test output on the Ubuntu machine: there the pixelDelta showed "120" / "-120" - the same as angleDelta on my openSuse.
So we might assume, that the Qt-guys did change something in between the last minor releases.

The suggested fix works on both my platforms :-)
Comment 11 Johannes Zarl-Zierl 2022-12-11 23:40:05 UTC
Thanks for the analysis! The suggested fix does not break anything on my end either...
Comment 12 Johannes Zarl-Zierl 2022-12-11 23:40:18 UTC
Git commit 12bd7cc6f9af57a4a85d0eab86e734657938377e by Johannes Zarl-Zierl.
Committed on 11/12/2022 at 23:36.
Pushed by johanneszarl into branch 'master'.

Always use angle instead of pixel scrolling for wheel events

On OpenSuse apparently sometimes QWheelEvent::pixelDelta() is zero, but
QWheelEvent::angleDelta() is not.

M  +3    -3    DateBar/DateBarWidget.cpp
M  +3    -3    Viewer/ViewerWidget.cpp

https://invent.kde.org/graphics/kphotoalbum/commit/12bd7cc6f9af57a4a85d0eab86e734657938377e
Comment 13 Andreas Schleth 2022-12-12 00:21:56 UTC
How nice!
v5.9.1-109-g12bd7cc6 works for me (TM)