Skip to content

Commit

Permalink
[feature] blend modes for composer map grids (fix #7777)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 13, 2013
1 parent 8880691 commit 881602e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
8 changes: 8 additions & 0 deletions src/app/composer/qgscomposer.cpp
Expand Up @@ -2087,6 +2087,14 @@ bool QgsComposer::containsAdvancedEffects() const
return true;
}
}
if ( currentMap->gridEnabled() )
{
// map contains an grid, check its blend mode
if ( currentMap->gridBlendMode() != QPainter::CompositionMode_SourceOver )
{
return true;
}
}
}
}
return false;
Expand Down
14 changes: 14 additions & 0 deletions src/app/composer/qgscomposermapwidget.cpp
Expand Up @@ -343,6 +343,9 @@ void QgsComposerMapWidget::updateGuiElements()
mFrameStyleComboBox->setCurrentIndex( mFrameStyleComboBox->findText( tr( "No frame" ) ) );
}

//grid blend mode
mGridBlendComboBox->setBlendMode( mComposerMap->gridBlendMode() );

//grid annotation format
QgsComposerMap::GridAnnotationFormat gf = mComposerMap->gridAnnotationFormat();
mAnnotationFormatComboBox->setCurrentIndex(( int )gf );
Expand Down Expand Up @@ -423,6 +426,7 @@ void QgsComposerMapWidget::blockAllSignals( bool b )
mSetToMapCanvasExtentButton->blockSignals( b );
mUpdatePreviewButton->blockSignals( b );
mGridLineStyleButton->blockSignals( b );
mGridBlendComboBox->blockSignals( b );
mDrawAnnotationCheckableGroupBox->blockSignals( b );
mAnnotationFontButton->blockSignals( b );
mAnnotationFormatComboBox->blockSignals( b );
Expand Down Expand Up @@ -705,6 +709,16 @@ void QgsComposerMapWidget::on_mCrossWidthSpinBox_valueChanged( double d )
mComposerMap->endCommand();
}

void QgsComposerMapWidget::on_mGridBlendComboBox_currentIndexChanged( int index )
{
Q_UNUSED( index );
if ( mComposerMap )
{
mComposerMap->setGridBlendMode( mGridBlendComboBox->blendMode() );
}

}

void QgsComposerMapWidget::on_mAnnotationFontButton_clicked()
{
if ( !mComposerMap )
Expand Down
1 change: 1 addition & 0 deletions src/app/composer/qgscomposermapwidget.h
Expand Up @@ -60,6 +60,7 @@ class QgsComposerMapWidget: public QWidget, private Ui::QgsComposerMapWidgetBase
void on_mGridLineStyleButton_clicked();
void on_mGridTypeComboBox_currentIndexChanged( const QString& text );
void on_mCrossWidthSpinBox_valueChanged( double d );
void on_mGridBlendComboBox_currentIndexChanged( int index );
void on_mAnnotationFontButton_clicked();
void on_mDistanceToMapFrameSpinBox_valueChanged( double d );

Expand Down
29 changes: 23 additions & 6 deletions src/core/composer/qgscomposermap.cpp
Expand Up @@ -43,9 +43,10 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition, int x, int y, int w
: QgsComposerItem( x, y, width, height, composition ), mKeepLayerSet( false ),
mOverviewFrameMapId( -1 ), mOverviewBlendMode( QPainter::CompositionMode_SourceOver ), mOverviewInverted( false ), mGridEnabled( false ), mGridStyle( Solid ),
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ),
mGridBlendMode( QPainter::CompositionMode_SourceOver ), mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ),
mTopGridAnnotationPosition( OutsideMapFrame ), mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ),
mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ), mTopGridAnnotationDirection( Horizontal ),
mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ),
mCrossLength( 3 ), mMapCanvas( 0 ), mDrawCanvasItems( true )
{
mComposition = composition;
Expand Down Expand Up @@ -86,9 +87,10 @@ QgsComposerMap::QgsComposerMap( QgsComposition *composition )
: QgsComposerItem( 0, 0, 10, 10, composition ), mKeepLayerSet( false ), mOverviewFrameMapId( -1 ),
mOverviewBlendMode( QPainter::CompositionMode_SourceOver ), mOverviewInverted( false ), mGridEnabled( false ), mGridStyle( Solid ),
mGridIntervalX( 0.0 ), mGridIntervalY( 0.0 ), mGridOffsetX( 0.0 ), mGridOffsetY( 0.0 ), mGridAnnotationPrecision( 3 ), mShowGridAnnotation( false ),
mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ), mTopGridAnnotationPosition( OutsideMapFrame ),
mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ), mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ),
mTopGridAnnotationDirection( Horizontal ), mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ), mCrossLength( 3 ),
mGridBlendMode( QPainter::CompositionMode_SourceOver ), mLeftGridAnnotationPosition( OutsideMapFrame ), mRightGridAnnotationPosition( OutsideMapFrame ),
mTopGridAnnotationPosition( OutsideMapFrame ), mBottomGridAnnotationPosition( OutsideMapFrame ), mAnnotationFrameDistance( 1.0 ),
mLeftGridAnnotationDirection( Horizontal ), mRightGridAnnotationDirection( Horizontal ), mTopGridAnnotationDirection( Horizontal ),
mBottomGridAnnotationDirection( Horizontal ), mGridFrameStyle( NoGridFrame ), mGridFrameWidth( 2.0 ), mCrossLength( 3 ),
mMapCanvas( 0 ), mDrawCanvasItems( true )
{
mOverviewFrameMapSymbol = 0;
Expand Down Expand Up @@ -792,6 +794,7 @@ bool QgsComposerMap::writeXML( QDomElement& elem, QDomDocument & doc ) const
gridElem.setAttribute( "crossLength", QString::number( mCrossLength ) );
gridElem.setAttribute( "gridFrameStyle", mGridFrameStyle );
gridElem.setAttribute( "gridFrameWidth", QString::number( mGridFrameWidth ) );
gridElem.setAttribute( "gridBlendMode", QgsMapRenderer::getBlendModeEnum( mGridBlendMode ) );
QDomElement gridLineStyleElem = QgsSymbolLayerV2Utils::saveSymbol( QString(), mGridLineSymbol, doc );
gridElem.appendChild( gridLineStyleElem );

Expand Down Expand Up @@ -938,6 +941,7 @@ bool QgsComposerMap::readXML( const QDomElement& itemElem, const QDomDocument& d
mCrossLength = gridElem.attribute( "crossLength", "3" ).toDouble();
mGridFrameStyle = ( QgsComposerMap::GridFrameStyle )gridElem.attribute( "gridFrameStyle", "0" ).toInt();
mGridFrameWidth = gridElem.attribute( "gridFrameWidth", "2.0" ).toDouble();
setGridBlendMode( QgsMapRenderer::getCompositionMode(( QgsMapRenderer::BlendMode ) gridElem.attribute( "gridBlendMode", "0" ).toUInt() ) );

QDomElement gridSymbolElem = gridElem.firstChildElement( "symbol" );
delete mGridLineSymbol;
Expand Down Expand Up @@ -1035,6 +1039,10 @@ void QgsComposerMap::drawGrid( QPainter* p )
QRectF thisPaintRect = QRectF( 0, 0, QGraphicsRectItem::rect().width(), QGraphicsRectItem::rect().height() );
p->setClipRect( thisPaintRect );

// set the blend mode for drawing grid lines
p->save();
p->setCompositionMode( mGridBlendMode );

//simpler approach: draw vertical lines first, then horizontal ones
if ( mGridStyle == QgsComposerMap::Solid )
{
Expand Down Expand Up @@ -1095,6 +1103,8 @@ void QgsComposerMap::drawGrid( QPainter* p )
drawGridLine( QLineF( hIt->second.p2(), crossEnd1 ), p );
}
}
// reset composition mode
p->restore();

p->setClipRect( thisPaintRect , Qt::NoClip );

Expand All @@ -1107,6 +1117,7 @@ void QgsComposerMap::drawGrid( QPainter* p )
{
drawCoordinateAnnotations( p, horizontalLines, verticalLines );
}

}

void QgsComposerMap::drawGridFrame( QPainter* p, const QList< QPair< double, QLineF > >& hLines, const QList< QPair< double, QLineF > >& vLines )
Expand Down Expand Up @@ -1597,6 +1608,12 @@ QPen QgsComposerMap::gridPen() const
return p;
}

void QgsComposerMap::setGridBlendMode( QPainter::CompositionMode blendMode )
{
mGridBlendMode = blendMode;
update();
}

QRectF QgsComposerMap::boundingRect() const
{
return mCurrentRectangle;
Expand Down
7 changes: 7 additions & 0 deletions src/core/composer/qgscomposermap.h
Expand Up @@ -328,6 +328,11 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
void setGridLineSymbol( QgsLineSymbolV2* symbol );
QgsLineSymbolV2* gridLineSymbol() { return mGridLineSymbol; }

/** Returns the grid's blending mode */
QPainter::CompositionMode gridBlendMode() const {return mGridBlendMode;}
/** Sets the grid's blending mode*/
void setGridBlendMode( QPainter::CompositionMode blendMode );

/**Sets mId to a number not yet used in the composition. mId is kept if it is not in use.
Usually, this function is called before adding the composer map to the composition*/
void assignFreeId();
Expand Down Expand Up @@ -421,6 +426,8 @@ class CORE_EXPORT QgsComposerMap : public QgsComposerItem
int mGridAnnotationPrecision;
/**True if coordinate values should be drawn*/
bool mShowGridAnnotation;
/**Blend mode for grid*/
QPainter::CompositionMode mGridBlendMode;

/**Annotation position for left map side (inside / outside / not shown)*/
GridAnnotationPosition mLeftGridAnnotationPosition;
Expand Down
12 changes: 11 additions & 1 deletion src/ui/qgscomposermapwidgetbase.ui
Expand Up @@ -56,7 +56,7 @@
<x>0</x>
<y>-202</y>
<width>440</width>
<height>1310</height>
<height>1347</height>
</rect>
</property>
<property name="sizePolicy">
Expand Down Expand Up @@ -448,6 +448,16 @@
</property>
</widget>
</item>
<item row="7" column="1">
<widget class="QgsBlendModeComboBox" name="mGridBlendComboBox"/>
</item>
<item row="7" column="0">
<widget class="QLabel" name="mGridBlendLabel">
<property name="text">
<string>Blend mode</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
Expand Down

0 comments on commit 881602e

Please sign in to comment.