Skip to content

Commit

Permalink
Avoid use of QStyleOptionGraphicsItem::matrix, which is removed
Browse files Browse the repository at this point in the history
in Qt 6
  • Loading branch information
nyalldawson committed Mar 29, 2021
1 parent 375ab0e commit d369236
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 22 deletions.
12 changes: 11 additions & 1 deletion python/core/auto_generated/layout/qgslayoututils.sip.in
Expand Up @@ -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 );
Expand Down
10 changes: 5 additions & 5 deletions src/core/layout/qgslayoutitem.cpp
Expand Up @@ -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 ) );

Expand All @@ -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
{
Expand Down Expand Up @@ -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() );
Expand Down Expand Up @@ -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 );

Expand Down
8 changes: 4 additions & 4 deletions src/core/layout/qgslayoutitemmap.cpp
Expand Up @@ -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 );
}
}
Expand All @@ -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 );
}

Expand Down Expand Up @@ -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 ) );
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitempage.cpp
Expand Up @@ -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
{
Expand Down
10 changes: 10 additions & 0 deletions src/core/layout/qgslayoututils.cpp
Expand Up @@ -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
Expand All @@ -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 )
Expand Down
11 changes: 10 additions & 1 deletion src/core/layout/qgslayoututils.h
Expand Up @@ -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
Expand Down
10 changes: 0 additions & 10 deletions tests/src/core/testqgslayoututils.cpp
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d369236

Please sign in to comment.