From db706737be98781d6c098a36a80e0653977eb95a Mon Sep 17 00:00:00 2001 From: Peter Hartmann Date: Tue, 3 Jan 2023 09:50:55 +0100 Subject: [PATCH] IOT dashboard: add edges to calculate neighbors correctly --- examples/iotdashboard/MainItem.cpp | 110 ++++++++++++++++++----------- examples/iotdashboard/MainItem.h | 21 +++--- examples/iotdashboard/MenuBar.cpp | 6 +- examples/iotdashboard/MenuBar.h | 1 - 4 files changed, 84 insertions(+), 54 deletions(-) diff --git a/examples/iotdashboard/MainItem.cpp b/examples/iotdashboard/MainItem.cpp index 3c7539e2..526a50f7 100644 --- a/examples/iotdashboard/MainItem.cpp +++ b/examples/iotdashboard/MainItem.cpp @@ -23,45 +23,46 @@ Cube::Position Cube::s_neighbors[ Cube::NumPositions ][ 4 ] = { // Left: - { Cube::Back, // Right - Cube::Front, // Left - Cube::Top, // Bottom - Cube::Bottom }, // Top + { Cube::BackPos, // Left + Cube::FrontPos, // Right + Cube::TopPos, // Top + Cube::BottomPos }, // Bottom // Right: - { Cube::Front, - Cube::Back, - Cube::Top, - Cube::Bottom }, + { Cube::FrontPos, + Cube::BackPos, + Cube::TopPos, + Cube::BottomPos }, // Top: - { Cube::Left, - Cube::Right, - Cube::Back, - Cube::Front }, + { Cube::LeftPos, + Cube::RightPos, + Cube::BackPos, + Cube::FrontPos }, // Bottom: - { Cube::Left, - Cube::Right, - Cube::Front, - Cube::Back }, + { Cube::LeftPos, + Cube::RightPos, + Cube::FrontPos, + Cube::BackPos }, // Front: - { Cube::Left, - Cube::Right, - Cube::Top, - Cube::Bottom }, + { Cube::LeftPos, + Cube::RightPos, + Cube::TopPos, + Cube::BottomPos }, // Back: - { Cube::Left, - Cube::Right, - Cube::Top, - Cube::Bottom }, + { Cube::RightPos, + Cube::LeftPos, + Cube::TopPos, + Cube::BottomPos }, }; Cube::Cube( QQuickItem* parent ) : QskStackBox( false, parent ) - , m_currentPosition( Front ) + , m_currentPosition( FrontPos ) + , m_previousPosition( FrontPos ) { // The code below covers the case where we need 2 cube movements to get // to the desired position. @@ -82,8 +83,11 @@ Cube::Cube( QQuickItem* parent ) } ); } -void Cube::startAnimation( Qsk::Direction direction, int position ) +void Cube::doSwitch( Qsk::Direction direction, Position position ) { + m_previousPosition = m_currentPosition; + m_currentPosition = position; + using Animator = QskStackBoxAnimator4; auto animator = qobject_cast< Animator* >( this->animator() ); @@ -105,12 +109,33 @@ void Cube::startAnimation( Qsk::Direction direction, int position ) animator->setInverted( inverted ); setCurrentIndex( position ); + Q_EMIT cubeIndexChanged( position ); // ### do we need this? } void Cube::switchPosition( const Qsk::Direction direction ) { - const auto position = s_neighbors[ m_currentPosition ][ direction ]; - switchToPosition( position ); + // ### needs to go to the other function: + + // keep track of from where we went to top and bottom, + // so that going up and down will result in going back + // to the same position: + // (We don't want to model the complete cube logic with + // keeping track of the edges here, because that doesn't + // make sense wrt. being upside down etc.) + + Position position; + + if( ( m_currentPosition == TopPos && direction == Qsk::BottomToTop ) + || ( m_currentPosition == BottomPos && direction == Qsk::TopToBottom ) ) + { + position = m_previousPosition; + } + else + { + position = neighbor( m_currentPosition, direction ); + } + + doSwitch( direction, position ); } void Cube::switchToPosition( const Position position ) @@ -118,16 +143,19 @@ void Cube::switchToPosition( const Position position ) if( currentIndex() == position ) return; - m_currentPosition = static_cast< Position >( position ); + const auto from = static_cast< Position >( currentIndex() ); + const auto direction = this->direction( from, m_currentPosition ); - const auto from = static_cast< Cube::Position >( currentIndex() ); - const auto d = direction( from, m_currentPosition ); - - startAnimation( d, position ); - Q_EMIT cubeIndexChanged( position ); + doSwitch( direction, position ); } -Qsk::Direction Cube::direction( const Position from, const Position to ) +Cube::Position Cube::neighbor( const Position position, const Qsk::Direction direction ) const +{ + const auto n = s_neighbors[ position ][ direction ]; + return n; +} + +Qsk::Direction Cube::direction( const Position from, const Position to ) const { // if direct neighbor: use that direction // otherwise: we need 2 swipes, direction doesn't matter, so choose right to left @@ -174,12 +202,12 @@ MainItem::MainItem( QQuickItem* parent ) auto* const storagePage = new StoragePage( m_cube ); auto* const membersPage = new MembersPage( m_cube ); - m_cube->insertItem( Cube::Left, statisticsPage ); - m_cube->insertItem( Cube::Right, roomsPage ); - m_cube->insertItem( Cube::Top, storagePage ); - m_cube->insertItem( Cube::Bottom, membersPage ); - m_cube->insertItem( Cube::Front, dashboardPage ); - m_cube->insertItem( Cube::Back, devicesPage ); + m_cube->insertItem( Cube::LeftPos, statisticsPage ); + m_cube->insertItem( Cube::RightPos, roomsPage ); + m_cube->insertItem( Cube::TopPos, storagePage ); + m_cube->insertItem( Cube::BottomPos, membersPage ); + m_cube->insertItem( Cube::FrontPos, dashboardPage ); + m_cube->insertItem( Cube::BackPos, devicesPage ); // the current item needs to be the one at the Front: m_cube->setCurrentItem( dashboardPage ); diff --git a/examples/iotdashboard/MainItem.h b/examples/iotdashboard/MainItem.h index ad1c099b..6ae03683 100644 --- a/examples/iotdashboard/MainItem.h +++ b/examples/iotdashboard/MainItem.h @@ -16,14 +16,15 @@ class Cube : public QskStackBox public: enum Position { - Left, - Right, - Top, - Bottom, - Front, - Back, + LeftPos, + RightPos, + TopPos, + BottomPos, + FrontPos, + BackPos, NumPositions }; + Q_ENUM( Position ) explicit Cube( QQuickItem* parent = nullptr ); @@ -36,10 +37,12 @@ class Cube : public QskStackBox void cubeIndexChanged( const int index ); private: - Qsk::Direction direction( const Position from, const Position to ); - void startAnimation( Qsk::Direction direction, int position ); + Position neighbor( const Position position, const Qsk::Direction direction ) const; + Qsk::Direction direction( const Position from, const Position to ) const; + void doSwitch( Qsk::Direction direction, Position position ); - Cube::Position m_currentPosition; + Position m_currentPosition; + Position m_previousPosition; static Position s_neighbors[ NumPositions ][ 4 ]; }; diff --git a/examples/iotdashboard/MenuBar.cpp b/examples/iotdashboard/MenuBar.cpp index ca092dbf..1f86e6ef 100644 --- a/examples/iotdashboard/MenuBar.cpp +++ b/examples/iotdashboard/MenuBar.cpp @@ -40,11 +40,11 @@ MenuBar::MenuBar( QQuickItem* parent ) graphicLabel->setMargins( marginHint( MenuBarTopLabel::Graphic ) ); graphicLabel->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed ); - m_entryStrings = { "Dashboard", "Rooms", "Devices", "Statistics", "Storage", "Members" }; + QVector< QString > entryStrings = { "Dashboard", "Rooms", "Devices", "Statistics", "Storage", "Members" }; - for( int i = 0; i < m_entryStrings.count(); ++i ) + for( int i = 0; i < entryStrings.count(); ++i ) { - auto* button = new MenuButton( m_entryStrings.at( i ), this ); + auto* button = new MenuButton( entryStrings.at( i ), this ); connect( button, &QskPushButton::pressed, this, [ this, i ]() { diff --git a/examples/iotdashboard/MenuBar.h b/examples/iotdashboard/MenuBar.h index bef86d13..277c8424 100644 --- a/examples/iotdashboard/MenuBar.h +++ b/examples/iotdashboard/MenuBar.h @@ -50,7 +50,6 @@ class MenuBar final : public QskLinearBox void setActivePage( const int index ); private: - QList< QString > m_entryStrings; QList< MenuButton* > m_entries; uint m_currentIndex; };