QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskEvent.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskEvent.h"
7#include "QskGesture.h"
8
9#include <qevent.h>
10
11#if QT_VERSION >= QT_VERSION_CHECK( 6, 4, 0 )
12 #include "QskPlatform.h"
13 #include <qpa/qplatformtheme.h>
14#endif
15
16static void qskRegisterEventTypes()
17{
18 // We don't ask QEvent::registerEventType for unique ids as it prevents
19 // using them in switch/case statements. To avoid conflicts with other
20 // applications we at least try to reserve as soon as possible, so that
21 // applications behaving better than us does not get them.
22
23 for ( int i = QskEvent::NoEvent; i <= QskEvent::MaxEvent; i++ )
24 {
25 const int id = QEvent::registerEventType( i );
26 Q_ASSERT( id == i );
27 }
28}
29
30Q_CONSTRUCTOR_FUNCTION( qskRegisterEventTypes )
31
32int qskFocusChainIncrement( const QEvent* event )
33{
34 if ( event && event->type() == QEvent::KeyPress )
35 {
36 const auto keyEvent = static_cast< const QKeyEvent* >( event );
37 if ( !( keyEvent->modifiers() & ( Qt::ControlModifier | Qt::AltModifier ) ) )
38 {
39 switch ( keyEvent->key() )
40 {
41 case Qt::Key_Tab:
42 return 1;
43
44 case Qt::Key_Backtab:
45 return -1;
46 }
47 }
48 }
49
50 return 0;
51}
52
53QPointF qskMousePosition( const QMouseEvent* event )
54{
55#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
56 return event->position();
57#else
58 return event->localPos();
59#endif
60}
61
62QPointF qskMouseScenePosition( const QMouseEvent* event )
63{
64#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
65 return event->scenePosition();
66#else
67 return event->windowPos();
68#endif
69}
70
71QPointF qskHoverPosition( const QHoverEvent* event )
72{
73#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
74 return event->position();
75#else
76 return event->posF();
77#endif
78}
79
80#ifndef QT_NO_WHEELEVENT
81
82QPointF qskWheelPosition( const QWheelEvent* event )
83{
84 return event->position();
85}
86
87#endif
88
89#ifndef QT_NO_WHEELEVENT
90
91qreal qskWheelSteps( const QWheelEvent* event )
92{
93 const auto angleDelta = event->angleDelta();
94
95 const qreal delta = angleDelta.y() ? angleDelta.y() : angleDelta.x();
96 return delta / QWheelEvent::DefaultDeltasPerStep;
97}
98
99qreal qskWheelIncrement( const QWheelEvent* event )
100{
101 /*
102 Some controls ( f.e a spin box ) are interpreting the wheel to
103 in/decrease a value. For those the physical direction is not relevant
104 and we might have to invert the y delta.
105 */
106
107 auto angleDelta = event->angleDelta();
108
109 if ( event->inverted() )
110 angleDelta.setY( -angleDelta.y() );
111
112 const qreal delta = angleDelta.y() ? angleDelta.y() : angleDelta.x();
113 return delta / QWheelEvent::DefaultDeltasPerStep;
114}
115
116#endif
117
118bool qskIsStandardKeyInput( const QKeyEvent* event, QKeySequence::StandardKey key )
119{
120#if 1
121 return event->matches( key );
122#else
123 constexpr auto mask = ~( Qt::KeypadModifier | Qt::GroupSwitchModifier );
124
125 // We should route this call through the skin. TODO
126 const auto theme = qskPlatformTheme();
127 const auto bindings = theme->keyBindings( ( event->modifiers() | event->key() ) & mask );
128
129 return bindings.contains( QKeySequence(searchkey) );
130#endif
131}
132
133bool qskIsButtonPressKey( const QKeyEvent* event )
134{
135#if QT_VERSION >= QT_VERSION_CHECK( 6, 4, 0 )
136
137 const auto hint = qskPlatformTheme()->themeHint( QPlatformTheme::ButtonPressKeys );
138
139 const auto keys = hint.value< QList< Qt::Key > >();
140 return keys.contains( static_cast< Qt::Key >( event->key() ) );
141
142#else
143
144 switch( event->key() )
145 {
146 case Qt::Key_Space:
147 case Qt::Key_Enter:
148 case Qt::Key_Return:
149 case Qt::Key_Select:
150 return true;
151 }
152
153 return false;
154#endif
155}
156
157bool qskIsTouchOrMouseEvent( QEvent::Type type )
158{
159 switch ( type )
160 {
161 case QEvent::MouseButtonPress:
162 case QEvent::MouseMove:
163 case QEvent::MouseButtonRelease:
164 case QEvent::MouseButtonDblClick:
165 case QEvent::UngrabMouse:
166
167 case QEvent::TouchBegin:
168 case QEvent::TouchCancel:
169 case QEvent::TouchUpdate:
170 case QEvent::TouchEnd:
171
172 return true;
173
174 default:
175 return false;
176 }
177}
178
179QskEvent::QskEvent( QskEvent::Type type )
180 : QEvent( static_cast< QEvent::Type >( type ) )
181{
182}
183
184#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
185
186QskEvent* QskEvent::clone() const
187{
188 return new QskEvent( *this );
189}
190
191#endif
192// -- QskGeometryChangeEvent
193
194QskGeometryChangeEvent::QskGeometryChangeEvent(
195 const QRectF& rect, const QRectF& oldRect )
196 : QskEvent( QskEvent::GeometryChange )
197 , m_rect( rect )
198 , m_oldRect( oldRect )
199{
200}
201
202QskGeometryChangeEvent* QskGeometryChangeEvent::clone() const
203{
204 return new QskGeometryChangeEvent( *this );
205}
206
207bool QskGeometryChangeEvent::isResized() const
208{
209 return ( m_rect.width() != m_oldRect.width() ) ||
210 ( m_rect.height() != m_oldRect.height() );
211}
212
213bool QskGeometryChangeEvent::isMoved() const
214{
215 return ( m_rect.x() != m_oldRect.x() ) ||
216 ( m_rect.y() != m_oldRect.y() );
217}
218
219// -- QskWindowChangeEvent
220
221QskWindowChangeEvent::QskWindowChangeEvent(
222 QQuickWindow* oldWindow, QQuickWindow* window )
223 : QskEvent( QskEvent::WindowChange )
224 , m_oldWindow( oldWindow )
225 , m_window( window )
226{
227}
228
229QskWindowChangeEvent* QskWindowChangeEvent::clone() const
230{
231 return new QskWindowChangeEvent( *this );
232}
233
234// -- QskPopupEvent
235
236QskPopupEvent::QskPopupEvent( Type type, QskPopup* popup )
237 : QskEvent( type )
238 , m_popup( popup )
239{
240}
241
242QskPopupEvent* QskPopupEvent::clone() const
243{
244 return new QskPopupEvent( *this );
245}
246
247// -- QskGestureEvent
248
249QskGestureEvent::QskGestureEvent( std::shared_ptr< const QskGesture > gesture )
250 : QskEvent( QskEvent::Gesture )
251 , m_gesture( gesture )
252{
253}
254
255QskGestureEvent* QskGestureEvent::clone() const
256{
257 return new QskGestureEvent( *this );
258}
259
260// -- QskAnimatorEvent
261
262QskAnimatorEvent::QskAnimatorEvent( QskAspect aspect, int index, State state )
263 : QskEvent( QskEvent::Animator )
264 , m_aspect( aspect )
265 , m_index( index )
266 , m_state( state )
267{
268}
269
270QskAnimatorEvent* QskAnimatorEvent::clone() const
271{
272 return new QskAnimatorEvent( *this );
273}
274
275// -- QskGestureFilterEvent
276
277QskGestureFilterEvent::QskGestureFilterEvent(
278 const QQuickItem* item, const QEvent* event )
279 : QskEvent( QskEvent::GestureFilter )
280 , m_item( item )
281 , m_event( event )
282 , m_maybeGesture( false )
283{
284}
285
286QskGestureFilterEvent* QskGestureFilterEvent::clone() const
287{
288 return new QskGestureFilterEvent( *this );
289}
Lookup key for a QskSkinHintTable.
Definition QskAspect.h:15