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-08 20:49:04 +00:00
|
|
|
#include <qnamespace.h>
|
|
|
|
|
|
|
|
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;
|
|
|
|
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-12 01:42:33 +00:00
|
|
|
|
|
|
|
setPositionHint( Ripple, -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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int QskRadioBox::selectedIndex() const {
|
|
|
|
return m_data->selectedIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
const QStringList& QskRadioBox::items() const {
|
|
|
|
return m_data->items;
|
|
|
|
}
|
|
|
|
|
2023-02-12 00:32:21 +00:00
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
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-12 01:42:33 +00:00
|
|
|
setPositionHint( Ripple, 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-12 01:42:33 +00:00
|
|
|
setPositionHint( Ripple, 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-12 01:42:33 +00:00
|
|
|
m_data->selectedIndex = positionHint( Ripple );
|
2023-02-08 20:49:04 +00:00
|
|
|
event->setAccepted( true );
|
|
|
|
update();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-02-12 01:42:33 +00:00
|
|
|
auto currentTabIndex = positionHint( Ripple );
|
|
|
|
auto nextTabIndex = currentTabIndex + qskFocusChainIncrement( event );
|
2023-02-08 20:49:04 +00:00
|
|
|
if( nextTabIndex >= items().size()
|
|
|
|
|| nextTabIndex < 0 ) {
|
2023-02-12 01:42:33 +00:00
|
|
|
Inherited::keyPressEvent( event );
|
|
|
|
setPositionHint(Ripple, -1);
|
|
|
|
update();
|
2023-02-08 20:49:04 +00:00
|
|
|
} else {
|
|
|
|
event->setAccepted( true );
|
2023-02-12 01:42:33 +00:00
|
|
|
setPositionHint( Ripple, (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-12 01:42:33 +00:00
|
|
|
|
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-12 01:42:33 +00:00
|
|
|
setPositionHint( Ripple, indexAtPosition );
|
2023-02-08 20:49:04 +00:00
|
|
|
|
|
|
|
e->setAccepted( true );
|
2023-02-12 01:42:33 +00:00
|
|
|
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-12 01:42:33 +00:00
|
|
|
setPositionHint( Ripple, 0 );
|
2023-02-08 20:49:04 +00:00
|
|
|
} else if( e->reason() == Qt::BacktabFocusReason ) {
|
2023-02-12 01:42:33 +00:00
|
|
|
setPositionHint( Ripple, items().size() - 1 );
|
2023-02-08 20:49:04 +00:00
|
|
|
}
|
|
|
|
|
2023-02-12 01:42:33 +00:00
|
|
|
update();
|
2023-02-08 20:49:04 +00:00
|
|
|
Inherited::focusInEvent( e );
|
|
|
|
}
|
|
|
|
|
|
|
|
void QskRadioBox::focusOutEvent( QFocusEvent* e ) {
|
2023-02-12 01:42:33 +00:00
|
|
|
setPositionHint(Ripple, -1);
|
2023-02-08 20:49:04 +00:00
|
|
|
update();
|
2023-02-12 01:42:33 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_QskRadioBox.cpp"
|