From 65e3290fc49acf26ab5750b3f2a698c3515aa3fa Mon Sep 17 00:00:00 2001 From: Uwe Rathmann Date: Wed, 13 Sep 2023 18:01:46 +0200 Subject: [PATCH] confusing event filtering and forwarding simplified --- examples/iotdashboard/MainItem.cpp | 72 ++++++++++++------------------ examples/iotdashboard/MainItem.h | 16 +++---- 2 files changed, 35 insertions(+), 53 deletions(-) diff --git a/examples/iotdashboard/MainItem.cpp b/examples/iotdashboard/MainItem.cpp index cf84cd67..d0c5ed29 100644 --- a/examples/iotdashboard/MainItem.cpp +++ b/examples/iotdashboard/MainItem.cpp @@ -193,35 +193,6 @@ void Cube::switchToPosition( const Position position ) doSwitch( direction, nextPosition ); } -void Cube::keyPressEvent( QKeyEvent* event ) -{ - Qsk::Direction direction; - - switch( event->key() ) - { - case Qt::Key_Up: - direction = Qsk::TopToBottom; - break; - - case Qt::Key_Down: - direction = Qsk::BottomToTop; - break; - - case Qt::Key_Left: - direction = Qsk::LeftToRight; - break; - - case Qt::Key_Right: - direction = Qsk::RightToLeft; - break; - - default: - return; - } - - switchPosition( direction ); -} - Cube::Position Cube::currentPosition() const { return static_cast< Position >( currentIndex() ); @@ -304,8 +275,6 @@ MainItem::MainItem( QQuickItem* parent ) // the current item needs to be the one at the Front: m_cube->setCurrentItem( dashboardPage ); - - installEventFilter( this ); } void MainItem::gestureEvent( QskGestureEvent* event ) @@ -332,19 +301,36 @@ void MainItem::gestureEvent( QskGestureEvent* event ) } } -bool MainItem::eventFilter( QObject* object, QEvent* event ) +void MainItem::keyPressEvent( QKeyEvent* event ) { - if ( event->type() == QEvent::KeyPress ) - { - QCoreApplication::sendEvent( m_cube, event ); - return true; - } - else - { - return QObject::eventFilter( object, event ); - } -} + // maybe using shortcuts ? + Qsk::Direction direction; + + switch( event->key() ) + { + case Qt::Key_Up: + direction = Qsk::TopToBottom; + break; + + case Qt::Key_Down: + direction = Qsk::BottomToTop; + break; + + case Qt::Key_Left: + direction = Qsk::LeftToRight; + break; + + case Qt::Key_Right: + direction = Qsk::RightToLeft; + break; + + default: + return; + } + + m_cube->switchPosition( direction ); +} bool MainItem::gestureFilter( const QQuickItem* item, const QEvent* event ) { auto& recognizer = m_panRecognizer; @@ -356,9 +342,7 @@ bool MainItem::gestureFilter( const QQuickItem* item, const QEvent* event ) if( ( item != this ) || ( recognizer.timeout() < 0 ) ) { if( recognizer.hasProcessedBefore( mouseEvent ) ) - { return false; - } } recognizer.setTimeout( ( item == this ) ? -1 : 100 ); diff --git a/examples/iotdashboard/MainItem.h b/examples/iotdashboard/MainItem.h index 44e7e478..daf2b2e5 100644 --- a/examples/iotdashboard/MainItem.h +++ b/examples/iotdashboard/MainItem.h @@ -40,22 +40,19 @@ class Cube : public QskStackBox explicit Cube( QQuickItem* parent = nullptr ); public Q_SLOTS: - void switchPosition( const Qsk::Direction direction ); - void switchToPosition( const Cube::Position position ); + void switchPosition( const Qsk::Direction ); + void switchToPosition( const Cube::Position ); Q_SIGNALS: // might be different from indexChanged: void cubeIndexChanged( const int index ); - protected: - void keyPressEvent( QKeyEvent* event ) override; - private: Position currentPosition() const; - Position neighbor( const Position position, const Qsk::Direction direction ) const; + Position neighbor( const Position, const Qsk::Direction ) const; Qsk::Direction direction( const Position from, const Position to ) const; - void updateEdge( Qsk::Direction direction, Position position ); - void doSwitch( Qsk::Direction direction, Position position ); + void updateEdge( Qsk::Direction, Position ); + void doSwitch( Qsk::Direction, Position ); Position m_destination; Edge m_currentEdge; @@ -73,7 +70,8 @@ class MainItem : public QskControl MainItem( QQuickItem* parent = nullptr ); protected: - bool eventFilter(QObject* obj, QEvent* event) override final; + void keyPressEvent( QKeyEvent* ) override final; + bool gestureFilter( const QQuickItem*, const QEvent* ) override final; void gestureEvent( QskGestureEvent* ) override final;