code moved from QskSetup to QskQuickItem

This commit is contained in:
Uwe Rathmann 2024-02-02 12:38:03 +01:00
parent 9d2414fd89
commit 4fddc4db24
3 changed files with 43 additions and 82 deletions

View File

@ -352,6 +352,12 @@ bool QskQuickItem::isTabFence() const
void QskQuickItem::setFocusPolicy( Qt::FocusPolicy policy )
{
/*
Qt::FocusPolicy has always been there with widgets, got lost with
Qt/Quick and has been reintroduced with Qt/Quick Controls 2 ( QC2 ).
Unfortunately this was done by adding code on top instead of fixing
the foundation.
*/
Q_D( QskQuickItem );
if ( policy != d->focusPolicy )
{
@ -361,7 +367,7 @@ void QskQuickItem::setFocusPolicy( Qt::FocusPolicy policy )
if ( !tabFocus && window() )
{
// Removing the activeFocusItem from the focus tab chain is not possible
// removing the activeFocusItem from the focus tab chain is not possible
if ( window()->activeFocusItem() == this )
{
if ( auto focusItem = nextItemInFocusChain( true ) )
@ -713,6 +719,42 @@ bool QskQuickItem::event( QEvent* event )
return true;
}
break;
}
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
{
if ( ( focusPolicy() & Qt::ClickFocus ) == Qt::ClickFocus )
{
if ( QGuiApplication::styleHints()->setFocusOnTouchRelease() )
{
if ( event->type() == QEvent::MouseButtonRelease )
forceActiveFocus( Qt::MouseFocusReason );
}
else
{
if ( event->type() == QEvent::MouseButtonPress )
forceActiveFocus( Qt::MouseFocusReason );
}
}
break;
}
case QEvent::Wheel:
{
if ( !isWheelEnabled() )
{
/*
We block further processing of the event. This is in line
with not receiving any mouse event that have not been
explicitly enabled with setAcceptedMouseButtons().
*/
event->ignore();
return true;
}
if ( ( focusPolicy() & Qt::WheelFocus ) == Qt::WheelFocus )
forceActiveFocus( Qt::MouseFocusReason );
break;
}
}

View File

@ -4,14 +4,10 @@
*****************************************************************************/
#include "QskSetup.h"
#include "QskQuickItem.h"
#include "QskGraphicProviderMap.h"
#include "QskSkinManager.h"
#include "QskSkin.h"
#include <qguiapplication.h>
#include <qstylehints.h>
QskSetup* QskSetup::s_instance = nullptr;
static inline bool qskHasEnvironment( const char* env )
@ -62,13 +58,7 @@ static void qskApplicationHook()
qAddPostRoutine( QskSetup::cleanup );
}
static void qskApplicationFilter()
{
QCoreApplication::instance()->installEventFilter( QskSetup::instance() );
}
Q_CONSTRUCTOR_FUNCTION( qskApplicationHook )
Q_COREAPP_STARTUP_FUNCTION( qskApplicationFilter )
class QskSetup::PrivateData
{
@ -162,73 +152,4 @@ QskGraphicProvider* QskSetup::graphicProvider( const QString& providerId ) const
return m_data->graphicProviders.provider( providerId );
}
bool QskSetup::eventFilter( QObject* object, QEvent* event )
{
if ( auto qskItem = qobject_cast< QskQuickItem* >( object ) )
{
/*
Qt::FocusPolicy has always been there with widgets, got lost with
Qt/Quick and has been reintroduced with Qt/Quick Controls 2 ( QC2 ).
Unfortunately this was done once more by adding code on top instead
of fixing the foundation.
But we also don't want to have how it is done in QC2 by adding
the focus management in the event handler of the base class.
This implementation reverts the expected default behaviour of when
events are accepted/ignored + is an error prone nightmare, when it
comes to overloading event handlers missing to call the base class.
That's why we prefer to do the focus management outside of the
event handlers.
*/
switch ( event->type() )
{
case QEvent::MouseButtonPress:
case QEvent::MouseButtonRelease:
{
if ( ( qskItem->focusPolicy() & Qt::ClickFocus ) == Qt::ClickFocus )
{
const bool focusOnRelease =
QGuiApplication::styleHints()->setFocusOnTouchRelease();
if ( focusOnRelease )
{
if ( event->type() == QEvent::MouseButtonRelease )
qskItem->forceActiveFocus( Qt::MouseFocusReason );
}
else
{
if ( event->type() == QEvent::MouseButtonPress )
qskItem->forceActiveFocus( Qt::MouseFocusReason );
}
}
break;
}
case QEvent::Wheel:
{
if ( !qskItem->isWheelEnabled() )
{
/*
We block further processing of the event. This is in line
with not receiving any mouse event that have not been
explicitly enabled with setAcceptedMouseButtons().
*/
event->ignore();
return true;
}
if ( ( qskItem->focusPolicy() & Qt::WheelFocus ) == Qt::WheelFocus )
qskItem->forceActiveFocus( Qt::MouseFocusReason );
break;
}
default:
break;
}
}
return false;
}
#include "moc_QskSetup.cpp"

View File

@ -50,8 +50,6 @@ class QSK_EXPORT QskSetup : public QObject
QskSetup();
~QskSetup() override;
bool eventFilter( QObject*, QEvent* ) override final;
static QskSetup* s_instance;
class PrivateData;