qskinny/src/controls/QskPushButtonSkinlet.cpp

293 lines
8.3 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"
#include "QskTextOptions.h"
#include "QskFunctions.h"
2017-07-21 18:21:34 +02:00
2018-07-19 14:10:48 +02:00
#include <qfontmetrics.h>
#include <qmath.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;
}
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 )
{
return textRect( button, contentsRect );
2017-07-21 18:21:34 +02:00
}
else if ( subControl == QskPushButton::Graphic )
{
return graphicRect( button, contentsRect );
2017-07-21 18:21:34 +02:00
}
else if ( subControl == QskPushButton::Panel )
{
return contentsRect;
2017-07-21 18:21:34 +02:00
}
else if ( subControl == QskPushButton::Ripple )
{
const auto clickPos = button->effectiveSkinHint( QskPushButton::Ripple | QskAspect::Position ).toPointF();
const auto ratio = button->metric( QskPushButton::Ripple | QskAspect::Size );
const auto w = contentsRect.width() * ratio;
const auto h = contentsRect.height() * ratio;
const auto x = clickPos.x() - w;
const auto y = clickPos.y() - h;
const QRectF r( x, y, w * 2, h * 2 );
return r.intersected( 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:
{
if( button->hasAnimationHint( QskPushButton::Ripple | QskAspect::Color )
&& button->metric( QskPushButton::Ripple | QskAspect::Size ) > 0.0 )
{
return updateBoxNode( button, node, QskPushButton::Ripple );
}
else
{
return nullptr;
}
}
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-06-15 11:38:07 +02:00
auto r = button->subControlContentsRect( contentsRect, Q::Panel );
if ( r.isEmpty() || !button->hasGraphic() )
return r;
/*
For horizontal layouts Text depends on Graphic, while
for vertical Graphic depends on Text. Confusing ...
*/
if ( qskOrientation( button ) == Qt::Horizontal )
2017-07-21 18:21:34 +02:00
{
const auto graphicsRect = subControlRect( button, contentsRect, Q::Graphic );
const auto spacing = button->spacingHint( Q::Panel );
2018-04-03 10:46:55 +02:00
r.setX( r.x() + graphicsRect.width() + spacing );
}
else
{
const qreal h = button->effectiveFontHeight( Q::Text );
if ( h < r.height() )
r.setTop( r.bottom() - h );
2017-07-21 18:21:34 +02:00
}
return r;
}
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-06-15 11:38:07 +02:00
auto r = button->subControlContentsRect( contentsRect, Q::Panel );
if ( r.isEmpty() || button->text().isEmpty() )
return r;
2017-07-21 18:21:34 +02:00
2022-06-15 11:38:07 +02:00
const auto orientation = qskOrientation( button );
if ( orientation == Qt::Vertical )
2017-07-21 18:21:34 +02:00
{
2022-06-15 11:38:07 +02:00
const auto textRect = subControlRect( button, contentsRect, Q::Text );
const auto h = textRect.height() + button->spacingHint( Q::Panel );
2017-07-21 18:21:34 +02:00
if ( h > r.height() )
return QRectF();
r.setBottom( r.bottom() - h );
2017-07-21 18:21:34 +02:00
}
const auto maxSize = button->graphicSourceSize();
if ( maxSize.width() >= 0 || maxSize.height() >= 0 )
{
// limiting the size by graphicSize
const qreal maxW = maxSize.width();
const qreal maxH = maxSize.height();
if ( maxW >= 0.0 && maxW < r.width() )
{
r.setX( r.center().x() - 0.5 * maxW );
r.setWidth( maxW );
}
2019-01-04 13:42:16 +01:00
if ( maxH >= 0.0 && maxH < r.height() )
{
r.setY( r.center().y() - 0.5 * maxH );
r.setHeight( maxH );
}
}
r = r.marginsRemoved( button->paddingHint( Q::Graphic ) );
2017-07-21 18:21:34 +02:00
if ( !r.isEmpty() )
{
auto sz = button->graphic().defaultSize();
if ( !sz.isEmpty() )
{
sz.scale( r.size(), Qt::KeepAspectRatio );
const auto align = ( orientation == Qt::Horizontal )
? ( Qt::AlignLeft | Qt::AlignTop ) : Qt::AlignCenter;
r = qskAlignedRectF( r, sz.width(), sz.height(), align );
}
}
return r;
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
return QskSkinlet::updateTextNode( button, node, rect, alignment,
2022-06-15 11:38:07 +02:00
button->text(), button->textOptions(), Q::Text );
2017-07-21 18:21:34 +02:00
}
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 );
QSizeF size( 0, 0 );
const QFontMetricsF fm( button->effectiveFont( QskPushButton::Text ) );
if ( !button->text().isEmpty() )
{
// in elide mode we might want to ignore the text width ???
size += fm.size( Qt::TextShowMnemonic, button->text() );
}
if ( button->hasGraphic() )
{
const auto sz = button->graphicSourceSize();
qreal w = sz.width();
qreal h = sz.height();
if ( w < 0.0 || h < 0.0 )
{
const auto graphic = button->graphic();
if ( !graphic.isEmpty() )
{
if ( ( w < 0.0 ) && ( h < 0.0 ) )
{
// strutSizeHint( Graphic ) ???
h = 1.5 * fm.height();
}
if ( w < 0 )
{
w = graphic.widthForHeight( h );
}
else if ( h < 0 )
{
h = graphic.heightForWidth( w );
}
}
}
2022-06-15 11:38:07 +02:00
const auto padding = button->paddingHint( Q::Graphic );
const auto orientation = qskOrientation( button );
2022-06-15 11:38:07 +02:00
if( orientation == Qt::Horizontal )
{
2022-06-15 11:38:07 +02:00
size.rwidth() += padding.left() + w + padding.right();
size.rheight() = qMax( size.height(), h );
}
else
{
size.rheight() += padding.top() + h + padding.bottom();
size.rwidth() = qMax( size.width(), w );
}
}
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"