qskinny/src/controls/QskSwipeView.cpp

152 lines
3.8 KiB
C++
Raw Normal View History

2023-06-19 11:22:34 +02:00
/******************************************************************************
2024-01-17 14:31:45 +01:00
* QSkinny - Copyright (C) The authors
2023-06-19 11:22:34 +02:00
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
#include "QskSwipeView.h"
#include "QskEvent.h"
#include "QskGesture.h"
#include "QskPanGestureRecognizer.h"
#include "QskStackBoxAnimator.h"
2023-08-10 19:54:06 +02:00
#include "QskPlatform.h"
2023-06-19 11:22:34 +02:00
class QskSwipeView::PrivateData
{
public:
QskPanGestureRecognizer panRecognizer;
2023-08-10 19:10:31 +02:00
int duration = -1; // should be a skinHint
2023-06-19 11:22:34 +02:00
};
QSK_SUBCONTROL( QskSwipeView, Panel )
QskSwipeView::QskSwipeView( QQuickItem* parent )
: Inherited( parent )
, m_data( new PrivateData() )
{
setFiltersChildMouseEvents( true );
setAcceptedMouseButtons( Qt::LeftButton );
2023-08-10 19:10:31 +02:00
auto& recognizer = m_data->panRecognizer;
recognizer.setWatchedItem( this );
2023-08-10 19:54:06 +02:00
// should be skin hints, TODO
2023-08-10 19:10:31 +02:00
recognizer.setOrientations( Qt::Horizontal );
recognizer.setTimeout( 100 );
2023-08-10 19:54:06 +02:00
resetSwipeDistance();
2023-08-10 19:10:31 +02:00
resetDuration();
2023-06-19 11:22:34 +02:00
}
QskSwipeView::~QskSwipeView()
{
}
2023-08-10 19:54:06 +02:00
void QskSwipeView::setOrientation( Qt::Orientation orientation )
2023-08-10 19:10:31 +02:00
{
2023-08-10 19:54:06 +02:00
if ( orientation != this->orientation() )
{
m_data->panRecognizer.setOrientations( orientation );
Q_EMIT orientationChanged( orientation );
}
}
2023-08-10 19:10:31 +02:00
2023-08-10 19:54:06 +02:00
Qt::Orientation QskSwipeView::orientation() const
{
return ( m_data->panRecognizer.orientations() == Qt::Vertical )
? Qt::Vertical : Qt::Horizontal;
}
int QskSwipeView::swipeDistance() const
{
return m_data->panRecognizer.minDistance();
}
void QskSwipeView::setSwipeDistance( int distance )
{
const auto oldDistance = m_data->panRecognizer.minDistance();
m_data->panRecognizer.setMinDistance( distance );
if ( oldDistance != m_data->panRecognizer.minDistance() )
Q_EMIT swipeDistanceChanged( m_data->panRecognizer.minDistance() );
}
void QskSwipeView::resetSwipeDistance()
{
setSwipeDistance( qRound( qskDpToPixels( 40 ) ) );
2023-08-10 19:10:31 +02:00
}
2023-06-19 11:22:34 +02:00
int QskSwipeView::duration() const
{
return m_data->duration;
}
void QskSwipeView::setDuration( int duration )
{
2023-08-10 19:54:06 +02:00
if ( duration != m_data->duration )
{
m_data->duration = duration;
Q_EMIT durationChanged( duration );
}
2023-06-19 11:22:34 +02:00
}
2023-08-10 19:10:31 +02:00
void QskSwipeView::resetDuration()
{
2023-08-10 19:54:06 +02:00
setDuration( 500 );
2023-08-10 19:10:31 +02:00
}
2023-06-19 11:22:34 +02:00
void QskSwipeView::gestureEvent( QskGestureEvent* event )
{
2023-08-10 19:10:31 +02:00
const auto gesture = static_cast< const QskPanGesture* >( event->gesture().get() );
2023-06-19 11:22:34 +02:00
2023-08-10 19:10:31 +02:00
if( gesture->type() == QskGesture::Pan && gesture->state() == QskGesture::Started )
{
2023-09-26 16:42:45 +02:00
if ( itemCount() <= 1 )
return;
2023-08-10 19:54:06 +02:00
bool forwards;
if ( orientation() == Qt::Horizontal )
forwards = gesture->angle() >= 90.0 && gesture->angle() <= 270.0;
else
forwards = gesture->angle() >= 180.0;
2023-06-19 11:22:34 +02:00
auto animator = qobject_cast< QskStackBoxAnimator1* >( this->animator() );
if ( animator == nullptr )
animator = new QskStackBoxAnimator1( this );
if ( orientation() == Qt::Horizontal )
animator->setDirection( forwards ? Qsk::LeftToRight : Qsk::RightToLeft );
else
animator->setDirection( forwards ? Qsk::TopToBottom : Qsk::BottomToTop );
2023-06-19 11:22:34 +02:00
animator->setDuration( m_data->duration );
QskStackBox::setAnimator( animator );
auto newIndex = forwards ? currentIndex() + 1 : currentIndex() - 1;
2023-06-19 11:22:34 +02:00
if( newIndex < 0 )
newIndex += itemCount();
newIndex %= itemCount();
setCurrentIndex( newIndex );
2023-08-10 19:10:31 +02:00
return;
2023-06-19 11:22:34 +02:00
}
2023-08-10 19:10:31 +02:00
Inherited::gestureEvent( event );
2023-06-19 11:22:34 +02:00
}
2023-08-10 19:54:06 +02:00
QskAspect::Subcontrol QskSwipeView::effectiveSubcontrol(
QskAspect::Subcontrol subControl ) const
{
if ( subControl == QskBox::Panel )
return QskSwipeView::Panel;
return Inherited::effectiveSubcontrol( subControl );
}
2023-06-19 11:22:34 +02:00
#include "moc_QskSwipeView.cpp"