QGIS API Documentation  3.22.4-Białowieża (ce8e65e95e)
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 
20 QgsTextFragment::QgsTextFragment( const QString &text, const QgsTextCharacterFormat &format )
21  : mText( text )
22  , mCharFormat( format )
23 {}
24 
25 QgsTextFragment::QgsTextFragment( const QTextFragment &fragment )
26  : mText( fragment.text() )
27  , mCharFormat( QgsTextCharacterFormat( fragment.charFormat() ) )
28 {
29 
30 }
31 
32 QString QgsTextFragment::text() const
33 {
34  return mText;
35 }
36 
37 void QgsTextFragment::setText( const QString &text )
38 {
39  mText = text;
40 }
41 
43 {
44  mCharFormat = charFormat;
45 }
46 
47 double QgsTextFragment::horizontalAdvance( const QFont &font, bool fontHasBeenUpdatedForFragment, double scaleFactor ) const
48 {
49  if ( fontHasBeenUpdatedForFragment )
50  {
51  const QFontMetricsF fm( font );
52  return fm.horizontalAdvance( mText );
53  }
54  else
55  {
56  QFont updatedFont = font;
57  mCharFormat.updateFontForFormat( updatedFont, scaleFactor );
58  const QFontMetricsF fm( updatedFont );
59  return fm.horizontalAdvance( mText );
60  }
61 }
62 
64 {
65  mText = QgsStringUtils::capitalize( mText, capitalization );
66 }
67 
static QString capitalize(const QString &string, Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Capitalization
Capitalization options.
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 ...
void applyCapitalization(QgsStringUtils::Capitalization capitalization)
Applies a capitalization style to the fragment's text.
QgsTextFragment(const QString &text=QString(), const QgsTextCharacterFormat &format=QgsTextCharacterFormat())
Constructor for QgsTextFragment, with the specified text and optional character format.