QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
QgsTextFragment::setText
void setText(const QString &text)
Sets the text content of the fragment.
Definition: qgstextfragment.cpp:38
QgsStringUtils::capitalize
static QString capitalize(const QString &string, Qgis::Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Definition: qgsstringutils.cpp:24
qgstextfragment.h
QgsTextFragment::applyCapitalization
void applyCapitalization(Qgis::Capitalization capitalization)
Applies a capitalization style to the fragment's text.
Definition: qgstextfragment.cpp:64
qgsstringutils.h
QgsTextFragment::text
QString text() const
Returns the text content of the fragment.
Definition: qgstextfragment.cpp:33
QgsTextCharacterFormat
Stores information relating to individual character formatting.
Definition: qgstextcharacterformat.h:39
QgsTextFragment::setCharacterFormat
void setCharacterFormat(const QgsTextCharacterFormat &format)
Sets the character format for the fragment.
Definition: qgstextfragment.cpp:43
Qgis::Capitalization
Capitalization
String capitalization options.
Definition: qgis.h:1592
QgsTextCharacterFormat::updateFontForFormat
void updateFontForFormat(QFont &font, double scaleFactor=1.0) const
Updates the specified font in place, applying character formatting options which are applicable on a ...
Definition: qgstextcharacterformat.cpp:75
QgsTextFragment::horizontalAdvance
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 ...
Definition: qgstextfragment.cpp:48
QgsTextFragment::QgsTextFragment
QgsTextFragment(const QString &text=QString(), const QgsTextCharacterFormat &format=QgsTextCharacterFormat())
Constructor for QgsTextFragment, with the specified text and optional character format.
Definition: qgstextfragment.cpp:21