QGIS API Documentation 3.41.0-Master (af5edcb665c)
Loading...
Searching...
No Matches
qgstextblockformat.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgstextblockformat.cpp
3 -----------------
4 begin : September 2024
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 "qgstextblockformat.h"
17#include "qgsrendercontext.h"
18
19#include <QTextBlockFormat>
20
21
22Qgis::TextHorizontalAlignment convertTextBlockFormatAlign( const QTextBlockFormat &format, bool &set )
23{
24 set = format.hasProperty( QTextFormat::BlockAlignment );
25 if ( format.alignment() & Qt::AlignLeft )
26 {
28 }
29 else if ( format.alignment() & Qt::AlignRight )
30 {
32 }
33 else if ( format.alignment() & Qt::AlignHCenter )
34 {
36 }
37 else if ( format.alignment() & Qt::AlignJustify )
38 {
40 }
41
42 set = false;
44}
45
46QgsTextBlockFormat::QgsTextBlockFormat( const QTextBlockFormat &format )
47 : mBackgroundBrush( format.background() )
48 , mBackgroundPath( format.stringProperty( QTextFormat::BackgroundImageUrl ) )
49 , mLineHeight( format.hasProperty( QTextFormat::LineHeight ) && format.lineHeightType() != QTextBlockFormat::ProportionalHeight ? format.lineHeight() : std::numeric_limits< double >::quiet_NaN() )
50 , mLineHeightPercentage( format.hasProperty( QTextFormat::LineHeight ) && format.lineHeightType() == QTextBlockFormat::ProportionalHeight ? ( format.lineHeight() / 100.0 ) : std::numeric_limits< double >::quiet_NaN() )
51{
52 mHorizontalAlign = convertTextBlockFormatAlign( format, mHasHorizontalAlignSet );
53
54 const double topMargin = format.hasProperty( QTextFormat::BlockTopMargin ) ? format.topMargin() : std::numeric_limits< double >::quiet_NaN();
55 const double leftMargin = format.hasProperty( QTextFormat::BlockLeftMargin ) ? format.leftMargin() : std::numeric_limits< double >::quiet_NaN();
56 const double rightMargin = format.hasProperty( QTextFormat::BlockRightMargin ) ? format.rightMargin() : std::numeric_limits< double >::quiet_NaN();
57 const double bottomMargin = format.hasProperty( QTextFormat::BlockBottomMargin ) ? format.bottomMargin() : std::numeric_limits< double >::quiet_NaN();
58 mMargins = QgsMargins( leftMargin, topMargin, rightMargin, bottomMargin );
59}
60
62{
63 if ( mHasHorizontalAlignSet && other.hasHorizontalAlignmentSet() )
64 {
65 mHorizontalAlign = other.mHorizontalAlign;
66 mHasHorizontalAlignSet = true;
67 }
68
69 if ( std::isnan( mLineHeight ) )
70 mLineHeight = other.mLineHeight;
71 if ( std::isnan( mLineHeightPercentage ) )
72 mLineHeightPercentage = other.mLineHeightPercentage;
73
74 if ( std::isnan( mMargins.left() ) )
75 mMargins.setLeft( other.mMargins.left() );
76 if ( std::isnan( mMargins.right() ) )
77 mMargins.setRight( other.mMargins.right() );
78 if ( std::isnan( mMargins.top() ) )
79 mMargins.setTop( other.mMargins.top() );
80 if ( std::isnan( mMargins.bottom() ) )
81 mMargins.setBottom( other.mMargins.bottom() );
82}
83
85{
86 return mLineHeight;
87}
88
90{
91 mLineHeight = height;
92}
93
95{
96 return mLineHeightPercentage;
97}
98
100{
101 mLineHeightPercentage = height;
102}
103
104void QgsTextBlockFormat::updateFontForFormat( QFont &, const QgsRenderContext &, const double ) const
105{
106
107}
108
110{
111 return mBackgroundBrush.style() != Qt::NoBrush || !mBackgroundPath.isEmpty();
112}
113
115{
116 return mBackgroundBrush;
117}
118
119void QgsTextBlockFormat::setBackgroundBrush( const QBrush &brush )
120{
121 mBackgroundBrush = brush;
122}
123
125{
126 return mBackgroundPath;
127}
128
130{
131 mBackgroundPath = path;
132}
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:2803
The QgsMargins class defines the four margins of a rectangle.
Definition qgsmargins.h:37
void setBottom(double bottom)
Sets the bottom margin to bottom.
Definition qgsmargins.h:113
double top() const
Returns the top margin.
Definition qgsmargins.h:77
void setLeft(double left)
Sets the left margin to left.
Definition qgsmargins.h:95
double right() const
Returns the right margin.
Definition qgsmargins.h:83
double bottom() const
Returns the bottom margin.
Definition qgsmargins.h:89
void setRight(double right)
Sets the right margin to right.
Definition qgsmargins.h:107
void setTop(double top)
Sets the top margin to top.
Definition qgsmargins.h:101
double left() const
Returns the left margin.
Definition qgsmargins.h:71
Contains information about the context of a rendering operation.
Stores information relating to individual block formatting.
bool hasBackground() const
Returns true if the block has a background set.
QBrush backgroundBrush() const
Returns the brush used for rendering the background of the block.
double lineHeight() const
Returns the line height in points, or NaN if the line height is not set and should be auto calculated...
double lineHeightPercentage() const
Returns the line height percentage size (as fraction of font size from 0.0 to 1.0),...
void overrideWith(const QgsTextBlockFormat &other)
Override all the default/unset properties of the current block format with the settings from another ...
void updateFontForFormat(QFont &font, const QgsRenderContext &context, double scaleFactor=1.0) const
Updates the specified font in place, applying block formatting options which are applicable on a font...
QString backgroundImagePath() const
Returns the path for the image to be used for rendering the background of the fragment.
void setBackgroundImagePath(const QString &path)
Sets the path for the image to be used for rendering the background of the fragment.
bool hasHorizontalAlignmentSet() const
Returns true if the format has an explicit horizontal alignment set.
void setLineHeightPercentage(double height)
Sets the line height percentage height (as fraction of font size from 0.0 to 1.0).
QgsTextBlockFormat()=default
void setLineHeight(double height)
Sets the font line height, in points.
void setBackgroundBrush(const QBrush &brush)
Sets the brush used for rendering the background of the block.
Qgis::TextHorizontalAlignment convertTextBlockFormatAlign(const QTextBlockFormat &format, bool &set)