Add font size, family, italic and bold to QgsTextCharacterFormat

This commit is contained in:
Nyall Dawson 2022-08-03 14:02:39 +10:00
parent 8f5c1cf288
commit ad5d9d89f5
5 changed files with 190 additions and 14 deletions

View File

@ -65,6 +65,88 @@ is desired and the default format color should be used.
.. seealso:: :py:func:`textColor`
%End
double fontPointSize() const;
%Docstring
Returns the font point size, or -1 if the font size is not set
and should be inherited.
.. seealso:: :py:func:`setFontPointSize`
.. versionadded:: 3.28
%End
void setFontPointSize( double size );
%Docstring
Sets the font point ``size``.
Set ``size`` to -1 if the font size is not set
and should be inherited.
.. seealso:: :py:func:`fontPointSize`
.. versionadded:: 3.28
%End
QString family() const;
%Docstring
Returns the font family name, or an empty string if the
family is not set and should be inherited.
.. seealso:: :py:func:`setFamily`
.. versionadded:: 3.28
%End
void setFamily( const QString &family );
%Docstring
Sets the font ``family`` name.
Set to an empty string if the family should be inherited.
.. seealso:: :py:func:`family`
.. versionadded:: 3.28
%End
int fontWeight() const;
%Docstring
Returns the font weight, or -1 if the font weight is not set
and should be inherited.
.. seealso:: :py:func:`setFontWeight`
.. versionadded:: 3.28
%End
void setFontWeight( int fontWeight );
%Docstring
Sets the font ``weight``.
Set ``weight`` to -1 if the font weight is not set
and should be inherited.
.. seealso:: :py:func:`fontWeight`
.. versionadded:: 3.28
%End
BooleanValue italic() const;
%Docstring
Returns whether the format has italic enabled.
.. seealso:: :py:func:`setItalic`
.. versionadded:: 3.28
%End
void setItalic( BooleanValue enabled );
%Docstring
Sets whether the format has italic ``enabled``.
.. seealso:: :py:func:`italic`
.. versionadded:: 3.28
%End
BooleanValue strikeOut() const;
%Docstring

View File

@ -19,12 +19,10 @@
QgsTextCharacterFormat::QgsTextCharacterFormat( const QTextCharFormat &format )
: mTextColor( format.hasProperty( QTextFormat::ForegroundBrush ) ? format.foreground().color() : QColor() )
#if 0 // settings which affect font metrics are disabled for now
, mFontWeight( format.hasProperty( QTextFormat::FontWeight ) ? format.fontWeight() : -1 )
, mItalic( format.hasProperty( QTextFormat::FontItalic ) ? ( format.fontItalic() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
, mFontPointSize( format.hasProperty( QTextFormat::FontPointSize ) ? format.fontPointSize() : - 1 )
, mFontFamily( format.hasProperty( QTextFormat::FontFamily ) ? format.fontFamily() : QString() )
#endif
, mStrikethrough( format.hasProperty( QTextFormat::FontStrikeOut ) ? ( format.fontStrikeOut() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
, mUnderline( format.hasProperty( QTextFormat::FontUnderline ) ? ( format.fontUnderline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
, mOverline( format.hasProperty( QTextFormat::FontOverline ) ? ( format.fontOverline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
@ -42,6 +40,26 @@ void QgsTextCharacterFormat::setTextColor( const QColor &textColor )
mTextColor = textColor;
}
double QgsTextCharacterFormat::fontPointSize() const
{
return mFontPointSize;
}
void QgsTextCharacterFormat::setFontPointSize( double size )
{
mFontPointSize = size;
}
QString QgsTextCharacterFormat::family() const
{
return mFontFamily;
}
void QgsTextCharacterFormat::setFamily( const QString &family )
{
mFontFamily = family;
}
QgsTextCharacterFormat::BooleanValue QgsTextCharacterFormat::strikeOut() const
{
return mStrikethrough;
@ -75,7 +93,7 @@ void QgsTextCharacterFormat::setOverline( QgsTextCharacterFormat::BooleanValue e
void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const double scaleFactor ) const
{
Q_UNUSED( scaleFactor );
#if 0 // settings which affect font metrics are disabled for now
if ( mItalic != QgsTextCharacterFormat::BooleanValue::NotSet )
font.setItalic( mItalic == QgsTextCharacterFormat::BooleanValue::SetTrue );
if ( mFontWeight != -1 )
@ -84,8 +102,6 @@ void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const double scal
font.setFamily( mFontFamily );
if ( mFontPointSize != -1 )
font.setPointSizeF( mFontPointSize );
#endif
if ( mUnderline != BooleanValue::NotSet )
font.setUnderline( mUnderline == QgsTextCharacterFormat::BooleanValue::SetTrue );
if ( mOverline != BooleanValue::NotSet )
@ -94,7 +110,6 @@ void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const double scal
font.setStrikeOut( mStrikethrough == QgsTextCharacterFormat::BooleanValue::SetTrue );
}
#if 0 // settings which affect font metrics are disabled for now
QgsTextCharacterFormat::BooleanValue QgsTextCharacterFormat::italic() const
{
return mItalic;
@ -114,4 +129,3 @@ void QgsTextCharacterFormat::setFontWeight( int fontWeight )
{
mFontWeight = fontWeight;
}
#endif

View File

@ -76,13 +76,51 @@ class CORE_EXPORT QgsTextCharacterFormat
*/
void setTextColor( const QColor &textColor );
#if 0
/**
* Returns the font point size, or -1 if the font size is not set
* and should be inherited.
*
* \see setFontPointSize()
* \since QGIS 3.28
*/
double fontPointSize() const;
/**
* Sets the font point \a size.
*
* Set \a size to -1 if the font size is not set
* and should be inherited.
*
* \see fontPointSize()
* \since QGIS 3.28
*/
void setFontPointSize( double size );
/**
* Returns the font family name, or an empty string if the
* family is not set and should be inherited.
*
* \see setFamily()
* \since QGIS 3.28
*/
QString family() const;
/**
* Sets the font \a family name.
*
* Set to an empty string if the family should be inherited.
*
* \see family()
* \since QGIS 3.28
*/
void setFamily( const QString &family );
/**
* Returns the font weight, or -1 if the font weight is not set
* and should be inherited.
*
* \see setFontWeight()
* \since QGIS 3.28
*/
int fontWeight() const;
@ -93,6 +131,7 @@ class CORE_EXPORT QgsTextCharacterFormat
* and should be inherited.
*
* \see fontWeight()
* \since QGIS 3.28
*/
void setFontWeight( int fontWeight );
@ -100,6 +139,7 @@ class CORE_EXPORT QgsTextCharacterFormat
* Returns whether the format has italic enabled.
*
* \see setItalic()
* \since QGIS 3.28
*/
BooleanValue italic() const;
@ -107,9 +147,9 @@ class CORE_EXPORT QgsTextCharacterFormat
* Sets whether the format has italic \a enabled.
*
* \see italic()
* \since QGIS 3.28
*/
void setItalic( BooleanValue enabled );
#endif
/**
* Returns whether the format has strikethrough enabled.
@ -167,14 +207,10 @@ class CORE_EXPORT QgsTextCharacterFormat
private:
QColor mTextColor;
#if 0 // settings which affect font metrics are disabled for now
int mFontWeight = -1;
BooleanValue mItalic = BooleanValue::NotSet;
double mFontPointSize = -1;
QString mFontFamily;
#endif
BooleanValue mStrikethrough = BooleanValue::NotSet;
BooleanValue mUnderline = BooleanValue::NotSet;
BooleanValue mOverline = BooleanValue::NotSet;

View File

@ -36,6 +36,8 @@ class TestQgsTextCharacterFormat(unittest.TestCase):
self.assertEqual(format.underline(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertEqual(format.strikeOut(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertEqual(format.overline(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertEqual(format.fontPointSize(), -1)
self.assertFalse(format.family())
format.setTextColor(QColor(255, 0, 0))
self.assertTrue(format.textColor().isValid())
@ -50,14 +52,26 @@ class TestQgsTextCharacterFormat(unittest.TestCase):
format.setOverline(QgsTextCharacterFormat.BooleanValue.SetTrue)
self.assertEqual(format.overline(), QgsTextCharacterFormat.BooleanValue.SetTrue)
format.setFontPointSize(12.5)
self.assertEqual(format.fontPointSize(), 12.5)
format.setFamily('comic sans')
self.assertEqual(format.family(), 'comic sans')
def testUpdateFont(self):
font = QgsFontUtils.getStandardTestFont()
old_size = font.pointSizeF()
old_family = font.family()
format = QgsTextCharacterFormat()
format.updateFontForFormat(font)
self.assertFalse(font.underline())
self.assertFalse(font.strikeOut())
self.assertFalse(font.overline())
self.assertEqual(font.family(), old_family)
self.assertEqual(font.pointSizeF(), old_size)
format.setUnderline(QgsTextCharacterFormat.BooleanValue.SetTrue)
format.updateFontForFormat(font)
@ -89,6 +103,24 @@ class TestQgsTextCharacterFormat(unittest.TestCase):
format.setOverline(QgsTextCharacterFormat.BooleanValue.SetFalse)
format.updateFontForFormat(font)
self.assertFalse(font.overline())
self.assertEqual(font.pointSizeF(), old_size)
format.setFontPointSize(49)
format.updateFontForFormat(font)
self.assertEqual(font.pointSizeF(), 49)
format.setFontPointSize(-1)
font.setPointSizeF(old_size)
format.updateFontForFormat(font)
self.assertEqual(font.pointSizeF(), old_size)
self.assertEqual(font.family(), old_family)
format.setFamily('Serif')
format.updateFontForFormat(font)
self.assertEqual(font.family(), 'Serif')
format.setFamily('')
font.setFamily(old_family)
format.updateFontForFormat(font)
self.assertEqual(font.family(), old_family)
if __name__ == '__main__':

View File

@ -57,19 +57,31 @@ class TestQgsTextDocument(unittest.TestCase):
self.assertEqual(doc[2][0].text(), 'e')
def testFromHtml(self):
doc = QgsTextDocument.fromHtml(['abc<div style="color: red"><b style="text-decoration: underline">def</b> ghi<div>jkl</div></div>', 'b c d', 'e'])
doc = QgsTextDocument.fromHtml(['abc<div style="color: red"><b style="text-decoration: underline; font-style: italic; font-size: 15pt; font-family: Serif">def</b> ghi<div>jkl</div></div>', 'b c d', 'e'])
self.assertEqual(len(doc), 5)
self.assertEqual(len(doc[0]), 1)
self.assertEqual(doc[0][0].text(), 'abc')
self.assertEqual(doc[0][0].characterFormat().underline(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertEqual(doc[0][0].characterFormat().italic(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertEqual(doc[0][0].characterFormat().fontWeight(), -1)
self.assertFalse(doc[0][0].characterFormat().family())
self.assertEqual(doc[0][0].characterFormat().fontPointSize(), -1)
self.assertFalse(doc[0][0].characterFormat().textColor().isValid())
self.assertEqual(len(doc[1]), 2)
self.assertEqual(doc[1][0].text(), 'def')
self.assertEqual(doc[1][0].characterFormat().underline(), QgsTextCharacterFormat.BooleanValue.SetTrue)
self.assertEqual(doc[1][0].characterFormat().italic(), QgsTextCharacterFormat.BooleanValue.SetTrue)
self.assertEqual(doc[1][0].characterFormat().fontWeight(), 75)
self.assertEqual(doc[1][0].characterFormat().family(), 'Serif')
self.assertEqual(doc[1][0].characterFormat().textColor().name(), '#ff0000')
self.assertEqual(doc[1][0].characterFormat().fontPointSize(), 15)
self.assertEqual(doc[1][1].text(), ' ghi')
self.assertEqual(doc[1][1].characterFormat().underline(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertEqual(doc[1][1].characterFormat().italic(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertEqual(doc[1][1].characterFormat().fontWeight(), -1)
self.assertFalse(doc[1][1].characterFormat().family())
self.assertEqual(doc[1][1].characterFormat().textColor().name(), '#ff0000')
self.assertEqual(doc[1][1].characterFormat().fontPointSize(), -1)
self.assertEqual(len(doc[2]), 1)
self.assertEqual(doc[2][0].text(), 'jkl')
self.assertEqual(len(doc[3]), 1)