QmlMaterial 0.1.0
Loading...
Searching...
No Matches
scroll_wheel_p.hpp
1#pragma once
2
3#include <QByteArray>
4#include <QPointF>
5#include <QWheelEvent>
6#include <optional>
7
8namespace qml_material
9{
10enum class WheelDistanceMode
11{
12 Platform,
13 Steps
14};
15
16inline std::optional<WheelDistanceMode> parseWheelDistanceMode(QByteArray value) {
17 value = value.trimmed().toLower();
18 if (value.isEmpty() || value == "steps") return WheelDistanceMode::Steps;
19 if (value == "platform") return WheelDistanceMode::Platform;
20 return {};
21}
22
23struct WheelInput {
24 QPointF delta;
25 bool smooth;
26};
27
28inline WheelInput wheelInput(const QWheelEvent& event, WheelDistanceMode mode, int scrollLines) {
29 // Ordinary wheels can report pixels too; phases select direct input.
30 const bool smooth = event.phase() == Qt::NoScrollPhase;
31 const QPointF pixels = -QPointF(event.pixelDelta());
32 if (! pixels.isNull() && (! smooth || mode == WheelDistanceMode::Platform))
33 return { pixels, smooth };
34 return { -QPointF(event.angleDelta()) / 120 * (24.0 * scrollLines), smooth };
35}
36} // namespace qml_material