qskinny/src/controls/QskProgressBarSkinlet.cpp

175 lines
4.6 KiB
C++
Raw Normal View History

2020-07-31 16:57:22 +02:00
/******************************************************************************
* QSkinny - Copyright (C) 2016 Uwe Rathmann
* This file may be used under the terms of the QSkinny License, Version 1.0
*****************************************************************************/
2020-08-01 17:51:45 +02:00
#include "QskProgressBarSkinlet.h"
#include "QskProgressBar.h"
2020-08-03 08:02:13 +02:00
#include "QskIntervalF.h"
2020-08-06 09:28:18 +02:00
#include "QskBoxBorderMetrics.h"
2020-07-31 16:57:22 +02:00
2020-08-09 10:49:32 +02:00
#include <qeasingcurve.h>
2020-07-31 16:57:22 +02:00
#include <cmath>
2020-08-03 08:02:13 +02:00
static inline QskIntervalF qskBarInterval( const QskProgressBar* bar )
{
2020-08-09 10:49:32 +02:00
qreal pos1, pos2;
if ( bar->isIndeterminate() )
{
const auto pos = bar->metric( QskProgressBar::Bar | QskAspect::Position );
static const QEasingCurve curve( QEasingCurve::InOutCubic );
const qreal off = 0.15;
pos1 = curve.valueForProgress( qMax( pos - off, 0.0 ) );
pos2 = curve.valueForProgress( qMin( pos + off, 1.0 ) );
}
else
{
pos1 = bar->valueAsRatio( bar->origin() );
pos2 = bar->valueAsRatio( bar->value() );
}
2020-08-03 08:02:13 +02:00
if( bar->orientation() == Qt::Horizontal )
{
if ( bar->layoutMirroring() )
{
pos1 = 1.0 - pos1;
pos2 = 1.0 - pos2;
}
}
if ( pos1 > pos2 )
std::swap( pos1, pos2 );
return QskIntervalF( pos1, pos2 );
}
2020-08-01 17:51:45 +02:00
QskProgressBarSkinlet::QskProgressBarSkinlet( QskSkin* skin )
2020-07-31 16:57:22 +02:00
: QskSkinlet( skin )
{
2020-08-03 08:02:13 +02:00
setNodeRoles( { GrooveRole, BarRole } );
2020-07-31 16:57:22 +02:00
}
2020-08-01 17:51:45 +02:00
QskProgressBarSkinlet::~QskProgressBarSkinlet()
2020-07-31 16:57:22 +02:00
{
}
2020-08-01 17:51:45 +02:00
QRectF QskProgressBarSkinlet::subControlRect(
2020-07-31 16:57:22 +02:00
const QskSkinnable* skinnable, const QRectF& contentsRect,
QskAspect::Subcontrol subControl ) const
{
2020-08-01 17:51:45 +02:00
const auto bar = static_cast< const QskProgressBar* >( skinnable );
2020-07-31 16:57:22 +02:00
2020-08-01 17:51:45 +02:00
if( ( subControl == QskProgressBar::Groove ) )
2020-07-31 16:57:22 +02:00
{
const auto dim = bar->thickness();
auto rect = contentsRect;
if ( bar->orientation() == Qt::Horizontal )
{
rect.setY( rect.y() + 0.5 * ( rect.height() - dim ) );
rect.setHeight( dim );
}
else
{
rect.setX( rect.x() + 0.5 * ( rect.width() - dim ) );
rect.setWidth( dim );
}
return rect;
}
2020-08-03 08:02:13 +02:00
if( subControl == QskProgressBar::Bar )
2020-07-31 16:57:22 +02:00
{
2020-08-01 17:51:45 +02:00
const auto bar = static_cast< const QskProgressBar* >( skinnable );
2020-08-03 08:02:13 +02:00
return barRect( bar );
2020-07-31 16:57:22 +02:00
}
return Inherited::subControlRect( skinnable, contentsRect, subControl );
}
2020-08-01 17:51:45 +02:00
QSGNode* QskProgressBarSkinlet::updateSubNode(
2020-07-31 16:57:22 +02:00
const QskSkinnable* skinnable, quint8 nodeRole, QSGNode* node ) const
{
switch( nodeRole )
{
case GrooveRole:
{
2020-08-01 17:51:45 +02:00
return updateBoxNode( skinnable, node, QskProgressBar::Groove );
2020-07-31 16:57:22 +02:00
}
2020-08-03 08:02:13 +02:00
case BarRole:
2020-07-31 16:57:22 +02:00
{
2020-08-01 17:51:45 +02:00
const auto bar = static_cast< const QskProgressBar* >( skinnable );
2020-08-03 08:02:13 +02:00
return updateBarNode( bar, node );
2020-07-31 16:57:22 +02:00
}
}
return Inherited::updateSubNode( skinnable, nodeRole, node );
}
2020-08-03 08:02:13 +02:00
QSGNode* QskProgressBarSkinlet::updateBarNode(
2020-08-01 17:51:45 +02:00
const QskProgressBar* bar, QSGNode* node ) const
2020-07-31 16:57:22 +02:00
{
2020-08-03 08:02:13 +02:00
const auto subControl = QskProgressBar::Bar;
2020-07-31 16:57:22 +02:00
const auto rect = bar->subControlRect( subControl );
if ( rect.isEmpty() )
return nullptr;
auto gradient = bar->gradientHint( subControl );
if ( !gradient.isVisible() )
return nullptr;
gradient.setOrientation( bar->orientation() );
if ( !gradient.isMonochrome() )
{
2020-08-03 08:02:13 +02:00
const auto intv = qskBarInterval( bar );
gradient = gradient.extracted( intv.lowerBound(), intv.upperBound() );
2020-07-31 16:57:22 +02:00
2020-08-03 08:02:13 +02:00
if ( bar->orientation() == Qt::Vertical || bar->layoutMirroring() )
2020-07-31 16:57:22 +02:00
gradient.reverse();
}
return updateBoxNode( bar, node, rect, gradient, subControl );
}
2020-08-03 08:02:13 +02:00
QRectF QskProgressBarSkinlet::barRect( const QskProgressBar* bar ) const
2020-07-31 16:57:22 +02:00
{
2020-08-06 09:28:18 +02:00
const auto subControl = QskProgressBar::Groove;
auto rect = bar->subControlRect( subControl );
const auto borderMetrics = bar->boxBorderMetricsHint( subControl );
auto m = bar->marginsHint( subControl | QskAspect::Padding );
m += 0.5 * borderMetrics.toAbsolute( rect.size() ).widths();
rect = rect.marginsRemoved( m );
2020-07-31 16:57:22 +02:00
2020-08-03 08:02:13 +02:00
const auto intv = qskBarInterval( bar );
2020-07-31 16:57:22 +02:00
if( bar->orientation() == Qt::Horizontal )
{
const auto w = rect.width();
2020-08-03 08:02:13 +02:00
rect.setRight( rect.left() + intv.upperBound() * w );
rect.setLeft( rect.left() + intv.lowerBound() * w );
2020-07-31 16:57:22 +02:00
}
else
{
const auto h = rect.height();
2020-08-03 08:02:13 +02:00
rect.setTop( rect.bottom() - h * intv.upperBound() );
rect.setBottom( rect.bottom() - h * intv.lowerBound() );
2020-07-31 16:57:22 +02:00
}
return rect;
}
2020-08-01 17:51:45 +02:00
#include "moc_QskProgressBarSkinlet.cpp"