diff --git a/python/core/effects/qgsimageoperation.sip b/python/core/effects/qgsimageoperation.sip index 2ef76d36499..3164d2a24fe 100644 --- a/python/core/effects/qgsimageoperation.sip +++ b/python/core/effects/qgsimageoperation.sip @@ -131,12 +131,23 @@ class QgsImageOperation */ static void flipImage( QImage &image, FlipType type ); + /** Calculates the non-transparent region of an image. + * @param image source image + * @param minSize minimum size for returned region, if desired. If the + * non-transparent region of the image is smaller than this minimum size, + * it will be centered in the returned rectangle. + * @param center return rectangle will be centered on the center of the original image if set to true + * @note added in QGIS 2.9 + * @see cropTransparent + */ + static QRect nonTransparentImageRect( const QImage & image, const QSize& minSize = QSize(), bool center = false ); + /** Crop any transparent border from around an image. * @param image source image * @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 + * @param center cropped image will be centered on the center of the original image if set to true * @note added in QGIS 2.9 */ static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false ); diff --git a/src/core/effects/qgsimageoperation.cpp b/src/core/effects/qgsimageoperation.cpp index 85cae19b750..b4edc7e2f50 100644 --- a/src/core/effects/qgsimageoperation.cpp +++ b/src/core/effects/qgsimageoperation.cpp @@ -793,7 +793,7 @@ void QgsImageOperation::flipImage( QImage &image, QgsImageOperation::FlipType ty runLineOperation( image, flipOperation ); } -QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize, bool center ) +QRect QgsImageOperation::nonTransparentImageRect( const QImage &image, const QSize &minSize, bool center ) { int width = image.width(); int height = image.height(); @@ -838,7 +838,13 @@ QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &min ymin = qMax( 0, height / 2 - dy ); ymax = qMin( height, height / 2 + dy ); } - return image.copy( xmin, ymin, xmax - xmin, ymax - ymin ); + + return QRect( xmin, ymin, xmax - xmin, ymax - ymin ); +} + +QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize, bool center ) +{ + return image.copy( QgsImageOperation::nonTransparentImageRect( image, minSize, center ) ); } void QgsImageOperation::FlipLineOperation::operator()( QRgb *startRef, const int lineLength, const int bytesPerLine ) diff --git a/src/core/effects/qgsimageoperation.h b/src/core/effects/qgsimageoperation.h index 7532a178183..7b4ee210f19 100644 --- a/src/core/effects/qgsimageoperation.h +++ b/src/core/effects/qgsimageoperation.h @@ -162,12 +162,23 @@ class CORE_EXPORT QgsImageOperation */ static void flipImage( QImage &image, FlipType type ); + /** Calculates the non-transparent region of an image. + * @param image source image + * @param minSize minimum size for returned region, if desired. If the + * non-transparent region of the image is smaller than this minimum size, + * it will be centered in the returned rectangle. + * @param center return rectangle will be centered on the center of the original image if set to true + * @note added in QGIS 2.9 + * @see cropTransparent + */ + static QRect nonTransparentImageRect( const QImage & image, const QSize& minSize = QSize(), bool center = false ); + /** Crop any transparent border from around an image. * @param image source image * @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 + * @param center cropped image will be centered on the center of the original image if set to true * @note added in QGIS 2.9 */ static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false ); diff --git a/src/core/layertree/qgslayertreemodellegendnode.cpp b/src/core/layertree/qgslayertreemodellegendnode.cpp index 73fe19e0878..8cd440cf5b8 100644 --- a/src/core/layertree/qgslayertreemodellegendnode.cpp +++ b/src/core/layertree/qgslayertreemodellegendnode.cpp @@ -163,28 +163,19 @@ QSize QgsSymbolV2LegendNode::minimumIconSize() const if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Marker ) { QScopedPointer context( createTemporaryRenderContext() ); - QPixmap pix = QPixmap::fromImage( - QgsImageOperation::cropTransparent( - QgsSymbolLayerV2Utils::symbolPreviewPixmap( - mItem.symbol(), - QSize( 512, 512 ), - context.data() ).toImage(), - mIconSize, - true ) ); - minSz = pix.size(); + minSz = QgsImageOperation::nonTransparentImageRect( + QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), QSize( 512, 512 ), context.data() ).toImage(), + mIconSize, + true ).size(); } else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Line ) { QScopedPointer context( createTemporaryRenderContext() ); - QPixmap pix = QPixmap::fromImage( - QgsImageOperation::cropTransparent( - QgsSymbolLayerV2Utils::symbolPreviewPixmap( - mItem.symbol(), - QSize( mIconSize.width(), 512 ), - context.data() ).toImage(), - mIconSize, - true ) ); - minSz = pix.size(); + minSz = QgsImageOperation::nonTransparentImageRect( + QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(), QSize( mIconSize.width(), 512 ), + context.data() ).toImage(), + mIconSize, + true ).size(); } else {