QGIS API Documentation  3.16.0-Hannover (43b64b13f3)
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  QFontMetricsF fm( font );
52 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
53  return fm.width( mText );
54 #else
55  return fm.horizontalAdvance( mText );
56 #endif
57  }
58  else
59  {
60  QFont updatedFont = font;
61  mCharFormat.updateFontForFormat( updatedFont, scaleFactor );
62  QFontMetricsF fm( updatedFont );
63 #if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
64  return fm.width( mText );
65 #else
66  return fm.horizontalAdvance( mText );
67 #endif
68  }
69 }
70 
72 {
73  mText = QgsStringUtils::capitalize( mText, capitalization );
74 }
75 
QgsTextFragment::setText
void setText(const QString &text)
Sets the text content of the fragment.
Definition: qgstextfragment.cpp:37
qgstextfragment.h
QgsTextFragment::text
QString text() const
Returns the text content of the fragment.
Definition: qgstextfragment.cpp:32
QgsTextCharacterFormat
Stores information relating to individual character formatting.
Definition: qgstextcharacterformat.h:40
QgsStringUtils::Capitalization
Capitalization
Capitalization options.
Definition: qgsstringutils.h:189
QgsTextFragment::setCharacterFormat
void setCharacterFormat(const QgsTextCharacterFormat &format)
Sets the character format for the fragment.
Definition: qgstextfragment.cpp:42
QgsStringUtils::capitalize
static QString capitalize(const QString &string, Capitalization capitalization)
Converts a string by applying capitalization rules to the string.
Definition: qgsstringutils.cpp:25
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::applyCapitalization
void applyCapitalization(QgsStringUtils::Capitalization capitalization)
Applies a capitalization style to the fragment's text.
Definition: qgstextfragment.cpp:71
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:47
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:20