IOT dashboard: add edges to calculate neighbors correctly

This commit is contained in:
Peter Hartmann 2023-01-03 09:50:55 +01:00 committed by uwerat
parent dc356801e4
commit db706737be
4 changed files with 84 additions and 54 deletions

View File

@ -23,45 +23,46 @@
Cube::Position Cube::s_neighbors[ Cube::NumPositions ][ 4 ] = Cube::Position Cube::s_neighbors[ Cube::NumPositions ][ 4 ] =
{ {
// Left: // Left:
{ Cube::Back, // Right { Cube::BackPos, // Left
Cube::Front, // Left Cube::FrontPos, // Right
Cube::Top, // Bottom Cube::TopPos, // Top
Cube::Bottom }, // Top Cube::BottomPos }, // Bottom
// Right: // Right:
{ Cube::Front, { Cube::FrontPos,
Cube::Back, Cube::BackPos,
Cube::Top, Cube::TopPos,
Cube::Bottom }, Cube::BottomPos },
// Top: // Top:
{ Cube::Left, { Cube::LeftPos,
Cube::Right, Cube::RightPos,
Cube::Back, Cube::BackPos,
Cube::Front }, Cube::FrontPos },
// Bottom: // Bottom:
{ Cube::Left, { Cube::LeftPos,
Cube::Right, Cube::RightPos,
Cube::Front, Cube::FrontPos,
Cube::Back }, Cube::BackPos },
// Front: // Front:
{ Cube::Left, { Cube::LeftPos,
Cube::Right, Cube::RightPos,
Cube::Top, Cube::TopPos,
Cube::Bottom }, Cube::BottomPos },
// Back: // Back:
{ Cube::Left, { Cube::RightPos,
Cube::Right, Cube::LeftPos,
Cube::Top, Cube::TopPos,
Cube::Bottom }, Cube::BottomPos },
}; };
Cube::Cube( QQuickItem* parent ) Cube::Cube( QQuickItem* parent )
: QskStackBox( false, 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 // The code below covers the case where we need 2 cube movements to get
// to the desired position. // 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; using Animator = QskStackBoxAnimator4;
auto animator = qobject_cast< Animator* >( this->animator() ); auto animator = qobject_cast< Animator* >( this->animator() );
@ -105,12 +109,33 @@ void Cube::startAnimation( Qsk::Direction direction, int position )
animator->setInverted( inverted ); animator->setInverted( inverted );
setCurrentIndex( position ); setCurrentIndex( position );
Q_EMIT cubeIndexChanged( position ); // ### do we need this?
} }
void Cube::switchPosition( const Qsk::Direction direction ) void Cube::switchPosition( const Qsk::Direction direction )
{ {
const auto position = s_neighbors[ m_currentPosition ][ direction ]; // ### needs to go to the other function:
switchToPosition( position );
// 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 ) void Cube::switchToPosition( const Position position )
@ -118,16 +143,19 @@ void Cube::switchToPosition( const Position position )
if( currentIndex() == position ) if( currentIndex() == position )
return; 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() ); doSwitch( direction, position );
const auto d = direction( from, m_currentPosition );
startAnimation( d, position );
Q_EMIT cubeIndexChanged( 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 // if direct neighbor: use that direction
// otherwise: we need 2 swipes, direction doesn't matter, so choose right to left // 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 storagePage = new StoragePage( m_cube );
auto* const membersPage = new MembersPage( m_cube ); auto* const membersPage = new MembersPage( m_cube );
m_cube->insertItem( Cube::Left, statisticsPage ); m_cube->insertItem( Cube::LeftPos, statisticsPage );
m_cube->insertItem( Cube::Right, roomsPage ); m_cube->insertItem( Cube::RightPos, roomsPage );
m_cube->insertItem( Cube::Top, storagePage ); m_cube->insertItem( Cube::TopPos, storagePage );
m_cube->insertItem( Cube::Bottom, membersPage ); m_cube->insertItem( Cube::BottomPos, membersPage );
m_cube->insertItem( Cube::Front, dashboardPage ); m_cube->insertItem( Cube::FrontPos, dashboardPage );
m_cube->insertItem( Cube::Back, devicesPage ); m_cube->insertItem( Cube::BackPos, devicesPage );
// the current item needs to be the one at the Front: // the current item needs to be the one at the Front:
m_cube->setCurrentItem( dashboardPage ); m_cube->setCurrentItem( dashboardPage );

View File

@ -16,14 +16,15 @@ class Cube : public QskStackBox
public: public:
enum Position { enum Position {
Left, LeftPos,
Right, RightPos,
Top, TopPos,
Bottom, BottomPos,
Front, FrontPos,
Back, BackPos,
NumPositions NumPositions
}; };
Q_ENUM( Position )
explicit Cube( QQuickItem* parent = nullptr ); explicit Cube( QQuickItem* parent = nullptr );
@ -36,10 +37,12 @@ class Cube : public QskStackBox
void cubeIndexChanged( const int index ); void cubeIndexChanged( const int index );
private: private:
Qsk::Direction direction( const Position from, const Position to ); Position neighbor( const Position position, const Qsk::Direction direction ) const;
void startAnimation( Qsk::Direction direction, int position ); 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 ]; static Position s_neighbors[ NumPositions ][ 4 ];
}; };

View File

@ -40,11 +40,11 @@ MenuBar::MenuBar( QQuickItem* parent )
graphicLabel->setMargins( marginHint( MenuBarTopLabel::Graphic ) ); graphicLabel->setMargins( marginHint( MenuBarTopLabel::Graphic ) );
graphicLabel->setSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed ); 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 ]() connect( button, &QskPushButton::pressed, this, [ this, i ]()
{ {

View File

@ -50,7 +50,6 @@ class MenuBar final : public QskLinearBox
void setActivePage( const int index ); void setActivePage( const int index );
private: private:
QList< QString > m_entryStrings;
QList< MenuButton* > m_entries; QList< MenuButton* > m_entries;
uint m_currentIndex; uint m_currentIndex;
}; };