Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add methods to get default patch shapes to QgsLegendPatchShape
  • Loading branch information
nyalldawson committed Apr 10, 2020
1 parent fd95215 commit 8564c45
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 0 deletions.
Expand Up @@ -109,6 +109,11 @@ it is resized to fit a desired legend patch size.
The default behavior is to respect the geometry()'s aspect ratio.

.. seealso:: :py:func:`setPreserveAspectRatio`
%End

static QVector< QPolygonF > defaultPatch( QgsSymbol::SymbolType type, QSizeF size );
%Docstring
Returns the default patch geometry for the given symbol ``type`` and ``size`` as a set of QPolygonF objects.
%End

};
Expand Down
22 changes: 22 additions & 0 deletions src/core/layertree/qgslegendpatchshape.cpp
Expand Up @@ -51,6 +51,28 @@ void QgsLegendPatchShape::setPreserveAspectRatio( bool preserveAspectRatio )
mPreserveAspectRatio = preserveAspectRatio;
}

QVector<QPolygonF> QgsLegendPatchShape::defaultPatch( QgsSymbol::SymbolType type, QSizeF size )
{
switch ( type )
{
case QgsSymbol::Marker:
return QVector< QPolygonF >() << ( QPolygonF() << QPointF( size.width() / 2, size.height() / 2 ) );

case QgsSymbol::Line:
// we're adding 0.5 to get rid of blurred preview:
// drawing antialiased lines of width 1 at (x,0)-(x,100) creates 2px line
return QVector< QPolygonF >() << ( QPolygonF() << QPointF( 0, int( size.height() / 2 ) + 0.5 ) << QPointF( size.width(), int( size.height() / 2 ) + 0.5 ) );

case QgsSymbol::Fill:
return QVector< QPolygonF >() << QRectF( QPointF( 0, 0 ), QPointF( size.width(), size.height() ) );

case QgsSymbol::Hybrid:
return QVector<QPolygonF>();
}

return QVector<QPolygonF>();
}

QgsSymbol::SymbolType QgsLegendPatchShape::symbolType() const
{
return mSymbolType;
Expand Down
5 changes: 5 additions & 0 deletions src/core/layertree/qgslegendpatchshape.h
Expand Up @@ -119,6 +119,11 @@ class CORE_EXPORT QgsLegendPatchShape
*/
void setPreserveAspectRatio( bool preserve );

/**
* Returns the default patch geometry for the given symbol \a type and \a size as a set of QPolygonF objects.
*/
static QVector< QPolygonF > defaultPatch( QgsSymbol::SymbolType type, QSizeF size );

private:
QgsSymbol::SymbolType mSymbolType = QgsSymbol::Fill;
QgsGeometry mGeometry;
Expand Down
24 changes: 24 additions & 0 deletions tests/src/python/test_qgslegendpatchshape.py
Expand Up @@ -12,6 +12,9 @@

import qgis # NOQA

from qgis.PyQt.QtCore import (QSizeF,
QPointF)
from qgis.PyQt.QtGui import QPolygonF
from qgis.core import (QgsLegendPatchShape,
QgsGeometry,
QgsSymbol
Expand Down Expand Up @@ -42,6 +45,10 @@ def testBasic(self):
shape.setPreserveAspectRatio(True)
self.assertTrue(shape.preserveAspectRatio())

@staticmethod
def polys_to_list(polys):
return [[[p.x(), p.y()] for p in poly] for poly in polys]

def testNull(self):
shape = QgsLegendPatchShape()
self.assertTrue(shape.isNull())
Expand All @@ -50,6 +57,23 @@ def testNull(self):
shape.setGeometry(QgsGeometry())
self.assertTrue(shape.isNull())

def testDefault(self):
self.assertEqual(QgsLegendPatchShape.defaultPatch(QgsSymbol.Hybrid, QSizeF(1, 1)), [])
self.assertEqual(QgsLegendPatchShape.defaultPatch(QgsSymbol.Hybrid, QSizeF(10, 10)), [])

# markers
self.assertEqual(self.polys_to_list(QgsLegendPatchShape.defaultPatch(QgsSymbol.Marker, QSizeF(1, 1))), [[[0.5, 0.5]]])
self.assertEqual(self.polys_to_list(QgsLegendPatchShape.defaultPatch(QgsSymbol.Marker, QSizeF(10, 2))), [[[5.0, 1.0]]])

# lines
self.assertEqual(self.polys_to_list(QgsLegendPatchShape.defaultPatch(QgsSymbol.Line, QSizeF(1, 1))), [[[0.0, 0.5], [1.0, 0.5]]])
self.assertEqual(self.polys_to_list(QgsLegendPatchShape.defaultPatch(QgsSymbol.Line, QSizeF(10, 2))), [[[0.0, 1.5], [10.0, 1.5]]])
self.assertEqual(self.polys_to_list(QgsLegendPatchShape.defaultPatch(QgsSymbol.Line, QSizeF(9, 3))), [[[0.0, 1.5], [9.0, 1.5]]])

# fills
self.assertEqual(self.polys_to_list(QgsLegendPatchShape.defaultPatch(QgsSymbol.Fill, QSizeF(1, 1))), [[[0.0, 0.0], [1.0, 0.0], [1.0, 1.0], [0.0, 1.0], [0.0, 0.0]]])
self.assertEqual(self.polys_to_list(QgsLegendPatchShape.defaultPatch(QgsSymbol.Fill, QSizeF(10, 2))), [[[0.0, 0.0], [10.0, 0.0], [10.0, 2.0], [0.0, 2.0], [0.0, 0.0]]])


if __name__ == '__main__':
unittest.main()

0 comments on commit 8564c45

Please sign in to comment.