6#include "QskRadioBox.h"
8#include "QskAnimationHint.h"
25 int selectedIndex = -1;
26 int hoveredIndex = -1;
27 int focusedIndex = -1;
28 int pressedIndex = -1;
31QskRadioBox::QskRadioBox( QQuickItem* parent )
33 , m_data( new PrivateData() )
35 initSizePolicy( QskSizePolicy::Fixed, QskSizePolicy::Fixed );
37 setFocusPolicy( Qt::StrongFocus );
38 setAcceptedMouseButtons( Qt::LeftButton );
40 setPositionHint( CheckIndicator, -1 );
42 setAcceptHoverEvents(
true );
45QskRadioBox::QskRadioBox(
const QStringList& options, QQuickItem* parent )
48 setOptions( options );
51QskRadioBox::QskRadioBox(
const QStringList& items,
52 int selectedIndex, QQuickItem* parent )
55 if( selectedIndex >= 0 && selectedIndex < items.count() )
56 m_data->selectedIndex = selectedIndex;
59QskRadioBox::~QskRadioBox()
65 if ( m_data->focusedIndex < 0 )
72 const auto panelRect =
skinlet->sampleRect(
this,
73 rect, QskRadioBox::CheckIndicatorPanel, m_data->focusedIndex );
75 auto y = panelRect.y();
76 auto h = panelRect.height();
78 const auto textRect =
skinlet->sampleRect(
this,
79 rect, QskRadioBox::Text, m_data->focusedIndex );
81 if( textRect.height() > 0.0 )
83 y = qMin( y, textRect.y() );
84 h = qMax( h, textRect.height() );
87 return QRectF(
rect.x(), y,
rect.width(), h );
90int QskRadioBox::selectedIndex()
const
92 return m_data->selectedIndex;
95QStringList QskRadioBox::options()
const
97 return m_data->options;
100QString QskRadioBox::optionAt(
int index )
const
102 return m_data->options.value( index );
105int QskRadioBox::pressedIndex()
const
107 return m_data->pressedIndex;
110void QskRadioBox::setSelectedIndex(
int index )
112 if( index == m_data->selectedIndex || index >= m_data->options.count() )
116 m_data->selectedIndex = -1;
118 m_data->selectedIndex = index;
120 Q_EMIT selectedIndexChanged( m_data->selectedIndex );
123void QskRadioBox::setOptions(
const QStringList& options )
125 if( m_data->options == options )
128 m_data->options = options;
130 Q_EMIT optionsChanged( options );
131 setSelectedIndex( m_data->selectedIndex );
133 if( m_data->focusedIndex > options.size() )
134 setFocusedIndex( options.size() - 1 );
137void QskRadioBox::keyPressEvent( QKeyEvent* event )
139 switch ( event->key() )
144 m_data->selectedIndex = qMax( m_data->selectedIndex - 1, 0 );
145 setFocusedIndex( m_data->selectedIndex );
153 m_data->selectedIndex = qMin( m_data->selectedIndex + 1,
154 m_data->options.size() - 1 );
156 setFocusedIndex( m_data->selectedIndex );
165 m_data->selectedIndex = m_data->focusedIndex;
172 const auto currentTabIndex = m_data->focusedIndex;
173 const auto nextTabIndex = currentTabIndex + qskFocusChainIncrement( event );
175 if( nextTabIndex >= m_data->options.size() || nextTabIndex < 0 )
177 Inherited::keyPressEvent( event );
178 setFocusedIndex( -1 );
182 setFocusedIndex( nextTabIndex );
188 startTransition( aspect, hint, currentTabIndex, nextTabIndex );
194void QskRadioBox::keyReleaseEvent( QKeyEvent* )
198void QskRadioBox::mousePressEvent( QMouseEvent* event )
200 auto indexAtPosition = indexAt( qskMousePosition( event ) );
202 m_data->pressedIndex = indexAtPosition;
204 setFocusedIndex( indexAtPosition );
208void QskRadioBox::mouseUngrabEvent()
210 if ( m_data->pressedIndex >= 0 )
212 m_data->pressedIndex = -1;
217void QskRadioBox::mouseReleaseEvent( QMouseEvent* event )
219 const auto index = indexAt( qskMousePosition( event ) );
220 if( index == m_data->pressedIndex )
221 setSelectedIndex( index );
223 m_data->pressedIndex = -1;
227void QskRadioBox::hoverEnterEvent( QHoverEvent* event )
229 const auto index = indexAt( qskHoverPosition( event ) );
230 setHoveredIndex( index );
233void QskRadioBox::hoverMoveEvent( QHoverEvent* event )
235 const auto index = indexAt( qskHoverPosition( event ) );
236 setHoveredIndex( index );
239void QskRadioBox::hoverLeaveEvent( QHoverEvent* )
241 setHoveredIndex( -1 );
244void QskRadioBox::focusInEvent( QFocusEvent* event )
246 if( event->reason() == Qt::TabFocusReason )
248 setFocusedIndex( 0 );
250 else if( event->reason() == Qt::BacktabFocusReason )
252 setFocusedIndex( m_data->options.size() - 1 );
256 Inherited::focusInEvent( event );
259void QskRadioBox::focusOutEvent( QFocusEvent* event )
261 setFocusedIndex( -1 );
264 Inherited::focusOutEvent( event );
267int QskRadioBox::indexAt(
const QPointF& pos )
const
273void QskRadioBox::setHoveredIndex(
int index )
275 if ( m_data->hoveredIndex == index )
278 m_data->hoveredIndex = index;
279 setPositionHint( CheckIndicator |
Hovered, index );
284void QskRadioBox::setFocusedIndex(
int index )
286 if ( m_data->focusedIndex == index )
289 m_data->focusedIndex = index;
290 setPositionHint( CheckIndicator, index );
297#include "moc_QskRadioBox.cpp"
Lookup key for a QskSkinHintTable.
void focusIndicatorRectChanged()
static const QskAspect::State Hovered
QRectF contentsRect() const
QRectF focusIndicatorRect() const override
const QskSkinlet * skinlet() const
QskAnimationHint animationHint(QskAspect, QskSkinHintStatus *=nullptr) const
const QskSkinlet * effectiveSkinlet() const