Skip to content

Commit 5ac9745

Browse files
committedJul 6, 2017
Add methods to convert QFont to/from mime data
1 parent 0b9fb5d commit 5ac9745

File tree

4 files changed

+107
-1
lines changed

4 files changed

+107
-1
lines changed
 

‎python/core/qgsfontutils.sip

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010

1111

12+
1213
class QgsFontUtils
1314
{
1415

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

140+
static QMimeData *toMimeData( const QFont &font ) /Factory/;
141+
%Docstring
142+
Returns new mime data representing the specified ``font`` settings.
143+
Caller takes responsibility for deleting the returned object.
144+
.. seealso:: fromMimeData()
145+
.. versionadded:: 3.0
146+
:rtype: QMimeData
147+
%End
148+
149+
static QFont fromMimeData( const QMimeData *data, bool *ok /Out/ = 0 );
150+
%Docstring
151+
Attempts to parse the provided mime ``data`` as a QFont.
152+
If data can be parsed as a QFont, ``ok`` will be set to true.
153+
.. seealso:: toMimeData()
154+
.. versionadded:: 3.0
155+
:rtype: QFont
156+
%End
157+
139158
static QString translateNamedStyle( const QString &namedStyle );
140159
%Docstring
141160
Returns the localized named style of a font, if such a translation is available.

‎src/core/qgsfontutils.cpp

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
#include <QFontDatabase>
2525
#include <QFontInfo>
2626
#include <QStringList>
27-
27+
#include <QMimeData>
28+
#include <memory>
2829

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

364+
QMimeData *QgsFontUtils::toMimeData( const QFont &font )
365+
{
366+
std::unique_ptr< QMimeData >mimeData( new QMimeData );
367+
368+
QDomDocument fontDoc;
369+
QDomElement fontElem = toXmlElement( font, fontDoc, QStringLiteral( "font" ) );
370+
fontDoc.appendChild( fontElem );
371+
mimeData->setText( fontDoc.toString() );
372+
373+
return mimeData.release();
374+
}
375+
376+
QFont QgsFontUtils::fromMimeData( const QMimeData *data, bool *ok )
377+
{
378+
QFont font;
379+
if ( ok )
380+
*ok = false;
381+
382+
if ( !data )
383+
return font;
384+
385+
QString text = data->text();
386+
if ( !text.isEmpty() )
387+
{
388+
QDomDocument doc;
389+
QDomElement elem;
390+
391+
if ( doc.setContent( text ) )
392+
{
393+
elem = doc.documentElement();
394+
395+
if ( elem.nodeName() != QStringLiteral( "font" ) )
396+
elem = elem.firstChildElement( QStringLiteral( "font" ) );
397+
398+
if ( setFromXmlElement( font, elem ) )
399+
{
400+
if ( ok )
401+
*ok = true;
402+
}
403+
return font;
404+
}
405+
}
406+
return font;
407+
}
408+
363409
static QMap<QString, QString> createTranslatedStyleMap()
364410
{
365411
QMap<QString, QString> translatedStyleMap;

‎src/core/qgsfontutils.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
#include <QDomElement>
2222

2323
#include "qgis_core.h"
24+
#include "qgis_sip.h"
25+
26+
class QMimeData;
2427

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

126+
/**
127+
* Returns new mime data representing the specified \a font settings.
128+
* Caller takes responsibility for deleting the returned object.
129+
* \see fromMimeData()
130+
* \since QGIS 3.0
131+
*/
132+
static QMimeData *toMimeData( const QFont &font ) SIP_FACTORY;
133+
134+
/**
135+
* Attempts to parse the provided mime \a data as a QFont.
136+
* If data can be parsed as a QFont, \a ok will be set to true.
137+
* \see toMimeData()
138+
* \since QGIS 3.0
139+
*/
140+
static QFont fromMimeData( const QMimeData *data, bool *ok SIP_OUT = nullptr );
141+
123142
/** Returns the localized named style of a font, if such a translation is available.
124143
* \param namedStyle a named style, i.e. "Bold", "Italic", etc
125144
* \returns The localized named style

‎tests/src/python/test_qgsfontutils.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ def test_get_specific_test_font(self):
6666
msg = self._family + ' test font Bold Oblique at 14 pt not retrieved'
6767
assert res, msg
6868

69+
def testToFromMimeData(self):
70+
"""
71+
Test converting QFonts to and from mime data
72+
"""
73+
f = QgsFontUtils.getStandardTestFont('Bold Oblique', 14)
74+
mime_data = QgsFontUtils.toMimeData(f)
75+
self.assertTrue(mime_data is not None)
76+
77+
res, ok = QgsFontUtils.fromMimeData(None)
78+
self.assertFalse(ok)
79+
res, ok = QgsFontUtils.fromMimeData(mime_data)
80+
self.assertTrue(ok)
81+
82+
expected = (
83+
res.family() == self._family and
84+
res.bold() and
85+
res.italic() and
86+
res.pointSize() == 14
87+
)
88+
msg = self._family + ' test font Bold Oblique at 14 pt not retrieved from mime data'
89+
self.assertTrue(res, msg)
90+
6991

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

0 commit comments

Comments
 (0)
Please sign in to comment.