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