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
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
#include "QskGraphic.h"
|
|
|
|
#include "QskTextOptions.h"
|
|
|
|
|
2018-07-19 14:10:48 +02:00
|
|
|
#include <qfontmetrics.h>
|
|
|
|
#include <qmath.h>
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2018-08-03 08:15:28 +02:00
|
|
|
QskPushButtonSkinlet::QskPushButtonSkinlet( QskSkin* skin )
|
|
|
|
: Inherited( skin )
|
2017-07-21 18:21:34 +02:00
|
|
|
{
|
2017-09-01 11:55:55 +02:00
|
|
|
setNodeRoles( { PanelRole, 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 );
|
|
|
|
|
|
|
|
if ( subControl == QskPushButton::Text )
|
|
|
|
{
|
2019-04-25 14:23:39 +02:00
|
|
|
return textRect( button, contentsRect );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
else if ( subControl == QskPushButton::Graphic )
|
|
|
|
{
|
2019-04-25 14:23:39 +02:00
|
|
|
return graphicRect( button, contentsRect );
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
else if ( subControl == QskPushButton::Panel )
|
|
|
|
{
|
2019-04-25 14:23:39 +02:00
|
|
|
return contentsRect;
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// buttonShift -> TODO
|
2018-04-03 10:46:55 +02:00
|
|
|
|
|
|
|
QRectF r = button->innerBox( QskPushButton::Panel,
|
2019-04-25 14:23:39 +02:00
|
|
|
subControlRect( button, contentsRect, QskPushButton::Panel ) );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
if ( button->hasGraphic() )
|
|
|
|
{
|
|
|
|
// in case of having text + graphic we put the text at the bottom
|
2018-04-03 10:46:55 +02:00
|
|
|
|
2018-07-19 15:21:47 +02:00
|
|
|
qreal h = QFontMetricsF( button->effectiveFont( QskPushButton::Text ) ).height();
|
2017-07-21 18:21:34 +02:00
|
|
|
if ( h < r.height() )
|
|
|
|
r.setTop( r.bottom() - h );
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
// buttonShift -> TODO
|
2019-04-25 14:23:39 +02:00
|
|
|
|
|
|
|
QRectF r = button->innerBox( QskPushButton::Panel,
|
|
|
|
subControlRect( button, contentsRect, QskPushButton::Panel ) );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
if ( !button->text().isEmpty() )
|
|
|
|
{
|
2019-04-25 14:23:39 +02:00
|
|
|
qreal h = textRect( button, contentsRect ).height() +
|
2018-08-03 08:15:28 +02:00
|
|
|
button->metric( QskPushButton::Panel | QskAspect::Spacing );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
if ( h < r.height() )
|
|
|
|
r.setBottom( r.bottom() - h );
|
|
|
|
else
|
|
|
|
r.setHeight( 0 );
|
|
|
|
}
|
|
|
|
|
2018-11-26 17:52:16 +01: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
|
|
|
|
2018-11-26 17:52:16 +01:00
|
|
|
if ( maxH >= 0.0 && maxH < r.height() )
|
|
|
|
{
|
|
|
|
r.setY( r.center().y() - 0.5 * maxH );
|
|
|
|
r.setHeight( maxH );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 18:21:34 +02:00
|
|
|
const QSizeF sz = button->graphic().defaultSize();
|
|
|
|
|
2018-11-26 17:52:16 +01:00
|
|
|
if ( !( r.isEmpty() || sz.isEmpty() ) )
|
|
|
|
{
|
|
|
|
// inner rectangle according to the aspect ratio
|
|
|
|
|
|
|
|
const double scaleFactor =
|
|
|
|
qMin( r.width() / sz.width(), r.height() / sz.height() );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2018-11-26 17:52:16 +01:00
|
|
|
// early aligning to avoid pointless operations, that finally will
|
|
|
|
// have no effect, when drawing to an integer based paint device
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2018-11-26 17:52:16 +01:00
|
|
|
const int w = qFloor( scaleFactor * sz.width() );
|
|
|
|
const int h = qFloor( scaleFactor * sz.height() );
|
|
|
|
const int x = qFloor( r.center().x() - 0.5 * w );
|
|
|
|
const int y = qFloor( r.center().y() - 0.5 * h );
|
2017-07-21 18:21:34 +02:00
|
|
|
|
2018-11-26 17:52:16 +01:00
|
|
|
r = QRectF( x, y, w, h );
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
2017-07-21 18:21:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QSGNode* QskPushButtonSkinlet::updateTextNode(
|
|
|
|
const QskPushButton* button, QSGNode* node ) const
|
|
|
|
{
|
2019-04-25 14:23:39 +02:00
|
|
|
const auto rect = button->subControlRect( QskPushButton::Text ).toAlignedRect();
|
2017-07-21 18:21:34 +02:00
|
|
|
|
|
|
|
const QFontMetricsF fm( button->effectiveFont( QskPushButton::Text ) );
|
|
|
|
if ( !button->clip() && ( rect.height() < fm.height() ) )
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const auto alignment = button->flagHint< Qt::Alignment >(
|
|
|
|
QskPushButton::Text | QskAspect::Alignment, Qt::AlignCenter );
|
|
|
|
|
|
|
|
return QskSkinlet::updateTextNode( button, node, rect, alignment,
|
|
|
|
button->text(), button->textOptions(), QskPushButton::Text );
|
|
|
|
}
|
|
|
|
|
|
|
|
#include "moc_QskPushButtonSkinlet.cpp"
|