2023-06-19 09:22:34 +00:00
|
|
|
/******************************************************************************
|
|
|
|
* QSkinny - Copyright (C) 2016 Uwe Rathmann
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*****************************************************************************/
|
|
|
|
|
|
|
|
#include "QskSwipeView.h"
|
|
|
|
|
|
|
|
#include "QskEvent.h"
|
|
|
|
#include "QskGesture.h"
|
|
|
|
#include "QskPanGestureRecognizer.h"
|
|
|
|
#include "QskStackBoxAnimator.h"
|
|
|
|
|
|
|
|
class QskSwipeView::PrivateData
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
QskPanGestureRecognizer panRecognizer;
|
2023-08-10 17:10:31 +00:00
|
|
|
int duration = -1; // should be a skinHint
|
2023-06-19 09:22:34 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
QSK_SUBCONTROL( QskSwipeView, Panel )
|
|
|
|
|
|
|
|
QskSwipeView::QskSwipeView( QQuickItem* parent )
|
|
|
|
: Inherited( parent )
|
|
|
|
, m_data( new PrivateData() )
|
|
|
|
{
|
|
|
|
setFiltersChildMouseEvents( true );
|
|
|
|
setAcceptedMouseButtons( Qt::LeftButton );
|
|
|
|
|
2023-08-10 17:10:31 +00:00
|
|
|
auto& recognizer = m_data->panRecognizer;
|
|
|
|
|
|
|
|
recognizer.setWatchedItem( this );
|
|
|
|
|
|
|
|
// should be skin hints
|
|
|
|
recognizer.setOrientations( Qt::Horizontal );
|
|
|
|
recognizer.setMinDistance( 50 );
|
|
|
|
recognizer.setTimeout( 100 );
|
|
|
|
|
|
|
|
resetDuration();
|
2023-06-19 09:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QskSwipeView::~QskSwipeView()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-08-10 17:10:31 +00:00
|
|
|
QskAspect::Subcontrol QskSwipeView::effectiveSubcontrol(
|
|
|
|
QskAspect::Subcontrol subControl ) const
|
|
|
|
{
|
|
|
|
if ( subControl == QskBox::Panel )
|
|
|
|
return QskSwipeView::Panel;
|
|
|
|
|
|
|
|
return Inherited::effectiveSubcontrol( subControl );
|
|
|
|
}
|
2023-06-19 09:22:34 +00:00
|
|
|
|
|
|
|
int QskSwipeView::duration() const
|
|
|
|
{
|
|
|
|
return m_data->duration;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskSwipeView::setDuration( int duration )
|
|
|
|
{
|
|
|
|
m_data->duration = duration;
|
|
|
|
}
|
|
|
|
|
2023-08-10 17:10:31 +00:00
|
|
|
void QskSwipeView::resetDuration()
|
|
|
|
{
|
|
|
|
m_data->duration = 500;
|
|
|
|
}
|
2023-06-19 09:22:34 +00:00
|
|
|
|
2023-08-10 17:10:31 +00:00
|
|
|
bool QskSwipeView::gestureFilter( const QQuickItem* item, const QEvent* event )
|
2023-06-19 09:22:34 +00:00
|
|
|
{
|
|
|
|
// see QskScrollBox.cpp
|
|
|
|
|
|
|
|
auto& recognizer = m_data->panRecognizer;
|
|
|
|
|
|
|
|
if ( event->type() == QEvent::MouseButtonPress )
|
|
|
|
{
|
2023-08-10 17:10:31 +00:00
|
|
|
auto mouseEvent = static_cast< const QMouseEvent* >( event );
|
|
|
|
if ( recognizer.hasProcessedBefore( mouseEvent ) )
|
|
|
|
return false;
|
2023-06-19 09:22:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 17:10:31 +00:00
|
|
|
return recognizer.processEvent( item, event );
|
|
|
|
|
2023-06-19 09:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void QskSwipeView::gestureEvent( QskGestureEvent* event )
|
|
|
|
{
|
2023-08-10 17:10:31 +00:00
|
|
|
const auto gesture = static_cast< const QskPanGesture* >( event->gesture().get() );
|
2023-06-19 09:22:34 +00:00
|
|
|
|
2023-08-10 17:10:31 +00:00
|
|
|
if( gesture->type() == QskGesture::Pan && gesture->state() == QskGesture::Started )
|
|
|
|
{
|
2023-06-19 09:22:34 +00:00
|
|
|
auto animator = dynamic_cast< QskStackBoxAnimator1* >( this->animator() );
|
|
|
|
|
|
|
|
if ( animator == nullptr )
|
|
|
|
{
|
|
|
|
animator = new QskStackBoxAnimator1( this );
|
|
|
|
animator->setOrientation( Qt::Horizontal );
|
|
|
|
}
|
|
|
|
|
|
|
|
animator->setDuration( m_data->duration );
|
2023-08-10 17:10:31 +00:00
|
|
|
QskStackBox::setAnimator( animator );
|
2023-06-19 09:22:34 +00:00
|
|
|
|
|
|
|
const auto direction = ( ( gesture->angle() < 90.0 ) || ( gesture->angle() > 270.0 ) )
|
|
|
|
? Qsk::LeftToRight : Qsk::RightToLeft;
|
|
|
|
|
|
|
|
auto newIndex = ( direction == Qsk::LeftToRight ) ? currentIndex() - 1 : currentIndex() + 1;
|
|
|
|
|
|
|
|
if( newIndex < 0 )
|
|
|
|
newIndex += itemCount();
|
|
|
|
|
|
|
|
newIndex %= itemCount();
|
|
|
|
|
|
|
|
setCurrentIndex( newIndex );
|
2023-08-10 17:10:31 +00:00
|
|
|
return;
|
2023-06-19 09:22:34 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 17:10:31 +00:00
|
|
|
Inherited::gestureEvent( event );
|
2023-06-19 09:22:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_QskSwipeView.cpp"
|