Merge pull request #3777 from nirvn/symbols_preview_padding

[symbology] add padding value for symbol/coloramp preview
This commit is contained in:
Nyall Dawson 2016-11-17 15:28:45 +10:00 committed by GitHub
commit 6b6896bf5b
8 changed files with 84 additions and 40 deletions

View File

@ -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}

View File

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

View File

@ -168,7 +168,7 @@ 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();
@ -176,7 +176,7 @@ QSize QgsSymbolLegendNode::minimumIconSize( QgsRenderContext* context ) const
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();
@ -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
{

View File

@ -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 );
@ -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;
}
@ -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 );
@ -650,7 +662,7 @@ 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 );
@ -658,7 +670,7 @@ QPixmap QgsSymbolLayerUtils::colorRampPreviewPixmap( QgsColorRamp* ramp, QSize s
{
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;

View File

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

View File

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

View File

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

View File

@ -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">