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
193// -- QskGeometryChangeEvent
194
195QskGeometryChangeEvent::QskGeometryChangeEvent(
196 const QRectF& rect, const QRectF& oldRect )
197 : QskEvent( QskEvent::GeometryChange )
198 , m_rect( rect )
199 , m_oldRect( oldRect )
200{
201}
202
203QskGeometryChangeEvent* QskGeometryChangeEvent::clone() const
204{
205 return new QskGeometryChangeEvent( *this );
206}
207
208bool QskGeometryChangeEvent::isResized() const
209{
210 return ( m_rect.width() != m_oldRect.width() ) ||
211 ( m_rect.height() != m_oldRect.height() );
212}
213
214bool QskGeometryChangeEvent::isMoved() const
215{
216 return ( m_rect.x() != m_oldRect.x() ) ||
217 ( m_rect.y() != m_oldRect.y() );
218}
219
220// -- QskViewportChangeEvent
221
222QskViewportChangeEvent::QskViewportChangeEvent( QQuickItem* modifiedParent )
223 : QskEvent( QskEvent::ViewportChange )
224 , m_modifiedParent( modifiedParent )
225{
226}
227
228QskViewportChangeEvent* QskViewportChangeEvent::clone() const
229{
230 return new QskViewportChangeEvent( *this );
231}
232
233// -- QskWindowChangeEvent
234
235QskWindowChangeEvent::QskWindowChangeEvent(
236 QQuickWindow* oldWindow, QQuickWindow* window )
237 : QskEvent( QskEvent::WindowChange )
238 , m_oldWindow( oldWindow )
239 , m_window( window )
240{
241}
242
243QskWindowChangeEvent* QskWindowChangeEvent::clone() const
244{
245 return new QskWindowChangeEvent( *this );
246}
247
248// -- QskPopupEvent
249
250QskPopupEvent::QskPopupEvent( Type type, QskPopup* popup )
251 : QskEvent( type )
252 , m_popup( popup )
253{
254}
255
256QskPopupEvent* QskPopupEvent::clone() const
257{
258 return new QskPopupEvent( *this );
259}
260
261// -- QskGestureEvent
262
263QskGestureEvent::QskGestureEvent( std::shared_ptr< const QskGesture > gesture )
264 : QskEvent( QskEvent::Gesture )
265 , m_gesture( gesture )
266{
267}
268
269QskGestureEvent* QskGestureEvent::clone() const
270{
271 return new QskGestureEvent( *this );
272}
273
274// -- QskAnimatorEvent
275
276QskAnimatorEvent::QskAnimatorEvent( QskAspect aspect, int index, State state )
277 : QskEvent( QskEvent::Animator )
278 , m_aspect( aspect )
279 , m_index( index )
280 , m_state( state )
281{
282}
283
284QskAnimatorEvent* QskAnimatorEvent::clone() const
285{
286 return new QskAnimatorEvent( *this );
287}
288
289// -- QskGestureFilterEvent
290
291QskGestureFilterEvent::QskGestureFilterEvent(
292 const QQuickItem* item, const QEvent* event )
293 : QskEvent( QskEvent::GestureFilter )
294 , m_item( item )
295 , m_event( event )
296 , m_maybeGesture( false )
297{
298}
299
300QskGestureFilterEvent* QskGestureFilterEvent::clone() const
301{
302 return new QskGestureFilterEvent( *this );
303}
Lookup key for a QskSkinHintTable.
Definition QskAspect.h:15