Skip to content
Permalink
Browse files

add option to cropTransparent to allow centering

use that in qgslayertreemodellegendnode
  • Loading branch information
vmora committed May 12, 2015
1 parent d13ace5 commit b8bc1811d5431bc53d19090c0f4a22c52e03ef0f
@@ -136,8 +136,9 @@ class QgsImageOperation
* @param minSize minimum size for cropped image, if desired. If the
* cropped image is smaller than the minimum size, it will be centered
* in the returned image.
* @param center croped image will be centered on the center of the original image
* @note added in QGIS 2.9
*/
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize() );
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false );

};
@@ -793,7 +793,7 @@ void QgsImageOperation::flipImage( QImage &image, QgsImageOperation::FlipType ty
runLineOperation( image, flipOperation );
}

QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize )
QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize, bool center )
{
int width = image.width();
int height = image.height();
@@ -828,6 +828,16 @@ QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &min
ymax = ymin + minSize.height();
}
}
if ( center )
{
// recompute min and max to center image
const int dx = qMax( qAbs( xmax - width / 2 ), qAbs( xmin - width / 2 ) );
const int dy = qMax( qAbs( ymax - height / 2 ), qAbs( ymin - height / 2 ) );
xmin = qMax( 0, width / 2 - dx );
xmax = qMin( width, width / 2 + dx );
ymin = qMax( 0, height / 2 - dy );
ymax = qMin( height, height / 2 + dy );
}
return image.copy( xmin, ymin, xmax - xmin, ymax - ymin );
}

@@ -167,9 +167,10 @@ class CORE_EXPORT QgsImageOperation
* @param minSize minimum size for cropped image, if desired. If the
* cropped image is smaller than the minimum size, it will be centered
* in the returned image.
* @param center croped image will be centered on the center of the original image
* @note added in QGIS 2.9
*/
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize() );
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false );

private:

@@ -163,21 +163,27 @@ QSize QgsSymbolV2LegendNode::minimumIconSize() const
if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Marker )
{
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
QPixmap pix = QPixmap::fromImage( QgsImageOperation::cropTransparent(
QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(),
QSize( 512, 512 ),
context.data()
).toImage(), mIconSize ) );
QPixmap pix = QPixmap::fromImage(
QgsImageOperation::cropTransparent(
QgsSymbolLayerV2Utils::symbolPreviewPixmap(
mItem.symbol(),
QSize( 512, 512 ),
context.data() ).toImage(),
mIconSize,
true ) );
minSz = pix.size();
}
else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Line )
{
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
QPixmap pix = QPixmap::fromImage( QgsImageOperation::cropTransparent(
QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(),
QSize( mIconSize.width(), 512 ),
context.data()
).toImage(), mIconSize ) );
QPixmap pix = QPixmap::fromImage(
QgsImageOperation::cropTransparent(
QgsSymbolLayerV2Utils::symbolPreviewPixmap(
mItem.symbol(),
QSize( mIconSize.width(), 512 ),
context.data() ).toImage(),
mIconSize,
true ) );
minSz = pix.size();
}
else

0 comments on commit b8bc181

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