IOT dashboard: fix menu bar

This commit is contained in:
Peter Hartmann 2023-01-03 14:48:59 +01:00 committed by uwerat
parent db706737be
commit 978183916f
4 changed files with 49 additions and 30 deletions

View File

@ -61,7 +61,7 @@ Cube::Position Cube::s_neighbors[ Cube::NumPositions ][ 4 ] =
Cube::Cube( QQuickItem* parent ) Cube::Cube( QQuickItem* parent )
: QskStackBox( false, parent ) : QskStackBox( false, parent )
, m_currentPosition( FrontPos ) , m_destination( FrontPos )
, m_previousPosition( 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
@ -73,11 +73,16 @@ Cube::Cube( QQuickItem* parent )
{ {
const bool animationIsFinished = ( position == qFloor( position ) ); const bool animationIsFinished = ( position == qFloor( position ) );
if( animationIsFinished && position != m_currentPosition ) if( animationIsFinished)
{
qDebug() << "finished." << position << m_destination << ( position == m_destination );
}
if( animationIsFinished && position != m_destination )
{ {
QTimer::singleShot( 0, this, [this]() QTimer::singleShot( 0, this, [this]()
{ {
switchToPosition( m_currentPosition ); switchToPosition( m_destination );
} ); } );
} }
} ); } );
@ -85,8 +90,7 @@ Cube::Cube( QQuickItem* parent )
void Cube::doSwitch( Qsk::Direction direction, Position position ) void Cube::doSwitch( Qsk::Direction direction, Position position )
{ {
m_previousPosition = m_currentPosition; m_previousPosition = m_destination;
m_currentPosition = position;
using Animator = QskStackBoxAnimator4; using Animator = QskStackBoxAnimator4;
@ -109,13 +113,11 @@ void Cube::doSwitch( Qsk::Direction direction, Position position )
animator->setInverted( inverted ); animator->setInverted( inverted );
setCurrentIndex( position ); setCurrentIndex( position );
Q_EMIT cubeIndexChanged( position ); // ### do we need this? Q_EMIT cubeIndexChanged( position );
} }
void Cube::switchPosition( const Qsk::Direction direction ) void Cube::switchPosition( const Qsk::Direction direction )
{ {
// ### needs to go to the other function:
// keep track of from where we went to top and bottom, // keep track of from where we went to top and bottom,
// so that going up and down will result in going back // so that going up and down will result in going back
// to the same position: // to the same position:
@ -123,19 +125,17 @@ void Cube::switchPosition( const Qsk::Direction direction )
// keeping track of the edges here, because that doesn't // keeping track of the edges here, because that doesn't
// make sense wrt. being upside down etc.) // make sense wrt. being upside down etc.)
Position position; if( ( m_destination == TopPos && direction == Qsk::BottomToTop )
|| ( m_destination == BottomPos && direction == Qsk::TopToBottom ) )
if( ( m_currentPosition == TopPos && direction == Qsk::BottomToTop )
|| ( m_currentPosition == BottomPos && direction == Qsk::TopToBottom ) )
{ {
position = m_previousPosition; m_destination = m_previousPosition; // ### doesn't work completely yet
} }
else else
{ {
position = neighbor( m_currentPosition, direction ); m_destination = neighbor( m_destination, direction );
} }
doSwitch( direction, position ); doSwitch( direction, m_destination );
} }
void Cube::switchToPosition( const Position position ) void Cube::switchToPosition( const Position position )
@ -143,10 +143,14 @@ void Cube::switchToPosition( const Position position )
if( currentIndex() == position ) if( currentIndex() == position )
return; return;
const auto from = static_cast< Position >( currentIndex() ); m_destination = position;
const auto direction = this->direction( from, m_currentPosition );
doSwitch( direction, position ); const auto from = static_cast< Position >( currentIndex() );
const auto direction = this->direction( from, position );
const auto currentPosition = static_cast< Position >( currentIndex() );
const auto intermediatePosition = neighbor( currentPosition, direction );
doSwitch( direction, intermediatePosition );
} }
Cube::Position Cube::neighbor( const Position position, const Qsk::Direction direction ) const Cube::Position Cube::neighbor( const Position position, const Qsk::Direction direction ) const

View File

@ -41,7 +41,7 @@ class Cube : public QskStackBox
Qsk::Direction direction( const Position from, const Position to ) const; Qsk::Direction direction( const Position from, const Position to ) const;
void doSwitch( Qsk::Direction direction, Position position ); void doSwitch( Qsk::Direction direction, Position position );
Position m_currentPosition; Position m_destination;
Position m_previousPosition; Position m_previousPosition;
static Position s_neighbors[ NumPositions ][ 4 ]; static Position s_neighbors[ NumPositions ][ 4 ];

View File

@ -4,6 +4,7 @@
*****************************************************************************/ *****************************************************************************/
#include "MenuBar.h" #include "MenuBar.h"
#include "MainItem.h"
QSK_SUBCONTROL( MenuBarTopLabel, Graphic ) QSK_SUBCONTROL( MenuBarTopLabel, Graphic )
@ -40,21 +41,35 @@ 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 );
QVector< QString > entryStrings = { "Dashboard", "Rooms", "Devices", "Statistics", "Storage", "Members" }; // ### unify the information with the one from MainItem
QVector< QPair< Cube::Position, QString > > entries =
for( int i = 0; i < entryStrings.count(); ++i )
{ {
auto* button = new MenuButton( entryStrings.at( i ), this ); { Cube::FrontPos, "Dashboard" },
{ Cube::RightPos, "Rooms" },
{ Cube::BackPos, "Devices" },
{ Cube::LeftPos, "Statistics" },
{ Cube::TopPos, "Storage" },
{ Cube::BottomPos, "Members" },
};
connect( button, &QskPushButton::pressed, this, [ this, i ]() for( const auto& entry : entries )
{
auto* button = new MenuButton( entry.second, this );
m_buttons.append( button );
connect( button, &QskPushButton::pressed, this, [ this, entry ]()
{ {
Q_EMIT pageChangeRequested( i ); for( auto* button : qAsConst( m_buttons ) )
} ); {
// the right button will be set to checked after this
button->setChecked( false );
}
m_entries.append( button ); Q_EMIT pageChangeRequested( entry.first );
} );
} }
m_entries.at( m_currentIndex )->setChecked( true ); m_buttons.at( m_currentIndex )->setChecked( true );
addSpacer( 0, 1 ); // fill the space at the bottom addSpacer( 0, 1 ); // fill the space at the bottom
@ -63,7 +78,7 @@ MenuBar::MenuBar( QQuickItem* parent )
void MenuBar::setActivePage( const int index ) void MenuBar::setActivePage( const int index )
{ {
m_entries.at( m_currentIndex )->setChecked( false ); m_buttons.at( m_currentIndex )->setChecked( false );
m_currentIndex = index; m_currentIndex = index;
} }

View File

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