Skip to content

Commit

Permalink
avoid required call to leaveCategory by adding a RAII private class
Browse files Browse the repository at this point in the history
  • Loading branch information
3nids committed Feb 28, 2018
1 parent 8989292 commit 8582517
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 32 deletions.
18 changes: 4 additions & 14 deletions python/core/qgsreadwritecontext.sip.in
Expand Up @@ -9,6 +9,7 @@




class QgsReadWriteContext
{
%Docstring
Expand Down Expand Up @@ -63,26 +64,13 @@ Returns path resolver for conversion between relative and absolute paths
Sets up path resolver for conversion between relative and absolute paths
%End

void pushMessage( const QString &message, Qgis::MessageLevel level );
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );
%Docstring
Append a message to the context

.. versionadded:: 3.2
%End

void enterCategory( const QString &category, const QString &details = QString() );
%Docstring
Push a category to the stack

.. versionadded:: 3.2
%End

void leaveCategory();
%Docstring
Pop the last category

.. versionadded:: 3.2
%End

QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();
%Docstring
Expand All @@ -91,8 +79,10 @@ Return the stored messages and remove them
.. versionadded:: 3.2
%End


};


/************************************************************************
* This file has been generated automatically from *
* *
Expand Down
5 changes: 2 additions & 3 deletions src/core/qgseditformconfig.cpp
Expand Up @@ -260,7 +260,8 @@ void QgsEditFormConfig::setSuppress( QgsEditFormConfig::FeatureFormSuppress s )

void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &context )
{
context.enterCategory( QObject::tr( "Edit form config" ) );
MAYBE_UNUSED QgsReadWriteContextCategoryPopper p = context.enterCategory( QObject::tr( "Edit form config" ) );

d.detach();

QDomNode editFormNode = node.namedItem( QStringLiteral( "editform" ) );
Expand Down Expand Up @@ -388,8 +389,6 @@ void QgsEditFormConfig::readXml( const QDomNode &node, QgsReadWriteContext &cont
onRelationsLoaded();
}
}

context.leaveCategory();
}

void QgsEditFormConfig::writeXml( QDomNode &node, const QgsReadWriteContext &context ) const
Expand Down
4 changes: 1 addition & 3 deletions src/core/qgsmaplayer.cpp
Expand Up @@ -430,7 +430,7 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
// the subclass can also read custom properties
readCustomProperties( layerElement );

context.enterCategory( tr( "Layer" ), mne.text() );
MAYBE_UNUSED QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Layer" ), mne.text() );

// now let the children grab what they need from the Dom node.
layerError = !readXml( layerElement, context );
Expand Down Expand Up @@ -557,8 +557,6 @@ bool QgsMapLayer::readLayerXml( const QDomElement &layerElement, QgsReadWriteCo
QDomElement metadataElem = layerElement.firstChildElement( QStringLiteral( "resourceMetadata" ) );
mMetadata.readMetadataXml( metadataElem );

context.leaveCategory();

return true;
} // bool QgsMapLayer::readLayerXML

Expand Down
3 changes: 2 additions & 1 deletion src/core/qgsreadwritecontext.cpp
Expand Up @@ -35,12 +35,13 @@ void QgsReadWriteContext::pushMessage( const QString &message, Qgis::MessageLeve
mMessages.append( ReadWriteMessage( message, level, mCategories ) );
}

void QgsReadWriteContext::enterCategory( const QString &category, const QString &details )
QgsReadWriteContextCategoryPopper QgsReadWriteContext::enterCategory( const QString &category, const QString &details )
{
QString message = category;
if ( !details.isEmpty() )
message.append( QString( " :: %1" ).arg( details ) );
mCategories.push_back( message );
return QgsReadWriteContextCategoryPopper( this );
}

void QgsReadWriteContext::leaveCategory()
Expand Down
44 changes: 36 additions & 8 deletions src/core/qgsreadwritecontext.h
Expand Up @@ -21,6 +21,8 @@
#include "qgspathresolver.h"
#include "qgis.h"

class QgsReadWriteContextCategoryPopper;

/**
* \class QgsReadWriteContext
* \ingroup core
Expand Down Expand Up @@ -76,30 +78,56 @@ class CORE_EXPORT QgsReadWriteContext
* Append a message to the context
* \since QGIS 3.2
*/
void pushMessage( const QString &message, Qgis::MessageLevel level );
void pushMessage( const QString &message, Qgis::MessageLevel level = Qgis::Warning );

/**
* Push a category to the stack
* \since QGIS 3.2
*/
void enterCategory( const QString &category, const QString &details = QString() );
#ifndef SIP_RUN

/**
* Pop the last category
* Push a category to the stack
* \note The return value should always be used so category can be automatically left.
* \note Not available in the Python bindings.
* \since QGIS 3.2
*/
void leaveCategory();
NODISCARD QgsReadWriteContextCategoryPopper enterCategory( const QString &category, const QString &details = QString() );
#endif

/**
* Return the stored messages and remove them
* \since QGIS 3.2
*/
QList<QgsReadWriteContext::ReadWriteMessage> takeMessages();


private:
//! Pop the last category
void leaveCategory();

QgsPathResolver mPathResolver;
QList<ReadWriteMessage> mMessages;
QStringList mCategories = QStringList();

friend class QgsReadWriteContextCategoryPopper;
};

#ifndef SIP_RUN
///@cond PRIVATE
class QgsReadWriteContextCategoryPopper
{
public:

QgsReadWriteContextCategoryPopper( QgsReadWriteContext *context )
: mContext( context )
{}

~QgsReadWriteContextCategoryPopper()
{
if ( mContext )
mContext->leaveCategory();
}

QgsReadWriteContext *mContext;
};
///@endcond PRIVATE
#endif

#endif // QGSREADWRITECONTEXT_H
4 changes: 1 addition & 3 deletions src/core/qgsvectorlayer.cpp
Expand Up @@ -1688,7 +1688,7 @@ void QgsVectorLayer::resolveReferences( QgsProject *project )

bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMessage, QgsReadWriteContext &context )
{
context.enterCategory( tr( "Symbology" ) );
MAYBE_UNUSED QgsReadWriteContextCategoryPopper p = context.enterCategory( tr( "Symbology" ) );

if ( !mExpressionFieldBuffer )
mExpressionFieldBuffer = new QgsExpressionFieldBuffer();
Expand Down Expand Up @@ -1875,8 +1875,6 @@ bool QgsVectorLayer::readSymbology( const QDomNode &layerNode, QString &errorMes

updateFields();

context.leaveCategory();

return true;
}

Expand Down

0 comments on commit 8582517

Please sign in to comment.