qskinny/src/controls/QskPushButtonSkinlet.cpp

197 lines
5.6 KiB
C++
Raw Normal View History

2017-07-21 18:21:34 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
#include "QskPushButtonSkinlet.h"
#include "QskPushButton.h"
2023-02-16 11:42:07 +01:00
#include "QskTextOptions.h"
2018-08-03 08:30:23 +02:00
#include "QskAnimationHint.h"
2017-07-21 18:21:34 +02:00
#include "QskGraphic.h"
2022-08-26 12:56:12 +02:00
#include "QskSubcontrolLayoutEngine.h"
#include "QskSGNode.h"
2017-07-21 18:21:34 +02:00
2022-06-15 11:38:07 +02:00
static inline Qt::Orientation qskOrientation( const QskPushButton* button )
{
// For the moment we only handle the orientation TODO ...
const auto direction = button->flagHint(
QskPushButton::Panel | QskAspect::Direction, Qsk::LeftToRight );
if ( direction == Qsk::LeftToRight || direction == Qsk::RightToLeft )
return Qt::Horizontal;
else
return Qt::Vertical;
}
2022-08-26 12:56:12 +02:00
namespace
{
class LayoutEngine : public QskSubcontrolLayoutEngine
{
public:
LayoutEngine( const QskPushButton* button )
: QskSubcontrolLayoutEngine( qskOrientation( button ) )
{
setSpacing( button->spacingHint( QskPushButton::Panel ) );
setGraphicTextElements( button,
QskPushButton::Text, button->text(),
QskPushButton::Graphic, button->graphic().defaultSize() );
const auto alignment = button->alignmentHint(
QskPushButton::Panel, Qt::AlignCenter );
2023-02-16 11:42:07 +01:00
setFixedContent( QskPushButton::Text, Qt::Horizontal, alignment );
2022-08-26 12:56:12 +02:00
}
};
}
2018-08-03 08:15:28 +02:00
QskPushButtonSkinlet::QskPushButtonSkinlet( QskSkin* skin )
: Inherited( skin )
2017-07-21 18:21:34 +02:00
{
setNodeRoles( { PanelRole, SplashRole, GraphicRole, TextRole } );
2017-07-21 18:21:34 +02:00
}
QskPushButtonSkinlet::~QskPushButtonSkinlet() = default;
QRectF QskPushButtonSkinlet::subControlRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
2017-07-21 18:21:34 +02:00
{
using Q = QskPushButton;
2017-07-21 18:21:34 +02:00
const auto button = static_cast< const QskPushButton* >( skinnable );
if ( subControl == Q::Panel )
return contentsRect;
if ( subControl == Q::Splash )
return splashRect( button, contentsRect );
if ( ( subControl == Q::Text ) || ( subControl == Q::Graphic ) )
2017-07-21 18:21:34 +02:00
{
const auto r = button->subControlContentsRect( contentsRect, Q::Panel );
2022-09-13 12:22:20 +02:00
LayoutEngine layoutEngine( button );
layoutEngine.setGeometries( r );
return layoutEngine.subControlRect( subControl );
2017-07-21 18:21:34 +02:00
}
return Inherited::subControlRect( skinnable, contentsRect, subControl );
2017-07-21 18:21:34 +02:00
}
2017-09-01 11:55:55 +02:00
QSGNode* QskPushButtonSkinlet::updateSubNode(
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
{
using Q = QskPushButton;
2017-09-01 11:55:55 +02:00
const auto button = static_cast< const QskPushButton* >( skinnable );
2018-08-03 08:15:28 +02:00
switch ( nodeRole )
2017-09-01 11:55:55 +02:00
{
case PanelRole:
return updateBoxNode( button, node, Q::Panel );
2017-09-01 11:55:55 +02:00
case SplashRole:
return updateSplashNode( button, node );
2017-09-01 11:55:55 +02:00
case TextRole:
return updateTextNode( button, node );
case GraphicRole:
return updateGraphicNode( button, node, button->graphic(), Q::Graphic );
2017-09-01 11:55:55 +02:00
}
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
QRectF QskPushButtonSkinlet::splashRect(
const QskPushButton* button, const QRectF& contentsRect ) const
{
using Q = QskPushButton;
QRectF rect;
const auto ratio = button->metric( Q::Splash | QskAspect::Size );
if ( ratio > 0.0 )
{
rect = subControlRect( button, contentsRect, Q::Panel );
const auto pos = button->positionHint( Q::Splash );
const qreal w = 2.0 * rect.width() * ratio;
rect.setX( pos - 0.5 * w );
rect.setWidth( w );
}
2022-08-26 12:56:12 +02:00
return rect;
}
2017-07-21 18:21:34 +02:00
QSGNode* QskPushButtonSkinlet::updateTextNode(
const QskPushButton* button, QSGNode* node ) const
{
2022-06-15 11:38:07 +02:00
using Q = QskPushButton;
const auto rect = button->subControlRect( Q::Text ).toAlignedRect();
2017-07-21 18:21:34 +02:00
2022-06-15 11:38:07 +02:00
const auto textHeight = button->effectiveFontHeight( Q::Text );
if ( !button->clip() && ( rect.height() < textHeight ) )
2017-07-21 18:21:34 +02:00
return nullptr;
2022-06-15 11:38:07 +02:00
const auto alignment = button->alignmentHint( Q::Text, Qt::AlignCenter );
2017-07-21 18:21:34 +02:00
2022-08-25 09:39:33 +02:00
return QskSkinlet::updateTextNode( button, node, rect,
alignment, button->text(), Q::Text );
2017-07-21 18:21:34 +02:00
}
QSGNode* QskPushButtonSkinlet::updateSplashNode(
const QskPushButton* button, QSGNode* node ) const
{
using Q = QskPushButton;
const auto splashRect = button->subControlRect( Q::Splash );
if ( splashRect.isEmpty() )
return nullptr;
auto clipNode = updateBoxClipNode( button, node,
button->subControlRect( Q::Panel ), Q::Panel );
if ( clipNode )
{
auto boxNode = QskSGNode::findChildNode( clipNode, SplashRole );
boxNode = updateBoxNode( button, boxNode, splashRect, Q::Splash );
if ( boxNode == nullptr )
return nullptr;
QskSGNode::setNodeRole( boxNode, SplashRole );
if ( boxNode->parent() != clipNode )
clipNode->appendChildNode( boxNode );
}
2022-08-26 12:56:12 +02:00
return clipNode;
}
QSizeF QskPushButtonSkinlet::sizeHint( const QskSkinnable* skinnable,
Qt::SizeHint which, const QSizeF& ) const
{
2022-06-15 11:38:07 +02:00
using Q = QskPushButton;
if ( which != Qt::PreferredSize )
return QSizeF();
const auto button = static_cast< const QskPushButton* >( skinnable );
2022-08-26 12:56:12 +02:00
LayoutEngine layoutEngine( button );
2022-08-26 12:56:12 +02:00
auto size = layoutEngine.sizeHint( which, QSizeF() );
2022-06-15 11:38:07 +02:00
size = button->outerBoxSize( Q::Panel, size );
2023-01-16 17:34:59 +01:00
size = size.expandedTo( button->strutSizeHint( Q::Panel ) );
size = size.grownBy( skinnable->marginHint( Q::Panel ) );
return size;
}
2017-07-21 18:21:34 +02:00
#include "moc_QskPushButtonSkinlet.cpp"