KWin
Loading...
Searching...
No Matches
shakedetector.cpp
Go to the documentation of this file.
1/*
2 SPDX-FileCopyrightText: 2023 Vlad Zahorodnii <vlad.zahorodnii@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-or-later
5*/
6
7#include "shakedetector.h"
8
9#include <cmath>
10
14
16{
17 return m_interval;
18}
19
20void ShakeDetector::setInterval(quint64 interval)
21{
22 m_interval = interval;
23}
24
26{
27 return m_sensitivity;
28}
29
30void ShakeDetector::setSensitivity(qreal sensitivity)
31{
32 m_sensitivity = sensitivity;
33}
34
35std::optional<qreal> ShakeDetector::update(QMouseEvent *event)
36{
37 // Prune the old entries in the history.
38 auto it = m_history.begin();
39 for (; it != m_history.end(); ++it) {
40 if (event->timestamp() - it->timestamp < m_interval) {
41 break;
42 }
43 }
44 if (it != m_history.begin()) {
45 m_history.erase(m_history.begin(), it);
46 }
47
48 m_history.emplace_back(HistoryItem{
49 .position = event->localPos(),
50 .timestamp = event->timestamp(),
51 });
52
53 qreal left = m_history[0].position.x();
54 qreal top = m_history[0].position.y();
55 qreal right = m_history[0].position.x();
56 qreal bottom = m_history[0].position.y();
57 qreal distance = 0;
58
59 for (size_t i = 1; i < m_history.size(); ++i) {
60 // Compute the length of the mouse path.
61 const qreal deltaX = m_history.at(i).position.x() - m_history.at(i - 1).position.x();
62 const qreal deltaY = m_history.at(i).position.y() - m_history.at(i - 1).position.y();
63 distance += std::sqrt(deltaX * deltaX + deltaY * deltaY);
64
65 // Compute the bounds of the mouse path.
66 left = std::min(left, m_history.at(i).position.x());
67 top = std::min(top, m_history.at(i).position.y());
68 right = std::max(right, m_history.at(i).position.x());
69 bottom = std::max(bottom, m_history.at(i).position.y());
70 }
71
72 const qreal boundsWidth = right - left;
73 const qreal boundsHeight = bottom - top;
74 const qreal diagonal = std::sqrt(boundsWidth * boundsWidth + boundsHeight * boundsHeight);
75 if (diagonal < 100) {
76 return std::nullopt;
77 }
78
79 const qreal shakeFactor = distance / diagonal;
80 if (shakeFactor > m_sensitivity) {
81 return shakeFactor - m_sensitivity;
82 }
83
84 return std::nullopt;
85}
void setSensitivity(qreal sensitivity)
qreal sensitivity() const
std::optional< qreal > update(QMouseEvent *event)
quint64 interval() const
void setInterval(quint64 interval)