Skip to content
Permalink
Browse files
[FEATURE] Blend modes for composer items
  • Loading branch information
nyalldawson committed Apr 1, 2013
1 parent 52558e0 commit b7064db6220e0b9f3a7215fb9e380e9068c8c577
@@ -354,6 +354,7 @@ void QgsComposerItemWidget::setValuesForGuiElements()
mBackgroundGroupBox->blockSignals( true );
mItemIdLineEdit->blockSignals( true );
mItemUuidLineEdit->blockSignals( true );
mBlendModeCombo->blockSignals( true );
// mTransparencySpinBox->blockSignals( true );

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

// mTransparencySlider->blockSignals( false );
mOutlineWidthSpinBox->blockSignals( false );
mFrameGroupBox->blockSignals( false );
mBackgroundGroupBox->blockSignals( false );
mItemIdLineEdit->blockSignals( false );
mItemUuidLineEdit->blockSignals( false );
mBlendModeCombo->blockSignals( false );
// mTransparencySpinBox->blockSignals( false );
}

void QgsComposerItemWidget::on_mBlendModeCombo_currentIndexChanged( int index )
{
Q_UNUSED( index );
if ( mItem )
{
mItem->setBlendMode(( QgsMapRenderer::BlendMode ) mBlendModeCombo->blendMode() );
}
}

void QgsComposerItemWidget::on_mItemIdLineEdit_editingFinished()
{
@@ -70,6 +70,8 @@ class QgsComposerItemWidget: public QWidget, private Ui::QgsComposerItemWidgetBa
void on_mLowerMiddleCheckBox_stateChanged( int state ) { Q_UNUSED( state ); changeItemPosition(); }
void on_mLowerRightCheckBox_stateChanged( int state ) { Q_UNUSED( state ); changeItemPosition(); }

void on_mBlendModeCombo_currentIndexChanged( int index );

void setValuesForGuiElements();
void setValuesForGuiPositionElements();

@@ -148,6 +148,7 @@ SET(QGIS_CORE_SRCS
composer/qgscomposertexttable.cpp
composer/qgscomposerscalebar.cpp
composer/qgscomposershape.cpp
composer/qgscomposereffect.cpp
composer/qgsatlascomposition.cpp
composer/qgslegendmodel.cpp
composer/qgscomposerlegend.cpp
@@ -339,6 +340,7 @@ SET(QGIS_CORE_MOC_HDRS
composer/qgscomposerattributetable.h
composer/qgscomposerhtml.h
composer/qgscomposermultiframe.h
composer/qgscomposereffect.h
composer/qgsatlascomposition.h
composer/qgscomposition.h

@@ -0,0 +1,51 @@
/***************************************************************************
qgscomposereffect.cpp
-------------------
begin : March 2013
copyright : (C) 2013 by Nyall Dawson
email : nyall.dawson@gmail.com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#include <QPainter>

#include "qgscomposereffect.h"

QgsComposerEffect::QgsComposerEffect()
: mCompositionMode( QPainter::CompositionMode_SourceOver )
{
}

QgsComposerEffect::~QgsComposerEffect()
{
}

void QgsComposerEffect::draw( QPainter *painter )
{
QPoint offset;
QPixmap pixmap;

// Set desired composition mode then draw source
painter->setCompositionMode( mCompositionMode );
drawSource( painter );

}

void QgsComposerEffect::setCompositionMode( const QPainter::CompositionMode compositionMode )
{
mCompositionMode = compositionMode;

// force redraw with new composition mode
update();
}



@@ -0,0 +1,44 @@
/***************************************************************************
qgscomposereffect.h
-------------------
begin : March 2013
copyright : (C) 2013 by Nyall Dawson
email : nyall.dawson@gmail.com
***************************************************************************/

/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/

#ifndef QGSCOMPOSEREFFECT_H
#define QGSCOMPOSEREFFECT_H

#include <QtGui>
#include <QGraphicsEffect>

class CORE_EXPORT QgsComposerEffect : public QGraphicsEffect
{
Q_OBJECT

public:
QgsComposerEffect();
~QgsComposerEffect();

void setCompositionMode( const QPainter::CompositionMode compositionMode );

protected:
/** Called whenever source needs to be drawn */
virtual void draw( QPainter *painter );

private:

QPainter::CompositionMode mCompositionMode;
};

#endif // QGSCOMPOSEREFFECT_H

@@ -23,6 +23,7 @@
#include <QGraphicsView>
#include <QPainter>
#include <QUuid>
#include <QGraphicsEffect>

#include "qgsproject.h"

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

#include <cmath>

@@ -52,6 +54,7 @@ QgsComposerItem::QgsComposerItem( QgsComposition* composition, bool manageZValue
, mItemPositionLocked( false )
, mLastValidViewScaleFactor( -1 )
, mRotation( 0 )
, mBlendMode( QgsMapRenderer::BlendNormal )
, mLastUsedPositionMode( UpperLeft )
, mId( "" )
, mUuid( QUuid::createUuid().toString() )
@@ -71,6 +74,7 @@ QgsComposerItem::QgsComposerItem( qreal x, qreal y, qreal width, qreal height, Q
, mItemPositionLocked( false )
, mLastValidViewScaleFactor( -1 )
, mRotation( 0 )
, mBlendMode( QgsMapRenderer::BlendNormal )
, mLastUsedPositionMode( UpperLeft )
, mId( "" )
, mUuid( QUuid::createUuid().toString() )
@@ -95,6 +99,10 @@ void QgsComposerItem::init( bool manageZValue )
{
mComposition->addItemToZList( this );
}

// Setup composer effect
mEffect = new QgsComposerEffect();
setGraphicsEffect( mEffect );
}

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

delete mBoundingResizeRectangle;
delete mEffect;
deleteAlignItems();
}

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

//blend mode
composerItemElem.setAttribute( "blendMode", QString::number( mBlendMode ) );

itemElem.appendChild( composerItemElem );

return true;
@@ -311,6 +323,10 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
setBrush( QBrush( brushColor ) );
}
}

//blend mode
setBlendMode(( QgsMapRenderer::BlendMode ) itemElem.attribute( "blendMode" , "0" ).toInt() );

return true;
}

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

void QgsComposerItem::setBlendMode( QgsMapRenderer::BlendMode blendMode )
{
mBlendMode = blendMode;
// Update the composer effect to use the new blend mode
mEffect->setCompositionMode( QgsMapRenderer::getCompositionMode( mBlendMode ) );
}


void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
{
if ( isSelected() )
@@ -18,6 +18,8 @@
#define QGSCOMPOSERITEM_H

#include "qgscomposeritemcommand.h"
#include "qgscomposereffect.h"
#include "qgsmaprenderer.h" // for blend mode functions & enums
#include <QGraphicsRectItem>
#include <QObject>

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

/** Returns the item's composition blending mode */
QgsMapRenderer::BlendMode blendMode() const {return mBlendMode;}

/** Sets the item's composition blending mode*/
void setBlendMode( QgsMapRenderer::BlendMode blendMode );

/**Composite operations for item groups do nothing per default*/
virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
virtual void removeItems() {}
@@ -297,7 +305,6 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
/**True if item background needs to be painted*/
bool mBackground;


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

/**Composition blend mode for item*/
QgsMapRenderer::BlendMode mBlendMode;

QgsComposerEffect *mEffect;

/**The item's position mode
@note: this member was added in version 2.0*/
ItemPositionMode mLastUsedPositionMode;
@@ -46,6 +46,7 @@
#include <QSettings>
#include <QDir>


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

}

QgsComposition::~QgsComposition()
@@ -252,7 +252,7 @@ class CORE_EXPORT QgsMapRenderer : public QObject
void setLabelingEngine( QgsLabelingEngineInterface* iface );

//! Returns a QPainter::CompositionMode corresponding to a BlendMode
QPainter::CompositionMode getCompositionMode( const QgsMapRenderer::BlendMode blendMode );
static QPainter::CompositionMode getCompositionMode( const QgsMapRenderer::BlendMode blendMode );

signals:

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

private:
const QgsCoordinateTransform* tr( QgsMapLayer *layer );

};

#endif
@@ -337,6 +337,28 @@
</layout>
</widget>
</item>
<item>
<widget class="QgsCollapsibleGroupBoxBasic" name="groupRendering">
<property name="title">
<string>Rendering</string>
</property>
<property name="collapsed" stdset="0">
<bool>true</bool>
</property>
<layout class="QFormLayout" name="formLayout_3">
<item row="0" column="0">
<widget class="QLabel" name="labelBlendMode">
<property name="text">
<string>Blending mode</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QgsBlendModeComboBox" name="mBlendModeCombo"/>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
@@ -367,6 +389,11 @@
<header location="global">qgscollapsiblegroupbox.h</header>
<container>1</container>
</customwidget>
<customwidget>
<class>QgsBlendModeComboBox</class>
<extends>QComboBox</extends>
<header>qgsblendmodecombobox.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>

0 comments on commit b7064db

Please sign in to comment.