QGIS API Documentation  3.24.2-Tisler (13c1a02865)
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 
21 QgsTextFragment::QgsTextFragment( const QString &text, const QgsTextCharacterFormat &format )
22  : mText( text )
23  , mCharFormat( format )
24 {}
25 
26 QgsTextFragment::QgsTextFragment( const QTextFragment &fragment )
27  : mText( fragment.text() )
28  , mCharFormat( QgsTextCharacterFormat( fragment.charFormat() ) )
29 {
30 
31 }
32 
33 QString QgsTextFragment::text() const
34 {
35  return mText;
36 }
37 
38 void QgsTextFragment::setText( const QString &text )
39 {
40  mText = text;
41 }
42 
44 {
45  mCharFormat = charFormat;
46 }
47 
48 double QgsTextFragment::horizontalAdvance( const QFont &font, bool fontHasBeenUpdatedForFragment, double scaleFactor ) const
49 {
50  if ( fontHasBeenUpdatedForFragment )
51  {
52  const QFontMetricsF fm( font );
53  return fm.horizontalAdvance( mText );
54  }
55  else
56  {
57  QFont updatedFont = font;
58  mCharFormat.updateFontForFormat( updatedFont, scaleFactor );
59  const QFontMetricsF fm( updatedFont );
60  return fm.horizontalAdvance( mText );
61  }
62 }
63 
65 {
66  mText = QgsStringUtils::capitalize( mText, capitalization );
67 }
68 
Capitalization
String capitalization options.
Definition: qgis.h:1236
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, 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.
double horizontalAdvance(const QFont &font, 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.