Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use smart pointers to handle mSettings and mFilter
  • Loading branch information
Hugo Mercier committed Jun 30, 2018
1 parent cd39798 commit 831d1b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
19 changes: 7 additions & 12 deletions src/core/qgsrulebasedlabeling.cpp
Expand Up @@ -65,25 +65,21 @@ QgsRuleBasedLabeling::Rule::Rule( QgsPalLayerSettings *settings, int scaleMinDen
, mIsActive( true )

{
mRuleKey = QUuid::createUuid().toString();
initFilter();
}

QgsRuleBasedLabeling::Rule::~Rule()
{
delete mSettings;
delete mFilter;
qDeleteAll( mChildren );
// do NOT delete parent
}

void QgsRuleBasedLabeling::Rule::setSettings( QgsPalLayerSettings *settings )
{
if ( mSettings == settings )
if ( mSettings.get() == settings )
return;

delete mSettings;
mSettings = settings;
mSettings.reset( settings );
}

QgsRuleBasedLabeling::RuleList QgsRuleBasedLabeling::Rule::descendants() const
Expand All @@ -102,16 +98,15 @@ void QgsRuleBasedLabeling::Rule::initFilter()
if ( mElseRule || mFilterExp.compare( QLatin1String( "ELSE" ), Qt::CaseInsensitive ) == 0 )
{
mElseRule = true;
mFilter = nullptr;
mFilter.reset( nullptr );
}
else if ( !mFilterExp.isEmpty() )
{
delete mFilter;
mFilter = new QgsExpression( mFilterExp );
mFilter.reset( new QgsExpression( mFilterExp ) );
}
else
{
mFilter = nullptr;
mFilter.reset( nullptr );
}
}

Expand Down Expand Up @@ -204,7 +199,7 @@ QgsRuleBasedLabeling::Rule *QgsRuleBasedLabeling::Rule::findRuleByKey( const QSt

QgsRuleBasedLabeling::Rule *QgsRuleBasedLabeling::Rule::clone() const
{
QgsPalLayerSettings *s = mSettings ? new QgsPalLayerSettings( *mSettings ) : nullptr;
QgsPalLayerSettings *s = mSettings.get() ? new QgsPalLayerSettings( *mSettings ) : nullptr;
Rule *newrule = new Rule( s, mMaximumScale, mMinimumScale, mFilterExp, mDescription );
newrule->setActive( mIsActive );
// clone children
Expand Down Expand Up @@ -286,7 +281,7 @@ void QgsRuleBasedLabeling::Rule::createSubProviders( QgsVectorLayer *layer, QgsR
if ( mSettings )
{
// add provider!
QgsVectorLayerLabelProvider *p = provider->createProvider( layer, mRuleKey, false, mSettings );
QgsVectorLayerLabelProvider *p = provider->createProvider( layer, mRuleKey, false, mSettings.get() );
delete subProviders.value( this, nullptr );
subProviders[this] = p;
}
Expand Down
9 changes: 4 additions & 5 deletions src/core/qgsrulebasedlabeling.h
Expand Up @@ -52,7 +52,7 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
class CORE_EXPORT Rule
{
public:
//! takes ownership of settings
//! takes ownership of settings, settings may be nullptr
Rule( QgsPalLayerSettings *settings SIP_TRANSFER, int maximumScale = 0, int minimumScale = 0, const QString &filterExp = QString(), const QString &description = QString(), bool elseRule = false );
~Rule();

Expand All @@ -72,7 +72,7 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling
/**
* Gets the labeling settings. May return a null pointer.
*/
QgsPalLayerSettings *settings() const { return mSettings; }
QgsPalLayerSettings *settings() const { return mSettings.get(); }

/**
* Determines if scale based labeling is active
Expand Down Expand Up @@ -325,7 +325,7 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling

private:
Rule *mParent; // parent rule (NULL only for root rule)
QgsPalLayerSettings *mSettings = nullptr;
std::unique_ptr<QgsPalLayerSettings> mSettings;
double mMaximumScale = 0;
double mMinimumScale = 0;
QString mFilterExp, mDescription;
Expand All @@ -336,8 +336,7 @@ class CORE_EXPORT QgsRuleBasedLabeling : public QgsAbstractVectorLayerLabeling

QString mRuleKey; // string used for unique identification of rule within labeling

// temporary
QgsExpression *mFilter = nullptr;
std::unique_ptr<QgsExpression> mFilter;

};

Expand Down

0 comments on commit 831d1b1

Please sign in to comment.