Skip to content
Permalink
Browse files

Draw background on painter before rendering map for composer map items (

fix #7461)
  • Loading branch information
nyalldawson committed Apr 17, 2013
1 parent 4fa44cf commit e6f3b2e0086bbfc3beed57855c1ec4b024ccd35d
@@ -102,7 +102,8 @@ void QgsComposerItemWidget::on_mBackgroundColorButton_colorChanged( const QColor
// QColor newColor( newBackgroundColor );
mItem->beginCommand( tr( "Background color changed" ) );
// newColor.setAlpha( 255 - ( mTransparencySpinBox->value() * 2.55 ) );
mItem->setBrush( QBrush( QColor( newBackgroundColor ), Qt::SolidPattern ) );
mItem->setBackgroundColor( newBackgroundColor );

//if the item is a composer map, we need to regenerate the map image
//because it usually is cached
QgsComposerMap* cm = dynamic_cast<QgsComposerMap *>( mItem );
@@ -248,6 +249,15 @@ void QgsComposerItemWidget::on_mBackgroundGroupBox_toggled( bool state )

mItem->beginCommand( tr( "Item background toggled" ) );
mItem->setBackgroundEnabled( state );

//if the item is a composer map, we need to regenerate the map image
//because it usually is cached
QgsComposerMap* cm = dynamic_cast<QgsComposerMap *>( mItem );
if ( cm )
{
cm->cache();
}

mItem->update();
mItem->endCommand();
}
@@ -51,6 +51,7 @@ QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue
, mVAlignSnapItem( 0 )
, mFrame( false )
, mBackground( true )
, mBackgroundColor( QColor( 255, 255, 255, 255 ) )
, mItemPositionLocked( false )
, mLastValidViewScaleFactor( -1 )
, mRotation( 0 )
@@ -72,6 +73,7 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
, mVAlignSnapItem( 0 )
, mFrame( false )
, mBackground( true )
, mBackgroundColor( QColor( 255, 255, 255, 255 ) )
, mItemPositionLocked( false )
, mLastValidViewScaleFactor( -1 )
, mRotation( 0 )
@@ -326,7 +328,7 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
if ( redOk && greenOk && blueOk && alphaOk )
{
QColor brushColor( bgRed, bgGreen, bgBlue, bgAlpha );
setBrush( QBrush( brushColor ) );
setBackgroundColor( brushColor );
}
}

@@ -884,6 +886,12 @@ void QgsComposerItem::drawBackground( QPainter* p )
}
}

void QgsComposerItem::setBackgroundColor( const QColor& backgroundColor )
{
mBackgroundColor = backgroundColor;
setBrush( QBrush( mBackgroundColor, Qt::SolidPattern ) );
}

void QgsComposerItem::setBlendMode( QPainter::CompositionMode blendMode )
{
mBlendMode = blendMode;
@@ -197,6 +197,19 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
*/
void setBackgroundEnabled( bool drawBackground ) {mBackground = drawBackground;}

/** Gets the background color for this item
* @returns background color
* @note introduced in 2.0
*/
QColor backgroundColor() const { return mBackgroundColor; }

/** Sets the background color for this item
* @param backgroundColor new background color
* @returns nothing
* @note introduced in 2.0
*/
void setBackgroundColor( const QColor& backgroundColor );

/** Returns the item's composition blending mode */
QPainter::CompositionMode blendMode() const {return mBlendMode;}

@@ -309,6 +322,8 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
bool mFrame;
/**True if item background needs to be painted*/
bool mBackground;
/**Background color*/
QColor mBackgroundColor;

/**True if item position and size cannot be changed with mouse move
@note: this member was added in version 1.2*/
@@ -228,7 +228,18 @@ void QgsComposerMap::cache( void )
double forcedWidthScaleFactor = w / requestExtent.width() / mapUnitsToMM();

mCacheImage = QImage( w, h, QImage::Format_ARGB32 );
mCacheImage.fill( QColor( 255, 255, 255, 0 ).rgba() ); // the background is drawn by composerItem, but we still need to start with that empty fill to avoid artifacts

if ( hasBackground() )
{
//Initially fill image with specified background color. This ensures that layers with blend modes will
//preview correctly
mCacheImage.fill( backgroundColor().rgba() );
}
else
{
//no background, but start with empty fill to avoid artifacts
mCacheImage.fill( QColor( 255, 255, 255, 0 ).rgba() );
}

double mapUnitsPerPixel = mExtent.width() / w;

@@ -257,10 +268,10 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
painter->save();
painter->setClipRect( thisPaintRect );

drawBackground( painter );

if ( mComposition->plotStyle() == QgsComposition::Preview && mPreviewMode == Rectangle )
{
// Fill with background color
drawBackground( painter );
QFont messageFont( "", 12 );
painter->setFont( messageFont );
painter->setPen( QColor( 0, 0, 0 ) );
@@ -272,6 +283,8 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
//Qt 4.4.0 and 4.4.1 have problems with recursive paintings
//QgsComposerMap::cache() and QgsComposerMap::update() need to be called by
//client functions

//Background color is already included in cached image, so no need to draw

QgsRectangle requestRectangle;
requestedExtent( requestRectangle );
@@ -323,6 +336,9 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
return;
}

// Fill with background color
drawBackground( painter );

QgsRectangle requestRectangle;
requestedExtent( requestRectangle );

0 comments on commit e6f3b2e

Please sign in to comment.
You can’t perform that action at this time.