Skip to content

Commit

Permalink
Add methods to convert QFont to/from mime data
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 6, 2017
1 parent 0b9fb5d commit 5ac9745
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 1 deletion.
19 changes: 19 additions & 0 deletions python/core/qgsfontutils.sip
Expand Up @@ -9,6 +9,7 @@




class QgsFontUtils
{

Expand Down Expand Up @@ -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.
Expand Down
48 changes: 47 additions & 1 deletion src/core/qgsfontutils.cpp
Expand Up @@ -24,7 +24,8 @@
#include <QFontDatabase>
#include <QFontInfo>
#include <QStringList>

#include <QMimeData>
#include <memory>

bool QgsFontUtils::fontMatchOnSystem( const QFont &f )
{
Expand Down Expand Up @@ -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;
Expand Down
19 changes: 19 additions & 0 deletions src/core/qgsfontutils.h
Expand Up @@ -21,6 +21,9 @@
#include <QDomElement>

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

class QMimeData;

/** \ingroup core
* \class QgsFontUtils
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions tests/src/python/test_qgsfontutils.py
Expand Up @@ -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.