qskinny/src/controls/QskRadioBoxSkinlet.cpp

296 lines
8.2 KiB
C++
Raw Normal View History

2023-02-26 17:04:47 +01: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 21:49:04 +01:00
#include "QskRadioBoxSkinlet.h"
#include "QskRadioBox.h"
#include "QskFunctions.h"
2023-02-26 17:04:47 +01:00
#include <qfontmetrics.h>
2023-02-08 21:49:04 +01:00
2023-02-26 17:04:47 +01:00
namespace
2023-02-08 21:49:04 +01:00
{
2023-02-26 17:04:47 +01:00
qreal lineHeight( const QskRadioBox* radioBox )
{
using Q = QskRadioBox;
auto strutHight = qMax( radioBox->strutSizeHint( Q::Button ).height(),
radioBox->strutSizeHint( Q::Text ).height() );
const auto textMargins = radioBox->marginHint( Q::Text );
auto fontHeight = radioBox->effectiveFontHeight( Q::Text );
fontHeight += textMargins.top() + textMargins.bottom();
return qMax( strutHight, fontHeight );
}
QRectF lineRect( const QskRadioBox* radioBox, const QRectF& rect, int index )
{
using Q = QskRadioBox;
auto h = lineHeight( radioBox );
auto y = rect.top();
if ( index > 0 )
{
const auto spacing = radioBox->spacingHint( Q::Panel );
y += index * ( h + spacing );
}
return QRectF( rect.x(), y, rect.width(), h );
}
inline qreal maxTextWidth( const QskRadioBox* radioBox )
{
qreal w = 0.0;
const QFontMetrics fm( radioBox->effectiveFont( QskRadioBox::Text ) );
const auto options = radioBox->options();
for( const auto& option : options )
w = std::max( w, qskHorizontalAdvance( fm, option ) );
return w;
}
#if 1
inline qreal lineSpacing( const QskRadioBox* )
{
// skinHint TODO ...
return 10;
}
#endif
2023-02-26 17:04:47 +01:00
}
QskRadioBoxSkinlet::QskRadioBoxSkinlet( QskSkin* )
{
setNodeRoles( { PanelRole, ButtonRole, IndicatorRole, TextRole, RippleRole } );
2023-02-26 17:04:47 +01:00
}
2023-02-26 17:04:47 +01:00
QskRadioBoxSkinlet::~QskRadioBoxSkinlet()
{
}
2023-02-08 21:49:04 +01:00
QRectF QskRadioBoxSkinlet::subControlRect( const QskSkinnable* skinnable,
2023-02-25 23:31:29 +01:00
const QRectF& contentsRect, QskAspect::Subcontrol subcontrol ) const
2023-02-08 21:49:04 +01:00
{
2023-02-26 17:04:47 +01:00
using Q = QskRadioBox;
auto radioBox = static_cast< const QskRadioBox* >( skinnable );
2023-02-08 21:49:04 +01:00
if( subcontrol == Q::Ripple )
return rippleRect( radioBox, contentsRect );
2023-02-08 21:49:04 +01:00
return contentsRect;
2023-02-26 17:04:47 +01:00
}
2023-02-08 21:49:04 +01:00
QSGNode* QskRadioBoxSkinlet::updateSubNode( const QskSkinnable* skinnable,
2023-02-25 23:31:29 +01:00
quint8 nodeRole, QSGNode* node ) const
2023-02-08 21:49:04 +01:00
{
2023-02-26 17:04:47 +01:00
using Q = QskRadioBox;
2023-02-08 21:49:04 +01:00
switch( nodeRole )
{
case PanelRole:
2023-02-26 17:04:47 +01:00
return updateBoxNode( skinnable, node, Q::Panel );
2023-02-08 21:49:04 +01:00
2023-02-11 21:13:32 +01:00
case ButtonRole:
2023-02-26 17:04:47 +01:00
return updateSeriesNode( skinnable, Q::Button, node );
2023-02-08 21:49:04 +01:00
case IndicatorRole:
return updateSeriesNode( skinnable, Q::Indicator, node );
2023-02-26 17:04:47 +01:00
2023-02-08 21:49:04 +01:00
case TextRole:
2023-02-26 17:04:47 +01:00
return updateSeriesNode( skinnable, Q::Text, node );
2023-02-08 21:49:04 +01:00
case RippleRole:
2023-03-03 13:01:07 +01:00
return updateBoxNode( skinnable, node, Q::Ripple );
2023-02-08 21:49:04 +01:00
};
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
2023-02-26 17:04:47 +01:00
int QskRadioBoxSkinlet::sampleCount(
const QskSkinnable* skinnable, QskAspect::Subcontrol ) const
{
const auto radioBox = static_cast< const QskRadioBox* >( skinnable );
return radioBox->options().count();
}
2023-02-26 17:04:47 +01:00
QRectF QskRadioBoxSkinlet::rippleRect(
const QskRadioBox* radioBox, const QRectF& rect ) const
{
using Q = QskRadioBox;
auto index = radioBox->positionHint( Q::Ripple );
if( index < 0 )
2023-02-26 17:04:47 +01:00
return QRectF();
QRectF r;
r.setSize( radioBox->strutSizeHint( Q::Ripple ) );
2023-02-26 17:04:47 +01:00
if ( !r.isEmpty() )
{
const auto buttonRect = sampleRect( radioBox, rect, Q::Button, index );
r.moveCenter( buttonRect.center() );
}
2023-02-26 17:04:47 +01:00
return r;
}
2023-02-26 17:04:47 +01:00
QRectF QskRadioBoxSkinlet::buttonRect( const QskRadioBox* radioBox,
const QRectF& rect, int index ) const
2023-02-26 17:04:47 +01:00
{
using Q = QskRadioBox;
auto r = lineRect( radioBox, rect, index );
r = r.marginsRemoved( radioBox->paddingHint( Q::Panel ) );
auto alignment = radioBox->alignmentHint( Q::Button, Qt::AlignCenter );
alignment &= Qt::AlignVertical_Mask;
alignment |= radioBox->layoutMirroring() ? Qt::AlignRight : Qt::AlignLeft;
2023-02-26 17:04:47 +01:00
auto size = radioBox->strutSizeHint( Q::Button );
size = size.grownBy( radioBox->marginHint( Q::Button ) );
return qskAlignedRectF( r, size.width(), size.height(), alignment );
2023-02-08 21:49:04 +01:00
}
2023-02-26 17:04:47 +01:00
QRectF QskRadioBoxSkinlet::textRect( const QskRadioBox* radioBox,
const QRectF& rect, int index ) const
{
using Q = QskRadioBox;
2023-02-25 22:24:39 +01:00
auto r = lineRect( radioBox, rect, index );
r = r.marginsRemoved( radioBox->paddingHint( Q::Panel ) );
2023-02-08 21:49:04 +01:00
const auto buttonRect = sampleRect( radioBox, rect, Q::Button, index );
const auto spacing = lineSpacing( radioBox );
2023-02-08 21:49:04 +01:00
if ( !radioBox->layoutMirroring() )
r.setLeft( buttonRect.right() + spacing );
2023-02-26 17:04:47 +01:00
else
r.setRight( buttonRect.left() - spacing );
2023-02-08 21:49:04 +01:00
return r;
2023-02-08 21:49:04 +01:00
}
QRectF QskRadioBoxSkinlet::sampleRect( const QskSkinnable* skinnable,
const QRectF& rect, QskAspect::Subcontrol subControl, int index ) const
2023-02-26 17:04:47 +01:00
{
using Q = QskRadioBox;
2023-02-25 23:31:29 +01:00
2023-02-26 17:04:47 +01:00
auto radioBox = static_cast< const QskRadioBox* >( skinnable );
2023-02-08 21:49:04 +01:00
if( subControl == Q::Text )
2023-02-26 17:04:47 +01:00
return textRect( radioBox, rect, index );
2023-02-08 21:49:04 +01:00
if( subControl == Q::Button )
return buttonRect( radioBox, rect, index);
if( subControl == Q::Indicator )
{
auto r = sampleRect( radioBox, rect, Q::Button, index );
r = r.marginsRemoved( radioBox->paddingHint( Q::Button ) );
return r;
}
return QRectF();
2023-02-08 21:49:04 +01:00
}
2023-03-03 13:01:07 +01:00
QskAspect::States QskRadioBoxSkinlet::sampleStates(
const QskSkinnable* skinnable, QskAspect::Subcontrol, int index ) const
2023-02-26 17:04:47 +01:00
{
2023-03-03 13:01:07 +01:00
using Q = QskRadioBox;
2023-02-26 17:04:47 +01:00
auto radioBox = static_cast< const QskRadioBox* >( skinnable );
2023-02-08 21:49:04 +01:00
2023-03-03 13:01:07 +01:00
auto states = radioBox->skinStates();
if( radioBox->selectedIndex() == index )
states |= Q::Selected;
if( radioBox->pressedIndex() == index )
states |= Q::Pressed;
#if 1
if( radioBox->positionHint( Q::Ripple ) == index )
states |= Q::Focused;
else
states &= ~Q::Focused;
#endif
return states;
2023-02-08 21:49:04 +01:00
}
2023-02-26 17:04:47 +01:00
QSGNode* QskRadioBoxSkinlet::updateSampleNode( const QskSkinnable* skinnable,
QskAspect::Subcontrol subcontrol, int index, QSGNode* node ) const
{
using Q = QskRadioBox;
2023-02-25 23:31:29 +01:00
2023-02-26 17:04:47 +01:00
auto radioBox = static_cast< const QskRadioBox* >( skinnable );
2023-02-08 21:49:04 +01:00
2023-02-26 17:04:47 +01:00
auto rect = sampleRect( skinnable, radioBox->contentsRect(), subcontrol, index );
if( subcontrol == Q::Text )
{
Qt::Alignment alignment = Qt::AlignVCenter;
alignment |= ( radioBox->layoutMirroring() ? Qt::AlignRight : Qt::AlignLeft );
alignment = radioBox->alignmentHint( Q::Text, Qt::AlignCenter );
alignment &= Qt::AlignVertical_Mask;
alignment |= radioBox->layoutMirroring() ? Qt::AlignRight : Qt::AlignLeft;
return updateTextNode( radioBox, node, rect,
alignment, radioBox->option( index ), subcontrol );
2023-02-26 17:04:47 +01:00
}
if ( subcontrol == Q::Button || subcontrol == Q::Indicator )
return updateBoxNode( radioBox, node, rect, subcontrol );
return node;
}
QSizeF QskRadioBoxSkinlet::sizeHint( const QskSkinnable* skinnable,
Qt::SizeHint which, const QSizeF& constraint ) const
{
using Q = QskRadioBox;
const auto radioBox = static_cast< const QskRadioBox* >( skinnable );
if ( which != Qt::PreferredSize )
return QSizeF();
if ( constraint.width() >= 0.0 )
2023-02-26 17:04:47 +01:00
{
// heightForWidth would make sense when word wrapping is enabled TODO ...
2023-02-26 17:04:47 +01:00
}
QSizeF textSize( maxTextWidth( radioBox ), 0.0 );
2023-02-26 17:04:47 +01:00
textSize = textSize.expandedTo( skinnable->strutSizeHint( Q::Text ) );
textSize = textSize.grownBy( skinnable->marginHint( Q::Text ) );
2023-02-26 17:04:47 +01:00
QSizeF buttonSize = skinnable->strutSizeHint( Q::Button );
buttonSize = buttonSize.grownBy( skinnable->marginHint( Q::Button ) );
2023-02-26 17:04:47 +01:00
const auto count = std::max( ( int )radioBox->options().count(), 1 );
2023-02-26 17:04:47 +01:00
const qreal w = textSize.width() + lineSpacing( radioBox ) + buttonSize.width();
const qreal h = count * std::max( textSize.height(), buttonSize.height() )
+ ( count - 1 ) * skinnable->spacingHint( Q::Panel );
2023-02-26 17:04:47 +01:00
QSizeF hint( w, h );
hint = hint.grownBy( skinnable->paddingHint( Q::Panel ) );
hint = hint.expandedTo( skinnable->strutSizeHint( Q::Panel ) );
2023-02-08 21:49:04 +01:00
return hint;
2023-02-08 21:49:04 +01:00
}
2023-02-20 22:13:14 +01:00
#include "moc_QskRadioBoxSkinlet.cpp"