Skip to content

Commit

Permalink
Cleaner API
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Sep 6, 2020
1 parent 039951e commit 37de374
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 44 deletions.
Expand Up @@ -15,6 +15,10 @@ class QgsMapBoxGlStyleConverter
Handles conversion of MapBox GL styles to QGIS vector tile renderers and labeling
settings.

Conversions are performed by calling :py:func:`~convert` with either a JSON map or JSON
string value, and then retrieving the results by calling :py:func:`~renderer` or :py:func:`~labeling`
respectively.

.. versionadded:: 3.16
%End

Expand All @@ -23,22 +27,41 @@ settings.
%End
public:

QgsMapBoxGlStyleConverter( const QVariantMap &style, const QString &styleName = QString() );
QgsMapBoxGlStyleConverter();
%Docstring
Constructor for QgsMapBoxGlStyleConverter.

The specified MapBox GL ``style`` JSON will be converted.
%End

QgsMapBoxGlStyleConverter( const QString &style, const QString &styleName = QString() );

~QgsMapBoxGlStyleConverter();

enum Result
{
Success,
NoLayerList,
};

Result convert( const QVariantMap &style );
%Docstring
Constructor for QgsMapBoxGlStyleConverter.
Converts a JSON ``style`` map, and returns the resultant status of the conversion.

The specified MapBox GL ``style`` string configuration will be converted.
If an error occurs during conversion then a descriptive error message can be retrieved
by calling :py:func:`~QgsMapBoxGlStyleConverter.errorMessage`.

After conversion, the resultant labeling and style rules can be retrieved by calling
:py:func:`~QgsMapBoxGlStyleConverter.renderer` or :py:func:`~QgsMapBoxGlStyleConverter.labeling` respectively.
%End

Result convert( const QString &style );
%Docstring
Converts a JSON ``style`` string, and returns the resultant status of the conversion.

~QgsMapBoxGlStyleConverter();
If an error occurs during conversion then a descriptive error message can be retrieved
by calling :py:func:`~QgsMapBoxGlStyleConverter.errorMessage`.

After conversion, the resultant labeling and style rules can be retrieved by calling
:py:func:`~QgsMapBoxGlStyleConverter.renderer` or :py:func:`~QgsMapBoxGlStyleConverter.labeling` respectively.
%End

QString errorMessage() const;
%Docstring
Expand Down Expand Up @@ -68,34 +91,32 @@ or ``None`` if the style could not be converted successfully.
Text
};

void parseLayers( const QVariantList &layers, const QString &styleName );
void parseLayers( const QVariantList &layers );
%Docstring
Parse list of ``layers`` from JSON
%End

static bool parseFillLayer( const QVariantMap &jsonLayer, const QString &styleName, QgsVectorTileBasicRendererStyle &style /Out/ );
static bool parseFillLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style /Out/ );
%Docstring
Parses a fill layer.

:param jsonLayer: fill layer to parse
:param styleName: style name

:return: - ``True`` if the layer was successfully parsed.
- style: generated QGIS vector tile style
%End

static bool parseLineLayer( const QVariantMap &jsonLayer, const QString &styleName, QgsVectorTileBasicRendererStyle &style /Out/ );
static bool parseLineLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style /Out/ );
%Docstring
Parses a line layer.

:param jsonLayer: fill layer to parse
:param styleName: style name

:return: - ``True`` if the layer was successfully parsed.
- style: generated QGIS vector tile style
%End

static void parseSymbolLayer( const QVariantMap &jsonLayer, const QString &styleName,
static void parseSymbolLayer( const QVariantMap &jsonLayer,
QgsVectorTileBasicRendererStyle &rendererStyle /Out/,
bool &hasRenderer /Out/,
QgsVectorTileBasicLabelingStyle &labelingStyle /Out/,
Expand All @@ -104,7 +125,6 @@ Parses a line layer.
Parses a symbol layer.

:param jsonLayer: fill layer to parse
:param styleName: style name
:param rendererStyle: generated QGIS vector tile style
:param hasRenderer: will be set to ``True`` if symbol layer generated a renderer style
:param labelingStyle: generated QGIS vector tile labeling
Expand Down
32 changes: 19 additions & 13 deletions src/core/vectortile/qgsmapboxglstyleconverter.cpp
Expand Up @@ -33,27 +33,33 @@

constexpr double PIXEL_RATIO = 1;

QgsMapBoxGlStyleConverter::QgsMapBoxGlStyleConverter( const QVariantMap &style, const QString &styleName )
: mStyle( style )
QgsMapBoxGlStyleConverter::QgsMapBoxGlStyleConverter()
{
if ( mStyle.contains( QStringLiteral( "layers" ) ) )
}

QgsMapBoxGlStyleConverter::Result QgsMapBoxGlStyleConverter::convert( const QVariantMap &style )
{
mError.clear();
if ( style.contains( QStringLiteral( "layers" ) ) )
{
parseLayers( mStyle.value( QStringLiteral( "layers" ) ).toList(), styleName );
parseLayers( mStyle.value( QStringLiteral( "layers" ) ).toList() );
}
else
{
mError = QObject::tr( "Could not find layers list in JSON" );
return NoLayerList;
}
return Success;
}

QgsMapBoxGlStyleConverter::QgsMapBoxGlStyleConverter( const QString &style, const QString &styleName )
: QgsMapBoxGlStyleConverter( QgsJsonUtils::parseJson( style ).toMap(), styleName )
QgsMapBoxGlStyleConverter::Result QgsMapBoxGlStyleConverter::convert( const QString &style )
{
return convert( QgsJsonUtils::parseJson( style ).toMap() );
}

QgsMapBoxGlStyleConverter::~QgsMapBoxGlStyleConverter() = default;

void QgsMapBoxGlStyleConverter::parseLayers( const QVariantList &layers, const QString &styleName )
void QgsMapBoxGlStyleConverter::parseLayers( const QVariantList &layers )
{
QList<QgsVectorTileBasicRendererStyle> rendererStyles;
QList<QgsVectorTileBasicLabelingStyle> labelingStyles;
Expand Down Expand Up @@ -87,15 +93,15 @@ void QgsMapBoxGlStyleConverter::parseLayers( const QVariantList &layers, const Q
bool hasLabelingStyle = false;
if ( layerType == QLatin1String( "fill" ) )
{
hasRendererStyle = parseFillLayer( jsonLayer, styleName, rendererStyle );
hasRendererStyle = parseFillLayer( jsonLayer, rendererStyle );
}
else if ( layerType == QLatin1String( "line" ) )
{
hasRendererStyle = parseLineLayer( jsonLayer, styleName, rendererStyle );
hasRendererStyle = parseLineLayer( jsonLayer, rendererStyle );
}
else if ( layerType == QLatin1String( "symbol" ) )
{
parseSymbolLayer( jsonLayer, styleName, rendererStyle, hasRendererStyle, labelingStyle, hasLabelingStyle );
parseSymbolLayer( jsonLayer, rendererStyle, hasRendererStyle, labelingStyle, hasLabelingStyle );
}
else
{
Expand Down Expand Up @@ -136,7 +142,7 @@ void QgsMapBoxGlStyleConverter::parseLayers( const QVariantList &layers, const Q
labeling->setStyles( labelingStyles );
}

bool QgsMapBoxGlStyleConverter::parseFillLayer( const QVariantMap &jsonLayer, const QString &, QgsVectorTileBasicRendererStyle &style )
bool QgsMapBoxGlStyleConverter::parseFillLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style )
{
if ( !jsonLayer.contains( QStringLiteral( "paint" ) ) )
{
Expand Down Expand Up @@ -318,7 +324,7 @@ bool QgsMapBoxGlStyleConverter::parseFillLayer( const QVariantMap &jsonLayer, co
return true;
}

bool QgsMapBoxGlStyleConverter::parseLineLayer( const QVariantMap &jsonLayer, const QString &, QgsVectorTileBasicRendererStyle &style )
bool QgsMapBoxGlStyleConverter::parseLineLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style )
{
if ( !jsonLayer.contains( QStringLiteral( "paint" ) ) )
{
Expand Down Expand Up @@ -510,7 +516,7 @@ bool QgsMapBoxGlStyleConverter::parseLineLayer( const QVariantMap &jsonLayer, co
return true;
}

void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer, const QString &, QgsVectorTileBasicRendererStyle &, bool &hasRenderer, QgsVectorTileBasicLabelingStyle &labelingStyle, bool &hasLabeling )
void QgsMapBoxGlStyleConverter::parseSymbolLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &, bool &hasRenderer, QgsVectorTileBasicLabelingStyle &labelingStyle, bool &hasLabeling )
{
hasLabeling = false;
hasRenderer = false;
Expand Down
55 changes: 38 additions & 17 deletions src/core/vectortile/qgsmapboxglstyleconverter.h
Expand Up @@ -32,6 +32,10 @@ class QgsVectorTileBasicLabelingStyle;
* Handles conversion of MapBox GL styles to QGIS vector tile renderers and labeling
* settings.
*
* Conversions are performed by calling convert() with either a JSON map or JSON
* string value, and then retrieving the results by calling renderer() or labeling()
* respectively.
*
* \since QGIS 3.16
*/
class CORE_EXPORT QgsMapBoxGlStyleConverter
Expand All @@ -40,17 +44,8 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter

/**
* Constructor for QgsMapBoxGlStyleConverter.
*
* The specified MapBox GL \a style JSON will be converted.
*/
QgsMapBoxGlStyleConverter( const QVariantMap &style, const QString &styleName = QString() );

/**
* Constructor for QgsMapBoxGlStyleConverter.
*
* The specified MapBox GL \a style string configuration will be converted.
*/
QgsMapBoxGlStyleConverter( const QString &style, const QString &styleName = QString() );
QgsMapBoxGlStyleConverter();

//! QgsMapBoxGlStyleConverter cannot be copied
QgsMapBoxGlStyleConverter( const QgsMapBoxGlStyleConverter &other ) = delete;
Expand All @@ -59,6 +54,35 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter

~QgsMapBoxGlStyleConverter();

//! Result of conversion
enum Result
{
Success = 0, //!< Conversion was successful
NoLayerList = 1, //!< No layer list was found in JSON input
};

/**
* Converts a JSON \a style map, and returns the resultant status of the conversion.
*
* If an error occurs during conversion then a descriptive error message can be retrieved
* by calling errorMessage().
*
* After conversion, the resultant labeling and style rules can be retrieved by calling
* renderer() or labeling() respectively.
*/
Result convert( const QVariantMap &style );

/**
* Converts a JSON \a style string, and returns the resultant status of the conversion.
*
* If an error occurs during conversion then a descriptive error message can be retrieved
* by calling errorMessage().
*
* After conversion, the resultant labeling and style rules can be retrieved by calling
* renderer() or labeling() respectively.
*/
Result convert( const QString &style );

/**
* Returns a descriptive error message if an error was encountered during the style conversion,
* or an empty string if no error was encountered.
Expand Down Expand Up @@ -90,39 +114,36 @@ class CORE_EXPORT QgsMapBoxGlStyleConverter
/**
* Parse list of \a layers from JSON
*/
void parseLayers( const QVariantList &layers, const QString &styleName );
void parseLayers( const QVariantList &layers );

/**
* Parses a fill layer.
*
* \param jsonLayer fill layer to parse
* \param styleName style name
* \param style generated QGIS vector tile style
* \returns TRUE if the layer was successfully parsed.
*/
static bool parseFillLayer( const QVariantMap &jsonLayer, const QString &styleName, QgsVectorTileBasicRendererStyle &style SIP_OUT );
static bool parseFillLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style SIP_OUT );

/**
* Parses a line layer.
*
* \param jsonLayer fill layer to parse
* \param styleName style name
* \param style generated QGIS vector tile style
* \returns TRUE if the layer was successfully parsed.
*/
static bool parseLineLayer( const QVariantMap &jsonLayer, const QString &styleName, QgsVectorTileBasicRendererStyle &style SIP_OUT );
static bool parseLineLayer( const QVariantMap &jsonLayer, QgsVectorTileBasicRendererStyle &style SIP_OUT );

/**
* Parses a symbol layer.
*
* \param jsonLayer fill layer to parse
* \param styleName style name
* \param rendererStyle generated QGIS vector tile style
* \param hasRenderer will be set to TRUE if symbol layer generated a renderer style
* \param labelingStyle generated QGIS vector tile labeling
* \param hasLabeling will be set to TRUE if symbol layer generated a labeling style
*/
static void parseSymbolLayer( const QVariantMap &jsonLayer, const QString &styleName,
static void parseSymbolLayer( const QVariantMap &jsonLayer,
QgsVectorTileBasicRendererStyle &rendererStyle SIP_OUT,
bool &hasRenderer SIP_OUT,
QgsVectorTileBasicLabelingStyle &labelingStyle SIP_OUT,
Expand Down

0 comments on commit 37de374

Please sign in to comment.