qskinny/src/controls/QskPushButtonSkinlet.cpp

279 lines
8.0 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"
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 ) )
{
using Q = QskPushButton;
const auto graphicSourceSize = button->graphic().defaultSize();
const bool hasText = !button->text().isEmpty();
const bool hasGraphic = !graphicSourceSize.isEmpty();
auto graphicElement = new GraphicElement( button, Q::Graphic );
graphicElement->setSourceSize( graphicSourceSize );
graphicElement->setIgnored( !hasGraphic );
auto textElement = new TextElement( button, Q::Text );
textElement->setText( button->text() );
textElement->setIgnored( !hasText );
using SP = QskSizePolicy;
if ( hasText && !hasGraphic )
{
textElement->setSizePolicy( SP::Preferred, SP::Constrained );
}
else if ( hasGraphic && !hasText )
{
const auto size = graphicElement->effectiveStrutSize();
if ( !size.isEmpty() )
graphicElement->setFixedSize( size );
else
graphicElement->setSizePolicy( SP::Ignored, SP::ConstrainedExpanding );
}
else if ( hasText && hasGraphic )
{
if ( orientation() == Qt::Horizontal )
{
graphicElement->setSizePolicy( SP::Constrained, SP::Fixed );
textElement->setSizePolicy( SP::Preferred, SP::Preferred );
}
else
{
graphicElement->setSizePolicy( SP::Fixed, SP::Fixed );
textElement->setSizePolicy( SP::Preferred, SP::Constrained );
}
auto size = graphicElement->effectiveStrutSize();
if ( size.isEmpty() )
{
const auto h = 1.5 * button->effectiveFontHeight( Q::Text );
size.setWidth( graphicElement->widthForHeight( h ) );
size.setHeight( h );
}
graphicElement->setPreferredSize( size );
}
setElementAt( 0, graphicElement );
setElementAt( 1, textElement );
}
};
}
2018-08-03 08:15:28 +02:00
QskPushButtonSkinlet::QskPushButtonSkinlet( QskSkin* skin )
: Inherited( skin )
2017-07-21 18:21:34 +02:00
{
setNodeRoles( { PanelRole, RippleRole, 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
{
const auto button = static_cast< const QskPushButton* >( skinnable );
if ( subControl == QskPushButton::Text )
2017-07-21 18:21:34 +02:00
{
return textRect( button, contentsRect );
2017-07-21 18:21:34 +02:00
}
if ( subControl == QskPushButton::Graphic )
2017-07-21 18:21:34 +02:00
{
return graphicRect( button, contentsRect );
2017-07-21 18:21:34 +02:00
}
if ( subControl == QskPushButton::Panel )
2017-07-21 18:21:34 +02:00
{
return contentsRect;
2017-07-21 18:21:34 +02:00
}
2022-07-17 14:44:19 +02:00
if ( subControl == QskPushButton::Ripple )
{
return rippleRect( button, contentsRect );
}
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
{
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, QskPushButton::Panel );
}
case RippleRole:
{
return updateRippleNode( button, node );
}
2017-09-01 11:55:55 +02:00
case TextRole:
{
return updateTextNode( button, node );
}
case GraphicRole:
{
return updateGraphicNode( button, node,
button->graphic(), QskPushButton::Graphic );
}
}
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
QRectF QskPushButtonSkinlet::textRect(
const QskPushButton* button, const QRectF& contentsRect ) const
2017-07-21 18:21:34 +02:00
{
2022-06-15 11:38:07 +02:00
using Q = QskPushButton;
2018-04-03 10:46:55 +02:00
2022-08-26 12:56:12 +02:00
const auto r = button->subControlContentsRect( contentsRect, Q::Panel );
2022-08-26 12:56:12 +02:00
LayoutEngine layoutEngine( button );
layoutEngine.setGeometries( r );
2017-07-21 18:21:34 +02:00
2022-08-26 12:56:12 +02:00
return layoutEngine.elementAt( 1 )->geometry();
2017-07-21 18:21:34 +02:00
}
QRectF QskPushButtonSkinlet::graphicRect(
const QskPushButton* button, const QRectF& contentsRect ) const
2017-07-21 18:21:34 +02:00
{
2022-06-15 11:38:07 +02:00
using Q = QskPushButton;
2022-08-26 12:56:12 +02:00
const auto r = button->subControlContentsRect( contentsRect, Q::Panel );
2022-08-26 12:56:12 +02:00
LayoutEngine layoutEngine( button );
layoutEngine.setGeometries( r );
2022-08-26 12:56:12 +02:00
return layoutEngine.elementAt( 0 )->geometry();
2017-07-21 18:21:34 +02:00
}
QRectF QskPushButtonSkinlet::rippleRect(
const QskPushButton* button, const QRectF& contentsRect ) const
{
using Q = QskPushButton;
QRectF rect;
const auto ratio = button->metric( Q::Ripple | QskAspect::Size );
if ( ratio > 0.0 )
{
const auto pos = button->effectiveSkinHint(
Q::Ripple | QskAspect::Metric | QskAspect::Position ).toPointF();
const auto panelRect = subControlRect( button, contentsRect, Q::Panel );
rect.setSize( 2.0 * panelRect.size() * ratio );
rect.moveCenter( pos );
}
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::updateRippleNode(
const QskPushButton* button, QSGNode* node ) const
{
using Q = QskPushButton;
const auto rippleRect = button->subControlRect( Q::Ripple );
if ( rippleRect.isEmpty() )
return nullptr;
auto clipNode = updateBoxClipNode( button, node,
button->subControlRect( Q::Panel ), Q::Panel );
if ( clipNode )
{
auto boxNode = QskSGNode::findChildNode( clipNode, RippleRole );
boxNode = updateBoxNode( button, boxNode, rippleRect, Q::Ripple );
if ( boxNode == nullptr )
return nullptr;
QskSGNode::setNodeRole( boxNode, RippleRole );
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 = size.expandedTo( button->strutSizeHint( Q::Panel ) );
size = button->outerBoxSize( Q::Panel, size );
return size;
}
2017-07-21 18:21:34 +02:00
#include "moc_QskPushButtonSkinlet.cpp"