#include "MainItem.h" #include "DashboardPage.h" #include "DevicesPage.h" #include "MenuBar.h" #include "MembersPage.h" #include "RoomsPage.h" #include "StatisticsPage.h" #include "StoragePage.h" #include #include #include #include #include #include #include #include #include Cube::Position Cube::s_neighbors[ Cube::NumPositions ][ 4 ] = { // Left: { Cube::Back, // Right Cube::Front, // Left Cube::Top, // Bottom Cube::Bottom }, // Top // Right: { Cube::Front, Cube::Back, Cube::Top, Cube::Bottom }, // Top: { Cube::Left, Cube::Right, Cube::Back, Cube::Front }, // Bottom: { Cube::Left, Cube::Right, Cube::Front, Cube::Back }, // Front: { Cube::Left, Cube::Right, Cube::Top, Cube::Bottom }, // Back: { Cube::Left, Cube::Right, Cube::Top, Cube::Bottom }, }; Cube::Cube( QQuickItem* parent ) : QskStackBox( false, parent ) , m_currentPosition( Front ) { // The code below covers the case where we need 2 cube movements to get // to the desired position. // We use transientIndexChanged here to be sure to start a new transition // at the end; indexChanged doesn't work here. connect( this, &QskStackBox::transientIndexChanged, this, [ this ]( qreal position ) { const bool animationIsFinished = ( position == qFloor( position ) ); if( animationIsFinished && position != m_currentPosition ) { QTimer::singleShot( 0, this, [this]() { switchToPosition( m_currentPosition ); } ); } } ); } void Cube::startAnimation( Qsk::Direction direction, int position ) { using Animator = QskStackBoxAnimator4; auto animator = qobject_cast< Animator* >( this->animator() ); if ( animator == nullptr ) { animator = new Animator( this ); animator->setEasingCurve( QEasingCurve::InOutQuad ); animator->setDuration( 1000 ); setAnimator( animator ); } const auto orientation = ( direction == Qsk::LeftToRight || direction == Qsk::RightToLeft ) ? Qt::Horizontal : Qt::Vertical; animator->setOrientation( orientation ); const bool inverted = ( direction == Qsk::LeftToRight || direction == Qsk::TopToBottom ); animator->setInverted( inverted ); setCurrentIndex( position ); } void Cube::switchPosition( const Qsk::Direction direction ) { const auto position = s_neighbors[ m_currentPosition ][ direction ]; switchToPosition( position ); } void Cube::switchToPosition( const Position position ) { if( currentIndex() == position ) return; m_currentPosition = static_cast< Position >( position ); const auto from = static_cast< Cube::Position >( currentIndex() ); const auto d = direction( from, m_currentPosition ); startAnimation( d, position ); Q_EMIT cubeIndexChanged( position ); } Qsk::Direction Cube::direction( const Position from, const Position to ) { // if direct neighbor: use that direction // otherwise: we need 2 swipes, direction doesn't matter, so choose right to left const auto neighbors = s_neighbors[ from ]; for( int i = 0; i < 4; ++i ) { if( neighbors[ i ] == to ) { return static_cast< Qsk::Direction >( i ); } } return Qsk::RightToLeft; } MainItem::MainItem( QQuickItem* parent ) : QskControl( parent ) , m_mainLayout( new QskLinearBox( Qt::Horizontal, this ) ) , m_menuBar( new MenuBar( m_mainLayout ) ) , m_cube( new Cube( m_mainLayout ) ) { setAutoLayoutChildren( true ); setAcceptedMouseButtons( Qt::LeftButton ); setFiltersChildMouseEvents( true ); m_panRecognizer.setOrientations( Qt::Horizontal | Qt::Vertical ); m_panRecognizer.setMinDistance( 50 ); m_panRecognizer.setWatchedItem( this ); m_mainLayout->setSpacing( 0 ); connect( m_menuBar, &MenuBar::pageChangeRequested, this, [this]( int index ) { const auto position = static_cast< Cube::Position >( index ); m_cube->switchToPosition( position ); } ); auto* const dashboardPage = new DashboardPage( m_cube ); auto* const roomsPage = new RoomsPage( m_cube ); auto* const devicesPage = new DevicesPage( m_cube ); auto* const statisticsPage = new StatisticsPage( m_cube ); 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 ); // the current item needs to be the one at the Front: m_cube->setCurrentItem( dashboardPage ); } void MainItem::gestureEvent( QskGestureEvent* event ) { if( event->gesture()->state() == QskGesture::Finished && event->gesture()->type() == QskGesture::Pan ) { const auto* panGesture = static_cast< const QskPanGesture* >( event->gesture().get() ); const auto delta = panGesture->origin() - panGesture->position(); Qsk::Direction direction; if( qAbs( delta.x() ) > qAbs( delta.y() ) ) { direction = ( delta.x() < 0 ) ? Qsk::LeftToRight : Qsk::RightToLeft; } else { direction = ( delta.y() < 0 ) ? Qsk::TopToBottom : Qsk::BottomToTop; } m_cube->switchPosition( direction ); } } bool MainItem::gestureFilter( QQuickItem* item, QEvent* event ) { auto& recognizer = m_panRecognizer; if( event->type() == QEvent::MouseButtonPress ) { const auto mouseEvent = static_cast< QMouseEvent* >( event ); if( ( item != this ) || ( recognizer.timeout() < 0 ) ) { if( recognizer.hasProcessedBefore( mouseEvent ) ) { return false; } } recognizer.setTimeout( ( item == this ) ? -1 : 100 ); } return recognizer.processEvent( item, event, false ); } #include "moc_MainItem.cpp"