Skip to content

Commit

Permalink
[symbology] add padding value for symbol/coloramp preview
Browse files Browse the repository at this point in the history
  • Loading branch information
nirvn committed Nov 17, 2016
1 parent e624518 commit a8a05ba
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 40 deletions.
1 change: 1 addition & 0 deletions doc/api_break.dox
Expand Up @@ -1423,6 +1423,7 @@ QgsSymbolLayerUtils (renamed from QgsSymbolLayerUtilsV2) {#qgis_api_break

- encodeOutputUnit() and decodeOutputUnit() were removed. QgsUnitTypes::encodeUnit() and QgsUnitTypes::decodeRenderUnit() should be used instead.
- The signatures for wellKnownMarkerToSld() and wellKnownMarkerFromSld() were changed.
- The symbolPreviewPixmap() customContext is now the fourth parameter


QgsSymbolSelectorWidget {#qgis_api_break_3_0_QgsSymbolSelectorWidget}
Expand Down
37 changes: 31 additions & 6 deletions python/core/symbology-ng/qgssymbollayerutils.sip
Expand Up @@ -95,7 +95,23 @@ class QgsSymbolLayerUtils

static QPainter::CompositionMode decodeBlendMode( const QString& s );

static QIcon symbolPreviewIcon( QgsSymbol* symbol, QSize size );
/** Returns an icon preview for a color ramp.
* @param symbol symbol
* @param size target pixmap size
* @param padding space between icon edge and symbol
* @see symbolPreviewPixmap()
*/
static QIcon symbolPreviewIcon( QgsSymbol* symbol, QSize size, int padding = 0 );

/** Returns a pixmap preview for a color ramp.
* @param symbol symbol
* @param size target pixmap size
* @param padding space between icon edge and symbol
* @param customContext render context to use when rendering symbol
* @note customContext parameter added in 2.6
* @see symbolPreviewIcon()
*/
static QPixmap symbolPreviewPixmap( QgsSymbol* symbol, QSize size, int padding = 0, QgsRenderContext* customContext = 0 );

/** Draws a symbol layer preview to a QPicture
* @param layer symbol layer to draw
Expand All @@ -118,14 +134,23 @@ class QgsSymbolLayerUtils
*/
static QIcon symbolLayerPreviewIcon( QgsSymbolLayer* layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );

static QIcon colorRampPreviewIcon( QgsColorRamp* ramp, QSize size );
/** Returns a icon preview for a color ramp.
* @param ramp color ramp
* @param size target icon size
* @param padding space between icon edge and symbol
* @see colorRampPreviewPixmap()
*/
static QIcon colorRampPreviewIcon( QgsColorRamp* ramp, QSize size, int padding = 0 );
/** Returns a pixmap preview for a color ramp.
* @param ramp color ramp
* @param size target pixmap size
* @param padding space between icon edge and symbol
* @see colorRampPreviewIcon()
*/
static QPixmap colorRampPreviewPixmap( QgsColorRamp* ramp, QSize size, int padding = 0 );

static void drawStippledBackground( QPainter* painter, QRect rect );

//! @note customContext parameter added in 2.6
static QPixmap symbolPreviewPixmap( QgsSymbol* symbol, QSize size, QgsRenderContext* customContext = 0 );
static QPixmap colorRampPreviewPixmap( QgsColorRamp* ramp, QSize size );

/** Returns the maximum estimated bleed for the symbol */
static double estimateMaxSymbolBleed( QgsSymbol* symbol );

Expand Down
6 changes: 3 additions & 3 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -168,15 +168,15 @@ QSize QgsSymbolLegendNode::minimumIconSize( QgsRenderContext* context ) const
if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbol::Marker )
{
minSz = QgsImageOperation::nonTransparentImageRect(
QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), QSize( 512, 512 ),
QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), QSize( 512, 512 ), 0,
context ).toImage(),
minSz,
true ).size();
}
else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbol::Line )
{
minSz = QgsImageOperation::nonTransparentImageRect(
QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), QSize( minSz.width(), 512 ),
QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), QSize( minSz.width(), 512 ), 0,
context ).toImage(),
minSz,
true ).size();
Expand Down Expand Up @@ -273,7 +273,7 @@ QVariant QgsSymbolLegendNode::data( int role ) const
if ( mItem.symbol() )
{
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), mIconSize, context.data() );
pix = QgsSymbolLayerUtils::symbolPreviewPixmap( mItem.symbol(), mIconSize, 0, context.data() );
}
else
{
Expand Down
28 changes: 20 additions & 8 deletions src/core/symbology-ng/qgssymbollayerutils.cpp
Expand Up @@ -574,12 +574,12 @@ QPainter::CompositionMode QgsSymbolLayerUtils::decodeBlendMode( const QString &s
return QPainter::CompositionMode_SourceOver; // "Normal"
}

QIcon QgsSymbolLayerUtils::symbolPreviewIcon( QgsSymbol* symbol, QSize size )
QIcon QgsSymbolLayerUtils::symbolPreviewIcon( QgsSymbol* symbol, QSize size, int padding )
{
return QIcon( symbolPreviewPixmap( symbol, size ) );
return QIcon( symbolPreviewPixmap( symbol, size, padding ) );
}

QPixmap QgsSymbolLayerUtils::symbolPreviewPixmap( QgsSymbol* symbol, QSize size, QgsRenderContext* customContext )
QPixmap QgsSymbolLayerUtils::symbolPreviewPixmap( QgsSymbol* symbol, QSize size, int padding, QgsRenderContext* customContext )
{
Q_ASSERT( symbol );

Expand All @@ -588,9 +588,21 @@ QPixmap QgsSymbolLayerUtils::symbolPreviewPixmap( QgsSymbol* symbol, QSize size,
QPainter painter;
painter.begin( &pixmap );
painter.setRenderHint( QPainter::Antialiasing );

if ( customContext )
{
customContext->setPainter( &painter );
}

if ( padding > 0 )
{
size.setWidth( size.rwidth() - ( padding * 2 ) );
size.setHeight( size.rheight() - ( padding * 2 ) );
painter.translate( padding, padding );
}

symbol->drawPreviewIcon( &painter, size, customContext );

painter.end();
return pixmap;
}
Expand Down Expand Up @@ -636,12 +648,12 @@ QIcon QgsSymbolLayerUtils::symbolLayerPreviewIcon( QgsSymbolLayer* layer, QgsUni
return QIcon( pixmap );
}

QIcon QgsSymbolLayerUtils::colorRampPreviewIcon( QgsColorRamp* ramp, QSize size )
QIcon QgsSymbolLayerUtils::colorRampPreviewIcon( QgsColorRamp* ramp, QSize size, int padding )
{
return QIcon( colorRampPreviewPixmap( ramp, size ) );
return QIcon( colorRampPreviewPixmap( ramp, size, padding ) );
}

QPixmap QgsSymbolLayerUtils::colorRampPreviewPixmap( QgsColorRamp* ramp, QSize size )
QPixmap QgsSymbolLayerUtils::colorRampPreviewPixmap( QgsColorRamp* ramp, QSize size, int padding )
{
QPixmap pixmap( size );
pixmap.fill( Qt::transparent );
Expand All @@ -650,15 +662,15 @@ QPixmap QgsSymbolLayerUtils::colorRampPreviewPixmap( QgsColorRamp* ramp, QSize s
painter.begin( &pixmap );

//draw stippled background, for transparent images
drawStippledBackground( &painter, QRect( 0, 0, size.width(), size.height() ) );
drawStippledBackground( &painter, QRect( padding, padding, size.width() - padding * 2, size.height() - padding * 2 ) );

// antialising makes the colors duller, and no point in antialiasing a color ramp
// painter.setRenderHint( QPainter::Antialiasing );
for ( int i = 0; i < size.width(); i++ )
{
QPen pen( ramp->color( static_cast< double >( i ) / size.width() ) );
painter.setPen( pen );
painter.drawLine( i, 0, i, size.height() - 1 );
painter.drawLine( i, 0 + padding, i, size.height() - 1 - padding );
}
painter.end();
return pixmap;
Expand Down
29 changes: 22 additions & 7 deletions src/core/symbology-ng/qgssymbollayerutils.h
Expand Up @@ -138,7 +138,23 @@ class CORE_EXPORT QgsSymbolLayerUtils

static QPainter::CompositionMode decodeBlendMode( const QString& s );

static QIcon symbolPreviewIcon( QgsSymbol* symbol, QSize size );
/** Returns an icon preview for a color ramp.
* @param symbol symbol
* @param size target pixmap size
* @param padding space between icon edge and symbol
* @see symbolPreviewPixmap()
*/
static QIcon symbolPreviewIcon( QgsSymbol* symbol, QSize size, int padding = 0 );

/** Returns a pixmap preview for a color ramp.
* @param symbol symbol
* @param size target pixmap size
* @param padding space between icon edge and symbol
* @param customContext render context to use when rendering symbol
* @note customContext parameter added in 2.6
* @see symbolPreviewIcon()
*/
static QPixmap symbolPreviewPixmap( QgsSymbol* symbol, QSize size, int padding = 0, QgsRenderContext* customContext = nullptr );

/** Draws a symbol layer preview to a QPicture
* @param layer symbol layer to draw
Expand All @@ -161,25 +177,24 @@ class CORE_EXPORT QgsSymbolLayerUtils
*/
static QIcon symbolLayerPreviewIcon( QgsSymbolLayer* layer, QgsUnitTypes::RenderUnit u, QSize size, const QgsMapUnitScale& scale = QgsMapUnitScale() );

/** Returns a icon preview for a color ramp.
/** Returns an icon preview for a color ramp.
* @param ramp color ramp
* @param size target icon size
* @param padding space between icon edge and color ramp
* @see colorRampPreviewPixmap()
*/
static QIcon colorRampPreviewIcon( QgsColorRamp* ramp, QSize size );
static QIcon colorRampPreviewIcon( QgsColorRamp* ramp, QSize size, int padding = 0 );

/** Returns a pixmap preview for a color ramp.
* @param ramp color ramp
* @param size target pixmap size
* @param padding space between icon edge and color ramp
* @see colorRampPreviewIcon()
*/
static QPixmap colorRampPreviewPixmap( QgsColorRamp* ramp, QSize size );
static QPixmap colorRampPreviewPixmap( QgsColorRamp* ramp, QSize size, int padding = 0 );

static void drawStippledBackground( QPainter* painter, QRect rect );

//! @note customContext parameter added in 2.6
static QPixmap symbolPreviewPixmap( QgsSymbol* symbol, QSize size, QgsRenderContext* customContext = nullptr );

//! Returns the maximum estimated bleed for the symbol
static double estimateMaxSymbolBleed( QgsSymbol* symbol );

Expand Down
17 changes: 4 additions & 13 deletions src/gui/symbology-ng/qgsstylemanagerdialog.cpp
Expand Up @@ -236,17 +236,8 @@ void QgsStyleManagerDialog::on_tabItemType_currentChanged( int )
actnExportAsPNG->setVisible( flag );
actnExportAsSVG->setVisible( flag );

// set icon and grid size, depending on type
if ( currentItemType() == 1 || currentItemType() == 3 )
{
listItems->setIconSize( QSize( 75, 50 ) );
listItems->setGridSize( QSize( 100, 80 ) );
}
else
{
listItems->setIconSize( QSize( 50, 50 ) );
listItems->setGridSize( QSize( 75, 80 ) );
}
listItems->setIconSize( QSize( 100, 90 ) );
listItems->setGridSize( QSize( 120, 110 ) );

populateList();
}
Expand Down Expand Up @@ -275,7 +266,7 @@ void QgsStyleManagerDialog::populateSymbols( const QStringList& symbolNames, boo
if ( symbol && symbol->type() == type )
{
QStandardItem* item = new QStandardItem( name );
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( symbol, listItems->iconSize() );
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( symbol, listItems->iconSize(), 18 );
item->setIcon( icon );
item->setData( name ); // used to find out original name when user edited the name
item->setCheckable( check );
Expand All @@ -301,7 +292,7 @@ void QgsStyleManagerDialog::populateColorRamps( const QStringList& colorRamps, b
QScopedPointer< QgsColorRamp > ramp( mStyle->colorRamp( name ) );

QStandardItem* item = new QStandardItem( name );
QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( ramp.data(), listItems->iconSize() );
QIcon icon = QgsSymbolLayerUtils::colorRampPreviewIcon( ramp.data(), listItems->iconSize(), 18 );
item->setIcon( icon );
item->setData( name ); // used to find out original name when user edited the name
item->setCheckable( check );
Expand Down
2 changes: 1 addition & 1 deletion src/gui/symbology-ng/qgssymbolslistwidget.cpp
Expand Up @@ -243,7 +243,7 @@ void QgsSymbolsListWidget::populateSymbols( const QStringList& names )
itemFont.setPointSize( 10 );
item->setFont( itemFont );
// create preview icon
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( s, previewSize );
QIcon icon = QgsSymbolLayerUtils::symbolPreviewIcon( s, previewSize, 15 );
item->setIcon( icon );
// add to model
model->appendRow( item );
Expand Down
4 changes: 2 additions & 2 deletions src/ui/symbollayer/widget_symbolslist.ui
Expand Up @@ -37,8 +37,8 @@
</property>
<property name="iconSize">
<size>
<width>48</width>
<height>48</height>
<width>77</width>
<height>70</height>
</size>
</property>
<property name="textElideMode">
Expand Down

0 comments on commit a8a05ba

Please sign in to comment.