Skip to content

Commit

Permalink
Merge branch 'composer_blend' of https://github.com/nyalldawson/Quant…
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Apr 1, 2013
2 parents 6c3f418 + 7b6e322 commit 9d6428f
Show file tree
Hide file tree
Showing 10 changed files with 251 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/app/composer/qgscomposeritemwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,8 @@ void QgsComposerItemWidget::setValuesForGuiElements()
mBackgroundGroupBox->blockSignals( true );
mItemIdLineEdit->blockSignals( true );
mItemUuidLineEdit->blockSignals( true );
mBlendModeCombo->blockSignals( true );
mTransparencySlider->blockSignals( true );
// mTransparencySpinBox->blockSignals( true );

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

// mTransparencySlider->blockSignals( false );
mOutlineWidthSpinBox->blockSignals( false );
mFrameGroupBox->blockSignals( false );
mBackgroundGroupBox->blockSignals( false );
mItemIdLineEdit->blockSignals( false );
mItemUuidLineEdit->blockSignals( false );
mBlendModeCombo->blockSignals( false );
mTransparencySlider->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_mTransparencySlider_valueChanged( int value )
{
if ( mItem )
{
mItem->setTransparency( value );
}
}

void QgsComposerItemWidget::on_mItemIdLineEdit_editingFinished()
{
Expand Down
3 changes: 3 additions & 0 deletions src/app/composer/qgscomposeritemwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ 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 on_mTransparencySlider_valueChanged( int value );

void setValuesForGuiElements();
void setValuesForGuiPositionElements();

Expand Down
2 changes: 2 additions & 0 deletions src/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
72 changes: 72 additions & 0 deletions src/core/composer/qgscomposereffect.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/***************************************************************************
qgscomposereffect.cpp
-------------------
begin : March 2013
copyright : (C) 2013 by Nyall Dawson
email : [email protected]
***************************************************************************/

/***************************************************************************
* *
* 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 );

if ( mCompositionMode == QPainter::CompositionMode_SourceOver )
{
// Normal (sourceover) blending, do faster drawSource operation
drawSource( painter );
return;
}

// Otherwise, draw using pixmap so QPrinter output works as expected
if ( sourceIsPixmap() )
{
// No point in drawing in device coordinates (pixmap will be scaled anyways).
pixmap = sourcePixmap( Qt::LogicalCoordinates, &offset );
}
else
{
// Draw pixmap in device coordinates to avoid pixmap scaling;
pixmap = sourcePixmap( Qt::DeviceCoordinates, &offset );
painter->setWorldTransform( QTransform() );
}

painter->drawPixmap( offset, pixmap );

}

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

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



44 changes: 44 additions & 0 deletions src/core/composer/qgscomposereffect.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/***************************************************************************
qgscomposereffect.h
-------------------
begin : March 2013
copyright : (C) 2013 by Nyall Dawson
email : [email protected]
***************************************************************************/

/***************************************************************************
* *
* 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

39 changes: 39 additions & 0 deletions src/core/composer/qgscomposeritem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QGraphicsView>
#include <QPainter>
#include <QUuid>
#include <QGraphicsEffect>

#include "qgsproject.h"

Expand All @@ -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>

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

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

}

QgsComposerItem::~QgsComposerItem()
Expand All @@ -105,6 +116,7 @@ QgsComposerItem::~QgsComposerItem()
}

delete mBoundingResizeRectangle;
delete mEffect;
deleteAlignItems();
}

Expand Down Expand Up @@ -192,6 +204,12 @@ 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 ) );

//transparency
composerItemElem.setAttribute( "transparency", QString::number( mTransparency ) );

itemElem.appendChild( composerItemElem );

return true;
Expand Down Expand Up @@ -311,6 +329,13 @@ bool QgsComposerItem::_readXML( const QDomElement& itemElem, const QDomDocument&
setBrush( QBrush( brushColor ) );
}
}

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

//transparency
setTransparency( itemElem.attribute( "transparency" , "0" ).toInt() );

return true;
}

Expand Down Expand Up @@ -859,6 +884,20 @@ 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::setTransparency( int transparency )
{
mTransparency = transparency;
// Set the QGraphicItem's opacity
setOpacity( 1. - ( transparency / 100. ) );
}

void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
{
if ( isSelected() )
Expand Down
22 changes: 21 additions & 1 deletion src/core/composer/qgscomposeritem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand Down Expand Up @@ -195,6 +197,17 @@ 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 );

/** Returns the item's transparency */
int transparency() const {return mTransparency;}
/** Sets the item's transparency */
void setTransparency( int transparency );

/**Composite operations for item groups do nothing per default*/
virtual void addItem( QgsComposerItem* item ) { Q_UNUSED( item ); }
virtual void removeItems() {}
Expand Down Expand Up @@ -297,7 +310,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;
Expand All @@ -308,6 +320,14 @@ 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;

/**Item transparency*/
int mTransparency;

/**The item's position mode
@note: this member was added in version 2.0*/
ItemPositionMode mLastUsedPositionMode;
Expand Down
2 changes: 2 additions & 0 deletions src/core/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ),
Expand All @@ -64,6 +65,7 @@ QgsComposition::QgsComposition():
mAlignmentSnapTolerance( 2 ), mActiveItemCommand( 0 ), mActiveMultiFrameCommand( 0 ), mAtlasComposition( this )
{
loadSettings();

}

QgsComposition::~QgsComposition()
Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsmaprenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:

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

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

};

#endif
Expand Down
Loading

0 comments on commit 9d6428f

Please sign in to comment.