Navigation Menu

Skip to content

Commit

Permalink
add option to cropTransparent to allow centering
Browse files Browse the repository at this point in the history
use that in qgslayertreemodellegendnode
  • Loading branch information
vmora committed May 12, 2015
1 parent d13ace5 commit b8bc181
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
3 changes: 2 additions & 1 deletion python/core/effects/qgsimageoperation.sip
Expand Up @@ -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 );

};
12 changes: 11 additions & 1 deletion src/core/effects/qgsimageoperation.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -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 );
}

Expand Down
3 changes: 2 additions & 1 deletion src/core/effects/qgsimageoperation.h
Expand Up @@ -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:

Expand Down
26 changes: 16 additions & 10 deletions src/core/layertree/qgslayertreemodellegendnode.cpp
Expand Up @@ -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
Expand Down

0 comments on commit b8bc181

Please sign in to comment.