QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskSwipeView.cpp
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#include "QskSwipeView.h"
7
8#include "QskEvent.h"
9#include "QskGesture.h"
10#include "QskPanGestureRecognizer.h"
11#include "QskStackBoxAnimator.h"
12#include "QskPlatform.h"
13
14class QskSwipeView::PrivateData
15{
16 public:
17 QskPanGestureRecognizer panRecognizer;
18 int duration = -1; // should be a skinHint
19};
20
21QSK_SUBCONTROL( QskSwipeView, Panel )
22
23QskSwipeView::QskSwipeView( QQuickItem* parent )
24 : Inherited( parent )
25 , m_data( new PrivateData() )
26{
27 setFiltersChildMouseEvents( true );
28 setAcceptedMouseButtons( Qt::LeftButton );
29
30 auto& recognizer = m_data->panRecognizer;
31
32 recognizer.setWatchedItem( this );
33
34 // should be skin hints, TODO
35 recognizer.setOrientations( Qt::Horizontal );
36 recognizer.setTimeout( 100 );
37
38 resetSwipeDistance();
39 resetDuration();
40}
41
42QskSwipeView::~QskSwipeView()
43{
44}
45
46void QskSwipeView::setOrientation( Qt::Orientation orientation )
47{
48 if ( orientation != this->orientation() )
49 {
50 m_data->panRecognizer.setOrientations( orientation );
51 Q_EMIT orientationChanged( orientation );
52 }
53}
54
55Qt::Orientation QskSwipeView::orientation() const
56{
57 return ( m_data->panRecognizer.orientations() == Qt::Vertical )
58 ? Qt::Vertical : Qt::Horizontal;
59}
60
61int QskSwipeView::swipeDistance() const
62{
63 return m_data->panRecognizer.minDistance();
64}
65
66void QskSwipeView::setSwipeDistance( int distance )
67{
68 const auto oldDistance = m_data->panRecognizer.minDistance();
69 m_data->panRecognizer.setMinDistance( distance );
70
71 if ( oldDistance != m_data->panRecognizer.minDistance() )
72 Q_EMIT swipeDistanceChanged( m_data->panRecognizer.minDistance() );
73}
74
75void QskSwipeView::resetSwipeDistance()
76{
77 setSwipeDistance( qRound( qskMMToPixels( window(), 8 ) ) );
78}
79
80int QskSwipeView::duration() const
81{
82 return m_data->duration;
83}
84
85void QskSwipeView::setDuration( int duration )
86{
87 if ( duration != m_data->duration )
88 {
89 m_data->duration = duration;
90 Q_EMIT durationChanged( duration );
91 }
92}
93
94void QskSwipeView::resetDuration()
95{
96 setDuration( 500 );
97}
98
99void QskSwipeView::gestureEvent( QskGestureEvent* event )
100{
101 const auto gesture = static_cast< const QskPanGesture* >( event->gesture().get() );
102
103 if( gesture->type() == QskGesture::Pan && gesture->state() == QskGesture::Started )
104 {
105 if ( itemCount() <= 1 )
106 return;
107
108 bool forwards;
109
110 if ( orientation() == Qt::Horizontal )
111 forwards = gesture->angle() >= 90.0 && gesture->angle() <= 270.0;
112 else
113 forwards = gesture->angle() >= 180.0;
114
115 auto animator = qobject_cast< QskStackBoxAnimator1* >( this->animator() );
116
117 if ( animator == nullptr )
118 animator = new QskStackBoxAnimator1( this );
119
120 if ( orientation() == Qt::Horizontal )
121 animator->setDirection( forwards ? Qsk::LeftToRight : Qsk::RightToLeft );
122 else
123 animator->setDirection( forwards ? Qsk::TopToBottom : Qsk::BottomToTop );
124
125 animator->setDuration( m_data->duration );
126 QskStackBox::setAnimator( animator );
127
128 auto newIndex = forwards ? currentIndex() + 1 : currentIndex() - 1;
129 if( newIndex < 0 )
130 newIndex += itemCount();
131
132 newIndex %= itemCount();
133
134 setCurrentIndex( newIndex );
135 return;
136 }
137
138 Inherited::gestureEvent( event );
139}
140
141QskAspect::Subcontrol QskSwipeView::effectiveSubcontrol(
142 QskAspect::Subcontrol subControl ) const
143{
144 if ( subControl == QskBox::Panel )
145 return QskSwipeView::Panel;
146
147 return Inherited::effectiveSubcontrol( subControl );
148}
149
150
151#include "moc_QskSwipeView.cpp"
Subcontrol
For use within the rendering or lay-outing of a specific QskSkinnable.
Definition QskAspect.h:104
QskAspect::Subcontrol effectiveSubcontrol(QskAspect::Subcontrol) const
Global definitions.
@ LeftToRight
@ RightToLeft
@ BottomToTop
@ TopToBottom