Skip to content

Commit 9d6428f

Browse files
committedApr 1, 2013
Merge branch 'composer_blend' of https://github.com/nyalldawson/Quantum-GIS
2 parents 6c3f418 + 7b6e322 commit 9d6428f

10 files changed

+251
-2
lines changed
 

‎src/app/composer/qgscomposeritemwidget.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,8 @@ void QgsComposerItemWidget::setValuesForGuiElements()
354354
mBackgroundGroupBox->blockSignals( true );
355355
mItemIdLineEdit->blockSignals( true );
356356
mItemUuidLineEdit->blockSignals( true );
357+
mBlendModeCombo->blockSignals( true );
358+
mTransparencySlider->blockSignals( true );
357359
// mTransparencySpinBox->blockSignals( true );
358360

359361
mBackgroundColorButton->setColor( mItem->brush().color() );
@@ -371,16 +373,36 @@ void QgsComposerItemWidget::setValuesForGuiElements()
371373
mItemUuidLineEdit->setText( mItem->uuid() );
372374
mFrameGroupBox->setChecked( mItem->hasFrame() );
373375
mBackgroundGroupBox->setChecked( mItem->hasBackground() );
376+
mBlendModeCombo->setBlendMode( mItem->blendMode() );
377+
mTransparencySlider->setValue( mItem->transparency() );
374378

375379
// mTransparencySlider->blockSignals( false );
376380
mOutlineWidthSpinBox->blockSignals( false );
377381
mFrameGroupBox->blockSignals( false );
378382
mBackgroundGroupBox->blockSignals( false );
379383
mItemIdLineEdit->blockSignals( false );
380384
mItemUuidLineEdit->blockSignals( false );
385+
mBlendModeCombo->blockSignals( false );
386+
mTransparencySlider->blockSignals( false );
381387
// mTransparencySpinBox->blockSignals( false );
382388
}
383389

390+
void QgsComposerItemWidget::on_mBlendModeCombo_currentIndexChanged( int index )
391+
{
392+
Q_UNUSED( index );
393+
if ( mItem )
394+
{
395+
mItem->setBlendMode(( QgsMapRenderer::BlendMode ) mBlendModeCombo->blendMode() );
396+
}
397+
}
398+
399+
void QgsComposerItemWidget::on_mTransparencySlider_valueChanged( int value )
400+
{
401+
if ( mItem )
402+
{
403+
mItem->setTransparency( value );
404+
}
405+
}
384406

385407
void QgsComposerItemWidget::on_mItemIdLineEdit_editingFinished()
386408
{

‎src/app/composer/qgscomposeritemwidget.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ class QgsComposerItemWidget: public QWidget, private Ui::QgsComposerItemWidgetBa
7070
void on_mLowerMiddleCheckBox_stateChanged( int state ) { Q_UNUSED( state ); changeItemPosition(); }
7171
void on_mLowerRightCheckBox_stateChanged( int state ) { Q_UNUSED( state ); changeItemPosition(); }
7272

73+
void on_mBlendModeCombo_currentIndexChanged( int index );
74+
void on_mTransparencySlider_valueChanged( int value );
75+
7376
void setValuesForGuiElements();
7477
void setValuesForGuiPositionElements();
7578

‎src/core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ SET(QGIS_CORE_SRCS
148148
composer/qgscomposertexttable.cpp
149149
composer/qgscomposerscalebar.cpp
150150
composer/qgscomposershape.cpp
151+
composer/qgscomposereffect.cpp
151152
composer/qgsatlascomposition.cpp
152153
composer/qgslegendmodel.cpp
153154
composer/qgscomposerlegend.cpp
@@ -339,6 +340,7 @@ SET(QGIS_CORE_MOC_HDRS
339340
composer/qgscomposerattributetable.h
340341
composer/qgscomposerhtml.h
341342
composer/qgscomposermultiframe.h
343+
composer/qgscomposereffect.h
342344
composer/qgsatlascomposition.h
343345
composer/qgscomposition.h
344346

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/***************************************************************************
2+
qgscomposereffect.cpp
3+
-------------------
4+
begin : March 2013
5+
copyright : (C) 2013 by Nyall Dawson
6+
email : nyall.dawson@gmail.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#include <QPainter>
19+
20+
#include "qgscomposereffect.h"
21+
22+
QgsComposerEffect::QgsComposerEffect()
23+
: mCompositionMode( QPainter::CompositionMode_SourceOver )
24+
{
25+
}
26+
27+
QgsComposerEffect::~QgsComposerEffect()
28+
{
29+
}
30+
31+
void QgsComposerEffect::draw( QPainter *painter )
32+
{
33+
QPoint offset;
34+
QPixmap pixmap;
35+
36+
// Set desired composition mode then draw source
37+
painter->setCompositionMode( mCompositionMode );
38+
39+
if ( mCompositionMode == QPainter::CompositionMode_SourceOver )
40+
{
41+
// Normal (sourceover) blending, do faster drawSource operation
42+
drawSource( painter );
43+
return;
44+
}
45+
46+
// Otherwise, draw using pixmap so QPrinter output works as expected
47+
if ( sourceIsPixmap() )
48+
{
49+
// No point in drawing in device coordinates (pixmap will be scaled anyways).
50+
pixmap = sourcePixmap( Qt::LogicalCoordinates, &offset );
51+
}
52+
else
53+
{
54+
// Draw pixmap in device coordinates to avoid pixmap scaling;
55+
pixmap = sourcePixmap( Qt::DeviceCoordinates, &offset );
56+
painter->setWorldTransform( QTransform() );
57+
}
58+
59+
painter->drawPixmap( offset, pixmap );
60+
61+
}
62+
63+
void QgsComposerEffect::setCompositionMode( const QPainter::CompositionMode compositionMode )
64+
{
65+
mCompositionMode = compositionMode;
66+
67+
// force redraw with new composition mode
68+
update();
69+
}
70+
71+
72+

‎src/core/composer/qgscomposereffect.h

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/***************************************************************************
2+
qgscomposereffect.h
3+
-------------------
4+
begin : March 2013
5+
copyright : (C) 2013 by Nyall Dawson
6+
email : nyall.dawson@gmail.com
7+
***************************************************************************/
8+
9+
/***************************************************************************
10+
* *
11+
* This program is free software; you can redistribute it and/or modify *
12+
* it under the terms of the GNU General Public License as published by *
13+
* the Free Software Foundation; either version 2 of the License, or *
14+
* (at your option) any later version. *
15+
* *
16+
***************************************************************************/
17+
18+
#ifndef QGSCOMPOSEREFFECT_H
19+
#define QGSCOMPOSEREFFECT_H
20+
21+
#include <QtGui>
22+
#include <QGraphicsEffect>
23+
24+
class CORE_EXPORT QgsComposerEffect : public QGraphicsEffect
25+
{
26+
Q_OBJECT
27+
28+
public:
29+
QgsComposerEffect();
30+
~QgsComposerEffect();
31+
32+
void setCompositionMode( const QPainter::CompositionMode compositionMode );
33+
34+
protected:
35+
/** Called whenever source needs to be drawn */
36+
virtual void draw( QPainter *painter );
37+
38+
private:
39+
40+
QPainter::CompositionMode mCompositionMode;
41+
};
42+
43+
#endif // QGSCOMPOSEREFFECT_H
44+

‎src/core/composer/qgscomposeritem.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <QGraphicsView>
2424
#include <QPainter>
2525
#include <QUuid>
26+
#include <QGraphicsEffect>
2627

2728
#include "qgsproject.h"
2829

@@ -35,6 +36,7 @@
3536
#include "qgsrectangle.h" //just for debugging
3637
#include "qgslogger.h"
3738
#include "qgssymbollayerv2utils.h" //for pointOnLineWithDistance
39+
#include "qgsmaprenderer.h" //for getCompositionMode
3840

3941
#include <cmath>
4042

@@ -52,6 +54,8 @@ QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue
5254
, mItemPositionLocked( false )
5355
, mLastValidViewScaleFactor( -1 )
5456
, mRotation( 0 )
57+
, mBlendMode( QgsMapRenderer::BlendNormal )
58+
, mTransparency( 0 )
5559
, mLastUsedPositionMode( UpperLeft )
5660
, mId( "" )
5761
, mUuid( QUuid::createUuid().toString() )
@@ -71,6 +75,8 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
7175
, mItemPositionLocked( false )
7276
, mLastValidViewScaleFactor( -1 )
7377
, mRotation( 0 )
78+
, mBlendMode( QgsMapRenderer::BlendNormal )
79+
, mTransparency( 0 )
7480
, mLastUsedPositionMode( UpperLeft )
7581
, mId( "" )
7682
, mUuid( QUuid::createUuid().toString() )
@@ -95,6 +101,11 @@ void QgsComposerItem::init( bool manageZValue )
95101
{
96102
mComposition->addItemToZList( this );
97103
}
104+
105+
// Setup composer effect
106+
mEffect = new QgsComposerEffect();
107+
setGraphicsEffect( mEffect );
108+
98109
}
99110

100111
QgsComposerItem::~QgsComposerItem()
@@ -105,6 +116,7 @@ QgsComposerItem::~QgsComposerItem()
105116
}
106117

107118
delete mBoundingResizeRectangle;
119+
delete mEffect;
108120
deleteAlignItems();
109121
}
110122

@@ -192,6 +204,12 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons
192204
bgColorElem.setAttribute( "alpha", QString::number( bgColor.alpha() ) );
193205
composerItemElem.appendChild( bgColorElem );
194206

207+
//blend mode
208+
composerItemElem.setAttribute( "blendMode", QString::number( mBlendMode ) );
209+
210+
//transparency
211+
composerItemElem.setAttribute( "transparency", QString::number( mTransparency ) );
212+
195213
itemElem.appendChild( composerItemElem );
196214

197215
return true;
@@ -311,6 +329,13 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
311329
setBrush( QBrush( brushColor ) );
312330
}
313331
}
332+
333+
//blend mode
334+
setBlendMode(( QgsMapRenderer::BlendMode ) itemElem.attribute( "blendMode" , "0" ).toInt() );
335+
336+
//transparency
337+
setTransparency( itemElem.attribute( "transparency" , "0" ).toInt() );
338+
314339
return true;
315340
}
316341

@@ -859,6 +884,20 @@ void QgsComposerItem::drawBackground( QPainter* p )
859884
}
860885
}
861886

887+
void QgsComposerItem::setBlendMode( QgsMapRenderer::BlendMode blendMode )
888+
{
889+
mBlendMode = blendMode;
890+
// Update the composer effect to use the new blend mode
891+
mEffect->setCompositionMode( QgsMapRenderer::getCompositionMode( mBlendMode ) );
892+
}
893+
894+
void QgsComposerItem::setTransparency( int transparency )
895+
{
896+
mTransparency = transparency;
897+
// Set the QGraphicItem's opacity
898+
setOpacity( 1. - ( transparency / 100. ) );
899+
}
900+
862901
void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
863902
{
864903
if ( isSelected() )

‎src/core/composer/qgscomposeritem.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#define QGSCOMPOSERITEM_H
1919

2020
#include "qgscomposeritemcommand.h"
21+
#include "qgscomposereffect.h"
22+
#include "qgsmaprenderer.h" // for blend mode functions & enums
2123
#include <QGraphicsRectItem>
2224
#include <QObject>
2325

@@ -195,6 +197,17 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
195197
*/
196198
void setBackgroundEnabled( bool drawBackground ) {mBackground = drawBackground;}
197199

200+
/** Returns the item's composition blending mode */
201+
QgsMapRenderer::BlendMode blendMode() const {return mBlendMode;}
202+
203+
/** Sets the item's composition blending mode*/
204+
void setBlendMode( QgsMapRenderer::BlendMode blendMode );
205+
206+
/** Returns the item's transparency */
207+
int transparency() const {return mTransparency;}
208+
/** Sets the item's transparency */
209+
void setTransparency( int transparency );
210+
198211
/**Composite operations for item groups do nothing per default*/
199212
virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
200213
virtual void removeItems() {}
@@ -297,7 +310,6 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
297310
/**True if item background needs to be painted*/
298311
bool mBackground;
299312

300-
301313
/**True if item position and size cannot be changed with mouse move
302314
@note: this member was added in version 1.2*/
303315
bool mItemPositionLocked;
@@ -308,6 +320,14 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
308320
/**Item rotation in degrees, clockwise*/
309321
double mRotation;
310322

323+
/**Composition blend mode for item*/
324+
QgsMapRenderer::BlendMode mBlendMode;
325+
326+
QgsComposerEffect *mEffect;
327+
328+
/**Item transparency*/
329+
int mTransparency;
330+
311331
/**The item's position mode
312332
@note: this member was added in version 2.0*/
313333
ItemPositionMode mLastUsedPositionMode;

‎src/core/composer/qgscomposition.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include <QSettings>
4747
#include <QDir>
4848

49+
4950
QgsComposition::QgsComposition( QgsMapRenderer* mapRenderer ) :
5051
QGraphicsScene( 0 ), mMapRenderer( mapRenderer ), mPlotStyle( QgsComposition::Preview ), mPageWidth( 297 ), mPageHeight( 210 ), mSpaceBetweenPages( 10 ), mPrintAsRaster( false ), mSelectionTolerance( 0.0 ),
5152
mSnapToGrid( false ), mSnapGridResolution( 10.0 ), mSnapGridOffsetX( 0.0 ), mSnapGridOffsetY( 0.0 ), mAlignmentSnap( true ), mAlignmentSnapTolerance( 2 ),
@@ -64,6 +65,7 @@ QgsComposition::QgsComposition():
6465
mAlignmentSnapTolerance( 2 ), mActiveItemCommand( 0 ), mActiveMultiFrameCommand( 0 ), mAtlasComposition( this )
6566
{
6667
loadSettings();
68+
6769
}
6870

6971
QgsComposition::~QgsComposition()

‎src/core/qgsmaprenderer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ class CORE_EXPORT QgsMapRenderer : public QObject
252252
void setLabelingEngine( QgsLabelingEngineInterface* iface );
253253

254254
//! Returns a QPainter::CompositionMode corresponding to a BlendMode
255-
QPainter::CompositionMode getCompositionMode( const QgsMapRenderer::BlendMode blendMode );
255+
static QPainter::CompositionMode getCompositionMode( const QgsMapRenderer::BlendMode blendMode );
256256

257257
signals:
258258

@@ -347,6 +347,7 @@ class CORE_EXPORT QgsMapRenderer : public QObject
347347

348348
private:
349349
const QgsCoordinateTransform* tr( QgsMapLayer *layer );
350+
350351
};
351352

352353
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.