qskinny/examples/iotdashboard/LightDisplaySkinlet.cpp

193 lines
6.8 KiB
C++
Raw Normal View History

/******************************************************************************
* 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"
2021-11-05 16:13:12 +00:00
#include "nodes/BoxShadowNode.h"
2021-11-06 10:11:44 +00:00
#include <QskArcMetrics.h>
#include <QskTextOptions.h>
#include <QFontMetrics>
2021-11-06 12:44:51 +00:00
#include <QtMath>
LightDisplaySkinlet::LightDisplaySkinlet( QskSkin* skin )
: QskSkinlet( skin )
{
2021-11-08 15:59:06 +00:00
setNodeRoles( { GrooveRole, PanelRole, ColdAndWarmArcRole, TickmarksRole,
ValueTextRole, LeftLabelRole, RightLabelRole, KnobRole } );
}
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;
2021-11-05 16:13:12 +00:00
if( subControl == LightDisplay::Groove
2021-11-06 09:39:25 +00:00
|| subControl == LightDisplay::Panel
|| subControl == LightDisplay::ValueText )
{
2021-11-06 10:11:44 +00:00
QSizeF textSize = textLabelsSize( display );
QskArcMetrics arcMetrics = display->arcMetricsHint( LightDisplay::ColdAndWarmArc );
2021-11-08 15:59:06 +00:00
const qreal ticksWidth = display->metric( LightDisplay::Tickmarks );
2021-11-08 15:59:06 +00:00
const qreal x = textSize.width() + arcMetrics.width() + ticksWidth;
const qreal w = contentsRect.width() - ( 2 * ( textSize.width() + arcMetrics.width() + ticksWidth ) );
const qreal y = arcMetrics.width() + ticksWidth;
const qreal h = contentsRect.height() - 2 * ( arcMetrics.width() + ticksWidth );
const qreal diameter = qMin( w, h );
rect = QRectF( x, y, diameter, diameter );
return rect;
}
2021-11-06 10:11:44 +00:00
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;
}
2021-11-08 15:59:06 +00:00
else if( subControl == LightDisplay::Tickmarks )
{
const QRectF arcRect = subControlRect( skinnable, contentsRect,
LightDisplay::ColdAndWarmArc );
auto tickWidth = display->metric( LightDisplay::Tickmarks );
auto rect = arcRect.marginsAdded( { tickWidth, tickWidth, tickWidth, tickWidth } );
return rect;
}
else if( subControl == LightDisplay::LeftLabel )
{
2021-11-08 15:59:06 +00:00
const QRectF ticksRect = subControlRect( skinnable, contentsRect, LightDisplay::Tickmarks );
QSizeF size = textLabelsSize( display );
rect.setWidth( size.width() );
2021-11-08 15:59:06 +00:00
rect.setY( ticksRect.y() + ( ticksRect.height() - size.height() ) / 2 );
rect.setHeight( size.height() );
return rect;
}
else if( subControl == LightDisplay::RightLabel )
{
2021-11-08 15:59:06 +00:00
QRectF ticksRect = subControlRect( skinnable, contentsRect, LightDisplay::Tickmarks );
QSizeF size = textLabelsSize( display );
2021-11-08 15:59:06 +00:00
rect.setX( ticksRect.x() + ticksRect.width() );
2021-11-08 15:59:06 +00:00
rect.setY( ticksRect.y() + ( ticksRect.height() - size.height() ) / 2 );
rect.setHeight( size.height() );
2021-11-06 12:44:51 +00:00
return rect;
}
else if( subControl == LightDisplay::Knob )
{
QRectF arcRect = subControlRect( skinnable, contentsRect, LightDisplay::ColdAndWarmArc );
QskArcMetrics arcMetrics = display->arcMetricsHint( LightDisplay::ColdAndWarmArc );
QSizeF knobSize = display->strutSizeHint( LightDisplay::Knob );
const qreal radius = ( arcRect.width() - arcMetrics.width() ) / 2;
const qreal angle = display->valueAsRatio() * 180;
const qreal cos = qFastCos( qDegreesToRadians( angle ) );
const qreal sin = qFastSin( qDegreesToRadians( angle ) );
const auto x = arcRect.center().x() - knobSize.width() / 2 - radius * cos;
const auto y = arcRect.center().y() - knobSize.height() / 2 - radius * sin;
rect = QRectF( x, y, knobSize.width(), knobSize.height() );
return rect;
}
return contentsRect;
}
QSGNode* LightDisplaySkinlet::updateSubNode(
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
{
2021-11-05 16:13:12 +00:00
auto* display = static_cast< const LightDisplay* >( skinnable );
switch( nodeRole )
{
2021-11-05 16:13:12 +00:00
case PanelRole:
{
return updateBoxNode( skinnable, node, LightDisplay::Panel );
}
case GrooveRole:
{
2021-11-05 16:13:12 +00:00
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;
}
2021-11-06 10:11:44 +00:00
case ColdAndWarmArcRole:
{
2021-11-08 15:22:22 +00:00
return updateArcNode( skinnable, node, LightDisplay::ColdAndWarmArc );
}
2021-11-08 15:59:06 +00:00
case TickmarksRole:
{
return nullptr;
}
case ValueTextRole:
{
2021-11-06 09:39:25 +00:00
const QString valueText = QString::number( display->value(), 'f', 0 )
+ QStringLiteral( " %" );
return updateTextNode( skinnable, node, valueText, {},
LightDisplay::ValueText );
}
case LeftLabelRole:
{
2021-11-05 16:13:12 +00:00
return updateTextNode( skinnable, node, QStringLiteral( "0 " ), {},
LightDisplay::LeftLabel );
}
case RightLabelRole:
{
2021-11-05 16:13:12 +00:00
return updateTextNode( skinnable, node, QStringLiteral( " 100" ), {},
LightDisplay::RightLabel );
}
2021-11-06 12:44:51 +00:00
case KnobRole:
{
return updateBoxNode( skinnable, node, LightDisplay::Knob );
}
}
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
QSizeF LightDisplaySkinlet::textLabelsSize( const LightDisplay* display ) const
{
QFont font = display->effectiveFont( LightDisplay::LeftLabel );
QFontMetricsF fm( font );
2021-11-05 16:13:12 +00:00
qreal w = fm.width( QStringLiteral( " 100" ) );
qreal h = fm.height();
return { w, h };
}
#include "moc_LightDisplaySkinlet.cpp"