Skip to content

Commit e6f3b2e

Browse files
committedApr 17, 2013
Draw background on painter before rendering map for composer map items (fix #7461)
1 parent 4fa44cf commit e6f3b2e

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed
 

‎src/app/composer/qgscomposeritemwidget.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ void QgsComposerItemWidget::on_mBackgroundColorButton_colorChanged( const QColor
102102
// QColor newColor( newBackgroundColor );
103103
mItem->beginCommand( tr( "Background color changed" ) );
104104
// newColor.setAlpha( 255 - ( mTransparencySpinBox->value() * 2.55 ) );
105-
mItem->setBrush( QBrush( QColor( newBackgroundColor ), Qt::SolidPattern ) );
105+
mItem->setBackgroundColor( newBackgroundColor );
106+
106107
//if the item is a composer map, we need to regenerate the map image
107108
//because it usually is cached
108109
QgsComposerMap* cm = dynamic_cast<QgsComposerMap *>( mItem );
@@ -248,6 +249,15 @@ void QgsComposerItemWidget::on_mBackgroundGroupBox_toggled( bool state )
248249

249250
mItem->beginCommand( tr( "Item background toggled" ) );
250251
mItem->setBackgroundEnabled( state );
252+
253+
//if the item is a composer map, we need to regenerate the map image
254+
//because it usually is cached
255+
QgsComposerMap* cm = dynamic_cast<QgsComposerMap *>( mItem );
256+
if ( cm )
257+
{
258+
cm->cache();
259+
}
260+
251261
mItem->update();
252262
mItem->endCommand();
253263
}

‎src/core/composer/qgscomposeritem.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue
5151
, mVAlignSnapItem( 0 )
5252
, mFrame( false )
5353
, mBackground( true )
54+
, mBackgroundColor( QColor( 255, 255, 255, 255 ) )
5455
, mItemPositionLocked( false )
5556
, mLastValidViewScaleFactor( -1 )
5657
, mRotation( 0 )
@@ -72,6 +73,7 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
7273
, mVAlignSnapItem( 0 )
7374
, mFrame( false )
7475
, mBackground( true )
76+
, mBackgroundColor( QColor( 255, 255, 255, 255 ) )
7577
, mItemPositionLocked( false )
7678
, mLastValidViewScaleFactor( -1 )
7779
, mRotation( 0 )
@@ -326,7 +328,7 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
326328
if ( redOk && greenOk && blueOk && alphaOk )
327329
{
328330
QColor brushColor( bgRed, bgGreen, bgBlue, bgAlpha );
329-
setBrush( QBrush( brushColor ) );
331+
setBackgroundColor( brushColor );
330332
}
331333
}
332334

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

889+
void QgsComposerItem::setBackgroundColor( const QColor& backgroundColor )
890+
{
891+
mBackgroundColor = backgroundColor;
892+
setBrush( QBrush( mBackgroundColor, Qt::SolidPattern ) );
893+
}
894+
887895
void QgsComposerItem::setBlendMode( QPainter::CompositionMode blendMode )
888896
{
889897
mBlendMode = blendMode;

‎src/core/composer/qgscomposeritem.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,19 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
197197
*/
198198
void setBackgroundEnabled( bool drawBackground ) {mBackground = drawBackground;}
199199

200+
/** Gets the background color for this item
201+
* @returns background color
202+
* @note introduced in 2.0
203+
*/
204+
QColor backgroundColor() const { return mBackgroundColor; }
205+
206+
/** Sets the background color for this item
207+
* @param backgroundColor new background color
208+
* @returns nothing
209+
* @note introduced in 2.0
210+
*/
211+
void setBackgroundColor( const QColor& backgroundColor );
212+
200213
/** Returns the item's composition blending mode */
201214
QPainter::CompositionMode blendMode() const {return mBlendMode;}
202215

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

313328
/**True if item position and size cannot be changed with mouse move
314329
@note: this member was added in version 1.2*/

‎src/core/composer/qgscomposermap.cpp

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,18 @@ void QgsComposerMap::cache( void )
228228
double forcedWidthScaleFactor = w / requestExtent.width() / mapUnitsToMM();
229229

230230
mCacheImage = QImage( w, h, QImage::Format_ARGB32 );
231-
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
231+
232+
if ( hasBackground() )
233+
{
234+
//Initially fill image with specified background color. This ensures that layers with blend modes will
235+
//preview correctly
236+
mCacheImage.fill( backgroundColor().rgba() );
237+
}
238+
else
239+
{
240+
//no background, but start with empty fill to avoid artifacts
241+
mCacheImage.fill( QColor( 255, 255, 255, 0 ).rgba() );
242+
}
232243

233244
double mapUnitsPerPixel = mExtent.width() / w;
234245

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

260-
drawBackground( painter );
261-
262271
if ( mComposition->plotStyle() == QgsComposition::Preview && mPreviewMode == Rectangle )
263272
{
273+
// Fill with background color
274+
drawBackground( painter );
264275
QFont messageFont( "", 12 );
265276
painter->setFont( messageFont );
266277
painter->setPen( QColor( 0, 0, 0 ) );
@@ -272,6 +283,8 @@ void QgsComposerMap::paint( QPainter* painter, const QStyleOptionGraphicsItem* i
272283
//Qt 4.4.0 and 4.4.1 have problems with recursive paintings
273284
//QgsComposerMap::cache() and QgsComposerMap::update() need to be called by
274285
//client functions
286+
287+
//Background color is already included in cached image, so no need to draw
275288

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

339+
// Fill with background color
340+
drawBackground( painter );
341+
326342
QgsRectangle requestRectangle;
327343
requestedExtent( requestRectangle );
328344

0 commit comments

Comments
 (0)
Please sign in to comment.