Skip to content
Permalink
Browse files

Really accept strings for arrow data defined properties

  • Loading branch information
nyalldawson committed May 8, 2018
1 parent 79035ac commit 10475a6adf370af634f47c67e3ca911d36f14fa0
@@ -11,6 +11,7 @@




class QgsSymbolLayer
{

@@ -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.
@@ -24,6 +24,7 @@
#include "qgssymbollayer.h"
#include "qgisapp.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"

#include <QGraphicsPixmapItem>
#include <QMouseEvent>
@@ -20,6 +20,7 @@
#include "qgseffectstack.h"
#include "qgsgloweffect.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"

#define ROOF_EXPRESSION \
"translate(" \
@@ -14,6 +14,7 @@
***************************************************************************/

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

QgsArrowSymbolLayer::QgsArrowSymbolLayer()
{
@@ -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;
}
}
}
@@ -21,6 +21,7 @@
#include "qgslogger.h"
#include "qgsunittypes.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"

#include <QPainter>
#include <QSet>
@@ -25,6 +25,7 @@
#include "qgspainteffectregistry.h"
#include "qgsproperty.h"
#include "qgsexpressioncontext.h"
#include "qgssymbollayerutils.h"

#include <QSize>
#include <QPainter>
@@ -28,7 +28,6 @@
#include <QDomElement>

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

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

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

/**
* \ingroup core
* \class QgsSymbolLayer
@@ -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() ) );
@@ -29,6 +29,7 @@
#include "qgis.h"
#include "qgsmapunitscale.h"
#include "qgscolorramp.h"
#include "qgsarrowsymbollayer.h"

class QgsExpression;
class QgsPathResolver;
@@ -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()
@@ -18,6 +18,7 @@
#include "qgsvectorfieldsymbollayer.h"
#include "qgsvectorlayer.h"
#include "qgsunittypes.h"
#include "qgssymbollayerutils.h"

QgsVectorFieldSymbolLayer::QgsVectorFieldSymbolLayer()
{
@@ -24,7 +24,7 @@
#include "qgssettings.h"
#include "qgsunittypes.h"
#include "qgsvectorlayer.h"

#include "qgssymbollayerutils.h"

class TestQgsProject : public QObject
{
@@ -32,6 +32,7 @@
#include <qgssinglesymbolrenderer.h>
#include "qgsmarkersymbollayer.h"
#include "qgsproperty.h"
#include "qgssymbollayerutils.h"

//qgis test includes
#include "qgsrenderchecker.h"
@@ -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
@@ -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

0 comments on commit 10475a6

Please sign in to comment.
You can’t perform that action at this time.