comparison widgets/WheelCounter.h @ 1303:13f5f84fbfad

Collect the bits of bookkeeping for mouse wheel events, and use in all widgets
author Chris Cannam
date Fri, 22 Jun 2018 17:19:48 +0100
parents
children
comparison
equal deleted inserted replaced
1302:f3d3fab250ac 1303:13f5f84fbfad
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
2
3 /*
4 Sonic Visualiser
5 An audio file viewer and annotation editor.
6 Centre for Digital Music, Queen Mary, University of London.
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef SV_WHEEL_COUNTER_H
16 #define SV_WHEEL_COUNTER_H
17
18 #include <QWheelEvent>
19
20 /**
21 * Manage the little bit of tedious book-keeping associated with
22 * translating vertical wheel events into up/down notch counts
23 */
24 class WheelCounter
25 {
26 public:
27 WheelCounter() : m_pendingWheelAngle(0) { }
28
29 ~WheelCounter() { }
30
31 int count(QWheelEvent *e) {
32
33 e->accept();
34
35 int delta = e->angleDelta().y();
36 if (delta == 0) {
37 return 0;
38 }
39
40 if (e->phase() == Qt::ScrollBegin ||
41 std::abs(delta) >= 120 ||
42 (delta > 0 && m_pendingWheelAngle < 0) ||
43 (delta < 0 && m_pendingWheelAngle > 0)) {
44 m_pendingWheelAngle = delta;
45 } else {
46 m_pendingWheelAngle += delta;
47 }
48
49 if (abs(m_pendingWheelAngle) >= 600) {
50 // Sometimes on Linux we're seeing absurdly extreme angles
51 // on the first wheel event -- discard those entirely
52 m_pendingWheelAngle = 0;
53 return 0;
54 }
55
56 int count = m_pendingWheelAngle / 120;
57 m_pendingWheelAngle -= count * 120;
58 return count;
59 }
60
61 private:
62 int m_pendingWheelAngle;
63 };
64
65 #endif