@@ -947,10 +947,7 @@ QgsShapeburstFillSymbolLayer::QgsShapeburstFillSymbolLayer( const QColor &color,
947
947
mColor = color;
948
948
}
949
949
950
- QgsShapeburstFillSymbolLayer::~QgsShapeburstFillSymbolLayer ()
951
- {
952
- delete mGradientRamp ;
953
- }
950
+ QgsShapeburstFillSymbolLayer::~QgsShapeburstFillSymbolLayer () = default ;
954
951
955
952
QgsSymbolLayer *QgsShapeburstFillSymbolLayer::create ( const QgsStringMap &props )
956
953
{
@@ -1054,8 +1051,10 @@ QString QgsShapeburstFillSymbolLayer::layerType() const
1054
1051
1055
1052
void QgsShapeburstFillSymbolLayer::setColorRamp ( QgsColorRamp *ramp )
1056
1053
{
1057
- delete mGradientRamp ;
1058
- mGradientRamp = ramp;
1054
+ if ( mGradientRamp .get () == ramp )
1055
+ return ;
1056
+
1057
+ mGradientRamp .reset ( ramp );
1059
1058
}
1060
1059
1061
1060
void QgsShapeburstFillSymbolLayer::applyDataDefinedSymbology ( QgsSymbolRenderContext &context, QColor &color, QColor &color2, int &blurRadius, bool &useWholeShape,
@@ -1169,9 +1168,10 @@ void QgsShapeburstFillSymbolLayer::renderPolygon( const QPolygonF &points, QList
1169
1168
}
1170
1169
1171
1170
// if we are using the two color mode, create a gradient ramp
1171
+ std::unique_ptr< QgsGradientColorRamp > twoColorGradientRamp;
1172
1172
if ( mColorType == QgsShapeburstFillSymbolLayer::SimpleTwoColor )
1173
1173
{
1174
- mTwoColorGradientRamp = new QgsGradientColorRamp ( color1, color2 );
1174
+ twoColorGradientRamp = qgis::make_unique< QgsGradientColorRamp > ( color1, color2 );
1175
1175
}
1176
1176
1177
1177
// no stroke for shapeburst fills
@@ -1180,23 +1180,37 @@ void QgsShapeburstFillSymbolLayer::renderPolygon( const QPolygonF &points, QList
1180
1180
// calculate margin size in pixels so that QImage of polygon has sufficient space to draw the full blur effect
1181
1181
int sideBuffer = 4 + ( blurRadius + 2 ) * 4 ;
1182
1182
// create a QImage to draw shapeburst in
1183
- double imWidth = points.boundingRect ().width () + ( sideBuffer * 2 );
1184
- double imHeight = points.boundingRect ().height () + ( sideBuffer * 2 );
1185
- QImage *fillImage = new QImage ( imWidth,
1186
- imHeight, QImage::Format_ARGB32_Premultiplied );
1183
+ int pointsWidth = static_cast < int >( std::round ( points.boundingRect ().width () ) );
1184
+ int pointsHeight = static_cast < int >( std::round ( points.boundingRect ().height () ) );
1185
+ int imWidth = pointsWidth + ( sideBuffer * 2 );
1186
+ int imHeight = pointsHeight + ( sideBuffer * 2 );
1187
+ std::unique_ptr< QImage > fillImage = qgis::make_unique< QImage >( imWidth,
1188
+ imHeight, QImage::Format_ARGB32_Premultiplied );
1189
+ if ( fillImage->isNull () )
1190
+ {
1191
+ QgsMessageLog::logMessage ( QObject::tr ( " Could not allocate sufficient memory for shapeburst fill" ) );
1192
+ return ;
1193
+ }
1194
+
1195
+ // also create an image to store the alpha channel
1196
+ std::unique_ptr< QImage > alphaImage = qgis::make_unique< QImage >( fillImage->width (), fillImage->height (), QImage::Format_ARGB32_Premultiplied );
1197
+ if ( alphaImage->isNull () )
1198
+ {
1199
+ QgsMessageLog::logMessage ( QObject::tr ( " Could not allocate sufficient memory for shapeburst fill" ) );
1200
+ return ;
1201
+ }
1202
+
1187
1203
// Fill this image with black. Initially the distance transform is drawn in greyscale, where black pixels have zero distance from the
1188
1204
// polygon boundary. Since we don't care about pixels which fall outside the polygon, we start with a black image and then draw over it the
1189
1205
// polygon in white. The distance transform function then fills in the correct distance values for the white pixels.
1190
1206
fillImage->fill ( Qt::black );
1191
1207
1192
- // also create an image to store the alpha channel
1193
- QImage *alphaImage = new QImage ( fillImage->width (), fillImage->height (), QImage::Format_ARGB32_Premultiplied );
1194
1208
// initially fill the alpha channel image with a transparent color
1195
1209
alphaImage->fill ( Qt::transparent );
1196
1210
1197
1211
// now, draw the polygon in the alpha channel image
1198
1212
QPainter imgPainter;
1199
- imgPainter.begin ( alphaImage );
1213
+ imgPainter.begin ( alphaImage. get () );
1200
1214
imgPainter.setRenderHint ( QPainter::Antialiasing, true );
1201
1215
imgPainter.setBrush ( QBrush ( Qt::white ) );
1202
1216
imgPainter.setPen ( QPen ( Qt::black ) );
@@ -1206,7 +1220,7 @@ void QgsShapeburstFillSymbolLayer::renderPolygon( const QPolygonF &points, QList
1206
1220
1207
1221
// now that we have a render of the polygon in white, draw this onto the shapeburst fill image too
1208
1222
// (this avoids calling _renderPolygon twice, since that can be slow)
1209
- imgPainter.begin ( fillImage );
1223
+ imgPainter.begin ( fillImage. get () );
1210
1224
if ( !ignoreRings )
1211
1225
{
1212
1226
imgPainter.drawImage ( 0 , 0 , *alphaImage );
@@ -1224,18 +1238,14 @@ void QgsShapeburstFillSymbolLayer::renderPolygon( const QPolygonF &points, QList
1224
1238
imgPainter.end ();
1225
1239
1226
1240
// apply distance transform to image, uses the current color ramp to calculate final pixel colors
1227
- double *dtArray = distanceTransform ( fillImage );
1241
+ double *dtArray = distanceTransform ( fillImage. get () );
1228
1242
1229
1243
// copy distance transform values back to QImage, shading by appropriate color ramp
1230
- dtArrayToQImage ( dtArray, fillImage, mColorType == QgsShapeburstFillSymbolLayer::SimpleTwoColor ? mTwoColorGradientRamp : mGradientRamp ,
1244
+ dtArrayToQImage ( dtArray, fillImage. get () , mColorType == QgsShapeburstFillSymbolLayer::SimpleTwoColor ? twoColorGradientRamp. get () : mGradientRamp . get () ,
1231
1245
context.opacity (), useWholeShape, outputPixelMaxDist );
1232
1246
1233
1247
// clean up some variables
1234
1248
delete [] dtArray;
1235
- if ( mColorType == QgsShapeburstFillSymbolLayer::SimpleTwoColor )
1236
- {
1237
- delete mTwoColorGradientRamp ;
1238
- }
1239
1249
1240
1250
// apply blur if desired
1241
1251
if ( blurRadius > 0 )
@@ -1244,12 +1254,12 @@ void QgsShapeburstFillSymbolLayer::renderPolygon( const QPolygonF &points, QList
1244
1254
}
1245
1255
1246
1256
// apply alpha channel to distance transform image, so that areas outside the polygon are transparent
1247
- imgPainter.begin ( fillImage );
1257
+ imgPainter.begin ( fillImage. get () );
1248
1258
imgPainter.setCompositionMode ( QPainter::CompositionMode_DestinationIn );
1249
1259
imgPainter.drawImage ( 0 , 0 , *alphaImage );
1250
1260
imgPainter.end ();
1251
1261
// we're finished with the alpha channel image now
1252
- delete alphaImage;
1262
+ alphaImage. reset () ;
1253
1263
1254
1264
// draw shapeburst image in correct place in the destination painter
1255
1265
@@ -1264,8 +1274,6 @@ void QgsShapeburstFillSymbolLayer::renderPolygon( const QPolygonF &points, QList
1264
1274
1265
1275
p->drawImage ( points.boundingRect ().left () - sideBuffer, points.boundingRect ().top () - sideBuffer, *fillImage );
1266
1276
1267
- delete fillImage;
1268
-
1269
1277
if ( !mOffset .isNull () )
1270
1278
{
1271
1279
p->translate ( -offset );
0 commit comments