Skip to content

Commit

Permalink
Draw background on painter before rendering map for composer map items (
Browse files Browse the repository at this point in the history
fix #7461)
  • Loading branch information
nyalldawson committed Apr 17, 2013
1 parent 4fa44cf commit e6f3b2e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/app/composer/qgscomposeritemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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();
}
Expand Down
10 changes: 9 additions & 1 deletion src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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 )
Expand Down Expand Up @@ -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 );
}
}

Expand Down Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;}

Expand Down Expand Up @@ -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*/
Expand Down
22 changes: 19 additions & 3 deletions src/core/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 ) );
Expand All @@ -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 );
Expand Down Expand Up @@ -323,6 +336,9 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
return;
}

// Fill with background color
drawBackground( painter );

QgsRectangle requestRectangle;
requestedExtent( requestRectangle );

Expand Down

0 comments on commit e6f3b2e

Please sign in to comment.