QGIS API Documentation 3.41.0-Master (3440c17df1d)
Loading...
Searching...
No Matches
qgstextfragment.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextfragment.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
16#include "qgstextfragment.h"
17#include <QFontMetricsF>
18#include <QTextFragment>
19#include "qgsstringutils.h"
20
21QgsTextFragment::QgsTextFragment( const QString &text, const QgsTextCharacterFormat &format )
22 : mText( text != QStringLiteral( "\ufffc" ) ? text : QString() )
23 , mIsImage( text == QStringLiteral( "\ufffc" ) )
24 , mCharFormat( format )
25{}
26
27QgsTextFragment::QgsTextFragment( const QTextFragment &fragment )
28 : mText( fragment.text() != QStringLiteral( "\ufffc" ) ? fragment.text() : QString() )
29 , mIsImage( fragment.text() == QStringLiteral( "\ufffc" ) )
30 , mCharFormat( fragment.charFormat() )
31{
32}
33
34QString QgsTextFragment::text() const
35{
36 return mText;
37}
38
39void QgsTextFragment::setText( const QString &text )
40{
41 mText = text;
42}
43
45{
46 mCharFormat = charFormat;
47}
48
50{
51 return mIsImage;
52}
53
54double QgsTextFragment::horizontalAdvance( const QFont &font, const QgsRenderContext &context, bool fontHasBeenUpdatedForFragment, double scaleFactor ) const
55{
56 if ( fontHasBeenUpdatedForFragment )
57 {
58 const QFontMetricsF fm( font );
59 return fm.horizontalAdvance( mText );
60 }
61 else
62 {
63 QFont updatedFont = font;
64 mCharFormat.updateFontForFormat( updatedFont, context, scaleFactor );
65 const QFontMetricsF fm( updatedFont );
66 return fm.horizontalAdvance( mText );
67 }
68}
69
71{
72 mText = QgsStringUtils::capitalize( mText, capitalization );
73}
74
Capitalization
String capitalization options.
Definition qgis.h:3140
Contains information about the context of a rendering operation.
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Stores information relating to individual character formatting.
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 setText(const QString &text)
Sets the text content of the fragment.
QString text() const
Returns the text content of the fragment.
void setCharacterFormat(const QgsTextCharacterFormat &format)
Sets the character format for the fragment.
bool isImage() const
Returns true if the fragment represents an image.
double horizontalAdvance(const QFont &font, const QgsRenderContext &context, bool fontHasBeenUpdatedForFragment=false, double scaleFactor=1.0) const
Returns the horizontal advance associated with this fragment, when rendered using the specified base ...
QgsTextFragment(const QString &text=QString(), const QgsTextCharacterFormat &format=QgsTextCharacterFormat())
Constructor for QgsTextFragment, with the specified text and optional character format.
void applyCapitalization(Qgis::Capitalization capitalization)
Applies a capitalization style to the fragment's text.