Skip to content

Commit

Permalink
Add methods to convert QgsTextFormat to and from QFonts
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed May 11, 2018
1 parent e35d1d0 commit e53adc1
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
26 changes: 26 additions & 0 deletions python/core/qgstextrenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,8 @@ of rendered text.
.. seealso:: :py:func:`setFont`

.. seealso:: :py:func:`namedStyle`

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

QFont scaledFont( const QgsRenderContext &context ) const;
Expand Down Expand Up @@ -1262,6 +1264,8 @@ of rendered text.
.. seealso:: :py:func:`font`

.. seealso:: :py:func:`setNamedStyle`

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

QString namedStyle() const;
Expand Down Expand Up @@ -1445,6 +1449,28 @@ Returns new mime data representing the text format settings.
Caller takes responsibility for deleting the returned object.

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

static QgsTextFormat fromQFont( const QFont &font );
%Docstring
Returns a text format matching the settings from an input ``font``.
Unlike setFont(), this method also handles the size and size units
from ``font``.

.. versionadded:: 3.2

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

QFont toQFont() const;
%Docstring
Returns a QFont matching the relevant settings from this text format.
Unlike font(), this method also handles the size and size units
from the text format.

.. versionadded:: 3.2

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

static QgsTextFormat fromMimeData( const QMimeData *data, bool *ok /Out/ = 0 );
Expand Down
49 changes: 49 additions & 0 deletions src/core/qgstextrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,55 @@ QMimeData *QgsTextFormat::toMimeData() const
return mimeData;
}

QgsTextFormat QgsTextFormat::fromQFont( const QFont &font )
{
QgsTextFormat format;
format.setFont( font );
if ( font.pointSizeF() > 0 )
{
format.setSize( font.pointSizeF() );
format.setSizeUnit( QgsUnitTypes::RenderPoints );
}
else if ( font.pixelSize() > 0 )
{
format.setSize( font.pixelSize() );
format.setSizeUnit( QgsUnitTypes::RenderPixels );
}

return format;
}

QFont QgsTextFormat::toQFont() const
{
QFont f = font();
switch ( sizeUnit() )
{
case QgsUnitTypes::RenderPoints:
f.setPointSizeF( size() );
break;

case QgsUnitTypes::RenderMillimeters:
f.setPointSizeF( size() * 2.83464567 );
break;

case QgsUnitTypes::RenderInches:
f.setPointSizeF( size() * 72 );
break;

case QgsUnitTypes::RenderPixels:
f.setPixelSize( static_cast< int >( std::round( size() ) ) );
break;

case QgsUnitTypes::RenderMapUnits:
case QgsUnitTypes::RenderMetersInMapUnits:
case QgsUnitTypes::RenderUnknownUnit:
case QgsUnitTypes::RenderPercentage:
// no meaning here
break;
}
return f;
}

QgsTextFormat QgsTextFormat::fromMimeData( const QMimeData *data, bool *ok )
{
if ( ok )
Expand Down
20 changes: 20 additions & 0 deletions src/core/qgstextrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,7 @@ class CORE_EXPORT QgsTextFormat
* \see scaledFont()
* \see setFont()
* \see namedStyle()
* \see toQFont()
*/
QFont font() const;

Expand All @@ -1075,6 +1076,7 @@ class CORE_EXPORT QgsTextFormat
* \param font desired font
* \see font()
* \see setNamedStyle()
* \see fromQFont()
*/
void setFont( const QFont &font );

Expand Down Expand Up @@ -1224,6 +1226,24 @@ class CORE_EXPORT QgsTextFormat
*/
QMimeData *toMimeData() const SIP_FACTORY;

/**
* Returns a text format matching the settings from an input \a font.
* Unlike setFont(), this method also handles the size and size units
* from \a font.
* \since QGIS 3.2
* \see toQFont()
*/
static QgsTextFormat fromQFont( const QFont &font );

/**
* Returns a QFont matching the relevant settings from this text format.
* Unlike font(), this method also handles the size and size units
* from the text format.
* \since QGIS 3.2
* \see fromQFont()
*/
QFont toQFont() const;

/**
* Attempts to parse the provided mime \a data as a QgsTextFormat.
* If data can be parsed as a text format, \a ok will be set to true.
Expand Down
44 changes: 44 additions & 0 deletions tests/src/python/test_qgstextrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,50 @@ def testFontFoundFromXml(self):
f.readXml(parent, QgsReadWriteContext())
self.assertTrue(f.fontFound())

def testFromQFont(self):
qfont = getTestFont()
qfont.setPointSizeF(16.5)
qfont.setLetterSpacing(QFont.AbsoluteSpacing, 3)

format = QgsTextFormat.fromQFont(qfont)
self.assertEqual(format.font().family(), qfont.family())
self.assertEqual(format.font().letterSpacing(), 3.0)
self.assertEqual(format.size(), 16.5)
self.assertEqual(format.sizeUnit(), QgsUnitTypes.RenderPoints)

qfont.setPixelSize(12)
format = QgsTextFormat.fromQFont(qfont)
self.assertEqual(format.size(), 12.0)
self.assertEqual(format.sizeUnit(), QgsUnitTypes.RenderPixels)

def testToQFont(self):
s = QgsTextFormat()
f = getTestFont()
f.setLetterSpacing(QFont.AbsoluteSpacing, 3)
s.setFont(f)
s.setNamedStyle('Italic')
s.setSize(5.5)
s.setSizeUnit(QgsUnitTypes.RenderPoints)

qfont = s.toQFont()
self.assertEqual(qfont.family(), f.family())
self.assertEqual(qfont.pointSizeF(), 5.5)
self.assertEqual(qfont.letterSpacing(), 3.0)

s.setSize(5)
s.setSizeUnit(QgsUnitTypes.RenderPixels)
qfont = s.toQFont()
self.assertEqual(qfont.pixelSize(), 5)

s.setSize(5)
s.setSizeUnit(QgsUnitTypes.RenderMillimeters)
qfont = s.toQFont()
self.assertAlmostEqual(qfont.pointSizeF(), 14.17, 2)

s.setSizeUnit(QgsUnitTypes.RenderInches)
qfont = s.toQFont()
self.assertAlmostEqual(qfont.pointSizeF(), 360.0, 2)

def imageCheck(self, name, reference_image, image):
self.report += "<h2>Render {}</h2>\n".format(name)
temp_dir = QDir.tempPath() + '/'
Expand Down

0 comments on commit e53adc1

Please sign in to comment.