176 lines
4.5 KiB
C++
Raw Normal View History

2021-09-24 14:47:56 +02:00
#include "MainItem.h"
#include "DashboardPage.h"
2021-09-24 14:47:56 +02:00
#include "MenuBar.h"
2022-07-14 14:43:21 +02:00
#include "RoomsPage.h"
2021-09-24 14:47:56 +02:00
#include <QskGesture.h>
#include <QskEvent.h>
#include <QskLinearBox.h>
2022-08-02 09:36:22 +02:00
#include <QskStackBoxAnimator.h>
2021-09-24 14:47:56 +02:00
#include <QQuickFramebufferObject>
#include <QGuiApplication>
#include <QQuickWindow>
namespace
{
Qsk::Direction direction( const int from, const int to )
{
if( from < Cube::Top ) // side
{
if( to < Cube::Top ) // side to side
{
return ( to > from ) ? Qsk::LeftToRight : Qsk::RightToLeft; // ### 2x case
}
else
{
return ( to == Cube::Top ) ? Qsk::BottomToTop : Qsk::TopToBottom; // ### 2x case
}
}
else if( from == Cube::Top )
{
return Qsk::TopToBottom; // ### 2x case
}
else
{
return Qsk::BottomToTop; // ### 2x case
}
}
}
2022-08-02 09:36:22 +02:00
Cube::Cube( QQuickItem* parent )
: QskStackBox( false, parent )
{
}
void Cube::startAnimation( Qsk::Direction direction )
{
2022-08-03 09:40:18 +02:00
using Animator = QskStackBoxAnimator4;
auto animator = qobject_cast< Animator* >( this->animator() );
2022-08-02 09:36:22 +02:00
if ( animator == nullptr )
{
2022-08-03 09:40:18 +02:00
animator = new Animator( this );
2022-08-02 09:36:22 +02:00
animator->setEasingCurve( QEasingCurve::InOutQuad );
animator->setDuration( 1000 );
2022-08-03 09:40:18 +02:00
setAnimator( animator );
2022-08-02 09:36:22 +02:00
}
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 );
2022-10-07 19:00:40 +02:00
int newIndex = 0;
2022-08-02 09:36:22 +02:00
switch( direction )
{
2022-08-03 09:40:18 +02:00
case Qsk::LeftToRight:
case Qsk::TopToBottom:
newIndex = currentIndex() + 1;
break;
case Qsk::RightToLeft:
case Qsk::BottomToTop:
newIndex = currentIndex() - 1;
break;
2022-08-02 09:36:22 +02:00
}
newIndex %= itemCount();
if( newIndex < 0 )
newIndex += itemCount();
setCurrentIndex( newIndex );
}
2021-09-24 14:47:56 +02:00
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 ) )
, m_currentIndex( 0 )
2021-09-24 14:47:56 +02:00
{
setAutoLayoutChildren( true );
setAcceptedMouseButtons( Qt::LeftButton );
setFiltersChildMouseEvents( true );
2022-08-02 09:36:22 +02:00
m_panRecognizer.setOrientations( Qt::Horizontal | Qt::Vertical );
2021-09-24 14:47:56 +02:00
m_panRecognizer.setMinDistance( 50 );
m_panRecognizer.setWatchedItem( this );
m_mainLayout->setSpacing( 0 );
connect( m_menuBar, &MenuBar::pageChangeRequested, this, &MainItem::switchToPage );
2022-07-14 14:43:21 +02:00
auto* const dashboardPage = new DashboardPage( m_cube );
auto* const roomsPage = new RoomsPage( m_cube );
2021-09-24 14:47:56 +02:00
m_cube->addItem( dashboardPage );
m_cube->addItem( roomsPage );
2022-07-14 14:43:21 +02:00
m_cube->setCurrentItem( dashboardPage );
2021-09-24 14:47:56 +02:00
}
void MainItem::gestureEvent( QskGestureEvent* event )
{
2022-08-02 09:36:22 +02:00
if( event->gesture()->state() == QskGesture::Finished
&& event->gesture()->type() == QskGesture::Pan )
2021-09-24 14:47:56 +02:00
{
2022-08-02 09:36:22 +02:00
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;
}
2022-08-03 09:40:18 +02:00
m_cube->startAnimation( direction );
2021-09-24 14:47:56 +02:00
}
}
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 );
}
2022-09-09 09:15:26 +02:00
void MainItem::switchToPage( const int index )
{
if( m_currentIndex == index )
return;
const auto d = direction( m_currentIndex, index );
m_cube->startAnimation( d );
m_menuBar->setActivePage( index );
m_currentIndex = index;
}
2022-09-09 09:15:26 +02:00
#include "moc_MainItem.cpp"