qskinny/src/controls/QskEvent.cpp

257 lines
5.9 KiB
C++
Raw Normal View History

2018-08-03 08:15:28 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
2017-07-21 18:21:34 +02:00
#include "QskEvent.h"
#include "QskGesture.h"
2018-07-19 14:10:48 +02:00
#include <qevent.h>
2017-07-21 18:21:34 +02:00
2023-03-06 12:45:17 +01:00
#if QT_VERSION >= QT_VERSION_CHECK( 6, 4, 0 )
QSK_QT_PRIVATE_BEGIN
#include <private/qguiapplication_p.h>
QSK_QT_PRIVATE_END
#include <qpa/qplatformtheme.h>
#endif
2017-07-21 18:21:34 +02:00
static void qskRegisterEventTypes()
{
// We don't ask QEvent::registerEventType for unique ids as it prevents
// using them in switch/case statements. To avoid conflicts with other
// applications we at least try to reserve as soon as possible, so that
// applications behaving better than us does not get them.
for ( int i = QskEvent::NoEvent; i <= QskEvent::MaxEvent; i++ )
{
const int id = QEvent::registerEventType( i );
Q_ASSERT( id == i );
}
}
Q_CONSTRUCTOR_FUNCTION( qskRegisterEventTypes )
2020-07-27 18:03:02 +02:00
int qskFocusChainIncrement( const QEvent* event )
2018-02-06 14:57:34 +01:00
{
if ( event && event->type() == QEvent::KeyPress )
{
const auto keyEvent = static_cast< const QKeyEvent* >( event );
if ( !( keyEvent->modifiers() & ( Qt::ControlModifier | Qt::AltModifier ) ) )
{
2018-08-03 08:15:28 +02:00
switch ( keyEvent->key() )
2018-02-06 14:57:34 +01:00
{
case Qt::Key_Tab:
return 1;
case Qt::Key_Backtab:
return -1;
}
}
}
return 0;
}
QPointF qskMousePosition( const QMouseEvent* event )
{
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
return event->position();
#else
return event->localPos();
#endif
}
QPointF qskMouseScenePosition( const QMouseEvent* event )
{
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
return event->scenePosition();
#else
return event->windowPos();
#endif
}
2022-01-05 11:59:32 +01:00
QPointF qskHoverPosition( const QHoverEvent* event )
2020-10-25 17:35:50 +01:00
{
2022-01-05 11:59:32 +01:00
#if QT_VERSION >= QT_VERSION_CHECK( 6, 0, 0 )
2020-10-25 17:35:50 +01:00
return event->position();
#else
return event->posF();
#endif
}
2022-01-05 11:59:32 +01:00
#ifndef QT_NO_WHEELEVENT
QPointF qskWheelPosition( const QWheelEvent* event )
2020-10-25 17:35:50 +01:00
{
return event->position();
}
2022-01-05 11:59:32 +01:00
#endif
#ifndef QT_NO_WHEELEVENT
2022-01-10 14:59:22 +01:00
qreal qskWheelSteps( const QWheelEvent* event )
2022-01-05 11:59:32 +01:00
{
2022-01-10 14:59:22 +01:00
const auto angleDelta = event->angleDelta();
2022-01-05 11:59:32 +01:00
2022-01-10 14:59:22 +01:00
const qreal delta = angleDelta.y() ? angleDelta.y() : angleDelta.x();
2022-03-23 11:54:34 +01:00
return delta / QWheelEvent::DefaultDeltasPerStep;
2022-01-05 11:59:32 +01:00
}
2022-01-10 14:59:22 +01:00
qreal qskWheelIncrement( const QWheelEvent* event )
2022-01-05 11:59:32 +01:00
{
2022-01-10 14:59:22 +01:00
/*
Some controls ( f.e a spin box ) are interpreting the wheel to
in/decrease a value. For those the physical direction is not relevant
and we might have to invert the y delta.
*/
2022-01-05 11:59:32 +01:00
2022-01-10 14:59:22 +01:00
auto angleDelta = event->angleDelta();
2022-01-05 11:59:32 +01:00
2022-01-10 14:59:22 +01:00
if ( event->inverted() )
angleDelta.setY( -angleDelta.y() );
2022-01-05 11:59:32 +01:00
2022-01-10 14:59:22 +01:00
const qreal delta = angleDelta.y() ? angleDelta.y() : angleDelta.x();
2022-03-23 11:54:34 +01:00
return delta / QWheelEvent::DefaultDeltasPerStep;
2022-01-05 11:59:32 +01:00
}
#endif
2022-03-11 14:24:10 +01:00
bool qskIsStandardKeyInput( const QKeyEvent* event, QKeySequence::StandardKey key )
{
#if 1
return event->matches( key );
#else
2022-03-23 11:54:34 +01:00
constexpr auto mask = ~( Qt::KeypadModifier | Qt::GroupSwitchModifier );
// We should route this call through the skin. TODO
const auto theme = QGuiApplicationPrivate::platformTheme();
const auto bindings = theme->keyBindings( ( event->modifiers() | event->key() ) & mask );
return bindings.contains( QKeySequence(searchkey) );
#endif
}
bool qskIsButtonPressKey( const QKeyEvent* event )
{
2023-03-06 12:45:17 +01:00
#if QT_VERSION >= QT_VERSION_CHECK( 6, 4, 0 )
const auto hint = QGuiApplicationPrivate::platformTheme()->themeHint(
QPlatformTheme::ButtonPressKeys );
const auto keys = hint.value< QList< Qt::Key > >();
return keys.contains( static_cast< Qt::Key >( event->key() ) );
#else
switch( event->key() )
{
case Qt::Key_Space:
case Qt::Key_Enter:
case Qt::Key_Return:
case Qt::Key_Select:
return true;
}
return false;
#endif
}
2018-08-03 08:15:28 +02:00
QskEvent::QskEvent( QskEvent::Type type )
: QEvent( static_cast< QEvent::Type >( type ) )
2017-07-21 18:21:34 +02:00
{
}
2020-12-09 14:56:10 +01:00
#if QT_VERSION < QT_VERSION_CHECK( 6, 0, 0 )
QskEvent* QskEvent::clone() const
{
return new QskEvent( *this );
}
#endif
2017-07-21 18:21:34 +02:00
// -- QskGeometryChangeEvent
QskGeometryChangeEvent::QskGeometryChangeEvent(
2018-08-03 08:15:28 +02:00
const QRectF& rect, const QRectF& oldRect )
: QskEvent( QskEvent::GeometryChange )
, m_rect( rect )
, m_oldRect( oldRect )
2017-07-21 18:21:34 +02:00
{
}
2020-12-09 14:56:10 +01:00
QskGeometryChangeEvent* QskGeometryChangeEvent::clone() const
{
return new QskGeometryChangeEvent( *this );
}
2017-07-21 18:21:34 +02:00
bool QskGeometryChangeEvent::isResized() const
{
return ( m_rect.width() != m_oldRect.width() ) ||
( m_rect.height() != m_oldRect.height() );
}
bool QskGeometryChangeEvent::isMoved() const
{
return ( m_rect.x() != m_oldRect.x() ) ||
( m_rect.y() != m_oldRect.y() );
}
2017-12-22 14:15:24 +01:00
// -- QskWindowChangeEvent
2017-07-21 18:21:34 +02:00
QskWindowChangeEvent::QskWindowChangeEvent(
2018-08-03 08:15:28 +02:00
QQuickWindow* oldWindow, QQuickWindow* window )
: QskEvent( QskEvent::WindowChange )
, m_oldWindow( oldWindow )
, m_window( window )
2017-07-21 18:21:34 +02:00
{
}
2020-12-09 14:56:10 +01:00
QskWindowChangeEvent* QskWindowChangeEvent::clone() const
{
return new QskWindowChangeEvent( *this );
}
// -- QskPopupEvent
QskPopupEvent::QskPopupEvent( Type type, QskPopup* popup )
: QskEvent( type )
, m_popup( popup )
{
}
2020-12-09 14:56:10 +01:00
QskPopupEvent* QskPopupEvent::clone() const
{
return new QskPopupEvent( *this );
}
2017-07-21 18:21:34 +02:00
// -- QskGestureEvent
2020-12-09 15:58:27 +01:00
QskGestureEvent::QskGestureEvent( std::shared_ptr< const QskGesture > gesture )
2018-08-03 08:15:28 +02:00
: QskEvent( QskEvent::Gesture )
, m_gesture( gesture )
2017-07-21 18:21:34 +02:00
{
}
2020-12-09 15:58:27 +01:00
QskGestureEvent* QskGestureEvent::clone() const
{
return new QskGestureEvent( *this );
}
2017-07-21 18:21:34 +02:00
// -- QskAnimatorEvent
QskAnimatorEvent::QskAnimatorEvent( QskAspect aspect, int index, State state )
2018-08-03 08:15:28 +02:00
: QskEvent( QskEvent::Animator )
, m_aspect( aspect )
, m_index( index )
2018-08-03 08:15:28 +02:00
, m_state( state )
2017-07-21 18:21:34 +02:00
{
}
2020-12-09 14:56:10 +01:00
QskAnimatorEvent* QskAnimatorEvent::clone() const
2017-07-21 18:21:34 +02:00
{
2020-12-09 14:56:10 +01:00
return new QskAnimatorEvent( *this );
2017-07-21 18:21:34 +02:00
}