Skip to content

Commit

Permalink
Really accept strings for arrow data defined properties
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 8, 2018
1 parent 79035ac commit 10475a6
Show file tree
Hide file tree
Showing 14 changed files with 151 additions and 7 deletions.
1 change: 1 addition & 0 deletions python/core/symbology/qgssymbollayer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@




class QgsSymbolLayer
{

Expand Down
14 changes: 14 additions & 0 deletions python/core/symbology/qgssymbollayerutils.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ class QgsSymbolLayerUtils
static QString encodeSldBrushStyle( Qt::BrushStyle style );
static Qt::BrushStyle decodeSldBrushStyle( const QString &str );

static QgsArrowSymbolLayer::HeadType decodeArrowHeadType( const QVariant &value, bool *ok /Out/ = 0 );
%Docstring
Decodes a ``value`` representing an arrow head type.

.. versionadded:: 3.2
%End

static QgsArrowSymbolLayer::ArrowType decodeArrowType( const QVariant &value, bool *ok /Out/ = 0 );
%Docstring
Decodes a ``value`` representing an arrow type.

.. versionadded:: 3.2
%End

static QString encodePoint( QPointF point );
%Docstring
Encodes a QPointF to a string.
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsmaptooloffsetpointsymbol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "qgssymbollayer.h"
#include "qgisapp.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"

#include <QGraphicsPixmapItem>
#include <QMouseEvent>
Expand Down
1 change: 1 addition & 0 deletions src/core/symbology/qgs25drenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "qgseffectstack.h"
#include "qgsgloweffect.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"

#define ROOF_EXPRESSION \
"translate(" \
Expand Down
9 changes: 5 additions & 4 deletions src/core/symbology/qgsarrowsymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
***************************************************************************/

#include "qgsarrowsymbollayer.h"
#include "qgssymbollayerutils.h"

QgsArrowSymbolLayer::QgsArrowSymbolLayer()
{
Expand Down Expand Up @@ -648,21 +649,21 @@ void QgsArrowSymbolLayer::_resolveDataDefined( QgsSymbolRenderContext &context )
{
context.setOriginalValueVariable( headType() );
exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyArrowHeadType, context.renderContext().expressionContext() );
int h = exprVal.toInt( &ok );
HeadType h = QgsSymbolLayerUtils::decodeArrowHeadType( exprVal, &ok );
if ( ok )
{
mComputedHeadType = static_cast<HeadType>( h );
mComputedHeadType = h;
}
}

if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyArrowType ) )
{
context.setOriginalValueVariable( arrowType() );
exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyArrowType, context.renderContext().expressionContext() );
int h = exprVal.toInt( &ok );
ArrowType h = QgsSymbolLayerUtils::decodeArrowType( exprVal, &ok );
if ( ok )
{
mComputedArrowType = static_cast<ArrowType>( h );
mComputedArrowType = h;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/core/symbology/qgsellipsesymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "qgslogger.h"
#include "qgsunittypes.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"

#include <QPainter>
#include <QSet>
Expand Down
1 change: 1 addition & 0 deletions src/core/symbology/qgssymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "qgspainteffectregistry.h"
#include "qgsproperty.h"
#include "qgsexpressioncontext.h"
#include "qgssymbollayerutils.h"

#include <QSize>
#include <QPainter>
Expand Down
5 changes: 4 additions & 1 deletion src/core/symbology/qgssymbollayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
#include <QDomElement>

#include "qgssymbol.h"
#include "qgssymbollayerutils.h" // QgsStringMap
#include "qgsfields.h"
#include "qgspropertycollection.h"

Expand All @@ -41,6 +40,10 @@ class QgsExpression;
class QgsRenderContext;
class QgsPaintEffect;

#ifndef SIP_RUN
typedef QMap<QString, QString> QgsStringMap;
#endif

/**
* \ingroup core
* \class QgsSymbolLayer
Expand Down
50 changes: 50 additions & 0 deletions src/core/symbology/qgssymbollayerutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,56 @@ Qt::BrushStyle QgsSymbolLayerUtils::decodeSldBrushStyle( const QString &str )
return Qt::NoBrush;
}

QgsArrowSymbolLayer::HeadType QgsSymbolLayerUtils::decodeArrowHeadType( const QVariant &value, bool *ok )
{
if ( ok )
*ok = true;

bool intOk = false;
QString s = value.toString().toLower().trimmed();
if ( s == QLatin1String( "single" ) )
return QgsArrowSymbolLayer::HeadSingle;
else if ( s == QLatin1String( "reversed" ) )
return QgsArrowSymbolLayer::HeadReversed;
else if ( s == QLatin1String( "double" ) )
return QgsArrowSymbolLayer::HeadDouble;
else if ( value.toInt() == 1 )
return QgsArrowSymbolLayer::HeadReversed;
else if ( value.toInt() == 2 )
return QgsArrowSymbolLayer::HeadDouble;
else if ( value.toInt( &intOk ) == 0 && intOk )
return QgsArrowSymbolLayer::HeadSingle;

if ( ok )
*ok = false;
return QgsArrowSymbolLayer::HeadSingle;
}

QgsArrowSymbolLayer::ArrowType QgsSymbolLayerUtils::decodeArrowType( const QVariant &value, bool *ok )
{
if ( ok )
*ok = true;

bool intOk = false;
QString s = value.toString().toLower().trimmed();
if ( s == QLatin1String( "plain" ) )
return QgsArrowSymbolLayer::ArrowPlain;
else if ( s == QLatin1String( "lefthalf" ) )
return QgsArrowSymbolLayer::ArrowLeftHalf;
else if ( s == QLatin1String( "righthalf" ) )
return QgsArrowSymbolLayer::ArrowRightHalf;
else if ( value.toInt() == 1 )
return QgsArrowSymbolLayer::ArrowLeftHalf;
else if ( value.toInt() == 2 )
return QgsArrowSymbolLayer::ArrowRightHalf;
else if ( value.toInt( &intOk ) == 0 && intOk )
return QgsArrowSymbolLayer::ArrowPlain;

if ( ok )
*ok = false;
return QgsArrowSymbolLayer::ArrowPlain;
}

QString QgsSymbolLayerUtils::encodePoint( QPointF point )
{
return QStringLiteral( "%1,%2" ).arg( qgsDoubleToString( point.x() ), qgsDoubleToString( point.y() ) );
Expand Down
13 changes: 13 additions & 0 deletions src/core/symbology/qgssymbollayerutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "qgis.h"
#include "qgsmapunitscale.h"
#include "qgscolorramp.h"
#include "qgsarrowsymbollayer.h"

class QgsExpression;
class QgsPathResolver;
Expand Down Expand Up @@ -87,6 +88,18 @@ class CORE_EXPORT QgsSymbolLayerUtils
static QString encodeSldBrushStyle( Qt::BrushStyle style );
static Qt::BrushStyle decodeSldBrushStyle( const QString &str );

/**
* Decodes a \a value representing an arrow head type.
* \since QGIS 3.2
*/
static QgsArrowSymbolLayer::HeadType decodeArrowHeadType( const QVariant &value, bool *ok SIP_OUT = nullptr );

/**
* Decodes a \a value representing an arrow type.
* \since QGIS 3.2
*/
static QgsArrowSymbolLayer::ArrowType decodeArrowType( const QVariant &value, bool *ok SIP_OUT = nullptr );

/**
* Encodes a QPointF to a string.
* \see decodePoint()
Expand Down
1 change: 1 addition & 0 deletions src/core/symbology/qgsvectorfieldsymbollayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "qgsvectorfieldsymbollayer.h"
#include "qgsvectorlayer.h"
#include "qgsunittypes.h"
#include "qgssymbollayerutils.h"

QgsVectorFieldSymbolLayer::QgsVectorFieldSymbolLayer()
{
Expand Down
2 changes: 1 addition & 1 deletion tests/src/core/testqgsproject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include "qgssettings.h"
#include "qgsunittypes.h"
#include "qgsvectorlayer.h"

#include "qgssymbollayerutils.h"

class TestQgsProject : public QObject
{
Expand Down
1 change: 1 addition & 0 deletions tests/src/core/testqgssvgmarker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <qgssinglesymbolrenderer.h>
#include "qgsmarkersymbollayer.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"

//qgis test includes
#include "qgsrenderchecker.h"
Expand Down
58 changes: 57 additions & 1 deletion tests/src/python/test_qgssymbollayerutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@

import qgis # NOQA

from qgis.core import QgsSymbolLayerUtils, QgsMarkerSymbol
from qgis.core import (QgsSymbolLayerUtils,
QgsMarkerSymbol,
QgsArrowSymbolLayer)
from qgis.PyQt.QtGui import QColor
from qgis.PyQt.QtCore import QSizeF, QPointF
from qgis.testing import unittest, start_app
Expand Down Expand Up @@ -52,6 +54,60 @@ def testEncodeDecodePoint(self):
s2 = QgsSymbolLayerUtils.decodePoint('')
self.assertEqual(s2, QPointF())

def testDecodeArrowHeadType(self):
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType(0)
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.HeadSingle)
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType('single')
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.HeadSingle)
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType(' SINGLE ')
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.HeadSingle)
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType(1)
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.HeadReversed)
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType('reversed')
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.HeadReversed)
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType(2)
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.HeadDouble)
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType('double')
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.HeadDouble)
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType('xxxxx')
self.assertFalse(ok)
type, ok = QgsSymbolLayerUtils.decodeArrowHeadType(34)
self.assertFalse(ok)

def testDecodeArrowType(self):
type, ok = QgsSymbolLayerUtils.decodeArrowType(0)
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.ArrowPlain)
type, ok = QgsSymbolLayerUtils.decodeArrowType('plain')
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.ArrowPlain)
type, ok = QgsSymbolLayerUtils.decodeArrowType(' PLAIN ')
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.ArrowPlain)
type, ok = QgsSymbolLayerUtils.decodeArrowType(1)
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.ArrowLeftHalf)
type, ok = QgsSymbolLayerUtils.decodeArrowType('lefthalf')
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.ArrowLeftHalf)
type, ok = QgsSymbolLayerUtils.decodeArrowType(2)
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.ArrowRightHalf)
type, ok = QgsSymbolLayerUtils.decodeArrowType('righthalf')
self.assertTrue(ok)
self.assertEqual(type, QgsArrowSymbolLayer.ArrowRightHalf)
type, ok = QgsSymbolLayerUtils.decodeArrowType('xxxxx')
self.assertFalse(ok)
type, ok = QgsSymbolLayerUtils.decodeArrowType(34)
self.assertFalse(ok)

def testSymbolToFromMimeData(self):
"""
Test converting symbols to and from mime data
Expand Down

0 comments on commit 10475a6

Please sign in to comment.