qskinny/examples/iotdashboard/LightDisplaySkinlet.cpp

163 lines
5.4 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 <QskArcMetrics.h>
#include <QskTextOptions.h>
#include <QFontMetrics>
LightDisplaySkinlet::LightDisplaySkinlet( QskSkin* skin )
: QskSkinlet( skin )
{
setNodeRoles( { GrooveRole, PanelRole, ColdAndWarmArcRole,
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
|| subControl == LightDisplay::ValueText )
{
QSizeF textSize = textLabelsSize( display );
QskArcMetrics arcMetrics = display->arcMetricsHint( LightDisplay::ColdAndWarmArc );
const qreal x = textSize.width() + arcMetrics.width();
const qreal w = contentsRect.width() - ( 2 * ( textSize.width() + arcMetrics.width() ) );
const qreal y = arcMetrics.width();
const qreal h = contentsRect.height() - 2 * arcMetrics.width();
const qreal diameter = qMin( w, h );
rect = QRectF( x, y, diameter, diameter );
return rect;
}
else if( subControl == LightDisplay::ColdAndWarmArc )
{
const QRectF panelRect = subControlRect( skinnable, contentsRect,
LightDisplay::Panel );
auto barWidth = display->arcMetricsHint( LightDisplay::ColdAndWarmArc ).width();
auto rect = panelRect.marginsAdded( { barWidth, barWidth, barWidth, barWidth } );
return rect;
}
else if( subControl == LightDisplay::LeftLabel )
{
// ### rename to panelRect?
const 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 arcRect = subControlRect( skinnable, contentsRect, LightDisplay::ColdAndWarmArc );
QSizeF size = textLabelsSize( display );
rect.setX( arcRect.x() + arcRect.width() );
rect.setY( arcRect.y() + ( arcRect.height() - size.height() ) / 2 );
rect.setHeight( size.height() );
qDebug() << "rl rect:" << rect;
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 ColdAndWarmArcRole:
{
const qreal startAngle = 0;
const qreal spanAngle = 180 * 16;
return updateArcNode( skinnable, node, startAngle, spanAngle,
LightDisplay::ColdAndWarmArc );
}
case ValueTextRole:
{
const QString valueText = QString::number( display->value(), 'f', 0 )
+ QStringLiteral( " %" );
return updateTextNode( skinnable, node, valueText, {},
LightDisplay::ValueText );
}
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"