qskinny/src/controls/QskGraphicLabelSkinlet.cpp

134 lines
3.8 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 "QskGraphicLabelSkinlet.h"
2018-08-03 08:30:23 +02:00
#include "QskGraphicLabel.h"
2017-07-21 18:21:34 +02:00
#include "QskAspect.h"
2018-07-19 14:10:48 +02:00
#include "QskColorFilter.h"
2017-07-21 18:21:34 +02:00
#include "QskFunctions.h"
2018-08-03 08:15:28 +02:00
#include "QskGraphic.h"
#include "QskTextureNode.h"
2017-07-21 18:21:34 +02:00
2018-08-03 08:15:28 +02:00
QskGraphicLabelSkinlet::QskGraphicLabelSkinlet( QskSkin* skin )
: Inherited( skin )
2017-07-21 18:21:34 +02:00
{
setNodeRoles( { GraphicRole } );
}
QskGraphicLabelSkinlet::~QskGraphicLabelSkinlet() = default;
QRectF QskGraphicLabelSkinlet::subControlRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
2017-07-21 18:21:34 +02:00
{
const auto label = static_cast< const QskGraphicLabel* >( skinnable );
if ( subControl == QskGraphicLabel::Graphic )
{
return graphicRect( label, 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* QskGraphicLabelSkinlet::updateSubNode(
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
{
const auto label = static_cast< const QskGraphicLabel* >( skinnable );
2018-08-03 08:15:28 +02:00
switch ( nodeRole )
2017-09-01 11:55:55 +02:00
{
case GraphicRole:
{
return updateGraphicNode( label, node );
}
}
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
QRect QskGraphicLabelSkinlet::graphicRect(
const QskGraphicLabel* label, const QRectF& contentsRect ) const
2017-07-21 18:21:34 +02:00
{
// textures are in integers, to avoid useless recalculations
// that finally will be rounded anyway, we calculate in integers
const auto fillMode = label->fillMode();
const QRect graphicRect = contentsRect.toAlignedRect();
2017-07-21 18:21:34 +02:00
if ( fillMode == QskGraphicLabel::Stretch )
{
return graphicRect;
}
QSizeF sz = label->effectiveSourceSize();
if ( fillMode == QskGraphicLabel::PreserveAspectFit )
{
sz.scale( graphicRect.size(), Qt::KeepAspectRatio );
}
else if ( fillMode == QskGraphicLabel::PreserveAspectCrop )
{
sz.scale( graphicRect.size(), Qt::KeepAspectRatioByExpanding );
}
return qskAlignedRect( graphicRect,
2018-08-03 08:15:28 +02:00
( int ) sz.width(), ( int ) sz.height(), label->alignment() );
2017-07-21 18:21:34 +02:00
}
QSGNode* QskGraphicLabelSkinlet::updateGraphicNode(
const QskGraphicLabel* label, QSGNode* node ) const
{
const auto colorFilter = label->graphicFilter();
const auto rect = label->subControlRect( QskGraphicLabel::Graphic );
2017-07-21 18:21:34 +02:00
2020-11-01 15:44:15 +01:00
Qt::Orientations mirrored;
if ( label->mirror() )
mirrored = Qt::Horizontal;
if ( label->fillMode() == QskGraphicLabel::Stretch )
{
node = QskSkinlet::updateGraphicNode( label, node,
2020-11-01 15:44:15 +01:00
label->graphic(), colorFilter, rect, mirrored );
}
else
{
node = QskSkinlet::updateGraphicNode( label, node,
2020-11-01 15:44:15 +01:00
label->graphic(), colorFilter, rect, Qt::AlignCenter, mirrored );
2017-07-21 18:21:34 +02:00
}
return node;
}
QSizeF QskGraphicLabelSkinlet::sizeHint( const QskSkinnable* skinnable,
Qt::SizeHint which, const QSizeF& constraint ) const
{
if ( which != Qt::PreferredSize )
return QSizeF();
const auto label = static_cast< const QskGraphicLabel* >( skinnable );
auto sz = label->effectiveSourceSize();
if ( !sz.isEmpty() )
{
if ( constraint.width() >= 0.0 )
{
sz.setHeight( sz.height() * constraint.width() / sz.width() );
sz.setWidth( -1.0 );
}
else if ( constraint.height() >= 0.0 )
{
sz.setWidth( sz.width() * constraint.height() / sz.height() );
sz.setHeight( -1.0 );
}
}
return sz;
}
2017-07-21 18:21:34 +02:00
#include "moc_QskGraphicLabelSkinlet.cpp"