qskinny/src/controls/QskRadioBox.cpp

271 lines
6.4 KiB
C++
Raw Normal View History

2023-02-26 16:04:47 +00:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
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-25 22:31:29 +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
2023-02-26 16:04:47 +00:00
class QskRadioBox::PrivateData
{
public:
2023-02-08 20:49:04 +00:00
QStringList items;
2023-02-26 16:04:47 +00:00
2023-02-08 20:49:04 +00:00
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;
};
2023-02-26 16:04:47 +00:00
QskRadioBox::QskRadioBox( QQuickItem* parent )
: Inherited( parent )
, m_data( new PrivateData() )
{
2023-02-08 20:49:04 +00:00
setFocusPolicy( Qt::NoFocus );
setAcceptedMouseButtons( Qt::LeftButton );
connect(this, &QskRadioBox::itemsChanged, this,
2023-02-26 16:04:47 +00:00
[this]( const QStringList& items )
{ setFocusPolicy( items.count() > 0 ? Qt::StrongFocus : Qt::NoFocus ); }
2023-02-26 16:04:47 +00:00
);
2023-02-14 23:20:19 +00:00
setFocusedIndex( -1 );
2023-02-08 20:49:04 +00:00
}
2023-02-26 16:04:47 +00:00
QskRadioBox::QskRadioBox( const QStringList& list, QQuickItem* parent )
: QskRadioBox( parent )
{
2023-02-08 20:49:04 +00:00
setItems( list );
}
QskRadioBox::QskRadioBox( const QStringList& items,
2023-02-26 16:04:47 +00:00
int selectedIndex, QQuickItem* parent )
: QskRadioBox( items, parent )
{
2023-02-08 20:49:04 +00:00
if( selectedIndex >= 0 && selectedIndex < items.count() )
2023-02-26 16:04:47 +00:00
m_data->selectedIndex = selectedIndex;
}
QskRadioBox::~QskRadioBox()
{
2023-02-08 20:49:04 +00:00
}
2023-02-26 16:04:47 +00:00
QRectF QskRadioBox::focusIndicatorRect() const
{
if( m_data->focusedIndex > -1)
{
auto skinlet = effectiveSkinlet();
auto textRect = skinlet->sampleRect( this,
contentsRect(), QskRadioBox::Text, m_data->focusedIndex );
2023-02-14 23:20:19 +00:00
2023-02-26 16:04:47 +00:00
auto buttonRect = skinlet->sampleRect( this,
contentsRect(), QskRadioBox::Button, m_data->focusedIndex );
2023-02-14 23:20:19 +00:00
2023-02-26 16:04:47 +00:00
if( textRect == QRectF() )
return buttonRect;
auto result = QRectF(
qMin( textRect.x(), buttonRect.x() ),
2023-02-25 22:31:29 +00:00
qMin( textRect.y(), buttonRect.y() ),
buttonRect.width() + textRect.width(),
2023-02-26 16:04:47 +00:00
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;
2023-02-14 23:20:19 +00:00
}
return QRectF();
}
2023-02-26 16:04:47 +00:00
int QskRadioBox::selectedIndex() const
{
2023-02-08 20:49:04 +00:00
return m_data->selectedIndex;
}
2023-02-26 16:04:47 +00:00
const QStringList& QskRadioBox::items() const
{
2023-02-08 20:49:04 +00:00
return m_data->items;
}
2023-02-26 16:04:47 +00:00
int QskRadioBox::pressedIndex() const
{
return m_data->pressedIndex;
}
2023-02-26 16:04:47 +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;
2023-02-08 20:49:04 +00:00
selectedIndexChanged( m_data->selectedIndex );
}
2023-02-26 16:04:47 +00:00
void QskRadioBox::setItems( const QStringList& items )
{
if( m_data->items == items )
return;
2023-02-08 20:49:04 +00:00
m_data->items = items;
2023-02-26 16:04:47 +00:00
2023-02-08 20:49:04 +00:00
itemsChanged( items );
setSelectedIndex( m_data->selectedIndex );
2023-02-14 23:20:19 +00:00
2023-02-26 16:04:47 +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() )
{
2023-02-26 16:04:47 +00:00
case Qt::Key_Up:
case Qt::Key_Left:
{
m_data->selectedIndex = qMax( m_data->selectedIndex - 1, 0 );
setFocusedIndex( m_data->selectedIndex );
update();
return;
}
case Qt::Key_Down:
case Qt::Key_Right:
{
m_data->selectedIndex = qMin( m_data->selectedIndex + 1,
items().size() - 1 );
setFocusedIndex( m_data->selectedIndex );
update();
return;
}
case Qt::Key_Select:
case Qt::Key_Return:
case Qt::Key_Space:
{
m_data->selectedIndex = m_data->focusedIndex;
update();
return;
}
2023-02-08 20:49:04 +00:00
}
2023-02-26 16:04:47 +00:00
const auto currentTabIndex = m_data->focusedIndex;
const auto nextTabIndex = currentTabIndex + qskFocusChainIncrement( event );
2023-02-26 16:04:47 +00:00
if( nextTabIndex >= items().size() || nextTabIndex < 0 )
{
Inherited::keyPressEvent( event );
setFocusedIndex( -1 );
}
else
{
setFocusedIndex( ( float ) nextTabIndex );
2023-02-26 16:04:47 +00:00
const auto aspect = Ripple | QskAspect::Metric | QskAspect::Position;
const auto hint = animationHint( aspect | skinStates() );
2023-02-26 16:04:47 +00:00
if( hint.isValid() )
{
startTransition( aspect, hint,
( float ) currentTabIndex, ( float ) nextTabIndex );
}
2023-02-08 20:49:04 +00:00
}
2023-02-26 16:04:47 +00:00
update();
2023-02-08 20:49:04 +00:00
}
2023-02-26 16:04:47 +00:00
void QskRadioBox::keyReleaseEvent( QKeyEvent* )
2023-02-08 20:49:04 +00:00
{
}
void QskRadioBox::mousePressEvent( QMouseEvent* e )
{
2023-02-25 22:31:29 +00:00
auto indexAtPosition = indexAt( e->localPos() );
2023-02-08 20:49:04 +00:00
m_data->pressedIndex = indexAtPosition;
m_data->selectedIndex = -1;
2023-02-14 23:20:19 +00:00
setFocusedIndex( indexAtPosition );
update();
2023-02-08 20:49:04 +00:00
}
void QskRadioBox::mouseReleaseEvent( QMouseEvent* e )
{
2023-02-26 16:04:47 +00:00
const auto index = indexAt( e->localPos() );
if( index == m_data->pressedIndex )
setSelectedIndex( index );
2023-02-08 20:49:04 +00:00
update();
}
2023-02-26 16:04:47 +00:00
void QskRadioBox::focusInEvent( QFocusEvent* e )
{
if( e->reason() == Qt::TabFocusReason )
{
setFocusedIndex( 0 );
}
else if( e->reason() == Qt::BacktabFocusReason )
{
setFocusedIndex( items().size() - 1 );
2023-02-08 20:49:04 +00:00
}
update();
2023-02-08 20:49:04 +00:00
Inherited::focusInEvent( e );
}
2023-02-26 16:04:47 +00:00
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-26 16:04:47 +00:00
2023-02-08 20:49:04 +00:00
Inherited::focusOutEvent( e );
}
2023-02-26 16:04:47 +00:00
int QskRadioBox::indexAt( const QPointF& target ) const
{
const auto itemHeight = contentsRect().height() / items().size();
2023-02-08 20:49:04 +00:00
auto index = target.y() / itemHeight;
2023-02-26 16:04:47 +00:00
if( index < 0 || index >= items().size() )
return -1;
2023-02-08 20:49:04 +00:00
return index;
}
2023-02-26 16:04:47 +00:00
void QskRadioBox::setFocusedIndex( int index )
{
2023-02-14 23:20:19 +00:00
m_data->focusedIndex = index;
setPositionHint( Ripple, index );
focusIndicatorRectChanged();
}
2023-02-08 20:49:04 +00:00
#include "moc_QskRadioBox.cpp"