Skip to content

Commit b7064db

Browse files
committedApr 1, 2013
[FEATURE] Blend modes for composer items
1 parent 52558e0 commit b7064db

10 files changed

+178
-2
lines changed
 

‎src/app/composer/qgscomposeritemwidget.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ void QgsComposerItemWidget::setValuesForGuiElements()
354354
mBackgroundGroupBox->blockSignals( true );
355355
mItemIdLineEdit->blockSignals( true );
356356
mItemUuidLineEdit->blockSignals( true );
357+
mBlendModeCombo->blockSignals( true );
357358
// mTransparencySpinBox->blockSignals( true );
358359

359360
mBackgroundColorButton->setColor( mItem->brush().color() );
@@ -371,16 +372,26 @@ void QgsComposerItemWidget::setValuesForGuiElements()
371372
mItemUuidLineEdit->setText( mItem->uuid() );
372373
mFrameGroupBox->setChecked( mItem->hasFrame() );
373374
mBackgroundGroupBox->setChecked( mItem->hasBackground() );
375+
mBlendModeCombo->setBlendMode( mItem->blendMode() );
374376

375377
// mTransparencySlider->blockSignals( false );
376378
mOutlineWidthSpinBox->blockSignals( false );
377379
mFrameGroupBox->blockSignals( false );
378380
mBackgroundGroupBox->blockSignals( false );
379381
mItemIdLineEdit->blockSignals( false );
380382
mItemUuidLineEdit->blockSignals( false );
383+
mBlendModeCombo->blockSignals( false );
381384
// mTransparencySpinBox->blockSignals( false );
382385
}
383386

387+
void QgsComposerItemWidget::on_mBlendModeCombo_currentIndexChanged( int index )
388+
{
389+
Q_UNUSED( index );
390+
if ( mItem )
391+
{
392+
mItem->setBlendMode(( QgsMapRenderer::BlendMode ) mBlendModeCombo->blendMode() );
393+
}
394+
}
384395

385396
void QgsComposerItemWidget::on_mItemIdLineEdit_editingFinished()
386397
{

‎src/app/composer/qgscomposeritemwidget.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ 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+
7375
void setValuesForGuiElements();
7476
void setValuesForGuiPositionElements();
7577

‎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: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
drawSource( painter );
39+
40+
}
41+
42+
void QgsComposerEffect::setCompositionMode( const QPainter::CompositionMode compositionMode )
43+
{
44+
mCompositionMode = compositionMode;
45+
46+
// force redraw with new composition mode
47+
update();
48+
}
49+
50+
51+

‎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: 24 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,7 @@ QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue
5254
, mItemPositionLocked( false )
5355
, mLastValidViewScaleFactor( -1 )
5456
, mRotation( 0 )
57+
, mBlendMode( QgsMapRenderer::BlendNormal )
5558
, mLastUsedPositionMode( UpperLeft )
5659
, mId( "" )
5760
, mUuid( QUuid::createUuid().toString() )
@@ -71,6 +74,7 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
7174
, mItemPositionLocked( false )
7275
, mLastValidViewScaleFactor( -1 )
7376
, mRotation( 0 )
77+
, mBlendMode( QgsMapRenderer::BlendNormal )
7478
, mLastUsedPositionMode( UpperLeft )
7579
, mId( "" )
7680
, mUuid( QUuid::createUuid().toString() )
@@ -95,6 +99,10 @@ void QgsComposerItem::init( bool manageZValue )
9599
{
96100
mComposition->addItemToZList( this );
97101
}
102+
103+
// Setup composer effect
104+
mEffect = new QgsComposerEffect();
105+
setGraphicsEffect( mEffect );
98106
}
99107

100108
QgsComposerItem::~QgsComposerItem()
@@ -105,6 +113,7 @@ QgsComposerItem::~QgsComposerItem()
105113
}
106114

107115
delete mBoundingResizeRectangle;
116+
delete mEffect;
108117
deleteAlignItems();
109118
}
110119

@@ -192,6 +201,9 @@ bool QgsComposerItem::_writeXML( QDomElement& itemElem, QDomDocument& doc ) cons
192201
bgColorElem.setAttribute( "alpha", QString::number( bgColor.alpha() ) );
193202
composerItemElem.appendChild( bgColorElem );
194203

204+
//blend mode
205+
composerItemElem.setAttribute( "blendMode", QString::number( mBlendMode ) );
206+
195207
itemElem.appendChild( composerItemElem );
196208

197209
return true;
@@ -311,6 +323,10 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
311323
setBrush( QBrush( brushColor ) );
312324
}
313325
}
326+
327+
//blend mode
328+
setBlendMode(( QgsMapRenderer::BlendMode ) itemElem.attribute( "blendMode" , "0" ).toInt() );
329+
314330
return true;
315331
}
316332

@@ -859,6 +875,14 @@ void QgsComposerItem::drawBackground( QPainter* p )
859875
}
860876
}
861877

878+
void QgsComposerItem::setBlendMode( QgsMapRenderer::BlendMode blendMode )
879+
{
880+
mBlendMode = blendMode;
881+
// Update the composer effect to use the new blend mode
882+
mEffect->setCompositionMode( QgsMapRenderer::getCompositionMode( mBlendMode ) );
883+
}
884+
885+
862886
void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
863887
{
864888
if ( isSelected() )

‎src/core/composer/qgscomposeritem.h

Lines changed: 13 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,12 @@ 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+
198206
/**Composite operations for item groups do nothing per default*/
199207
virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
200208
virtual void removeItems() {}
@@ -297,7 +305,6 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
297305
/**True if item background needs to be painted*/
298306
bool mBackground;
299307

300-
301308
/**True if item position and size cannot be changed with mouse move
302309
@note: this member was added in version 1.2*/
303310
bool mItemPositionLocked;
@@ -308,6 +315,11 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
308315
/**Item rotation in degrees, clockwise*/
309316
double mRotation;
310317

318+
/**Composition blend mode for item*/
319+
QgsMapRenderer::BlendMode mBlendMode;
320+
321+
QgsComposerEffect *mEffect;
322+
311323
/**The item's position mode
312324
@note: this member was added in version 2.0*/
313325
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

‎src/ui/qgscomposeritemwidgetbase.ui

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,28 @@
337337
</layout>
338338
</widget>
339339
</item>
340+
<item>
341+
<widget class="QgsCollapsibleGroupBoxBasic" name="groupRendering">
342+
<property name="title">
343+
<string>Rendering</string>
344+
</property>
345+
<property name="collapsed" stdset="0">
346+
<bool>true</bool>
347+
</property>
348+
<layout class="QFormLayout" name="formLayout_3">
349+
<item row="0" column="0">
350+
<widget class="QLabel" name="labelBlendMode">
351+
<property name="text">
352+
<string>Blending mode</string>
353+
</property>
354+
</widget>
355+
</item>
356+
<item row="0" column="1">
357+
<widget class="QgsBlendModeComboBox" name="mBlendModeCombo"/>
358+
</item>
359+
</layout>
360+
</widget>
361+
</item>
340362
<item>
341363
<spacer name="verticalSpacer">
342364
<property name="orientation">
@@ -367,6 +389,11 @@
367389
<header location="global">qgscollapsiblegroupbox.h</header>
368390
<container>1</container>
369391
</customwidget>
392+
<customwidget>
393+
<class>QgsBlendModeComboBox</class>
394+
<extends>QComboBox</extends>
395+
<header>qgsblendmodecombobox.h</header>
396+
</customwidget>
370397
</customwidgets>
371398
<resources/>
372399
<connections/>

0 commit comments

Comments
 (0)
Please sign in to comment.