qskinny/src/controls/QskTextFieldSkinlet.cpp

99 lines
2.7 KiB
C++
Raw Normal View History

/******************************************************************************
2024-01-17 14:31:45 +01:00
* QSkinny - Copyright (C) The authors
2023-04-06 09:23:37 +02:00
* SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/
#include "QskTextFieldSkinlet.h"
#include "QskTextField.h"
2024-12-04 10:08:39 +01:00
#include <qfontmetrics.h>
using Q = QskTextField;
QSK_SYSTEM_STATE( QskTextFieldSkinlet, Selected, QskAspect::FirstUserState >> 1 )
QskTextFieldSkinlet::QskTextFieldSkinlet( QskSkin* skin )
2018-08-03 08:15:28 +02:00
: Inherited( skin )
{
setNodeRoles( { PanelRole, PlaceholderTextRole, } );
}
QskTextFieldSkinlet::~QskTextFieldSkinlet()
{
}
QRectF QskTextFieldSkinlet::subControlRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
{
if ( subControl == Q::Panel )
{
return contentsRect;
}
else if ( subControl == Q::Text )
{
return skinnable->subControlContentsRect( contentsRect, Q::Panel );
}
else if ( subControl == Q::PlaceholderText )
{
const auto textField = static_cast< const QskTextField* >( skinnable );
if( textField->text().isEmpty() )
return subControlRect( skinnable, contentsRect, Q::Text );
return QRectF();
}
return Inherited::subControlRect( skinnable, contentsRect, subControl );
}
QSGNode* QskTextFieldSkinlet::updateSubNode(
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
{
const auto textField = static_cast< const QskTextField* >( skinnable );
2018-08-03 08:15:28 +02:00
switch ( nodeRole )
{
case PanelRole:
{
if ( !textField->hasPanel() )
2021-04-21 09:18:50 +02:00
return nullptr;
2021-08-04 09:31:16 +02:00
return updateBoxNode( skinnable, node, Q::Panel );
}
case PlaceholderTextRole:
{
if ( textField->text().isEmpty() )
{
return updateTextNode( skinnable, node,
textField->placeholderText(), Q::PlaceholderText );
}
return nullptr;
}
}
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
2024-12-04 10:08:39 +01:00
QSizeF QskTextFieldSkinlet::sizeHint( const QskSkinnable* skinnable,
Qt::SizeHint which, const QSizeF& ) const
{
if ( which != Qt::PreferredSize )
return QSizeF();
const auto textField = static_cast< const QskTextField* >( skinnable );
const QFontMetricsF fm( textField->effectiveFont( Q::Text ) );
auto hint = fm.size( Qt::TextSingleLine | Qt::TextExpandTabs, textField->text() );
if ( textField->hasPanel() )
{
hint = textField->outerBoxSize( Q::Panel, hint );
hint = hint.expandedTo( textField->strutSizeHint( Q::Panel ) );
}
return hint;
}
#include "moc_QskTextFieldSkinlet.cpp"