Skip to content
Permalink
Browse files
Fix high cpu usage with win32 builds when using shapes in composer (fix
  • Loading branch information
nyalldawson committed Apr 2, 2014
1 parent d398560 commit 2b3108f9aeadb6814ad0519b2c53840db2c834d1
Showing with 40 additions and 12 deletions.
  1. +30 −12 src/core/composer/qgscomposershape.cpp
  2. +10 −0 src/core/composer/qgscomposershape.h
@@ -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();
@@ -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 );
@@ -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();
}
@@ -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 )
@@ -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 )
{
@@ -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
@@ -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;
}
}
@@ -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 );
@@ -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 );
@@ -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.