Skip to content
Permalink
Browse files

Add methods to convert QFont to/from mime data

  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 0b9fb5d commit 5ac9745cad22b83879ffe76fb628dbd2bdcaaedf
Showing with 107 additions and 1 deletion.
  1. +19 −0 python/core/qgsfontutils.sip
  2. +47 −1 src/core/qgsfontutils.cpp
  3. +19 −0 src/core/qgsfontutils.h
  4. +22 −0 tests/src/python/test_qgsfontutils.py
@@ -9,6 +9,7 @@




class QgsFontUtils
{

@@ -136,6 +137,24 @@ class QgsFontUtils
:rtype: bool
%End

static QMimeData *toMimeData( const QFont &font ) /Factory/;
%Docstring
Returns new mime data representing the specified ``font`` settings.
Caller takes responsibility for deleting the returned object.
.. seealso:: fromMimeData()
.. versionadded:: 3.0
:rtype: QMimeData
%End

static QFont fromMimeData( const QMimeData *data, bool *ok /Out/ = 0 );
%Docstring
Attempts to parse the provided mime ``data`` as a QFont.
If data can be parsed as a QFont, ``ok`` will be set to true.
.. seealso:: toMimeData()
.. versionadded:: 3.0
:rtype: QFont
%End

static QString translateNamedStyle( const QString &namedStyle );
%Docstring
Returns the localized named style of a font, if such a translation is available.
@@ -24,7 +24,8 @@
#include <QFontDatabase>
#include <QFontInfo>
#include <QStringList>

#include <QMimeData>
#include <memory>

bool QgsFontUtils::fontMatchOnSystem( const QFont &f )
{
@@ -360,6 +361,51 @@ bool QgsFontUtils::setFromXmlChildNode( QFont &font, const QDomElement &element,
}
}

QMimeData *QgsFontUtils::toMimeData( const QFont &font )
{
std::unique_ptr< QMimeData >mimeData( new QMimeData );

QDomDocument fontDoc;
QDomElement fontElem = toXmlElement( font, fontDoc, QStringLiteral( "font" ) );
fontDoc.appendChild( fontElem );
mimeData->setText( fontDoc.toString() );

return mimeData.release();
}

QFont QgsFontUtils::fromMimeData( const QMimeData *data, bool *ok )
{
QFont font;
if ( ok )
*ok = false;

if ( !data )
return font;

QString text = data->text();
if ( !text.isEmpty() )
{
QDomDocument doc;
QDomElement elem;

if ( doc.setContent( text ) )
{
elem = doc.documentElement();

if ( elem.nodeName() != QStringLiteral( "font" ) )
elem = elem.firstChildElement( QStringLiteral( "font" ) );

if ( setFromXmlElement( font, elem ) )
{
if ( ok )
*ok = true;
}
return font;
}
}
return font;
}

static QMap<QString, QString> createTranslatedStyleMap()
{
QMap<QString, QString> translatedStyleMap;
@@ -21,6 +21,9 @@
#include <QDomElement>

#include "qgis_core.h"
#include "qgis_sip.h"

class QMimeData;

/** \ingroup core
* \class QgsFontUtils
@@ -120,6 +123,22 @@ class CORE_EXPORT QgsFontUtils
*/
static bool setFromXmlChildNode( QFont &font, const QDomElement &element, const QString &childNode );

/**
* Returns new mime data representing the specified \a font settings.
* Caller takes responsibility for deleting the returned object.
* \see fromMimeData()
* \since QGIS 3.0
*/
static QMimeData *toMimeData( const QFont &font ) SIP_FACTORY;

/**
* Attempts to parse the provided mime \a data as a QFont.
* If data can be parsed as a QFont, \a ok will be set to true.
* \see toMimeData()
* \since QGIS 3.0
*/
static QFont fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );

/** Returns the localized named style of a font, if such a translation is available.
* \param namedStyle a named style, i.e. "Bold", "Italic", etc
* \returns The localized named style
@@ -66,6 +66,28 @@ def test_get_specific_test_font(self):
msg = self._family + ' test font Bold Oblique at 14 pt not retrieved'
assert res, msg

def testToFromMimeData(self):
"""
Test converting QFonts to and from mime data
"""
f = QgsFontUtils.getStandardTestFont('Bold Oblique', 14)
mime_data = QgsFontUtils.toMimeData(f)
self.assertTrue(mime_data is not None)

res, ok = QgsFontUtils.fromMimeData(None)
self.assertFalse(ok)
res, ok = QgsFontUtils.fromMimeData(mime_data)
self.assertTrue(ok)

expected = (
res.family() == self._family and
res.bold() and
res.italic() and
res.pointSize() == 14
)
msg = self._family + ' test font Bold Oblique at 14 pt not retrieved from mime data'
self.assertTrue(res, msg)


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

0 comments on commit 5ac9745

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