Skip to content
Permalink
Browse files
Use unique_ptr instead of bare pointers in grid decorator code
  • Loading branch information
joto authored and nyalldawson committed Oct 23, 2021
1 parent 6a37ee7 commit 00b45af5449b192d832c191be30fe428c50575bb
Showing with 13 additions and 23 deletions.
  1. +9 −19 src/app/decorations/qgsdecorationgrid.cpp
  2. +4 −4 src/app/decorations/qgsdecorationgrid.h
@@ -61,31 +61,22 @@ QgsDecorationGrid::QgsDecorationGrid( QObject *parent )
setDisplayName( tr( "Grid" ) );
mConfigurationName = QStringLiteral( "Grid" );


mLineSymbol = nullptr;
mMarkerSymbol = nullptr;
projectRead();

connect( QgisApp::instance()->mapCanvas(), &QgsMapCanvas::destinationCrsChanged,
this, &QgsDecorationGrid::checkMapUnitsChanged );
}

QgsDecorationGrid::~QgsDecorationGrid()
{
delete mLineSymbol;
delete mMarkerSymbol;
}
QgsDecorationGrid::~QgsDecorationGrid() = default;

void QgsDecorationGrid::setLineSymbol( QgsLineSymbol *symbol )
{
delete mLineSymbol;
mLineSymbol = symbol;
mLineSymbol.reset( symbol );
}

void QgsDecorationGrid::setMarkerSymbol( QgsMarkerSymbol *symbol )
{
delete mMarkerSymbol;
mMarkerSymbol = symbol;
mMarkerSymbol.reset( symbol );
}

void QgsDecorationGrid::projectRead()
@@ -132,10 +123,10 @@ void QgsDecorationGrid::projectRead()
{
doc.setContent( xml );
elem = doc.documentElement();
mLineSymbol = QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( elem, rwContext );
mLineSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsLineSymbol>( elem, rwContext ) );
}
if ( ! mLineSymbol )
mLineSymbol = new QgsLineSymbol();
mLineSymbol = std::make_unique< QgsLineSymbol >();

if ( mMarkerSymbol )
setMarkerSymbol( nullptr );
@@ -144,15 +135,14 @@ void QgsDecorationGrid::projectRead()
{
doc.setContent( xml );
elem = doc.documentElement();
mMarkerSymbol = QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( elem, rwContext );
mMarkerSymbol.reset( QgsSymbolLayerUtils::loadSymbol<QgsMarkerSymbol>( elem, rwContext ) );
}
if ( ! mMarkerSymbol )
{
// set default symbol : cross with width=3
QgsSymbolLayerList symbolList;
symbolList << new QgsSimpleMarkerSymbolLayer( Qgis::MarkerShape::Cross, 3, 0 );
mMarkerSymbol = new QgsMarkerSymbol( symbolList );
// mMarkerSymbol = new QgsMarkerSymbol();
mMarkerSymbol = std::make_unique< QgsMarkerSymbol >( symbolList );
}
}

@@ -185,15 +175,15 @@ void QgsDecorationGrid::saveToProject()
rwContext.setPathResolver( QgsProject::instance()->pathResolver() );
if ( mLineSymbol )
{
elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "line symbol" ), mLineSymbol, doc, rwContext );
elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "line symbol" ), mLineSymbol.get(), doc, rwContext );
doc.appendChild( elem );
// FIXME this works, but XML will not be valid as < is replaced by &lt;
QgsProject::instance()->writeEntry( mConfigurationName, QStringLiteral( "/LineSymbol" ), doc.toString() );
}
if ( mMarkerSymbol )
{
doc.setContent( QString() );
elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "marker symbol" ), mMarkerSymbol, doc, rwContext );
elem = QgsSymbolLayerUtils::saveSymbol( QStringLiteral( "marker symbol" ), mMarkerSymbol.get(), doc, rwContext );
doc.appendChild( elem );
QgsProject::instance()->writeEntry( mConfigurationName, QStringLiteral( "/MarkerSymbol" ), doc.toString() );
}
@@ -118,11 +118,11 @@ class APP_EXPORT QgsDecorationGrid: public QgsDecorationItem

//! Sets symbol that is used to draw grid lines. Takes ownership
void setLineSymbol( QgsLineSymbol *symbol );
const QgsLineSymbol *lineSymbol() const { return mLineSymbol; }
const QgsLineSymbol *lineSymbol() const { return mLineSymbol.get(); }

//! Sets symbol that is used to draw markers. Takes ownership
void setMarkerSymbol( QgsMarkerSymbol *symbol );
const QgsMarkerSymbol *markerSymbol() const { return mMarkerSymbol; }
const QgsMarkerSymbol *markerSymbol() const { return mMarkerSymbol.get(); }

//! Sets map unit type
void setMapUnits( QgsUnitTypes::DistanceUnit t ) { mMapUnits = t; }
@@ -187,8 +187,8 @@ class APP_EXPORT QgsDecorationGrid: public QgsDecorationItem
//! Annotation can be horizontal / vertical or different for axes
GridAnnotationDirection mGridAnnotationDirection;

QgsLineSymbol *mLineSymbol = nullptr;
QgsMarkerSymbol *mMarkerSymbol = nullptr;
std::unique_ptr< QgsLineSymbol > mLineSymbol;
std::unique_ptr< QgsMarkerSymbol > mMarkerSymbol;

QgsUnitTypes::DistanceUnit mMapUnits;

0 comments on commit 00b45af

Please sign in to comment.