qskinny/examples/iotdashboard/LightDisplaySkinlet.cpp

152 lines
4.6 KiB
C++

/******************************************************************************
* Copyright (C) 2021 Edelhirsch Software GmbH
* This file may be used under the terms of the 3-clause BSD License
*****************************************************************************/
#include "LightDisplaySkinlet.h"
#include "LightDisplay.h"
#include "nodes/BoxShadowNode.h"
#include <QskTextOptions.h>
#include <QFontMetrics>
LightDisplaySkinlet::LightDisplaySkinlet( QskSkin* skin )
: QskSkinlet( skin )
{
setNodeRoles( { GrooveRole, PanelRole, WarmPartRole, ColdPartRole,
ValueTextRole, LeftLabelRole, RightLabelRole } );
}
LightDisplaySkinlet::~LightDisplaySkinlet()
{
}
QRectF LightDisplaySkinlet::subControlRect( const QskSkinnable* skinnable,
const QRectF& contentsRect, QskAspect::Subcontrol subControl ) const
{
auto* display = static_cast< const LightDisplay* >( skinnable );
QRectF rect = contentsRect;
if( subControl == LightDisplay::Groove
|| subControl == LightDisplay::Panel )
{
QSizeF size = textLabelsSize( display );
const qreal x = size.width();
const qreal w = contentsRect.width() - 2 * size.width();
const qreal y = 0;
const qreal h = contentsRect.height();
const qreal diameter = qMin( w, h );
rect = QRectF( x, y, diameter, diameter );
return rect;
}
else if( subControl == LightDisplay::LeftLabel )
{
QRectF grooveRect = subControlRect( skinnable, contentsRect, LightDisplay::Groove );
QSizeF size = textLabelsSize( display );
rect.setWidth( size.width() );
rect.setY( grooveRect.y() + ( grooveRect.height() - size.height() ) / 2 );
rect.setHeight( size.height() );
return rect;
}
else if( subControl == LightDisplay::RightLabel )
{
QRectF grooveRect = subControlRect( skinnable, contentsRect, LightDisplay::Groove );
QSizeF size = textLabelsSize( display );
rect.setX( rect.right() - size.width() );
rect.setY( grooveRect.y() + ( grooveRect.height() - size.height() ) / 2 );
rect.setHeight( size.height() );
return rect;
}
return contentsRect;
}
QSGNode* LightDisplaySkinlet::updateSubNode(
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
{
auto* display = static_cast< const LightDisplay* >( skinnable );
switch( nodeRole )
{
case PanelRole:
{
return updateBoxNode( skinnable, node, LightDisplay::Panel );
}
case GrooveRole:
{
const QRectF grooveRect = display->subControlRect( LightDisplay::Groove );
if ( grooveRect.isEmpty() )
return nullptr;
auto shadowNode = static_cast< BoxShadowNode* >( node );
if ( shadowNode == nullptr )
shadowNode = new BoxShadowNode();
const auto& shadowMetrics = display->shadow();
shadowNode->setRect( shadowMetrics.shadowRect( grooveRect ) );
shadowNode->setShape( grooveRect.width() / 2 );
shadowNode->setBlurRadius( shadowMetrics.blurRadius() );
shadowNode->setColor( display->shadowColor() );
shadowNode->setClipRect( grooveRect );
shadowNode->updateGeometry();
return shadowNode;
}
case WarmPartRole:
{
// const qreal startAngle = 90 * 16;
// const auto bar = static_cast< const LightDisplay* >( skinnable );
// const qreal spanAngle = bar->valueAsRatio() * -5760;
// return updateArcNode( skinnable, node, startAngle, spanAngle,
// LightDisplay::Bar );
}
case ColdPartRole:
{
return nullptr;
}
case ValueTextRole:
{
return nullptr;
}
case LeftLabelRole:
{
return updateTextNode( skinnable, node, QStringLiteral( "0 " ), {},
LightDisplay::LeftLabel );
}
case RightLabelRole:
{
return updateTextNode( skinnable, node, QStringLiteral( " 100" ), {},
LightDisplay::RightLabel );
}
}
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
QSizeF LightDisplaySkinlet::textLabelsSize( const LightDisplay* display ) const
{
QFont font = display->effectiveFont( LightDisplay::LeftLabel );
QFontMetricsF fm( font );
qreal w = fm.width( QStringLiteral( " 100" ) );
qreal h = fm.height();
return { w, h };
}
#include "moc_LightDisplaySkinlet.cpp"