From 6324908cdf726e755d6c2574b890068e08ae8f4e Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Wed, 4 Jan 2023 17:41:21 +0100 Subject: [PATCH] IOT dashboard: support key events for cube switching Resolves #222 --- examples/iotdashboard/MainItem.cpp | 40 ++++++++++++++++++++++++++++++ examples/iotdashboard/MainItem.h | 4 +++ 2 files changed, 44 insertions(+) diff --git a/examples/iotdashboard/MainItem.cpp b/examples/iotdashboard/MainItem.cpp index 523232c8..225fa16c 100644 --- a/examples/iotdashboard/MainItem.cpp +++ b/examples/iotdashboard/MainItem.cpp @@ -193,6 +193,31 @@ 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() ); @@ -275,6 +300,8 @@ 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 ) @@ -301,6 +328,19 @@ void MainItem::gestureEvent( QskGestureEvent* event ) } } +bool MainItem::eventFilter( QObject* object, QEvent* event ) +{ + if ( event->type() == QEvent::KeyPress ) + { + QCoreApplication::sendEvent( m_cube, event ); + return true; + } + else + { + return QObject::eventFilter( object, event ); + } +} + bool MainItem::gestureFilter( QQuickItem* item, QEvent* event ) { auto& recognizer = m_panRecognizer; diff --git a/examples/iotdashboard/MainItem.h b/examples/iotdashboard/MainItem.h index d83b2b7a..0bde2792 100644 --- a/examples/iotdashboard/MainItem.h +++ b/examples/iotdashboard/MainItem.h @@ -45,6 +45,9 @@ class Cube : public QskStackBox // 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; @@ -68,6 +71,7 @@ class MainItem : public QskControl MainItem( QQuickItem* parent = nullptr ); protected: + bool eventFilter(QObject *obj, QEvent *event) override final; bool gestureFilter( QQuickItem*, QEvent* ) override final; void gestureEvent( QskGestureEvent* ) override final;