setting QGradient::CoordinateMode when converting into a QGradient

This commit is contained in:
Uwe Rathmann 2022-12-20 15:47:26 +01:00
parent c66c61de0c
commit 03d2dad7ca
2 changed files with 70 additions and 0 deletions

View File

@ -598,6 +598,7 @@ QGradient QskGradient::toQGradient() const
case Linear:
{
QLinearGradient g( m_values[0], m_values[1], m_values[2], m_values[3] );
g.setCoordinateMode( QGradient::ObjectMode );
g.setSpread( static_cast< QGradient::Spread >( m_spread ) );
g.setStops( qskToQGradientStops( m_stops ) );
@ -607,6 +608,7 @@ QGradient QskGradient::toQGradient() const
case Radial:
{
QRadialGradient g( m_values[0], m_values[1], m_values[2] );
g.setCoordinateMode( QGradient::ObjectMode );
g.setSpread( static_cast< QGradient::Spread >( m_spread ) );
g.setStops( qskToQGradientStops( m_stops ) );
@ -622,6 +624,7 @@ QGradient QskGradient::toQGradient() const
}
QConicalGradient g( m_values[0], m_values[1], m_values[2] );
g.setCoordinateMode( QGradient::ObjectMode );
g.setSpread( static_cast< QGradient::Spread >( m_spread ) );
g.setStops( qskToQGradientStops( m_stops ) );
@ -631,6 +634,7 @@ QGradient QskGradient::toQGradient() const
default:
{
QGradient g;
g.setCoordinateMode( QGradient::ObjectMode );
g.setSpread( static_cast< QGradient::Spread >( m_spread ) );
g.setStops( qskToQGradientStops( m_stops ) );
@ -639,6 +643,71 @@ QGradient QskGradient::toQGradient() const
}
}
QGradient QskGradient::toQGradient( const QRectF& rect ) const
{
auto qGradient = toQGradient();
if ( qGradient.coordinateMode() != QGradient::ObjectMode )
return qGradient;
const qreal x = rect.x();
const qreal y = rect.y();
const qreal w = rect.width();
const qreal h = rect.height();
switch( qGradient.type() )
{
case QGradient::LinearGradient:
{
auto& g = *static_cast< QLinearGradient* >( &qGradient );
const auto x1 = x + g.start().x() * w;
const auto y1 = y + g.start().y() * h;
const auto x2 = x + g.finalStop().x() * w;
const auto y2 = y + g.finalStop().y() * h;
g.setStart( x1, y1 );
g.setFinalStop( x2, y2 );
break;
}
case QGradient::RadialGradient:
{
auto& g = *static_cast< QRadialGradient* >( &qGradient );
const qreal x0 = x + g.center().x() * w;
const qreal y0 = y + g.center().y() * h;
const qreal rx = w * g.radius() * w;
const qreal ry = w * g.radius() * h;
g.setCenter( x0, y0 );
g.setFocalPoint( x0, y0 );
g.setCenterRadius( qMin( rx, ry ) );
g.setFocalRadius( qMin( rx, ry ) );
break;
}
case QGradient::ConicalGradient:
{
auto& g = *static_cast< QConicalGradient* >( &qGradient );
const qreal x0 = x + g.center().x() * w;
const qreal y0 = y + g.center().y() * h;
g.setCenter( x0, y0 );
break;
}
default:
break;
}
qGradient.setCoordinateMode( QGradient::LogicalMode );
return qGradient;
}
#ifndef QT_NO_DEBUG_STREAM
#include <qdebug.h>

View File

@ -144,6 +144,7 @@ class QSK_EXPORT QskGradient
int stepCount() const noexcept;
QGradient toQGradient() const;
QGradient toQGradient( const QRectF& ) const;
private:
void updateStatusBits() const;