qskinny/examples/layouts/StackLayoutPage.cpp

129 lines
3.9 KiB
C++
Raw Normal View History

2017-07-21 18:21:34 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "StackLayoutPage.h"
#include "ButtonBox.h"
2018-08-03 08:15:28 +02:00
#include "TestRectangle.h"
2017-07-21 18:21:34 +02:00
#include <QskAspect.h>
#include <QskPageIndicator.h>
2018-08-03 08:15:28 +02:00
#include <QskRgbValue.h>
2017-07-21 18:21:34 +02:00
#include <QskStackBox.h>
#include <QskStackBoxAnimator.h>
2018-08-03 08:15:28 +02:00
#include <QskTextLabel.h>
2017-07-21 18:21:34 +02:00
namespace
{
2019-05-17 22:33:20 +02:00
class StackBox : public QskStackBox
2017-07-21 18:21:34 +02:00
{
2018-08-03 08:15:28 +02:00
public:
2019-05-17 22:33:20 +02:00
StackBox( QQuickItem* parent = nullptr )
2018-08-03 08:15:28 +02:00
: QskStackBox( parent )
2017-07-21 18:21:34 +02:00
{
setObjectName( "StackBox" );
setBackgroundColor( Qt::white );
setMargins( 30 );
addRectangle( "Gold" );
addRectangle( "SeaGreen" );
addRectangle( "SlateBlue" );
addRectangle( "Peru" );
for ( int i = 0; i < itemCount(); i += 2 )
{
2019-04-26 11:56:09 +02:00
if ( auto control = qskControlCast( itemAtIndex( i ) ) )
2017-07-21 18:21:34 +02:00
control->setFixedSize( 200, 200 );
}
}
2018-08-03 08:15:28 +02:00
public:
2017-07-21 18:21:34 +02:00
void incrementFading( int offset )
{
auto animator = dynamic_cast< QskStackBoxAnimator3* >( this->animator() );
if ( animator == nullptr )
{
animator = new QskStackBoxAnimator3( this );
animator->setEasingCurve( QEasingCurve::InQuad );
animator->setDuration( 500 );
}
setAnimator( animator );
setCurrentIndex( incrementedIndex( offset ) );
}
void incrementScrolling( Qt::Orientation orientation, int offset )
{
auto animator = dynamic_cast< QskStackBoxAnimator1* >( this->animator() );
if ( animator == nullptr )
{
animator = new QskStackBoxAnimator1( this );
animator->setDuration( 1000 );
}
animator->setOrientation( orientation );
setAnimator( animator );
setCurrentIndex( incrementedIndex( offset ) );
}
2018-08-03 08:15:28 +02:00
private:
2017-07-21 18:21:34 +02:00
void addRectangle( const char* colorName )
{
auto rect = new TestRectangle( colorName );
rect->setText( QString::number( itemCount() + 1 ) );
addItem( rect, Qt::AlignCenter );
}
int incrementedIndex( int offset ) const
{
int index = currentIndex();
if ( index < 0 )
index = 0;
int newIndex = ( index + offset ) % itemCount();
if ( newIndex < 0 )
newIndex += itemCount();
return newIndex;
}
};
}
2018-08-03 08:15:28 +02:00
StackLayoutPage::StackLayoutPage( QQuickItem* parent )
: QskLinearBox( Qt::Vertical, parent )
2017-07-21 18:21:34 +02:00
{
setObjectName( "StackLayoutPage" );
setMargins( 10 );
setBackgroundColor( QskRgbValue::LightSteelBlue );
2019-05-17 22:33:20 +02:00
auto* box = new StackBox();
2017-07-21 18:21:34 +02:00
auto* buttonBox = new ButtonBox();
buttonBox->setLayoutAlignmentHint( Qt::AlignTop | Qt::AlignLeft );
2017-12-14 09:41:41 +01:00
buttonBox->addButton( "<<", [ box ]() { box->incrementScrolling( Qt::Horizontal, +1 ); } );
buttonBox->addButton( ">>", [ box ]() { box->incrementScrolling( Qt::Horizontal, -1 ); } );
buttonBox->addButton( "^", [ box ]() { box->incrementScrolling( Qt::Vertical, -1 ); } );
buttonBox->addButton( "v", [ box ]() { box->incrementScrolling( Qt::Vertical, +1 ); } );
buttonBox->addButton( "Fading", [ box ]() { box->incrementFading( +1 ); } );
2017-07-21 18:21:34 +02:00
auto pageIndicator = new QskPageIndicator();
pageIndicator->setCount( box->itemCount() );
pageIndicator->setCurrentIndex( box->currentIndex() );
pageIndicator->setLayoutAlignmentHint( Qt::AlignCenter );
2017-07-21 18:21:34 +02:00
addItem( buttonBox );
addItem( pageIndicator );
2017-07-21 18:21:34 +02:00
addItem( box );
connect( box, &QskStackBox::currentIndexChanged,
pageIndicator, &QskPageIndicator::setCurrentIndex );
}