Skip to content
Permalink
Browse files

Fix hardcoded border for raster legend items (fix #13540)

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 d1be2ff2145d6a725d5eb577d186e86bb20e63e3
@@ -68,6 +68,7 @@ class QgsComposerMergeCommand : QgsComposerItemCommand
LegendIconSymbolSpace,
LegendBoxSpace,
LegendColumnSpace,
LegendRasterBorderWidth,
//composer picture
ComposerPictureRotation,
// composer scalebar
@@ -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;

@@ -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 );

@@ -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 );
@@ -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();

@@ -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 )
@@ -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()
@@ -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();
@@ -102,6 +102,7 @@ class CORE_EXPORT QgsComposerMergeCommand: public QgsComposerItemCommand
LegendIconSymbolSpace,
LegendBoxSpace,
LegendColumnSpace,
LegendRasterBorderWidth,
//composer picture
ComposerPictureRotation,
// composer scalebar
@@ -28,6 +28,7 @@
#include "qgslegendrenderer.h"
#include "qgslogger.h"
#include "qgsproject.h"
#include "qgssymbollayerv2utils.h"
#include <QDomDocument>
#include <QDomElement>
#include <QPainter>
@@ -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()
{
@@ -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() );
@@ -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
@@ -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;}

@@ -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() ) );
}
@@ -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 )

0 comments on commit d1be2ff

Please sign in to comment.
You can’t perform that action at this time.