Skip to content
Permalink
Browse files

Add QgsTextRenderer methods to convert Qt alignment flags

  • Loading branch information
nyalldawson committed Jul 9, 2020
1 parent eaedff3 commit 77dfaae6889d936c6de8a39e024b2a433e1b9468
@@ -46,13 +46,31 @@ and background shapes.
AlignRight,
};

static HAlignment convertQtHAlignment( Qt::Alignment alignment );
%Docstring
Converts a Qt horizontal ``alignment`` flag to a QgsTextRenderer.HAlignment value.

.. seealso:: :py:func:`convertQtVAlignment`

.. versionadded:: 3.16
%End

enum VAlignment
{
AlignTop,
AlignVCenter,
AlignBottom,
};

static VAlignment convertQtVAlignment( Qt::Alignment alignment );
%Docstring
Converts a Qt vertical ``alignment`` flag to a QgsTextRenderer.VAlignment value.

.. seealso:: :py:func:`convertQtHAlignment`

.. versionadded:: 3.16
%End

static int sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale = QgsMapUnitScale() );
%Docstring
Calculates pixel size (considering output size should be in pixel or map units, scale factors and optionally oversampling)
@@ -37,6 +37,36 @@ static void _fixQPictureDPI( QPainter *p )
static_cast< double >( qt_defaultDpiY() ) / p->device()->logicalDpiY() );
}

QgsTextRenderer::HAlignment QgsTextRenderer::convertQtHAlignment( Qt::Alignment alignment )
{
if ( alignment & Qt::AlignLeft )
return AlignLeft;
else if ( alignment & Qt::AlignRight )
return AlignRight;
else if ( alignment & Qt::AlignHCenter )
return AlignCenter;

// not supported
// Qt::AlignJustify
// Qt::AlignAbsolute
return AlignLeft;
}

QgsTextRenderer::VAlignment QgsTextRenderer::convertQtVAlignment( Qt::Alignment alignment )
{
if ( alignment & Qt::AlignTop )
return AlignTop;
else if ( alignment & Qt::AlignBottom )
return AlignBottom;
else if ( alignment & Qt::AlignVCenter )
return AlignVCenter;
//not supported
else if ( alignment & Qt::AlignBaseline )
return AlignBottom;

return AlignTop;
}

int QgsTextRenderer::sizeToPixel( double size, const QgsRenderContext &c, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &mapUnitScale )
{
return static_cast< int >( c.convertToPainterUnits( size, unit, mapUnitScale ) + 0.5 ); //NOLINT
@@ -62,6 +62,14 @@ class CORE_EXPORT QgsTextRenderer
AlignRight, //!< Right align
};

/**
* Converts a Qt horizontal \a alignment flag to a QgsTextRenderer::HAlignment value.
*
* \see convertQtVAlignment()
* \since QGIS 3.16
*/
static HAlignment convertQtHAlignment( Qt::Alignment alignment );

/**
* Vertical alignment
* \since QGIS 3.16
@@ -73,6 +81,14 @@ class CORE_EXPORT QgsTextRenderer
AlignBottom, //!< Align to bottom
};

/**
* Converts a Qt vertical \a alignment flag to a QgsTextRenderer::VAlignment value.
*
* \see convertQtHAlignment()
* \since QGIS 3.16
*/
static VAlignment convertQtVAlignment( Qt::Alignment alignment );

/**
* Calculates pixel size (considering output size should be in pixel or map units, scale factors and optionally oversampling)
* \param size size to convert
@@ -63,6 +63,20 @@ def tearDownClass(cls):
with open(report_file_path, 'a') as report_file:
report_file.write(cls.report)

def testAlignmentConversion(self):
self.assertEqual(QgsTextRenderer.convertQtHAlignment(Qt.AlignLeft), QgsTextRenderer.AlignLeft)
self.assertEqual(QgsTextRenderer.convertQtHAlignment(Qt.AlignRight), QgsTextRenderer.AlignRight)
self.assertEqual(QgsTextRenderer.convertQtHAlignment(Qt.AlignHCenter), QgsTextRenderer.AlignCenter)
# not supported, should fallback to left
self.assertEqual(QgsTextRenderer.convertQtHAlignment(Qt.AlignJustify), QgsTextRenderer.AlignLeft)
self.assertEqual(QgsTextRenderer.convertQtHAlignment(Qt.AlignAbsolute), QgsTextRenderer.AlignLeft)

self.assertEqual(QgsTextRenderer.convertQtVAlignment(Qt.AlignTop), QgsTextRenderer.AlignTop)
self.assertEqual(QgsTextRenderer.convertQtVAlignment(Qt.AlignBottom), QgsTextRenderer.AlignBottom)
self.assertEqual(QgsTextRenderer.convertQtVAlignment(Qt.AlignVCenter), QgsTextRenderer.AlignVCenter)
# note supported, should fallback to bottom
self.assertEqual(QgsTextRenderer.convertQtVAlignment(Qt.AlignBaseline), QgsTextRenderer.AlignBottom)

def createBufferSettings(self):
s = QgsTextBufferSettings()
s.setEnabled(True)

0 comments on commit 77dfaae

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