Skip to content

Commit

Permalink
Add QskSwipeView
Browse files Browse the repository at this point in the history
Resolves uwerat#107
  • Loading branch information
peter-ha committed Jun 19, 2023
1 parent 58de099 commit 64bc724
Show file tree
Hide file tree
Showing 7 changed files with 230 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/layouts/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(SOURCES
LinearLayoutPage.h LinearLayoutPage.cpp
DynamicConstraintsPage.h DynamicConstraintsPage.cpp
StackLayoutPage.h StackLayoutPage.cpp
SwipeViewPage.h SwipeViewPage.cpp
main.cpp
)
qt_add_resources(SOURCES layouts.qrc)
Expand Down
61 changes: 61 additions & 0 deletions examples/layouts/SwipeViewPage.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/

#include "SwipeViewPage.h"
#include "TestRectangle.h"

#include <QskRgbValue.h>
#include <QskSwipeView.h>

namespace
{
class SwipeView : public QskSwipeView
{
Q_OBJECT

public:
SwipeView( QQuickItem* parent = nullptr )
: QskSwipeView( parent )
{
setObjectName( "SwipeView" );

setBackgroundColor( Qt::white );
setDefaultAlignment( Qt::AlignCenter );

addRectangle( "Gold" );
addRectangle( "SeaGreen" );
addRectangle( "SlateBlue" );
addRectangle( "Peru" );

for ( int i = 0; i < itemCount(); i += 2 )
{
if ( auto control = qskControlCast( itemAtIndex( i ) ) )
control->setFixedSize( 200, 200 );
}
}
private:
void addRectangle( const char* colorName )
{
auto rect = new TestRectangle( colorName );
rect->setText( QString::number( itemCount() + 1 ) );
addItem( rect );
}

};
}

SwipeViewPage::SwipeViewPage( QQuickItem* parent )
: QskLinearBox( Qt::Vertical, parent )
{
setObjectName( "SwipeViewPage" );

setMargins( 10 );
setBackgroundColor( QskRgb::LightSteelBlue );

auto swipeView = new SwipeView();
addItem( swipeView );
}

#include "SwipeViewPage.moc"
14 changes: 14 additions & 0 deletions examples/layouts/SwipeViewPage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/

#pragma once

#include <QskLinearBox.h>

class SwipeViewPage : public QskLinearBox
{
public:
SwipeViewPage( QQuickItem* parent = nullptr );
};
2 changes: 2 additions & 0 deletions examples/layouts/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "LinearLayoutPage.h"
#include "GridLayoutPage.h"
#include "StackLayoutPage.h"
#include "SwipeViewPage.h"
#include "TestRectangle.h"

#include <SkinnyShortcut.h>
Expand Down Expand Up @@ -43,6 +44,7 @@ int main( int argc, char* argv[] )
tabView->addTab( "Linear Layout", new LinearLayoutPage() );
tabView->addTab( "Dynamic\nConstraints", new DynamicConstraintsPage() );
tabView->addTab( "Stack Layout", new StackLayoutPage() );
tabView->addTab( "Swipe View", new SwipeViewPage() );

tabView->setCurrentIndex( 0 );

Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ list(APPEND HEADERS
controls/QskSubWindowSkinlet.h
controls/QskSwitchButton.h
controls/QskSwitchButtonSkinlet.h
controls/QskSwipeView.h
controls/QskTabBar.h
controls/QskTabButton.h
controls/QskTabButtonSkinlet.h
Expand Down Expand Up @@ -334,6 +335,7 @@ list(APPEND SOURCES
controls/QskSubWindowSkinlet.cpp
controls/QskSwitchButton.cpp
controls/QskSwitchButtonSkinlet.cpp
controls/QskSwipeView.cpp
controls/QskTabBar.cpp
controls/QskTabButton.cpp
controls/QskTabButtonSkinlet.cpp
Expand Down
112 changes: 112 additions & 0 deletions src/controls/QskSwipeView.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/******************************************************************************
* 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;
int panRecognizerTimeout = 100;
int duration = 500;
};

QSK_SUBCONTROL( QskSwipeView, Panel )

QskSwipeView::QskSwipeView( QQuickItem* parent )
: Inherited( parent )
, m_data( new PrivateData() )
{
setSubcontrolProxy( QskBox::Panel, Panel );

setFiltersChildMouseEvents( true );

setAcceptedMouseButtons( Qt::LeftButton );

m_data->panRecognizer.setWatchedItem( this );
m_data->panRecognizer.setOrientations( Qt::Horizontal );
m_data->panRecognizer.setMinDistance( 50 );
}

QskSwipeView::~QskSwipeView()
{
}


int QskSwipeView::duration() const
{
return m_data->duration;
}

void QskSwipeView::setDuration( int duration )
{
m_data->duration = duration;
}


bool QskSwipeView::gestureFilter( QQuickItem* item, QEvent* event )
{
// see QskScrollBox.cpp

auto& recognizer = m_data->panRecognizer;

if ( event->type() == QEvent::MouseButtonPress )
{
if ( ( item != this ) && ( recognizer.timeout() < 0 ) )
{
const auto mouseEvent = static_cast< QMouseEvent* >( event );

if ( recognizer.hasProcessedBefore( mouseEvent ) )
return false;
}

recognizer.setTimeout( ( item == this ) ? -1 : m_data->panRecognizerTimeout );
}

return m_data->panRecognizer.processEvent( item, event );
}

void QskSwipeView::gestureEvent( QskGestureEvent* event )
{
if( event->gesture()->type() == QskGesture::Pan && event->gesture()->state() == QskGesture::Started )
{
const auto gesture = static_cast< const QskPanGesture* >( event->gesture().get() );

auto animator = dynamic_cast< QskStackBoxAnimator1* >( this->animator() );

if ( animator == nullptr )
{
animator = new QskStackBoxAnimator1( this );
animator->setOrientation( Qt::Horizontal );
}

animator->setDuration( m_data->duration );
setAnimator( animator );

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 );
}
else
{
Inherited::gestureEvent( event );
}

}

#include "moc_QskSwipeView.cpp"
38 changes: 38 additions & 0 deletions src/controls/QskSwipeView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/

#ifndef QSK_SWIPE_VIEW_H
#define QSK_SWIPE_VIEW_H

#include "QskStackBox.h"

class QskTabBar;
class QskTabButton;

class QSK_EXPORT QskSwipeView : public QskStackBox
{
Q_OBJECT

typedef QskStackBox Inherited;

public:
QSK_SUBCONTROLS( Panel )

QskSwipeView( QQuickItem* parent = nullptr );
~QskSwipeView() override;

int duration() const;
void setDuration( int );

protected:
bool gestureFilter( QQuickItem*, QEvent* ) override final;
void gestureEvent( QskGestureEvent* ) override final;

private:
class PrivateData;
std::unique_ptr< PrivateData > m_data;
};

#endif

0 comments on commit 64bc724

Please sign in to comment.