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
|
|
|
|
2022-07-13 07:25:24 +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"
|
2022-07-18 09:11:45 +02:00
|
|
|
#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;
|
|
|
|
|
2022-08-26 13:19:37 +02:00
|
|
|
setSpacing( button->spacingHint( Q::Panel ) );
|
|
|
|
|
2022-08-26 12:56:12 +02:00
|
|
|
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
|
|
|
{
|
2022-07-13 07:25:24 +02:00
|
|
|
setNodeRoles( { PanelRole, RippleRole, GraphicRole, TextRole } );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QskPushButtonSkinlet::~QskPushButtonSkinlet() = default;
|
|
|
|
|
2019-04-25 14:23:39 +02:00
|
|
|
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 );
|
|
|
|
|
2022-07-18 09:11:45 +02:00
|
|
|
if ( subControl == QskPushButton::Text )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2019-04-25 14:23:39 +02:00
|
|
|
return textRect( button, contentsRect );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
2022-07-18 09:11:45 +02:00
|
|
|
|
|
|
|
if ( subControl == QskPushButton::Graphic )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2019-04-25 14:23:39 +02:00
|
|
|
return graphicRect( button, contentsRect );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
2022-07-18 09:11:45 +02:00
|
|
|
|
|
|
|
if ( subControl == QskPushButton::Panel )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2019-04-25 14:23:39 +02:00
|
|
|
return contentsRect;
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
2022-07-17 14:44:19 +02:00
|
|
|
|
2022-07-18 09:11:45 +02:00
|
|
|
if ( subControl == QskPushButton::Ripple )
|
|
|
|
{
|
|
|
|
return rippleRect( button, contentsRect );
|
2022-07-13 07:25:24 +02:00
|
|
|
}
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2019-04-25 14:23:39 +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 );
|
|
|
|
}
|
|
|
|
|
2022-07-13 07:25:24 +02:00
|
|
|
case RippleRole:
|
|
|
|
{
|
2022-07-18 09:11:45 +02:00
|
|
|
return updateRippleNode( button, node );
|
2022-07-13 07:25:24 +02:00
|
|
|
}
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
2019-04-25 14:23:39 +02:00
|
|
|
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-06-18 11:32:48 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-04-25 14:23:39 +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;
|
2019-04-25 14:23:39 +02:00
|
|
|
|
2022-08-26 12:56:12 +02:00
|
|
|
const auto r = button->subControlContentsRect( contentsRect, Q::Panel );
|
2022-06-15 10:34:51 +02:00
|
|
|
|
2022-08-26 12:56:12 +02:00
|
|
|
LayoutEngine layoutEngine( button );
|
|
|
|
layoutEngine.setGeometries( r );
|
2018-11-26 17:52:16 +01:00
|
|
|
|
2022-08-26 12:56:12 +02:00
|
|
|
return layoutEngine.elementAt( 0 )->geometry();
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
2022-07-18 09:11:45 +02:00
|
|
|
QRectF QskPushButtonSkinlet::rippleRect(
|
|
|
|
const QskPushButton* button, const QRectF& contentsRect ) const
|
|
|
|
{
|
|
|
|
using Q = QskPushButton;
|
|
|
|
|
2022-07-18 16:40:53 +02:00
|
|
|
QRectF rect;
|
2022-07-18 09:11:45 +02:00
|
|
|
|
2022-07-18 16:40:53 +02:00
|
|
|
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();
|
2022-07-18 09:11:45 +02:00
|
|
|
|
2022-07-18 16:40:53 +02:00
|
|
|
const auto panelRect = subControlRect( button, contentsRect, Q::Panel );
|
2022-07-18 09:11:45 +02:00
|
|
|
|
2022-07-18 16:40:53 +02:00
|
|
|
rect.setSize( 2.0 * panelRect.size() * ratio );
|
|
|
|
rect.moveCenter( pos );
|
|
|
|
}
|
2022-08-26 12:56:12 +02:00
|
|
|
|
2022-07-18 16:40:53 +02:00
|
|
|
return rect;
|
2022-07-18 09:11:45 +02:00
|
|
|
}
|
|
|
|
|
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 );
|
2021-12-24 16:17:49 +01:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-07-18 09:11:45 +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
|
|
|
|
2022-07-18 09:11:45 +02:00
|
|
|
return clipNode;
|
|
|
|
}
|
|
|
|
|
2020-12-29 09:45:00 +01:00
|
|
|
QSizeF QskPushButtonSkinlet::sizeHint( const QskSkinnable* skinnable,
|
|
|
|
Qt::SizeHint which, const QSizeF& ) const
|
|
|
|
{
|
2022-06-15 11:38:07 +02:00
|
|
|
using Q = QskPushButton;
|
|
|
|
|
2020-12-29 09:45:00 +01:00
|
|
|
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 );
|
2020-12-29 09:45:00 +01:00
|
|
|
|
2022-08-26 12:56:12 +02:00
|
|
|
auto size = layoutEngine.sizeHint( which, QSizeF() );
|
2020-12-29 09:45:00 +01:00
|
|
|
|
2022-06-15 11:38:07 +02:00
|
|
|
size = size.expandedTo( button->strutSizeHint( Q::Panel ) );
|
|
|
|
size = button->outerBoxSize( Q::Panel, size );
|
2020-12-29 09:45:00 +01:00
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
#include "moc_QskPushButtonSkinlet.cpp"
|