qskinny/src/controls/QskTextLabelSkinlet.cpp

149 lines
4.2 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 "QskTextLabelSkinlet.h"
#include "QskTextLabel.h"
2018-08-03 08:30:23 +02:00
2017-07-21 18:21:34 +02:00
#include "QskTextOptions.h"
#include "QskTextRenderer.h"
#include <qfontmetrics.h>
#include <qmath.h>
2017-07-21 18:21:34 +02:00
2018-08-03 08:15:28 +02:00
QskTextLabelSkinlet::QskTextLabelSkinlet( QskSkin* skin )
: Inherited( skin )
2017-07-21 18:21:34 +02:00
{
2019-12-15 13:57:19 +01:00
setNodeRoles( { PanelRole, TextRole } );
2017-07-21 18:21:34 +02:00
}
QskTextLabelSkinlet::~QskTextLabelSkinlet() = default;
QRectF QskTextLabelSkinlet::subControlRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
2018-08-03 08:15:28 +02:00
{
2019-12-15 13:57:19 +01:00
if ( subControl == QskTextLabel::Panel )
2017-07-21 18:21:34 +02:00
{
return contentsRect;
2017-07-21 18:21:34 +02:00
}
2019-12-15 13:57:19 +01:00
else if ( subControl == QskTextLabel::Text )
{
2020-12-29 12:57:03 +01:00
const auto label = static_cast< const QskTextLabel* >( skinnable );
2019-12-15 13:57:19 +01:00
if ( label->hasPanel() )
2021-08-26 15:17:41 +02:00
{
return label->subControlContentsRect(
contentsRect, QskTextLabel::Panel );
}
2019-12-15 13:57:19 +01:00
return contentsRect;
}
2017-07-21 18:21:34 +02:00
return Inherited::subControlRect( skinnable, contentsRect, subControl );
2017-07-21 18:21:34 +02:00
}
2018-08-03 08:15:28 +02:00
QSGNode* QskTextLabelSkinlet::updateSubNode(
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
2017-07-21 18:21:34 +02:00
{
const auto label = static_cast< const QskTextLabel* >( skinnable );
2018-08-03 08:15:28 +02:00
switch ( nodeRole )
2017-07-21 18:21:34 +02:00
{
2019-12-15 13:57:19 +01:00
case PanelRole:
{
if ( !label->hasPanel() )
return nullptr;
return updateBoxNode( label, node, QskTextLabel::Panel );
}
2017-07-21 18:21:34 +02:00
case TextRole:
2017-09-01 11:55:55 +02:00
{
2019-01-04 13:42:16 +01:00
return updateTextNode( label, node,
2022-08-25 09:39:33 +02:00
label->text(), QskTextLabel::Text );
2017-09-01 11:55:55 +02:00
}
2017-07-21 18:21:34 +02:00
}
2017-09-01 11:55:55 +02:00
return Inherited::updateSubNode( skinnable, nodeRole, node );
2017-07-21 18:21:34 +02:00
}
QSizeF QskTextLabelSkinlet::sizeHint( const QskSkinnable* skinnable,
Qt::SizeHint which, const QSizeF& constraint ) const
{
if ( which != Qt::PreferredSize )
return QSizeF();
const auto label = static_cast< const QskTextLabel* >( skinnable );
const auto font = label->effectiveFont( QskTextLabel::Text );
auto textOptions = label->textOptions();
textOptions.setFormat( label->effectiveTextFormat() );
const auto text = label->text();
QSizeF hint;
const qreal lineHeight = QFontMetricsF( font ).height();
if ( text.isEmpty() )
{
if ( constraint.height() < 0.0 )
hint.setHeight( qCeil( lineHeight ) );
}
else if ( constraint.width() >= 0.0 )
{
if ( textOptions.effectiveElideMode() != Qt::ElideNone )
{
hint.setHeight( qCeil( lineHeight ) );
}
else
{
/*
In case of QskTextOptions::NoWrap we could count
the line numbers and calculate the height from
lineHeight. TODO ...
*/
qreal maxHeight = std::numeric_limits< qreal >::max();
if ( maxHeight / lineHeight > textOptions.maximumLineCount() )
{
// be careful with overflows
maxHeight = textOptions.maximumLineCount() * lineHeight;
}
QSizeF size( constraint.width(), maxHeight );
size = QskTextRenderer::textSize( text, font, textOptions, size );
if ( label->hasPanel() )
size = label->outerBoxSize( QskTextLabel::Panel, size );
hint.setHeight( qCeil( size.height() ) );
}
}
else if ( constraint.height() >= 0.0 )
{
const qreal maxWidth = std::numeric_limits< qreal >::max();
QSizeF size( maxWidth, constraint.height() );
size = QskTextRenderer::textSize( text, font, textOptions, size );
if ( label->hasPanel() )
size = label->outerBoxSize( QskTextLabel::Panel, size );
hint.setWidth( qCeil( size.width() ) );
}
else
{
hint = QskTextRenderer::textSize( text, font, textOptions );
if ( label->hasPanel() )
hint = label->outerBoxSize( QskTextLabel::Panel, hint );
}
return hint;
}
2017-07-21 18:21:34 +02:00
#include "moc_QskTextLabelSkinlet.cpp"