QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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
18#include "qgsstringutils.h"
19
20#include <QFontMetricsF>
21#include <QString>
22#include <QTextFragment>
23
24using namespace Qt::StringLiterals;
25
27 : mText( text != u"\ufffc"_s ? text : QString() )
28 , mIsImage( text == u"\ufffc"_s )
29 , mCharFormat( format )
30{}
31
32QgsTextFragment::QgsTextFragment( const QTextFragment &fragment )
33 : mText( fragment.text() != u"\ufffc"_s ? fragment.text() : QString() )
34 , mIsImage( fragment.text() == u"\ufffc"_s )
35 , mCharFormat( fragment.charFormat() )
36{}
37
38QString QgsTextFragment::text() const
39{
40 return mText;
41}
42
43void QgsTextFragment::setText( const QString &text )
44{
45 mText = text;
46}
47
49{
50 mCharFormat = charFormat;
51}
52
54{
55 return mIsImage;
56}
57
58double QgsTextFragment::horizontalAdvance( const QFont &font, const QgsRenderContext &context, bool fontHasBeenUpdatedForFragment, double scaleFactor ) const
59{
60 if ( fontHasBeenUpdatedForFragment )
61 {
62 const QFontMetricsF fm( font );
63 return fm.horizontalAdvance( mText );
64 }
65 else
66 {
67 QFont updatedFont = font;
68 mCharFormat.updateFontForFormat( updatedFont, context, scaleFactor );
69 const QFontMetricsF fm( updatedFont );
70 return fm.horizontalAdvance( mText );
71 }
72}
73
75{
76 mText = QgsStringUtils::capitalize( mText, capitalization );
77}
Capitalization
String capitalization options.
Definition qgis.h:3503
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 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.