Skip to content

Commit

Permalink
Fix hardcoded border for raster legend items (fix #13540)
Browse files Browse the repository at this point in the history
Previously raster legend items always had a fixed black border.
Now there's options to change the color/width of this border or
disable it entirely.
  • Loading branch information
nyalldawson committed Oct 8, 2015
1 parent 307806a commit d1be2ff
Show file tree
Hide file tree
Showing 15 changed files with 444 additions and 4 deletions.
1 change: 1 addition & 0 deletions python/core/composer/qgscomposeritemcommand.sip
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class QgsComposerMergeCommand : QgsComposerItemCommand
LegendIconSymbolSpace,
LegendBoxSpace,
LegendColumnSpace,
LegendRasterBorderWidth,
//composer picture
ComposerPictureRotation,
// composer scalebar
Expand Down
55 changes: 55 additions & 0 deletions python/core/composer/qgscomposerlegend.sip
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,61 @@ class QgsComposerLegend : QgsComposerItem
int equalColumnWidth() const;
void setEqualColumnWidth( bool s );

/** Returns whether a border will be drawn around raster symbol items.
* @see setDrawRasterBorder()
* @see rasterBorderColor()
* @see rasterBorderWidth()
* @note added in QGIS 2.12
*/
bool drawRasterBorder() const;

/** Sets whether a border will be drawn around raster symbol items.
* @param enabled set to true to draw borders
* @see drawRasterBorder()
* @see setRasterBorderColor()
* @see setRasterBorderWidth()
* @note added in QGIS 2.12
*/
void setDrawRasterBorder( bool enabled );

/** Returns the border color for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @see setRasterBorderColor()
* @see drawRasterBorder()
* @see rasterBorderWidth()
* @note added in QGIS 2.12
*/
QColor rasterBorderColor() const;

/** Sets the border color for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @param color border color
* @see rasterBorderColor()
* @see setDrawRasterBorder()
* @see setRasterBorderWidth()
* @note added in QGIS 2.12
*/
void setRasterBorderColor( const QColor& color );

/** Returns the border width (in millimeters) for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @see setRasterBorderWidth()
* @see drawRasterBorder()
* @see rasterBorderColor()
* @note added in QGIS 2.12
*/
double rasterBorderWidth() const;

/** Sets the border width for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @param width border width in millimeters
* @see rasterBorderWidth()
* @see setDrawRasterBorder()
* @see setRasterBorderColor()
* @note added in QGIS 2.12
*/
void setRasterBorderWidth( double width );

void setComposerMap( const QgsComposerMap* map );
const QgsComposerMap* composerMap() const;

Expand Down
55 changes: 55 additions & 0 deletions python/core/qgslegendsettings.sip
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,61 @@ class QgsLegendSettings
QSizeF symbolSize() const;
void setSymbolSize( QSizeF s );

/** Returns whether a border will be drawn around raster symbol items.
* @see setDrawRasterBorder()
* @see rasterBorderColor()
* @see rasterBorderWidth()
* @note added in QGIS 2.12
*/
bool drawRasterBorder() const;

/** Sets whether a border will be drawn around raster symbol items.
* @param enabled set to true to draw borders
* @see drawRasterBorder()
* @see setRasterBorderColor()
* @see setRasterBorderWidth()
* @note added in QGIS 2.12
*/
void setDrawRasterBorder( bool enabled );

/** Returns the border color for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @see setRasterBorderColor()
* @see drawRasterBorder()
* @see rasterBorderWidth()
* @note added in QGIS 2.12
*/
QColor rasterBorderColor() const;

/** Sets the border color for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @param color border color
* @see rasterBorderColor()
* @see setDrawRasterBorder()
* @see setRasterBorderWidth()
* @note added in QGIS 2.12
*/
void setRasterBorderColor( const QColor& color );

/** Returns the border width (in millimeters) for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @see setRasterBorderWidth()
* @see drawRasterBorder()
* @see rasterBorderColor()
* @note added in QGIS 2.12
*/
double rasterBorderWidth() const;

/** Sets the border width for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @param width border width in millimeters
* @see rasterBorderWidth()
* @see setDrawRasterBorder()
* @see setRasterBorderColor()
* @note added in QGIS 2.12
*/
void setRasterBorderWidth( double width );

QSizeF wmsLegendSize() const;
void setWmsLegendSize( QSizeF s );

Expand Down
53 changes: 53 additions & 0 deletions src/app/composer/qgscomposerlegendwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ QgsComposerLegendWidget::QgsComposerLegendWidget( QgsComposerLegend* legend )
mFontColorButton->setColorDialogTitle( tr( "Select font color" ) );
mFontColorButton->setContext( "composer" );

mRasterBorderColorButton->setColorDialogTitle( tr( "Select border color" ) );
mRasterBorderColorButton->setAllowAlpha( true );
mRasterBorderColorButton->setContext( "composer " );

//add widget for item properties
QgsComposerItemWidget* itemPropertiesWidget = new QgsComposerItemWidget( this, legend );
mainLayout->addWidget( itemPropertiesWidget );
Expand Down Expand Up @@ -161,6 +165,11 @@ void QgsComposerLegendWidget::setGuiElements()
mIconLabelSpaceSpinBox->setValue( mLegend->style( QgsComposerLegendStyle::SymbolLabel ).margin( QgsComposerLegendStyle::Left ) );
mBoxSpaceSpinBox->setValue( mLegend->boxSpace() );
mColumnSpaceSpinBox->setValue( mLegend->columnSpace() );

mRasterBorderGroupBox->setChecked( mLegend->drawRasterBorder() );
mRasterBorderWidthSpinBox->setValue( mLegend->rasterBorderWidth() );
mRasterBorderColorButton->setColor( mLegend->rasterBorderColor() );

mCheckBoxAutoUpdate->setChecked( mLegend->autoUpdateModel() );
refreshMapComboBox();

Expand Down Expand Up @@ -613,6 +622,47 @@ void QgsComposerLegendWidget::on_mMapComboBox_currentIndexChanged( int index )
}
}

void QgsComposerLegendWidget::on_mRasterBorderGroupBox_toggled( bool state )
{
if ( !mLegend )
{
return;
}

mLegend->beginCommand( tr( "Legend raster borders" ) );
mLegend->setDrawRasterBorder( state );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
}

void QgsComposerLegendWidget::on_mRasterBorderWidthSpinBox_valueChanged( double d )
{
if ( !mLegend )
{
return;
}

mLegend->beginCommand( tr( "Legend raster border width" ), QgsComposerMergeCommand::LegendRasterBorderWidth );
mLegend->setRasterBorderWidth( d );
mLegend->adjustBoxSize();
mLegend->update();
mLegend->endCommand();
}

void QgsComposerLegendWidget::on_mRasterBorderColorButton_colorChanged( const QColor& newColor )
{
if ( !mLegend )
{
return;
}

mLegend->beginCommand( tr( "Legend raster border color" ) );
mLegend->setRasterBorderColor( newColor );
mLegend->update();
mLegend->endCommand();
}

void QgsComposerLegendWidget::on_mAddToolButton_clicked()
{
if ( !mLegend )
Expand Down Expand Up @@ -894,6 +944,9 @@ void QgsComposerLegendWidget::blockAllSignals( bool b )
mBoxSpaceSpinBox->blockSignals( b );
mColumnSpaceSpinBox->blockSignals( b );
mFontColorButton->blockSignals( b );
mRasterBorderGroupBox->blockSignals( b );
mRasterBorderColorButton->blockSignals( b );
mRasterBorderWidthSpinBox->blockSignals( b );
}

void QgsComposerLegendWidget::refreshMapComboBox()
Expand Down
4 changes: 4 additions & 0 deletions src/app/composer/qgscomposerlegendwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ class QgsComposerLegendWidget: public QgsComposerItemBaseWidget, private Ui::Qgs
void on_mCheckBoxAutoUpdate_stateChanged( int state );
void on_mMapComboBox_currentIndexChanged( int index );

void on_mRasterBorderGroupBox_toggled( bool state );
void on_mRasterBorderWidthSpinBox_valueChanged( double d );
void on_mRasterBorderColorButton_colorChanged( const QColor& newColor );

//item manipulation
void on_mMoveDownToolButton_clicked();
void on_mMoveUpToolButton_clicked();
Expand Down
1 change: 1 addition & 0 deletions src/core/composer/qgscomposeritemcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
LegendIconSymbolSpace,
LegendBoxSpace,
LegendColumnSpace,
LegendRasterBorderWidth,
//composer picture
ComposerPictureRotation,
// composer scalebar
Expand Down
18 changes: 18 additions & 0 deletions src/core/composer/qgscomposerlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "qgslegendrenderer.h"
#include "qgslogger.h"
#include "qgsproject.h"
#include "qgssymbollayerv2utils.h"
#include <QDomDocument>
#include <QDomElement>
#include <QPainter>
Expand Down Expand Up @@ -241,6 +242,14 @@ void QgsComposerLegend::setSplitLayer( bool s ) { mSettings.setSplitLayer( s );
bool QgsComposerLegend::equalColumnWidth() const { return mSettings.equalColumnWidth(); }
void QgsComposerLegend::setEqualColumnWidth( bool s ) { mSettings.setEqualColumnWidth( s ); }

bool QgsComposerLegend::drawRasterBorder() const { return mSettings.drawRasterBorder(); }
void QgsComposerLegend::setDrawRasterBorder( bool enabled ) { mSettings.setDrawRasterBorder( enabled ); }

QColor QgsComposerLegend::rasterBorderColor() const { return mSettings.rasterBorderColor(); }
void QgsComposerLegend::setRasterBorderColor( const QColor& color ) { mSettings.setRasterBorderColor( color ); }

double QgsComposerLegend::rasterBorderWidth() const { return mSettings.rasterBorderWidth(); }
void QgsComposerLegend::setRasterBorderWidth( double width ) { mSettings.setRasterBorderWidth( width ); }

void QgsComposerLegend::synchronizeWithModel()
{
Expand Down Expand Up @@ -279,6 +288,11 @@ bool QgsComposerLegend::writeXML( QDomElement& elem, QDomDocument & doc ) const

composerLegendElem.setAttribute( "symbolWidth", QString::number( mSettings.symbolSize().width() ) );
composerLegendElem.setAttribute( "symbolHeight", QString::number( mSettings.symbolSize().height() ) );

composerLegendElem.setAttribute( "rasterBorder", mSettings.drawRasterBorder() );
composerLegendElem.setAttribute( "rasterBorderColor", QgsSymbolLayerV2Utils::encodeColor( mSettings.rasterBorderColor() ) );
composerLegendElem.setAttribute( "rasterBorderWidth", QString::number( mSettings.rasterBorderWidth() ) );

composerLegendElem.setAttribute( "wmsLegendWidth", QString::number( mSettings.wmsLegendSize().width() ) );
composerLegendElem.setAttribute( "wmsLegendHeight", QString::number( mSettings.wmsLegendSize().height() ) );
composerLegendElem.setAttribute( "wrapChar", mSettings.wrapChar() );
Expand Down Expand Up @@ -402,6 +416,10 @@ bool QgsComposerLegend::readXML( const QDomElement& itemElem, const QDomDocument
mSettings.setSymbolSize( QSizeF( itemElem.attribute( "symbolWidth", "7.0" ).toDouble(), itemElem.attribute( "symbolHeight", "14.0" ).toDouble() ) );
mSettings.setWmsLegendSize( QSizeF( itemElem.attribute( "wmsLegendWidth", "50" ).toDouble(), itemElem.attribute( "wmsLegendHeight", "25" ).toDouble() ) );

mSettings.setDrawRasterBorder( itemElem.attribute( "rasterBorder", "1" ) != "0" );
mSettings.setRasterBorderColor( QgsSymbolLayerV2Utils::decodeColor( itemElem.attribute( "rasterBorderColor", "0,0,0" ) ) );
mSettings.setRasterBorderWidth( itemElem.attribute( "rasterBorderWidth", "0" ).toDouble() );

mSettings.setWrapChar( itemElem.attribute( "wrapChar" ) );

//composer map
Expand Down
55 changes: 55 additions & 0 deletions src/core/composer/qgscomposerlegend.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,61 @@ class CORE_EXPORT QgsComposerLegend : public QgsComposerItem
bool equalColumnWidth() const;
void setEqualColumnWidth( bool s );

/** Returns whether a border will be drawn around raster symbol items.
* @see setDrawRasterBorder()
* @see rasterBorderColor()
* @see rasterBorderWidth()
* @note added in QGIS 2.12
*/
bool drawRasterBorder() const;

/** Sets whether a border will be drawn around raster symbol items.
* @param enabled set to true to draw borders
* @see drawRasterBorder()
* @see setRasterBorderColor()
* @see setRasterBorderWidth()
* @note added in QGIS 2.12
*/
void setDrawRasterBorder( bool enabled );

/** Returns the border color for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @see setRasterBorderColor()
* @see drawRasterBorder()
* @see rasterBorderWidth()
* @note added in QGIS 2.12
*/
QColor rasterBorderColor() const;

/** Sets the border color for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @param color border color
* @see rasterBorderColor()
* @see setDrawRasterBorder()
* @see setRasterBorderWidth()
* @note added in QGIS 2.12
*/
void setRasterBorderColor( const QColor& color );

/** Returns the border width (in millimeters) for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @see setRasterBorderWidth()
* @see drawRasterBorder()
* @see rasterBorderColor()
* @note added in QGIS 2.12
*/
double rasterBorderWidth() const;

/** Sets the border width for the border drawn around raster symbol items. The border is
* only drawn if drawRasterBorder() is true.
* @param width border width in millimeters
* @see rasterBorderWidth()
* @see setDrawRasterBorder()
* @see setRasterBorderColor()
* @note added in QGIS 2.12
*/
void setRasterBorderWidth( double width );

void setComposerMap( const QgsComposerMap* map );
const QgsComposerMap* composerMap() const { return mComposerMap;}

Expand Down
15 changes: 14 additions & 1 deletion src/core/layertree/qgslayertreemodellegendnode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,21 @@ QSizeF QgsRasterSymbolLegendNode::drawSymbol( const QgsLegendSettings& settings,
if ( QgsRasterRenderer* rasterRenderer = rasterLayer->renderer() )
itemColor.setAlpha( rasterRenderer->opacity() * 255.0 );
}

ctx->painter->setBrush( itemColor );

if ( settings.drawRasterBorder() )
{
QPen pen;
pen.setColor( settings.rasterBorderColor() );
pen.setWidthF( settings.rasterBorderWidth() );
pen.setJoinStyle( Qt::MiterJoin );
ctx->painter->setPen( pen );
}
else
{
ctx->painter->setPen( Qt::NoPen );
}

ctx->painter->drawRect( QRectF( ctx->point.x(), ctx->point.y() + ( itemHeight - settings.symbolSize().height() ) / 2,
settings.symbolSize().width(), settings.symbolSize().height() ) );
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/qgslegendsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ QgsLegendSettings::QgsLegendSettings()
, mColumnCount( 1 )
, mSplitLayer( false )
, mEqualColumnWidth( false )
, mRasterSymbolBorder( true )
, mRasterBorderColor( Qt::black )
, mRasterBorderWidth( 0.0 )
, mMmPerMapUnit( 1 )
, mUseAdvancedEffects( true )
, mMapScale( 1 )
Expand Down
Loading

0 comments on commit d1be2ff

Please sign in to comment.