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"
|
2023-02-12 01:42:33 +00:00
|
|
|
#include "QskAnimationHint.h"
|
2023-02-14 23:20:19 +00:00
|
|
|
#include "QskSkinlet.h"
|
2023-02-12 01:42:33 +00:00
|
|
|
|
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-03-05 15:31:55 +00:00
|
|
|
QSK_SUBCONTROL( QskRadioBox, CheckIndicatorPanel )
|
|
|
|
QSK_SUBCONTROL( QskRadioBox, CheckIndicator )
|
2023-02-08 20:49:04 +00:00
|
|
|
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 )
|
2023-02-08 20:49:04 +00:00
|
|
|
|
2023-02-26 16:04:47 +00:00
|
|
|
class QskRadioBox::PrivateData
|
|
|
|
{
|
|
|
|
public:
|
2023-03-03 11:49:22 +00:00
|
|
|
QStringList options;
|
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-03-03 11:49:22 +00:00
|
|
|
initSizePolicy( QskSizePolicy::Minimum, QskSizePolicy::Minimum );
|
2023-02-08 20:49:04 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
setFocusPolicy( Qt::StrongFocus );
|
|
|
|
setAcceptedMouseButtons( Qt::LeftButton );
|
2023-02-12 01:42:33 +00:00
|
|
|
|
2023-02-14 23:20:19 +00:00
|
|
|
setFocusedIndex( -1 );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
QskRadioBox::QskRadioBox( const QStringList& options, QQuickItem* parent )
|
2023-02-26 16:04:47 +00:00
|
|
|
: QskRadioBox( parent )
|
|
|
|
{
|
2023-03-03 11:49:22 +00:00
|
|
|
setOptions( options );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
{
|
2023-03-03 11:49:22 +00:00
|
|
|
if ( m_data->focusedIndex < 0 )
|
|
|
|
return QRectF();
|
2023-02-26 16:04:47 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
const auto rect = contentsRect();
|
2023-02-14 23:20:19 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
auto skinlet = effectiveSkinlet();
|
2023-02-14 23:20:19 +00:00
|
|
|
|
2023-03-05 15:31:55 +00:00
|
|
|
const auto panelRect = skinlet->sampleRect( this,
|
|
|
|
rect, QskRadioBox::CheckIndicatorPanel, m_data->focusedIndex );
|
2023-02-26 16:04:47 +00:00
|
|
|
|
2023-03-05 15:31:55 +00:00
|
|
|
auto y = panelRect.y();
|
|
|
|
auto h = panelRect.height();
|
2023-02-26 16:04:47 +00:00
|
|
|
|
2023-03-05 15:31:55 +00:00
|
|
|
const auto textRect = skinlet->sampleRect( this,
|
2023-03-03 11:49:22 +00:00
|
|
|
rect, QskRadioBox::Text, m_data->focusedIndex );
|
2023-02-26 16:04:47 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
if( textRect.height() > 0.0 )
|
|
|
|
{
|
2023-03-05 15:31:55 +00:00
|
|
|
y = qMin( y, textRect.y() );
|
|
|
|
h = qMax( h, textRect.height() );
|
2023-02-14 23:20:19 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
return QRectF( rect.x(), y, rect.width(), h );
|
2023-02-14 23:20:19 +00:00
|
|
|
}
|
|
|
|
|
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-03-03 11:49:22 +00:00
|
|
|
QStringList QskRadioBox::options() const
|
2023-02-26 16:04:47 +00:00
|
|
|
{
|
2023-03-03 11:49:22 +00:00
|
|
|
return m_data->options;
|
|
|
|
}
|
|
|
|
|
2023-03-05 15:31:55 +00:00
|
|
|
QString QskRadioBox::optionAt( int index ) const
|
2023-03-03 11:49:22 +00:00
|
|
|
{
|
|
|
|
return m_data->options.value( index );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 16:04:47 +00:00
|
|
|
int QskRadioBox::pressedIndex() const
|
|
|
|
{
|
2023-02-12 00:32:21 +00:00
|
|
|
return m_data->pressedIndex;
|
|
|
|
}
|
|
|
|
|
2023-02-26 16:04:47 +00:00
|
|
|
void QskRadioBox::setSelectedIndex( int index )
|
|
|
|
{
|
2023-03-03 11:49:22 +00:00
|
|
|
if( index == m_data->selectedIndex || index >= m_data->options.count() )
|
2023-02-26 16:04:47 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if( index < 0 )
|
|
|
|
m_data->selectedIndex = -1;
|
|
|
|
else
|
|
|
|
m_data->selectedIndex = index;
|
2023-02-08 20:49:04 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
Q_EMIT selectedIndexChanged( m_data->selectedIndex );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
void QskRadioBox::setOptions( const QStringList& options )
|
2023-02-26 16:04:47 +00:00
|
|
|
{
|
2023-03-03 11:49:22 +00:00
|
|
|
if( m_data->options == options )
|
2023-02-26 16:04:47 +00:00
|
|
|
return;
|
2023-02-08 20:49:04 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
m_data->options = options;
|
2023-02-26 16:04:47 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
Q_EMIT optionsChanged( options );
|
2023-02-08 20:49:04 +00:00
|
|
|
setSelectedIndex( m_data->selectedIndex );
|
2023-02-14 23:20:19 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
if( m_data->focusedIndex > options.size() )
|
2023-02-26 16:04:47 +00:00
|
|
|
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,
|
2023-03-03 11:49:22 +00:00
|
|
|
m_data->options.size() - 1 );
|
|
|
|
|
2023-02-26 16:04:47 +00:00
|
|
|
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-12 01:42:33 +00:00
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
if( nextTabIndex >= m_data->options.size() || nextTabIndex < 0 )
|
2023-02-26 16:04:47 +00:00
|
|
|
{
|
|
|
|
Inherited::keyPressEvent( event );
|
|
|
|
setFocusedIndex( -1 );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
setFocusedIndex( ( float ) nextTabIndex );
|
2023-02-12 01:42:33 +00:00
|
|
|
|
2023-02-26 16:04:47 +00:00
|
|
|
const auto aspect = Ripple | QskAspect::Metric | QskAspect::Position;
|
|
|
|
const auto hint = animationHint( aspect | skinStates() );
|
2023-02-12 01:42:33 +00:00
|
|
|
|
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
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-28 14:55:13 +00:00
|
|
|
void QskRadioBox::mousePressEvent( QMouseEvent* event )
|
2023-02-08 20:49:04 +00:00
|
|
|
{
|
2023-02-28 14:55:13 +00:00
|
|
|
auto indexAtPosition = indexAt( qskMousePosition( event ) );
|
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 );
|
2023-02-12 01:42:33 +00:00
|
|
|
update();
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-05 15:54:22 +00:00
|
|
|
void QskRadioBox::mouseUngrabEvent()
|
|
|
|
{
|
|
|
|
if ( m_data->pressedIndex >= 0 )
|
|
|
|
{
|
|
|
|
m_data->pressedIndex = -1;
|
|
|
|
update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-28 14:55:13 +00:00
|
|
|
void QskRadioBox::mouseReleaseEvent( QMouseEvent* event )
|
2023-02-08 20:49:04 +00:00
|
|
|
{
|
2023-02-28 14:55:13 +00:00
|
|
|
const auto index = indexAt( qskMousePosition( event ) );
|
2023-02-26 16:04:47 +00:00
|
|
|
if( index == m_data->pressedIndex )
|
|
|
|
setSelectedIndex( index );
|
2023-02-08 20:49:04 +00:00
|
|
|
|
2023-03-05 15:54:22 +00:00
|
|
|
m_data->pressedIndex = -1;
|
2023-02-08 20:49:04 +00:00
|
|
|
update();
|
|
|
|
}
|
|
|
|
|
2023-02-28 14:55:13 +00:00
|
|
|
void QskRadioBox::focusInEvent( QFocusEvent* event )
|
2023-02-26 16:04:47 +00:00
|
|
|
{
|
2023-02-28 14:55:13 +00:00
|
|
|
if( event->reason() == Qt::TabFocusReason )
|
2023-02-26 16:04:47 +00:00
|
|
|
{
|
|
|
|
setFocusedIndex( 0 );
|
|
|
|
}
|
2023-02-28 14:55:13 +00:00
|
|
|
else if( event->reason() == Qt::BacktabFocusReason )
|
2023-02-26 16:04:47 +00:00
|
|
|
{
|
2023-03-03 11:49:22 +00:00
|
|
|
setFocusedIndex( m_data->options.size() - 1 );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 01:42:33 +00:00
|
|
|
update();
|
2023-02-28 14:55:13 +00:00
|
|
|
Inherited::focusInEvent( event );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-28 14:55:13 +00:00
|
|
|
void QskRadioBox::focusOutEvent( QFocusEvent* event )
|
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
|
|
|
update();
|
2023-02-26 16:04:47 +00:00
|
|
|
|
2023-02-28 14:55:13 +00:00
|
|
|
Inherited::focusOutEvent( event );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-03 11:49:22 +00:00
|
|
|
int QskRadioBox::indexAt( const QPointF& pos ) const
|
2023-02-26 16:04:47 +00:00
|
|
|
{
|
2023-03-05 15:31:55 +00:00
|
|
|
return effectiveSkinlet()->sampleIndexAt( this,
|
|
|
|
contentsRect(), QskRadioBox::Button, pos );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-26 16:04:47 +00:00
|
|
|
void QskRadioBox::setFocusedIndex( int index )
|
|
|
|
{
|
2023-03-05 15:54:22 +00:00
|
|
|
if ( m_data->focusedIndex == index )
|
|
|
|
return;
|
|
|
|
|
2023-02-14 23:20:19 +00:00
|
|
|
m_data->focusedIndex = index;
|
|
|
|
setPositionHint( Ripple, index );
|
2023-03-03 11:49:22 +00:00
|
|
|
|
|
|
|
Q_EMIT focusIndicatorRectChanged();
|
2023-02-14 23:20:19 +00:00
|
|
|
}
|
|
|
|
|
2023-02-08 20:49:04 +00:00
|
|
|
#include "moc_QskRadioBox.cpp"
|