QGIS API Documentation 3.28.0-Firenze (ed3ad0430f)
qgstextcharacterformat.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextcharacterformat.cpp
3 -----------------
4 begin : May 2020
5 copyright : (C) Nyall Dawson
6 email : nyall dot dawson at gmail dot com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
17#include "qgsrendercontext.h"
18#include "qgsfontutils.h"
19
20#include <QTextCharFormat>
21
22QgsTextCharacterFormat::QgsTextCharacterFormat( const QTextCharFormat &format )
23 : mTextColor( format.hasProperty( QTextFormat::ForegroundBrush ) ? format.foreground().color() : QColor() )
24 , mFontWeight( format.hasProperty( QTextFormat::FontWeight ) ? format.fontWeight() : -1 )
25 , mStyleName( format.font().styleName() )
26 , mItalic( format.hasProperty( QTextFormat::FontItalic ) ? ( format.fontItalic() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
27 , mFontPointSize( format.hasProperty( QTextFormat::FontPointSize ) ? format.fontPointSize() : - 1 )
28 , mFontFamily( format.hasProperty( QTextFormat::FontFamily ) ? format.fontFamily() : QString() )
29 , mStrikethrough( format.hasProperty( QTextFormat::FontStrikeOut ) ? ( format.fontStrikeOut() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
30 , mUnderline( format.hasProperty( QTextFormat::FontUnderline ) ? ( format.fontUnderline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
31 , mOverline( format.hasProperty( QTextFormat::FontOverline ) ? ( format.fontOverline() ? BooleanValue::SetTrue : BooleanValue::SetFalse ) : BooleanValue::NotSet )
32{
33
34}
35
37{
38 return mTextColor;
39}
40
41void QgsTextCharacterFormat::setTextColor( const QColor &textColor )
42{
43 mTextColor = textColor;
44}
45
47{
48 return mFontPointSize;
49}
50
52{
53 mFontPointSize = size;
54}
55
57{
58 return mFontFamily;
59}
60
61void QgsTextCharacterFormat::setFamily( const QString &family )
62{
63 mFontFamily = family;
64}
65
67{
68 return mStrikethrough;
69}
70
72{
73 mStrikethrough = strikethrough;
74}
75
77{
78 return mUnderline;
79}
80
82{
83 mUnderline = underline;
84}
85
87{
88 return mOverline;
89}
90
92{
93 mOverline = enabled;
94}
95
96void QgsTextCharacterFormat::updateFontForFormat( QFont &font, const QgsRenderContext &context, const double scaleFactor ) const
97{
98 // important -- MUST set family first
99 if ( !mFontFamily.isEmpty() )
100 font.setFamily( mFontFamily );
101
102 if ( mFontPointSize != -1 )
103 font.setPixelSize( scaleFactor * context.convertToPainterUnits( mFontPointSize, QgsUnitTypes::RenderPoints ) );
104
106 font.setItalic( mItalic == QgsTextCharacterFormat::BooleanValue::SetTrue );
107
108 if ( mFontWeight != - 1 )
109 {
110#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
111 font.setWeight( mFontWeight );
112#else
113 if ( mFontWeight <= 150 )
114 font.setWeight( QFont::Thin );
115 else if ( mFontWeight <= 250 )
116 font.setWeight( QFont::ExtraLight );
117 else if ( mFontWeight <= 350 )
118 font.setWeight( QFont::Light );
119 else if ( mFontWeight <= 450 )
120 font.setWeight( QFont::Normal );
121 else if ( mFontWeight <= 550 )
122 font.setWeight( QFont::Medium );
123 else if ( mFontWeight <= 650 )
124 font.setWeight( QFont::DemiBold );
125 else if ( mFontWeight <= 750 )
126 font.setWeight( QFont::Bold );
127 else if ( mFontWeight <= 850 )
128 font.setWeight( QFont::ExtraBold );
129 else
130 font.setWeight( QFont::Black );
131#endif
132
133 // depending on the font, platform, and the phase of the moon, we need to both set the font weight AND the style name
134 // in order to get correct rendering!
135 font.setStyleName( mStyleName );
136 }
137
138 if ( mUnderline != BooleanValue::NotSet )
139 font.setUnderline( mUnderline == QgsTextCharacterFormat::BooleanValue::SetTrue );
140 if ( mOverline != BooleanValue::NotSet )
141 font.setOverline( mOverline == QgsTextCharacterFormat::BooleanValue::SetTrue );
142 if ( mStrikethrough != QgsTextCharacterFormat::BooleanValue::NotSet )
143 font.setStrikeOut( mStrikethrough == QgsTextCharacterFormat::BooleanValue::SetTrue );
144}
145
147{
148 return mItalic;
149}
150
152{
153 mItalic = enabled;
154}
155
157{
158 return mFontWeight;
159}
160
162{
163 mFontWeight = fontWeight;
164}
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
void setFamily(const QString &family)
Sets the font family name.
void updateFontForFormat(QFont &font, const QgsRenderContext &context, double scaleFactor=1.0) const
Updates the specified font in place, applying character formatting options which are applicable on a ...
void setFontWeight(int fontWeight)
Sets the font weight.
QColor textColor() const
Returns the character's text color, or an invalid color if no color override is set and the default f...
BooleanValue italic() const
Returns whether the format has italic enabled.
QgsTextCharacterFormat()=default
Constructor for QgsTextCharacterFormat.
void setStrikeOut(BooleanValue enabled)
Sets whether the format has strikethrough enabled.
void setOverline(BooleanValue enabled)
Sets whether the format has overline enabled.
BooleanValue
Status values for boolean format properties.
@ SetTrue
Property is set and true.
int fontWeight() const
Returns the font weight, or -1 if the font weight is not set and should be inherited.
BooleanValue strikeOut() const
Returns whether the format has strikethrough enabled.
double fontPointSize() const
Returns the font point size, or -1 if the font size is not set and should be inherited.
QString family() const
Returns the font family name, or an empty string if the family is not set and should be inherited.
BooleanValue underline() const
Returns whether the format has underline enabled.
void setTextColor(const QColor &textColor)
Sets the character's text color.
void setUnderline(BooleanValue enabled)
Sets whether the format has underline enabled.
void setItalic(BooleanValue enabled)
Sets whether the format has italic enabled.
BooleanValue overline() const
Returns whether the format has overline enabled.
void setFontPointSize(double size)
Sets the font point size.
@ RenderPoints
Points (e.g., for font sizes)
Definition: qgsunittypes.h:173