Skip to content
Permalink
Browse files

Avoid use of QStyleOptionGraphicsItem::matrix, which is removed

in Qt 6
  • Loading branch information
nyalldawson committed Mar 29, 2021
1 parent 375ab0e commit d3692361ad511f12f4e54bbccf5aa2c7b9f5e8f4
@@ -220,9 +220,19 @@ decoded orientation.
If the string was correctly decoded, ``ok`` will be set to ``True``.
%End

static double scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style );
static double scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style ) /Deprecated/;
%Docstring
Extracts the scale factor from an item ``style``.

.. deprecated::
Use the variant with a QPainter argument instead
%End

static double scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style, QPainter *painter );
%Docstring
Extracts the scale factor from an item ``style`` and ``painter``.

.. versionadded:: 3.20
%End

static QgsMapLayer *mapLayerFromString( const QString &string, QgsProject *project );
@@ -298,7 +298,7 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
}

bool previewRender = !mLayout || mLayout->renderContext().isPreviewRender();
double destinationDpi = previewRender ? QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle ) * 25.4 : mLayout->renderContext().dpi();
double destinationDpi = previewRender ? QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle, painter ) * 25.4 : mLayout->renderContext().dpi();
bool useImageCache = false;
bool forceRasterOutput = containsAdvancedEffects() && ( !mLayout || !( mLayout->renderContext().flags() & QgsLayoutRenderContext::FlagForceVectorOutput ) );

@@ -309,8 +309,8 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it

if ( previewRender )
{
widthInPixels = boundingRect().width() * QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
heightInPixels = boundingRect().height() * QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
widthInPixels = boundingRect().width() * QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle, painter );
heightInPixels = boundingRect().height() * QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle, painter );
}
else
{
@@ -368,7 +368,7 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it
p.scale( context.scaleFactor(), context.scaleFactor() );
drawBackground( context );
p.scale( 1 / context.scaleFactor(), 1 / context.scaleFactor() );
double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle, painter );
QgsLayoutItemRenderContext itemRenderContext( context, viewScale );
draw( itemRenderContext );
p.scale( context.scaleFactor(), context.scaleFactor() );
@@ -402,7 +402,7 @@ void QgsLayoutItem::paint( QPainter *painter, const QStyleOptionGraphicsItem *it

// scale painter from mm to dots
painter->scale( 1.0 / context.scaleFactor(), 1.0 / context.scaleFactor() );
double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
double viewScale = QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle, painter );
QgsLayoutItemRenderContext itemRenderContext( context, viewScale );
draw( itemRenderContext );

@@ -919,13 +919,13 @@ void QgsLayoutItemMap::paint( QPainter *painter, const QStyleOptionGraphicsItem
if ( mPainterJob && mCacheInvalidated && !mDrawingPreview )
{
// current job was invalidated - start a new one
mPreviewScaleFactor = QgsLayoutUtils::scaleFactorFromItemStyle( style );
mPreviewScaleFactor = QgsLayoutUtils::scaleFactorFromItemStyle( style, painter );
mBackgroundUpdateTimer->start( 1 );
}
else if ( !mPainterJob && !mDrawingPreview )
{
// this is the map's very first paint - trigger a cache update
mPreviewScaleFactor = QgsLayoutUtils::scaleFactorFromItemStyle( style );
mPreviewScaleFactor = QgsLayoutUtils::scaleFactorFromItemStyle( style, painter );
mBackgroundUpdateTimer->start( 1 );
}
}
@@ -934,7 +934,7 @@ void QgsLayoutItemMap::paint( QPainter *painter, const QStyleOptionGraphicsItem
if ( mCacheInvalidated && !mDrawingPreview )
{
// cache was invalidated - trigger a background update
mPreviewScaleFactor = QgsLayoutUtils::scaleFactorFromItemStyle( style );
mPreviewScaleFactor = QgsLayoutUtils::scaleFactorFromItemStyle( style, painter );
mBackgroundUpdateTimer->start( 1 );
}

@@ -980,7 +980,7 @@ void QgsLayoutItemMap::paint( QPainter *painter, const QStyleOptionGraphicsItem
if ( containsAdvancedEffects() && ( !mLayout || !( mLayout->renderContext().flags() & QgsLayoutRenderContext::FlagForceVectorOutput ) ) )
{
// rasterize
double destinationDpi = QgsLayoutUtils::scaleFactorFromItemStyle( style ) * 25.4;
double destinationDpi = QgsLayoutUtils::scaleFactorFromItemStyle( style, painter ) * 25.4;
double layoutUnitsInInches = mLayout ? mLayout->convertFromLayoutUnits( 1, QgsUnitTypes::LayoutInches ).length() : 1;
int widthInPixels = static_cast< int >( std::round( boundingRect().width() * layoutUnitsInInches * destinationDpi ) );
int heightInPixels = static_cast< int >( std::round( boundingRect().height() * layoutUnitsInInches * destinationDpi ) );
@@ -394,7 +394,7 @@ void QgsLayoutItemPageGrid::paint( QPainter *painter, const QStyleOptionGraphics
{
//dots are actually drawn as tiny crosses a few pixels across
//set halfCrossLength to equivalent of 1 pixel
halfCrossLength = 1 / QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle );
halfCrossLength = 1 / QgsLayoutUtils::scaleFactorFromItemStyle( itemStyle, painter );
}
else
{
@@ -398,6 +398,7 @@ QgsLayoutItemPage::Orientation QgsLayoutUtils::decodePaperOrientation( const QSt

double QgsLayoutUtils::scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style )
{
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
// workaround Qt bug 66185

// Refs #18027 - if a QGraphicsItem is rotated by 90 or 270 degrees, then the item
@@ -407,6 +408,15 @@ double QgsLayoutUtils::scaleFactorFromItemStyle( const QStyleOptionGraphicsItem

// TODO - ifdef this out if Qt fixes upstream
return !qgsDoubleNear( style->matrix.m11(), 0.0 ) ? style->matrix.m11() : style->matrix.m12();
#else
Q_UNUSED( style )
return 1;
#endif
}

double QgsLayoutUtils::scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style, QPainter *painter )
{
return style->levelOfDetailFromTransform( painter->worldTransform() );
}

QgsMapLayer *QgsLayoutUtils::mapLayerFromString( const QString &string, QgsProject *project )
@@ -208,8 +208,17 @@ class CORE_EXPORT QgsLayoutUtils

/**
* Extracts the scale factor from an item \a style.
*
* \deprecated Use the variant with a QPainter argument instead
*/
Q_DECL_DEPRECATED static double scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style ) SIP_DEPRECATED;

/**
* Extracts the scale factor from an item \a style and \a painter.
*
* \since QGIS 3.20
*/
static double scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style );
static double scaleFactorFromItemStyle( const QStyleOptionGraphicsItem *style, QPainter *painter );

/**
* Resolves a \a string into a map layer from a given \a project. Attempts different
@@ -55,7 +55,6 @@ class TestQgsLayoutUtils: public QObject
void drawTextRect(); //test drawing text in a rect
void largestRotatedRect(); //test largest rotated rect helper function
void decodePaperOrientation();
void scaleFactorFromItemStyle();
void mapLayerFromString();

private:
@@ -648,15 +647,6 @@ void TestQgsLayoutUtils::decodePaperOrientation()
QCOMPARE( orientation, QgsLayoutItemPage::Landscape );
}

void TestQgsLayoutUtils::scaleFactorFromItemStyle()
{
QStyleOptionGraphicsItem style;
style.matrix = QMatrix( 2, 0, 0, 0, 0, 0 );
QCOMPARE( QgsLayoutUtils::scaleFactorFromItemStyle( &style ), 2.0 );
style.matrix = QMatrix( 0, 2, 0, 0, 0, 0 );
QCOMPARE( QgsLayoutUtils::scaleFactorFromItemStyle( &style ), 2.0 );
}

void TestQgsLayoutUtils::mapLayerFromString()
{
// add some layers to a project

0 comments on commit d369236

Please sign in to comment.