qskinny/examples/gallery/inputs/InputPage.cpp

312 lines
8.6 KiB
C++
Raw Normal View History

2023-02-27 08:56:41 +00:00
/******************************************************************************
2024-01-17 13:31:45 +00:00
* QSkinny - Copyright (C) The authors
2023-04-06 07:23:37 +00:00
* SPDX-License-Identifier: BSD-3-Clause
2023-02-27 08:56:41 +00:00
*****************************************************************************/
#include "InputPage.h"
#include <QskGridBox.h>
#include <QskSlider.h>
2025-01-13 15:26:59 +00:00
#include <QskTextArea.h>
#include <QskTextField.h>
2023-02-27 08:56:41 +00:00
#include <QskSpinBox.h>
2025-02-07 10:35:36 +00:00
#include <QskComboBox.h>
#include <QskSeparator.h>
#include <QskFunctions.h>
#include <QskFontRole.h>
#include <QFontMetricsF>
2023-02-27 08:56:41 +00:00
namespace
{
class Slider : public QskSlider
{
public:
2024-10-22 13:45:09 +00:00
enum Style
{
2024-11-21 12:54:01 +00:00
Continuous,
2024-10-22 13:45:09 +00:00
Discrete,
Centered
};
2024-10-22 11:10:38 +00:00
Slider( Qt::Orientation orientation,
2024-10-22 13:45:09 +00:00
Style style, QQuickItem* parent = nullptr )
2023-02-27 08:56:41 +00:00
: QskSlider( orientation, parent )
{
2023-03-01 16:47:50 +00:00
setBoundaries( 0, 100 );
setValue( 30 );
2023-02-27 08:56:41 +00:00
2024-10-22 13:45:09 +00:00
switch( style )
2024-10-22 11:10:38 +00:00
{
2024-10-22 13:45:09 +00:00
case Discrete:
{
2024-11-21 12:54:01 +00:00
setSnapping( true );
2024-10-22 13:45:09 +00:00
setStepSize( 5 );
setPageSteps( 4 );
break;
}
2024-11-21 12:54:01 +00:00
case Continuous:
2024-10-22 13:45:09 +00:00
{
2024-11-21 12:54:01 +00:00
setSnapping( false );
2024-10-22 13:45:09 +00:00
setStepSize( 1 );
setPageSteps( 10 );
break;
}
case Centered:
{
// TODO
break;
}
2024-10-22 11:10:38 +00:00
}
2023-02-27 08:56:41 +00:00
#if 0
connect( this, &QskSlider::valueChanged,
[]( qreal value ) { qDebug() << value; } );
#endif
}
};
2025-03-14 13:06:38 +00:00
class TextField : public QskTextField
{
public:
enum Style
{
OutlinedStyle,
FilledStyle
};
TextField( QQuickItem* parent = nullptr )
: QskTextField( parent )
{
}
void setStyle( Style style )
{
if ( style != m_style )
{
m_style = style;
resetImplicitSize();
polish();
update();
}
}
Style style() const
{
return m_style;
}
QskAspect::Variation effectiveVariation() const override
{
return static_cast< QskAspect::Variation >( m_style );
}
private:
Style m_style = OutlinedStyle;
};
2024-12-03 12:33:17 +00:00
class TextInputBox : public QskLinearBox
2023-02-27 08:56:41 +00:00
{
public:
2024-12-03 12:33:17 +00:00
TextInputBox( QQuickItem* parent = nullptr )
2025-02-07 10:35:36 +00:00
: QskLinearBox( Qt::Horizontal, 3, parent )
2023-02-27 08:56:41 +00:00
{
setSpacing( 20 );
2025-02-07 10:35:36 +00:00
setDefaultAlignment( Qt::AlignHCenter | Qt::AlignTop );
2023-02-27 08:56:41 +00:00
{
2025-03-14 13:06:38 +00:00
auto field = new TextField( this );
2025-02-07 10:35:36 +00:00
field->setHeaderText( "Name" );
field->setText( "John Doe" );
field->setPlaceholderText( "<Name>" );
field->setFooterText( "Required *" );
}
2025-02-07 10:35:36 +00:00
{
2025-03-14 13:06:38 +00:00
auto field = new TextField( this );
2025-02-07 10:35:36 +00:00
field->setHeaderText( "Nickname" );
field->setPlaceholderText( "<Nickname>" );
field->setFooterText( "Optional" );
}
{
2025-03-14 13:06:38 +00:00
auto field = new TextField( this );
2025-02-07 10:35:36 +00:00
field->setIcon( {} );
field->setPlaceholderText( "<no header>" );
}
2025-02-07 10:35:36 +00:00
{
2025-03-14 13:06:38 +00:00
auto field = new TextField( this );
2025-02-07 10:35:36 +00:00
field->setSkinStateFlag( QskTextField::Error );
field->setText( "Error Text" );
field->setHeaderText( "error" );
field->setPlaceholderText( "<text>" );
field->setFooterText( "error text" );
}
2023-02-27 08:56:41 +00:00
2025-02-07 10:35:36 +00:00
{
2025-03-14 13:06:38 +00:00
auto field = new TextField( this );
2025-02-07 10:35:36 +00:00
field->setReadOnly( true );
field->setText( "Read Only" );
field->setHeaderText( "read only" );
field->setSizePolicy( Qt::Horizontal, QskSizePolicy::MinimumExpanding );
}
2023-02-27 08:56:41 +00:00
2025-02-07 10:35:36 +00:00
{
2025-03-14 13:06:38 +00:00
auto field = new TextField( this );
2025-02-07 10:35:36 +00:00
field->setMaxLength( 15 );
field->setHeaderText( "password" );
field->setEchoMode( QskTextField::Password );
field->setPlaceholderText( "<password>" );
2023-02-27 08:56:41 +00:00
}
}
2025-02-07 10:35:36 +00:00
2025-03-14 13:06:38 +00:00
void setStyle( int value )
2025-02-07 10:35:36 +00:00
{
auto textFields = findChildren< QskTextField* >();
for ( auto field : textFields )
2025-03-14 13:06:38 +00:00
{
const auto style = static_cast< TextField::Style >( value );
dynamic_cast< TextField* >(field)->setStyle( style );
}
2025-02-07 10:35:36 +00:00
}
2023-02-27 08:56:41 +00:00
};
2025-01-13 15:26:59 +00:00
class TextAreaBox : public QskLinearBox
{
public:
TextAreaBox( QQuickItem* parent = nullptr )
: QskLinearBox( Qt::Horizontal, parent )
{
setSpacing( 20 );
{
auto textArea = new QskTextArea( "here enter longer text\nwith multiple lines", this );
2025-01-13 15:26:59 +00:00
textArea->setWrapMode( QskTextOptions::Wrap );
textArea->setPlaceholderText( "placeholder text" );
#if 0
connect( textArea, &QskTextArea::textChanged,
this, [textArea]() { qDebug() << "Text:" << textArea->text(); } );
#endif
2025-01-13 15:26:59 +00:00
}
}
};
2025-02-07 10:35:36 +00:00
class StyleComboBox : public QskComboBox
{
public:
StyleComboBox( QQuickItem* parent = nullptr )
: QskComboBox( parent )
{
2025-03-10 14:01:29 +00:00
addOption( QString(), "Outlined" );
2025-03-14 13:06:38 +00:00
addOption( QString(), "Filled" );
2025-02-07 10:35:36 +00:00
}
};
2023-02-27 08:56:41 +00:00
}
InputPage::InputPage( QQuickItem* parent )
: Page( Qt::Horizontal, parent )
{
2024-10-22 13:45:09 +00:00
struct
{
Slider* continous;
2024-10-23 06:44:00 +00:00
Slider* discrete;
2024-11-27 15:27:57 +00:00
Slider* centered;
2024-10-22 13:45:09 +00:00
} sliders[2];
for ( int i = 0; i < 2; i++ )
{
const auto orientation = static_cast< Qt::Orientation >( i + 1 );
2024-10-22 11:10:38 +00:00
2024-11-21 12:54:01 +00:00
sliders[i].continous = new Slider( orientation, Slider::Continuous );
2024-10-23 06:44:00 +00:00
sliders[i].discrete = new Slider( orientation, Slider::Discrete );
2024-11-27 15:27:57 +00:00
auto slider = new Slider( orientation, Slider::Continuous );
slider->setOrigin( slider->minimum()
+ 0.5 * ( slider->maximum() - slider->minimum() ) );
sliders[i].centered = slider;
2024-10-22 13:45:09 +00:00
}
2024-10-22 11:10:38 +00:00
auto spinBox = new QskSpinBox( 0.0, 100.0, 1.0 );
2025-02-07 10:35:36 +00:00
spinBox->setObjectName( "SliderValueSpinBox" );
2024-10-22 11:10:38 +00:00
spinBox->setSizePolicy( Qt::Horizontal, QskSizePolicy::Fixed );
2025-02-07 10:35:36 +00:00
spinBox->setLayoutAlignmentHint( Qt::AlignCenter );
2023-02-27 08:56:41 +00:00
2024-12-03 12:33:17 +00:00
auto textInputBox = new TextInputBox();
textInputBox->setSizePolicy( Qt::Vertical, QskSizePolicy::Fixed );
2024-10-22 11:10:38 +00:00
2025-01-13 15:26:59 +00:00
auto textAreaBox = new TextAreaBox();
2025-02-07 10:35:36 +00:00
auto separator = new QskSeparator();
separator->setMargins( 5, 20, 5, 10 );
2023-02-27 08:56:41 +00:00
2025-02-07 10:35:36 +00:00
auto styleBox = new StyleComboBox();
auto vBox = new QskLinearBox( Qt::Vertical );
vBox->setSpacing( 20 );
2024-10-22 13:45:09 +00:00
vBox->addItem( sliders[0].continous );
2024-10-23 06:44:00 +00:00
vBox->addItem( sliders[0].discrete );
2024-11-27 15:27:57 +00:00
vBox->addItem( sliders[0].centered );
2024-10-22 11:10:38 +00:00
vBox->addItem( spinBox );
2025-01-13 15:26:59 +00:00
vBox->addItem( textInputBox );
2023-02-27 08:56:41 +00:00
2025-02-07 10:35:36 +00:00
auto hBox = new QskLinearBox( Qt::Horizontal );
hBox->setSpacing( 20 );
hBox->addItem( sliders[1].continous );
hBox->addItem( sliders[1].discrete );
hBox->addItem( sliders[1].centered );
auto gridBox = new QskGridBox( this );
gridBox->addItem( spinBox, 0, 0 );
gridBox->addItem( hBox, 1, 0, -1, 1 );
gridBox->addItem( vBox, 0, 1, 1, -1 );
gridBox->addItem( separator, 1, 1 );
gridBox->addItem( styleBox, 1, 2 );
gridBox->addItem( textInputBox, 2, 1, 1, -1 );
gridBox->addItem( textAreaBox, 3, 1, 1, -1 );
gridBox->setRowStretchFactor( 3, 10 );
gridBox->setColumnStretchFactor( 1, 10 );
2023-03-01 16:47:50 +00:00
auto inputs = findChildren< QskBoundedValueInput* >();
for ( auto input : inputs )
{
connect( input, &QskBoundedValueInput::valueChanged,
this, &InputPage::syncValues );
}
2024-10-23 06:44:00 +00:00
spinBox->setValue( 30.0 );
2025-02-07 10:35:36 +00:00
connect( styleBox, &QskComboBox::currentIndexChanged,
textInputBox, &TextInputBox::setStyle );
2025-03-14 13:06:38 +00:00
styleBox->setCurrentIndex( TextField::OutlinedStyle );
2023-03-01 16:47:50 +00:00
}
void InputPage::syncValues( qreal value )
{
static bool blockUpdates = false;
if ( blockUpdates )
return;
blockUpdates = true;
2024-10-22 11:10:38 +00:00
2023-03-01 16:47:50 +00:00
if ( qobject_cast< const QskSlider* >( sender() ) )
{
2025-02-07 10:35:36 +00:00
if ( auto spinBox = findChild< QskSpinBox* >( "SliderValueSpinBox" ) )
2023-03-01 16:47:50 +00:00
spinBox->setValue( value );
}
else
{
auto sliders = findChildren< QskSlider* >();
for ( auto slider : sliders )
slider->setValue( value );
}
blockUpdates = false;
2023-02-27 08:56:41 +00:00
}