Draw background on painter before rendering map for composer map items (fix #7461)

This commit is contained in:
nyalldawson 2013-04-17 06:56:37 +10:00
parent 4fa44cfc81
commit e6f3b2e008
4 changed files with 54 additions and 5 deletions

View File

@ -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();
}

View File

@ -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;

View File

@ -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*/

View File

@ -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 );