Skip to content

Commit

Permalink
Fix high cpu usage with win32 builds when using shapes in composer (fix
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Apr 2, 2014
1 parent d398560 commit 2b3108f
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/core/composer/qgscomposershape.cpp 100644 → 100755
Expand Up @@ -25,7 +25,8 @@ QgsComposerShape::QgsComposerShape( QgsComposition* composition ): QgsComposerIt
mShape( Ellipse ),
mCornerRadius( 0 ),
mUseSymbolV2( false ), //default to not using SymbolV2 for shapes, to preserve 2.0 api
mShapeStyleSymbol( 0 )
mShapeStyleSymbol( 0 ),
mMaxSymbolBleed( 0 )
{
setFrameEnabled( true );
createDefaultShapeStyleSymbol();
Expand All @@ -36,7 +37,8 @@ QgsComposerShape::QgsComposerShape( qreal x, qreal y, qreal width, qreal height,
mShape( Ellipse ),
mCornerRadius( 0 ),
mUseSymbolV2( false ), //default to not using SymbolV2 for shapes, to preserve 2.0 api
mShapeStyleSymbol( 0 )
mShapeStyleSymbol( 0 ),
mMaxSymbolBleed( 0 )
{
setSceneRect( QRectF( x, y, width, height ) );
setFrameEnabled( true );
Expand All @@ -58,12 +60,14 @@ void QgsComposerShape::setShapeStyleSymbol( QgsFillSymbolV2* symbol )
{
delete mShapeStyleSymbol;
mShapeStyleSymbol = symbol;
update();
emit frameChanged();
refreshSymbol();
}

void QgsComposerShape::refreshSymbol()
{
mMaxSymbolBleed = QgsSymbolLayerV2Utils::estimateMaxSymbolBleed( mShapeStyleSymbol );
updateBoundingRect();

update();
emit frameChanged();
}
Expand All @@ -78,6 +82,11 @@ void QgsComposerShape::createDefaultShapeStyleSymbol()
properties.insert( "color_border", "black" );
properties.insert( "width_border", "0.3" );
mShapeStyleSymbol = QgsFillSymbolV2::createSimple( properties );

mMaxSymbolBleed = QgsSymbolLayerV2Utils::estimateMaxSymbolBleed( mShapeStyleSymbol );
updateBoundingRect();

emit frameChanged();
}

void QgsComposerShape::paint( QPainter* painter, const QStyleOptionGraphicsItem* itemStyle, QWidget* pWidget )
Expand Down Expand Up @@ -205,13 +214,6 @@ void QgsComposerShape::drawShapeUsingSymbol( QPainter* p )

mShapeStyleSymbol->startRender( context );

double maxBleed = QgsSymbolLayerV2Utils::estimateMaxSymbolBleed( mShapeStyleSymbol );

//even though we aren't going to use it to draw the shape, set the pen width as 2 * symbol bleed
//so that the item is fully rendered within it's scene rect
//(QGraphicsRectItem considers the pen width when calculating an item's scene rect)
setPen( QPen( QBrush( Qt::NoBrush ), maxBleed * 2.0 ) );

//need to render using atlas feature properties?
if ( mComposition->atlasComposition().enabled() && mComposition->atlasMode() != QgsComposition::AtlasOff )
{
Expand Down Expand Up @@ -253,7 +255,7 @@ void QgsComposerShape::drawBackground( QPainter* p )

double QgsComposerShape::estimatedFrameBleed() const
{
return QgsSymbolLayerV2Utils::estimateMaxSymbolBleed( mShapeStyleSymbol );
return mMaxSymbolBleed;
}

bool QgsComposerShape::writeXML( QDomElement& elem, QDomDocument & doc ) const
Expand Down Expand Up @@ -330,3 +332,19 @@ void QgsComposerShape::setCornerRadius( double radius )
{
mCornerRadius = radius;
}

QRectF QgsComposerShape::boundingRect() const
{
return mCurrentRectangle;
}

void QgsComposerShape::updateBoundingRect()
{
QRectF rectangle = rect();
rectangle.adjust( -mMaxSymbolBleed, -mMaxSymbolBleed, mMaxSymbolBleed, mMaxSymbolBleed );
if ( rectangle != mCurrentRectangle )
{
prepareGeometryChange();
mCurrentRectangle = rectangle;
}
}
10 changes: 10 additions & 0 deletions src/core/composer/qgscomposershape.h 100644 → 100755
Expand Up @@ -80,6 +80,10 @@ class CORE_EXPORT QgsComposerShape: public QgsComposerItem
* Note: Added in v2.1 */
void setUseSymbolV2( bool useSymbolV2 );

/**Depending on the symbol style, the bounding rectangle can be larger than the shape
@note this function was added in version 2.3*/
QRectF boundingRect() const;

protected:
/* reimplement drawFrame, since it's not a rect, but a custom shape */
virtual void drawFrame( QPainter* p );
Expand All @@ -104,6 +108,9 @@ class CORE_EXPORT QgsComposerShape: public QgsComposerItem
bool mUseSymbolV2;

QgsFillSymbolV2* mShapeStyleSymbol;
double mMaxSymbolBleed;
/**Current bounding rectangle of shape*/
QRectF mCurrentRectangle;

/* draws the custom shape */
void drawShape( QPainter* p );
Expand All @@ -116,6 +123,9 @@ class CORE_EXPORT QgsComposerShape: public QgsComposerItem

/**Returns a point on the line from startPoint to directionPoint that is a certain distance away from the starting point*/
QPointF pointOnLineWithDistance( const QPointF& startPoint, const QPointF& directionPoint, double distance ) const;

/**Updates the bounding rect of this item*/
void updateBoundingRect();
};

#endif // QGSCOMPOSERSHAPEITEM_H

0 comments on commit 2b3108f

Please sign in to comment.