qskinny/src/controls/QskSpinBoxSkinlet.cpp

238 lines
9.0 KiB
C++
Raw Normal View History

2023-02-17 11:01:56 +00:00
/******************************************************************************
2023-02-17 13:05:05 +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-17 11:01:56 +00:00
*****************************************************************************/
#include "QskSpinBoxSkinlet.h"
#include "QskSpinBox.h"
2023-02-17 12:30:39 +00:00
#include <array>
2023-02-17 11:01:56 +00:00
2023-02-17 14:22:40 +00:00
QskSpinBoxSkinlet::QskSpinBoxSkinlet( QskSkin* )
2023-02-17 11:01:56 +00:00
{
2023-02-23 13:37:49 +00:00
setNodeRoles( { UpPanel, DownPanel, TextPanel,
UpIndicator, DownIndicator, Text } );
2023-02-17 11:01:56 +00:00
}
QSizeF QskSpinBoxSkinlet::sizeHint( const QskSkinnable* skinnable,
Qt::SizeHint which, const QSizeF& size ) const
2023-02-17 11:01:56 +00:00
{
if ( which != Qt::PreferredSize )
return QSizeF();
2023-02-23 13:37:49 +00:00
using Q = QskSpinBox;
const auto spinbox = static_cast< const QskSpinBox* >( skinnable );
2023-02-23 13:37:49 +00:00
const auto layout = spinbox->alignmentHint( Q::Panel );
const auto spacing = spinbox->spacingHint( Q::Panel );
2023-02-17 11:01:56 +00:00
2023-02-23 13:37:49 +00:00
const auto strutInc = spinbox->strutSizeHint( Q::UpPanel );
const auto strutDec = spinbox->strutSizeHint( Q::DownPanel );
const auto strutTxt = spinbox->strutSizeHint( Q::TextPanel );
2023-02-17 11:01:56 +00:00
if ( layout == Qt::AlignTop || layout == Qt::AlignBottom || layout == Qt::AlignVCenter )
2023-02-17 11:01:56 +00:00
{
const auto w = qMax( strutDec.width(), qMax( strutTxt.width(), strutInc.width() ) );
const auto h = strutDec.height() + strutTxt.height() + strutInc.height();
2023-02-17 14:22:40 +00:00
return QSizeF( w, h + 2.0 * spacing );
}
2023-02-17 14:22:40 +00:00
if ( layout == Qt::AlignLeft || layout == Qt::AlignRight || layout == Qt::AlignHCenter )
2023-02-17 11:01:56 +00:00
{
const auto w = strutDec.width() + strutTxt.width() + strutInc.width();
const auto h = qMax( strutDec.height(), qMax( strutTxt.height(), strutInc.height() ) );
return QSizeF( w + 2.0 * spacing, h );
2023-02-17 11:01:56 +00:00
}
if ( layout == ( Qt::AlignLeft | Qt::AlignVCenter ) ||
layout == ( Qt::AlignRight | Qt::AlignVCenter ) )
2023-02-17 11:01:56 +00:00
{
const auto w = strutTxt.width() + qMax( strutInc.width(), strutDec.width() );
const auto h =
qMax( 2.0 * qMax( strutInc.height(), strutDec.height() ), strutTxt.height() );
return QSizeF( w + spacing, h + spacing );
2023-02-17 11:01:56 +00:00
}
if ( layout == ( Qt::AlignTop | Qt::AlignHCenter ) ||
2023-02-23 13:37:49 +00:00
layout == ( Qt::AlignBottom | Qt::AlignHCenter ) )
2023-02-17 11:01:56 +00:00
{
const auto w = qMax( strutTxt.width(), strutInc.width() + strutDec.width() );
const auto h = strutTxt.height() + qMax( strutInc.height(), strutDec.height() );
return QSizeF( w + spacing, h + spacing );
2023-02-17 11:01:56 +00:00
}
return Inherited::sizeHint( skinnable, which, size );
}
QRectF QskSpinBoxSkinlet::subControlRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
{
using Q = QskSpinBox;
2023-02-23 13:37:49 +00:00
if ( subControl == Q::DownIndicator )
return skinnable->subControlContentsRect( contentsRect, Q::DownPanel );
2023-02-23 13:37:49 +00:00
if ( subControl == Q::UpIndicator )
return skinnable->subControlContentsRect( contentsRect, Q::UpPanel );
if ( subControl == Q::Text )
return skinnable->subControlContentsRect( contentsRect, Q::TextPanel );
const auto* const spinbox = static_cast< const QskSpinBox* >( skinnable );
const auto layout = spinbox->alignmentHint( Q::Panel );
const auto spacing = spinbox->spacingHint( Q::Panel );
2023-02-17 14:22:40 +00:00
2023-02-23 13:37:49 +00:00
enum
{
Dec = 0,
Txt = 1,
Inc = 2,
Count
};
2023-02-17 14:22:40 +00:00
std::array< QRectF, Count > rects = {
2023-02-23 13:37:49 +00:00
QRectF{ QPointF(), spinbox->strutSizeHint( Q::DownPanel ) },
QRectF{ QPointF(), spinbox->strutSizeHint( Q::TextPanel ) },
2023-02-23 13:37:49 +00:00
QRectF{ QPointF(), spinbox->strutSizeHint( Q::UpPanel ) },
2023-02-17 14:22:40 +00:00
};
const auto center = contentsRect.center();
2023-02-17 14:22:40 +00:00
if ( layout == Qt::AlignLeft )
{
rects[ Txt ].moveTopLeft( { 0.0, center.y() - rects[ Txt ].height() * 0.5 } );
rects[ Dec ].moveTopLeft(
{ rects[ Txt ].right() + spacing, center.y() - rects[ Dec ].height() * 0.5 } );
rects[ Inc ].moveTopLeft(
{ rects[ Dec ].right() + spacing, center.y() - rects[ Inc ].height() * 0.5 } );
}
else if ( layout == Qt::AlignRight )
{
rects[ Dec ].moveTopLeft( { 0.0, center.y() - rects[ Dec ].height() * 0.5 } );
rects[ Inc ].moveTopLeft(
{ rects[ Dec ].right() + spacing, center.y() - rects[ Inc ].height() * 0.5 } );
rects[ Txt ].moveTopLeft(
{ rects[ Inc ].right() + spacing, center.y() - rects[ Txt ].height() * 0.5 } );
}
else if ( layout == Qt::AlignTop )
{
rects[ Txt ].moveTopLeft( { center.x() - rects[ Txt ].width() * 0.5, 0.0 } );
rects[ Inc ].moveTopLeft(
{ center.x() - rects[ Inc ].width() * 0.5, rects[ Txt ].bottom() + spacing } );
rects[ Dec ].moveTopLeft(
{ center.x() - rects[ Dec ].width() * 0.5, rects[ Inc ].bottom() + spacing } );
}
else if ( layout == Qt::AlignBottom )
{
rects[ Inc ].moveTopLeft( { center.x() - rects[ Inc ].width() * 0.5, 0.0 } );
rects[ Dec ].moveTopLeft(
{ center.x() - rects[ Dec ].width() * 0.5, rects[ Inc ].bottom() + spacing } );
rects[ Txt ].moveTopLeft(
{ center.x() - rects[ Txt ].width() * 0.5, rects[ Dec ].bottom() + spacing } );
}
else if ( layout == Qt::AlignHCenter )
{
rects[ Dec ].moveTopLeft( { 0.0, center.y() - rects[ Dec ].height() * 0.5 } );
rects[ Txt ].moveTopLeft(
{ rects[ Dec ].right() + spacing, center.y() - rects[ Txt ].height() * 0.5 } );
rects[ Inc ].moveTopLeft(
{ rects[ Txt ].right() + spacing, center.y() - rects[ Inc ].height() * 0.5 } );
}
else if ( layout == Qt::AlignVCenter )
{
rects[ Inc ].moveTopLeft( { center.x() - rects[ Inc ].width() * 0.5, 0.0 } );
rects[ Txt ].moveTopLeft(
{ center.x() - rects[ Txt ].width() * 0.5, rects[ Inc ].bottom() + spacing } );
rects[ Dec ].moveTopLeft(
{ center.x() - rects[ Dec ].width() * 0.5, rects[ Txt ].bottom() + spacing } );
}
else if ( layout == ( Qt::AlignLeft | Qt::AlignVCenter ) )
{
rects[ Txt ].moveTopLeft( { 0.0, center.y() - rects[ Txt ].height() * 0.5 } );
rects[ Inc ].moveTopLeft( { rects[ Txt ].right() + spacing,
center.y() - spacing * 0.5 - rects[ Inc ].height() } );
rects[ Dec ].moveTopLeft( { rects[ Txt ].right() + spacing, center.y() + spacing * 0.5 } );
}
else if ( layout == ( Qt::AlignRight | Qt::AlignVCenter ) )
{
const auto dx = qMax( rects[ Inc ].width(), rects[ Dec ].width() );
rects[ Inc ].moveTopLeft(
{ dx - rects[ Inc ].width(), center.y() - spacing * 0.5 - rects[ Inc ].height() } );
rects[ Dec ].moveTopLeft( { dx - rects[ Dec ].width(), center.y() + spacing * 0.5 } );
rects[ Txt ].moveTopLeft( { dx + spacing, center.y() - rects[ Txt ].height() * 0.5 } );
}
else if ( layout == ( Qt::AlignTop | Qt::AlignHCenter ) )
{
rects[ Txt ].moveTopLeft( { center.x() - rects[ Txt ].width() * 0.5, 0.0 } );
rects[ Dec ].moveTopLeft(
{ rects[ Txt ].center().x() - spacing * 0.5 - rects[ Dec ].width(),
rects[ Txt ].bottom() + spacing } );
rects[ Inc ].moveTopLeft(
{ rects[ Txt ].center().x() + spacing * 0.5, rects[ Txt ].bottom() + spacing } );
}
else if ( layout == ( Qt::AlignBottom | Qt::AlignHCenter ) )
{
rects[ Txt ].moveTopLeft(
{ center.x() - rects[ Txt ].width() * 0.5, center.y() - rects[ Txt ].height() * 0.5 } );
rects[ Dec ].moveTopLeft( { center.x() - spacing * 0.5 - rects[ Dec ].width(),
rects[ Txt ].top() - spacing - rects[ Dec ].height() } );
rects[ Inc ].moveTopLeft(
{ center.x() + spacing * 0.5, rects[ Txt ].top() - spacing - rects[ Inc ].height() } );
}
2023-02-23 13:37:49 +00:00
if ( subControl == Q::DownPanel )
2023-02-17 14:22:40 +00:00
return rects[ Dec ];
if ( subControl == Q::TextPanel )
2023-02-17 14:22:40 +00:00
return rects[ Txt ];
2023-02-23 13:37:49 +00:00
if ( subControl == Q::UpPanel )
2023-02-17 14:22:40 +00:00
return rects[ Inc ];
return Inherited::subControlRect( skinnable, contentsRect, subControl );
2023-02-17 11:01:56 +00:00
}
2023-02-17 14:22:40 +00:00
QSGNode* QskSpinBoxSkinlet::updateSubNode(
const QskSkinnable* skinnable, const quint8 nodeRole, QSGNode* const node ) const
2023-02-17 11:01:56 +00:00
{
using Q = QskSpinBox;
switch( nodeRole )
2023-02-17 14:22:40 +00:00
{
2023-02-23 13:37:49 +00:00
case UpPanel:
return updateBoxNode( skinnable, node, Q::UpPanel );
2023-02-23 13:37:49 +00:00
case DownPanel:
return updateBoxNode( skinnable, node, Q::DownPanel );
2023-02-23 13:37:49 +00:00
case UpIndicator:
{
return updateTextNode( skinnable, node,
2023-02-23 13:37:49 +00:00
QStringLiteral( "+" ), Q::UpIndicator );
}
2023-02-23 13:37:49 +00:00
case DownIndicator:
{
return updateTextNode( skinnable, node,
2023-02-23 13:37:49 +00:00
QStringLiteral( "-" ), Q::DownIndicator );
}
case TextPanel:
2023-02-23 13:37:49 +00:00
return updateBoxNode( skinnable, node, Q::TextPanel );
case Text:
{
const auto* const spinbox = static_cast< const QskSpinBox* >( skinnable );
2023-02-24 07:24:32 +00:00
return updateTextNode( skinnable, node, spinbox->text(), Q::Text );
}
2023-02-17 14:22:40 +00:00
}
2023-02-17 14:22:40 +00:00
return Inherited::updateSubNode( skinnable, nodeRole, node );
2023-02-17 11:01:56 +00:00
}