qskinny/src/controls/QskGraphicLabelSkinlet.cpp

187 lines
5.1 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"
2017-07-21 18:21:34 +02:00
#include <QtMath>
2018-08-03 08:15:28 +02:00
QskGraphicLabelSkinlet::QskGraphicLabelSkinlet( QskSkin* skin )
: Inherited( skin )
2017-07-21 18:21:34 +02:00
{
2021-08-26 15:17:41 +02:00
setNodeRoles( { PanelRole, GraphicRole } );
2017-07-21 18:21:34 +02:00
}
QskGraphicLabelSkinlet::~QskGraphicLabelSkinlet() = default;
QRectF QskGraphicLabelSkinlet::subControlRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
2017-07-21 18:21:34 +02:00
{
2021-08-26 15:17:41 +02:00
if ( subControl == QskGraphicLabel::Panel )
2017-07-21 18:21:34 +02:00
{
2021-08-26 15:17:41 +02:00
return contentsRect;
}
else if ( subControl == QskGraphicLabel::Graphic )
{
auto innerRect = contentsRect;
const auto label = static_cast< const QskGraphicLabel* >( skinnable );
if ( label->hasPanel() )
{
innerRect = label->subControlContentsRect(
innerRect, QskGraphicLabel::Panel );
}
return graphicRect( label, innerRect );
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
{
2021-08-26 15:17:41 +02:00
case PanelRole:
{
if ( !label->hasPanel() )
return nullptr;
return updateBoxNode( label, node, QskGraphicLabel::Panel );
}
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
{
2021-08-26 15:17:41 +02:00
using Q = QskGraphicLabel;
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
2021-08-26 15:17:41 +02:00
if ( fillMode == Q::Stretch )
2017-07-21 18:21:34 +02:00
{
return graphicRect;
}
QSizeF sz = label->effectiveSourceSize();
2021-08-26 15:17:41 +02:00
if ( fillMode == Q::PreserveAspectFit )
2017-07-21 18:21:34 +02:00
{
sz.scale( graphicRect.size(), Qt::KeepAspectRatio );
}
2021-08-26 15:17:41 +02:00
else if ( fillMode == Q::PreserveAspectCrop )
2017-07-21 18:21:34 +02:00
{
sz.scale( graphicRect.size(), Qt::KeepAspectRatioByExpanding );
}
return qskAlignedRect( graphicRect,
qCeil( sz.width() ), qCeil( sz.height() ), label->alignment() );
2017-07-21 18:21:34 +02:00
}
QSGNode* QskGraphicLabelSkinlet::updateGraphicNode(
const QskGraphicLabel* label, QSGNode* node ) const
{
2021-08-26 15:17:41 +02:00
using Q = QskGraphicLabel;
const auto colorFilter = label->graphicFilter();
2021-08-26 15:17:41 +02:00
const auto rect = label->subControlRect( Q::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;
2021-08-26 15:17:41 +02:00
if ( label->fillMode() == Q::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
{
2021-08-26 15:17:41 +02:00
using Q = QskGraphicLabel;
if ( which != Qt::PreferredSize )
return QSizeF();
2021-08-26 15:17:41 +02:00
const bool hasConstraint =
( constraint.width() >= 0.0 ) || ( constraint.height() >= 0.0 );
const auto label = static_cast< const QskGraphicLabel* >( skinnable );
2021-08-26 15:17:41 +02:00
const auto sourceSize = label->effectiveSourceSize();
2021-08-26 15:17:41 +02:00
auto hint = sourceSize;
2021-08-26 15:17:41 +02:00
if ( hasConstraint && !sourceSize.isEmpty() )
{
2021-08-26 15:17:41 +02:00
auto innerConstraint = constraint;
if ( label->hasPanel() )
{
2021-08-26 15:17:41 +02:00
constexpr qreal max = std::numeric_limits< int >::max();
QRectF r( 0.0, 0.0, max, max );
2021-12-23 19:05:59 +01:00
2021-08-26 15:17:41 +02:00
if ( constraint.width() >= 0.0 )
r.setWidth( constraint.width() );
else
r.setHeight( constraint.height() );
innerConstraint = label->subControlContentsRect( r, Q::Panel ).size();
}
2021-08-26 15:17:41 +02:00
const qreal aspectRatio = sourceSize.width() / sourceSize.height();
if ( constraint.width() >= 0.0 )
hint.setHeight( innerConstraint.width() / aspectRatio );
2021-12-23 19:05:59 +01:00
else
2021-08-26 15:17:41 +02:00
hint.setWidth( innerConstraint.height() * aspectRatio );
}
2021-08-26 15:17:41 +02:00
hint = hint.expandedTo( label->strutSizeHint( Q::Graphic ) );
if ( label->hasPanel() )
{
hint = label->outerBoxSize( Q::Panel, hint );
hint = hint.expandedTo( label->strutSizeHint( Q::Panel ) );
}
if ( hasConstraint )
hint = hintWithoutConstraint( hint, constraint );
return hint;
}
2017-07-21 18:21:34 +02:00
#include "moc_QskGraphicLabelSkinlet.cpp"