Skip to content

Commit

Permalink
Ensure that we always call prepareGeometryChange BEFORE changing
Browse files Browse the repository at this point in the history
properties which effect the boundingRect of a layout item

Refs #52079
Refs https://bugreports.qt.io/browse/QTBUG-18021
  • Loading branch information
nyalldawson committed Oct 5, 2023
1 parent 668734a commit 2bd2507
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 33 deletions.
3 changes: 0 additions & 3 deletions python/core/auto_generated/layout/qgslayoutitemlabel.sip.in
Expand Up @@ -232,14 +232,11 @@ Returns the label font color.

virtual QRectF boundingRect() const;


virtual void setFrameEnabled( bool drawFrame );


virtual void setFrameStrokeWidth( QgsLayoutMeasurement strokeWidth );



QgsTextFormat textFormat() const;
%Docstring
Returns the text format used for drawing text in the label.
Expand Down
60 changes: 38 additions & 22 deletions src/core/layout/qgslayoutitemlabel.cpp
Expand Up @@ -20,7 +20,6 @@
#include "qgslayoututils.h"
#include "qgslayoutmodel.h"
#include "qgsexpression.h"
#include "qgsnetworkaccessmanager.h"
#include "qgsvectorlayer.h"
#include "qgsdistancearea.h"
#include "qgsfontutils.h"
Expand Down Expand Up @@ -59,6 +58,11 @@ QgsLayoutItemLabel::QgsLayoutItemLabel( QgsLayout *layout )
mFormat.setSize( 10 );
mFormat.setSizeUnit( Qgis::RenderUnit::Points );

connect( this, &QgsLayoutItem::sizePositionChanged, this, [ = ]
{
updateBoundingRect();
} );

//default to no background
setBackgroundEnabled( false );

Expand Down Expand Up @@ -219,6 +223,29 @@ void QgsLayoutItemLabel::refreshExpressionContext()
update();
}

void QgsLayoutItemLabel::updateBoundingRect()
{
QRectF rectangle = rect();
const double frameExtension = frameEnabled() ? pen().widthF() / 2.0 : 0.0;
if ( frameExtension > 0 )
rectangle.adjust( -frameExtension, -frameExtension, frameExtension, frameExtension );

if ( mMarginX < 0 )
{
rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
}
if ( mMarginY < 0 )
{
rectangle.adjust( 0, mMarginY, 0, -mMarginY );
}

if ( rectangle != mCurrentRectangle )
{
prepareGeometryChange();
mCurrentRectangle = rectangle;
}
}

QString QgsLayoutItemLabel::currentText() const
{
QString displayText = mText;
Expand Down Expand Up @@ -275,19 +302,19 @@ void QgsLayoutItemLabel::setMargin( const double m )
{
mMarginX = m;
mMarginY = m;
prepareGeometryChange();
updateBoundingRect();
}

void QgsLayoutItemLabel::setMarginX( const double margin )
{
mMarginX = margin;
prepareGeometryChange();
updateBoundingRect();
}

void QgsLayoutItemLabel::setMarginY( const double margin )
{
mMarginY = margin;
prepareGeometryChange();
updateBoundingRect();
}

void QgsLayoutItemLabel::adjustSizeToText()
Expand Down Expand Up @@ -413,6 +440,8 @@ bool QgsLayoutItemLabel::readPropertiesFromElement( const QDomElement &itemElem,
}
}

updateBoundingRect();

return true;
}

Expand Down Expand Up @@ -452,32 +481,19 @@ QString QgsLayoutItemLabel::displayName() const

QRectF QgsLayoutItemLabel::boundingRect() const
{
QRectF rectangle = rect();
const double penWidth = frameEnabled() ? ( pen().widthF() / 2.0 ) : 0;
rectangle.adjust( -penWidth, -penWidth, penWidth, penWidth );

if ( mMarginX < 0 )
{
rectangle.adjust( mMarginX, 0, -mMarginX, 0 );
}
if ( mMarginY < 0 )
{
rectangle.adjust( 0, mMarginY, 0, -mMarginY );
}

return rectangle;
return mCurrentRectangle;
}

void QgsLayoutItemLabel::setFrameEnabled( const bool drawFrame )
void QgsLayoutItemLabel::setFrameEnabled( bool drawFrame )
{
QgsLayoutItem::setFrameEnabled( drawFrame );
prepareGeometryChange();
updateBoundingRect();
}

void QgsLayoutItemLabel::setFrameStrokeWidth( const QgsLayoutMeasurement strokeWidth )
void QgsLayoutItemLabel::setFrameStrokeWidth( QgsLayoutMeasurement strokeWidth )
{
QgsLayoutItem::setFrameStrokeWidth( strokeWidth );
prepareGeometryChange();
updateBoundingRect();
}

void QgsLayoutItemLabel::refresh()
Expand Down
9 changes: 4 additions & 5 deletions src/core/layout/qgslayoutitemlabel.h
Expand Up @@ -211,14 +211,9 @@ class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem
// In case of negative margins, the bounding rect may be larger than the
// label's frame
QRectF boundingRect() const override;

// Reimplemented to call prepareGeometryChange after toggling frame
void setFrameEnabled( bool drawFrame ) override;

// Reimplemented to call prepareGeometryChange after changing stroke width
void setFrameStrokeWidth( QgsLayoutMeasurement strokeWidth ) override;


/**
* Returns the text format used for drawing text in the label.
* \see setTextFormat()
Expand Down Expand Up @@ -253,6 +248,8 @@ class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem
private slots:

void refreshExpressionContext();
//! Updates the bounding rect of this item
void updateBoundingRect();

private:
bool mFirstRender = true;
Expand Down Expand Up @@ -297,6 +294,8 @@ class CORE_EXPORT QgsLayoutItemLabel: public QgsLayoutItem

std::unique_ptr< QgsDistanceArea > mDistanceArea;

QRectF mCurrentRectangle;

friend class QgsLayoutItemHtml;
};

Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitemnodeitem.cpp
Expand Up @@ -344,10 +344,10 @@ void QgsLayoutNodesItem::updateBoundingRect()
{
QRectF br = rect();
br.adjust( -mMaxSymbolBleed, -mMaxSymbolBleed, mMaxSymbolBleed, mMaxSymbolBleed );
prepareGeometryChange();
mCurrentRectangle = br;

// update
prepareGeometryChange();
update();
}

Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitempage.cpp
Expand Up @@ -38,8 +38,8 @@ QgsLayoutItemPage::QgsLayoutItemPage( QgsLayout *layout )

connect( this, &QgsLayoutItem::sizePositionChanged, this, [ = ]
{
mBoundingRect = QRectF();
prepareGeometryChange();
mBoundingRect = QRectF();
} );

const QFont font;
Expand Down
2 changes: 1 addition & 1 deletion src/core/layout/qgslayoutitempolyline.cpp
Expand Up @@ -467,10 +467,10 @@ void QgsLayoutItemPolyline::updateBoundingRect()
margin += 0.5 * mArrowHeadWidth;
}
br.adjust( -margin, -margin, margin, margin );
prepareGeometryChange();
mCurrentRectangle = br;

// update
prepareGeometryChange();
update();
}

Expand Down

0 comments on commit 2bd2507

Please sign in to comment.