|
| 1 | +/* |
| 2 | + * This file is part of Notepad Next. |
| 3 | + * Copyright 2026 Justin Dailey |
| 4 | + * |
| 5 | + * Notepad Next is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * Notepad Next is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with Notepad Next. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "ShiftWheelToHorizontalScrollFilter.h" |
| 20 | + |
| 21 | +#include <QApplication> |
| 22 | +#include <QEvent> |
| 23 | +#include <QWheelEvent> |
| 24 | + |
| 25 | + |
| 26 | +bool isHorizontalWheelEvent(QWheelEvent *event) |
| 27 | +{ |
| 28 | +#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) |
| 29 | + return event->angleDelta().y() == 0; |
| 30 | +#else |
| 31 | + return event->orientation() == Qt::Horizontal; |
| 32 | +#endif |
| 33 | +} |
| 34 | + |
| 35 | +ShiftWheelToHorizontalScrollFilter::ShiftWheelToHorizontalScrollFilter(QObject *parent) |
| 36 | + : QObject(parent) |
| 37 | +{ |
| 38 | +} |
| 39 | + |
| 40 | +bool ShiftWheelToHorizontalScrollFilter::eventFilter(QObject *obj, QEvent *event) |
| 41 | +{ |
| 42 | + if (event->type() != QEvent::Wheel) { |
| 43 | + return QObject::eventFilter(obj, event); |
| 44 | + } |
| 45 | + |
| 46 | + auto *wheelEvent = static_cast<QWheelEvent *>(event); |
| 47 | + |
| 48 | + if (!(wheelEvent->modifiers() & Qt::ShiftModifier) |
| 49 | + || (wheelEvent->modifiers() & Qt::ControlModifier) |
| 50 | + || isHorizontalWheelEvent(wheelEvent)) { |
| 51 | + return QObject::eventFilter(obj, event); |
| 52 | + } |
| 53 | + |
| 54 | + const QPoint angleDelta(wheelEvent->angleDelta().y(), wheelEvent->angleDelta().x()); |
| 55 | + |
| 56 | + const QPoint pixelDelta(wheelEvent->pixelDelta().y(), wheelEvent->pixelDelta().x()); |
| 57 | + |
| 58 | + QWheelEvent horizontalEvent( |
| 59 | + wheelEvent->position(), |
| 60 | + wheelEvent->globalPosition(), |
| 61 | + pixelDelta, |
| 62 | + angleDelta, |
| 63 | + wheelEvent->buttons(), |
| 64 | + wheelEvent->modifiers() & ~Qt::ShiftModifier, |
| 65 | + wheelEvent->phase(), |
| 66 | + wheelEvent->inverted(), |
| 67 | + wheelEvent->source(), |
| 68 | + wheelEvent->pointingDevice()); |
| 69 | + |
| 70 | + QApplication::sendEvent(obj, &horizontalEvent); |
| 71 | + return true; |
| 72 | +} |
0 commit comments