Skip to content

Commit b8bc181

Browse files
committedMay 12, 2015
add option to cropTransparent to allow centering
use that in qgslayertreemodellegendnode
1 parent d13ace5 commit b8bc181

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed
 

‎python/core/effects/qgsimageoperation.sip

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,9 @@ class QgsImageOperation
136136
* @param minSize minimum size for cropped image, if desired. If the
137137
* cropped image is smaller than the minimum size, it will be centered
138138
* in the returned image.
139+
* @param center croped image will be centered on the center of the original image
139140
* @note added in QGIS 2.9
140141
*/
141-
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize() );
142+
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false );
142143

143144
};

‎src/core/effects/qgsimageoperation.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ void QgsImageOperation::flipImage( QImage &image, QgsImageOperation::FlipType ty
793793
runLineOperation( image, flipOperation );
794794
}
795795

796-
QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize )
796+
QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &minSize, bool center )
797797
{
798798
int width = image.width();
799799
int height = image.height();
@@ -828,6 +828,16 @@ QImage QgsImageOperation::cropTransparent( const QImage &image, const QSize &min
828828
ymax = ymin + minSize.height();
829829
}
830830
}
831+
if ( center )
832+
{
833+
// recompute min and max to center image
834+
const int dx = qMax( qAbs( xmax - width / 2 ), qAbs( xmin - width / 2 ) );
835+
const int dy = qMax( qAbs( ymax - height / 2 ), qAbs( ymin - height / 2 ) );
836+
xmin = qMax( 0, width / 2 - dx );
837+
xmax = qMin( width, width / 2 + dx );
838+
ymin = qMax( 0, height / 2 - dy );
839+
ymax = qMin( height, height / 2 + dy );
840+
}
831841
return image.copy( xmin, ymin, xmax - xmin, ymax - ymin );
832842
}
833843

‎src/core/effects/qgsimageoperation.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,10 @@ class CORE_EXPORT QgsImageOperation
167167
* @param minSize minimum size for cropped image, if desired. If the
168168
* cropped image is smaller than the minimum size, it will be centered
169169
* in the returned image.
170+
* @param center croped image will be centered on the center of the original image
170171
* @note added in QGIS 2.9
171172
*/
172-
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize() );
173+
static QImage cropTransparent( const QImage & image, const QSize& minSize = QSize(), bool center = false );
173174

174175
private:
175176

‎src/core/layertree/qgslayertreemodellegendnode.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -163,21 +163,27 @@ QSize QgsSymbolV2LegendNode::minimumIconSize() const
163163
if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Marker )
164164
{
165165
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
166-
QPixmap pix = QPixmap::fromImage( QgsImageOperation::cropTransparent(
167-
QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(),
168-
QSize( 512, 512 ),
169-
context.data()
170-
).toImage(), mIconSize ) );
166+
QPixmap pix = QPixmap::fromImage(
167+
QgsImageOperation::cropTransparent(
168+
QgsSymbolLayerV2Utils::symbolPreviewPixmap(
169+
mItem.symbol(),
170+
QSize( 512, 512 ),
171+
context.data() ).toImage(),
172+
mIconSize,
173+
true ) );
171174
minSz = pix.size();
172175
}
173176
else if ( mItem.symbol() && mItem.symbol()->type() == QgsSymbolV2::Line )
174177
{
175178
QScopedPointer<QgsRenderContext> context( createTemporaryRenderContext() );
176-
QPixmap pix = QPixmap::fromImage( QgsImageOperation::cropTransparent(
177-
QgsSymbolLayerV2Utils::symbolPreviewPixmap( mItem.symbol(),
178-
QSize( mIconSize.width(), 512 ),
179-
context.data()
180-
).toImage(), mIconSize ) );
179+
QPixmap pix = QPixmap::fromImage(
180+
QgsImageOperation::cropTransparent(
181+
QgsSymbolLayerV2Utils::symbolPreviewPixmap(
182+
mItem.symbol(),
183+
QSize( mIconSize.width(), 512 ),
184+
context.data() ).toImage(),
185+
mIconSize,
186+
true ) );
181187
minSz = pix.size();
182188
}
183189
else

0 commit comments

Comments
 (0)
Please sign in to comment.