qskinny/src/controls/QskRadioBox.cpp

251 lines
5.8 KiB
C++
Raw Normal View History

2023-02-08 20:49:04 +00:00
#include "QskRadioBox.h"
#include "QskEvent.h"
#include "QskAnimationHint.h"
2023-02-14 23:20:19 +00:00
#include "QskSkinlet.h"
2023-02-08 20:49:04 +00:00
QSK_SUBCONTROL( QskRadioBox, Panel )
2023-02-11 20:13:32 +00:00
QSK_SUBCONTROL( QskRadioBox, Button )
2023-02-08 20:49:04 +00:00
QSK_SUBCONTROL( QskRadioBox, Symbol )
QSK_SUBCONTROL( QskRadioBox, Text )
QSK_SUBCONTROL( QskRadioBox, Ripple )
2023-02-11 22:12:31 +00:00
QSK_STATE( QskRadioBox, Selected, QskAspect::FirstUserState << 1)
QSK_STATE( QskRadioBox, Pressed, QskAspect::FirstUserState << 2)
QSK_STATE( QskRadioBox, Focused, QskAspect::FirstUserState << 3)
2023-02-08 20:49:04 +00:00
class QskRadioBox::PrivateData {
public:
QStringList items;
int selectedIndex = -1;
2023-02-14 23:20:19 +00:00
int focusedIndex = -1;
2023-02-08 20:49:04 +00:00
int pressedIndex = -1;
};
QskRadioBox::QskRadioBox( QQuickItem* parent ) :
Inherited( parent ),
m_data( new PrivateData{} )
{
setFocusPolicy( Qt::NoFocus );
setAcceptedMouseButtons( Qt::LeftButton );
connect(this, &QskRadioBox::itemsChanged, this,
[this]( const QStringList& items ) {
if( items.count() > 0 ) {
setFocusPolicy( Qt::StrongFocus );
} else {
setFocusPolicy( Qt::NoFocus );
}
});
2023-02-14 23:20:19 +00:00
setFocusedIndex( -1 );
2023-02-08 20:49:04 +00:00
}
QskRadioBox::QskRadioBox( const QStringList& list, QQuickItem* parent ) :
QskRadioBox( parent )
{
setItems( list );
}
QskRadioBox::QskRadioBox( const QStringList& items,
int selectedIndex,
QQuickItem* parent ) :
QskRadioBox( items, parent )
{
if( selectedIndex >= 0 && selectedIndex < items.count() )
{
m_data->selectedIndex = selectedIndex;
}
}
2023-02-14 23:20:19 +00:00
QRectF QskRadioBox::focusIndicatorRect() const {
if( m_data->focusedIndex > -1) {
auto textRect = effectiveSkinlet()->sampleRect( this,
contentsRect(), QskRadioBox::Text, m_data->focusedIndex );
auto buttonRect = effectiveSkinlet()->sampleRect( this,
contentsRect(), QskRadioBox::Button, m_data->focusedIndex );
2023-02-25 21:24:39 +00:00
if( textRect == QRectF() ) {
return buttonRect;
}
2023-02-14 23:20:19 +00:00
auto result = QRectF(
qMin( textRect.x(), buttonRect.x() ),
qMin( textRect.y(), buttonRect.y() ),
buttonRect.width() + textRect.width(),
qMax( buttonRect.height(), textRect.height() ));
if( layoutMirroring() ) {
result.setWidth(
result.width() + marginHint( Text ).right()
+ marginHint( Button ).left() );
} else {
result.setWidth(
result.width() + marginHint( Text ).left()
+ marginHint( Button ).right() );
}
return result;
}
return QRectF();
}
2023-02-08 20:49:04 +00:00
int QskRadioBox::selectedIndex() const {
return m_data->selectedIndex;
}
const QStringList& QskRadioBox::items() const {
return m_data->items;
}
int QskRadioBox::pressedIndex() const {
return m_data->pressedIndex;
}
2023-02-08 20:49:04 +00:00
void QskRadioBox::setSelectedIndex( int index ) {
if( index == m_data->selectedIndex
|| index >= m_data->items.count() ) {
return;
}
if( index < 0 ) {
m_data->selectedIndex = -1;
} else {
m_data->selectedIndex = index;
}
selectedIndexChanged( m_data->selectedIndex );
}
void QskRadioBox::setItems( const QStringList& items ){
if( m_data->items == items ) {
return;
}
m_data->items = items;
itemsChanged( items );
setSelectedIndex( m_data->selectedIndex );
2023-02-14 23:20:19 +00:00
if( m_data->focusedIndex > items.size() ) {
setFocusedIndex( 0 );
}
2023-02-08 20:49:04 +00:00
}
void QskRadioBox::keyPressEvent( QKeyEvent* event )
{
switch ( event->key() )
{
case Qt::Key_Up:
case Qt::Key_Left:
m_data->selectedIndex = qMax(m_data->selectedIndex - 1, 0);
2023-02-14 23:20:19 +00:00
setFocusedIndex( m_data->selectedIndex );
2023-02-08 20:49:04 +00:00
event->setAccepted( true );
update();
return;
case Qt::Key_Down:
case Qt::Key_Right:
m_data->selectedIndex = qMin(m_data->selectedIndex + 1, items().size() - 1);
2023-02-14 23:20:19 +00:00
setFocusedIndex( m_data->selectedIndex );
2023-02-08 20:49:04 +00:00
event->setAccepted( true );
update();
return;
case Qt::Key_Select:
case Qt::Key_Return:
case Qt::Key_Space:
2023-02-14 23:20:19 +00:00
m_data->selectedIndex = m_data->focusedIndex;
2023-02-08 20:49:04 +00:00
event->setAccepted( true );
update();
return;
}
2023-02-14 23:20:19 +00:00
auto currentTabIndex = m_data->focusedIndex;
auto nextTabIndex = currentTabIndex + qskFocusChainIncrement( event );
2023-02-08 20:49:04 +00:00
if( nextTabIndex >= items().size()
|| nextTabIndex < 0 ) {
Inherited::keyPressEvent( event );
2023-02-14 23:20:19 +00:00
setFocusedIndex( -1);
update();
2023-02-08 20:49:04 +00:00
} else {
event->setAccepted( true );
2023-02-14 23:20:19 +00:00
setFocusedIndex( (float) nextTabIndex );
const auto aspect = Ripple | QskAspect::Metric | QskAspect::Position;
auto hint = animationHint(aspect | skinStates());
if( hint.isValid()) {
startTransition( aspect,
hint,
(float) currentTabIndex, (float) nextTabIndex );
}
2023-02-08 20:49:04 +00:00
update();
2023-02-08 20:49:04 +00:00
}
}
void QskRadioBox::keyReleaseEvent( QKeyEvent* e )
{
e->setAccepted( true );
}
void QskRadioBox::mousePressEvent( QMouseEvent* e )
{
auto indexAtPosition = indexAt(e->localPos());
m_data->pressedIndex = indexAtPosition;
m_data->selectedIndex = -1;
2023-02-14 23:20:19 +00:00
setFocusedIndex( indexAtPosition );
2023-02-08 20:49:04 +00:00
e->setAccepted( true );
update();
2023-02-08 20:49:04 +00:00
}
void QskRadioBox::mouseReleaseEvent( QMouseEvent* e )
{
auto index = indexAt( e->localPos() );
if( index == m_data->pressedIndex ) {
setSelectedIndex( index );
}
e->setAccepted( true );
update();
}
void QskRadioBox::focusInEvent( QFocusEvent* e ) {
if( e->reason() == Qt::TabFocusReason ) {
2023-02-14 23:20:19 +00:00
setFocusedIndex(0 );
2023-02-08 20:49:04 +00:00
} else if( e->reason() == Qt::BacktabFocusReason ) {
2023-02-14 23:20:19 +00:00
setFocusedIndex(items().size() - 1 );
2023-02-08 20:49:04 +00:00
}
update();
2023-02-08 20:49:04 +00:00
Inherited::focusInEvent( e );
}
void QskRadioBox::focusOutEvent( QFocusEvent* e ) {
2023-02-14 23:20:19 +00:00
setFocusedIndex( -1 );
2023-02-08 20:49:04 +00:00
update();
2023-02-08 20:49:04 +00:00
Inherited::focusOutEvent( e );
}
int QskRadioBox::indexAt( const QPointF& target ) const {
auto itemHeight = contentsRect().height() / items().size();
auto index = target.y() / itemHeight;
if( index < 0 || index >= items().size() )
return -1;
return index;
}
2023-02-14 23:20:19 +00:00
void QskRadioBox::setFocusedIndex( int index ) {
m_data->focusedIndex = index;
setPositionHint( Ripple, index );
focusIndicatorRectChanged();
}
2023-02-08 20:49:04 +00:00
#include "moc_QskRadioBox.cpp"