QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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 : mLineHeight( format.hasProperty( QTextFormat::LineHeight ) && format.lineHeightType() != QTextBlockFormat::ProportionalHeight ? format.lineHeight() : std::numeric_limits< double >::quiet_NaN() )
48 , mLineHeightPercentage( format.hasProperty( QTextFormat::LineHeight ) && format.lineHeightType() == QTextBlockFormat::ProportionalHeight ? ( format.lineHeight() / 100.0 ) : std::numeric_limits< double >::quiet_NaN() )
49{
50 mHorizontalAlign = convertTextBlockFormatAlign( format, mHasHorizontalAlignSet );
51
52 const double topMargin = format.hasProperty( QTextFormat::BlockTopMargin ) ? format.topMargin() : std::numeric_limits< double >::quiet_NaN();
53 const double leftMargin = format.hasProperty( QTextFormat::BlockLeftMargin ) ? format.leftMargin() : std::numeric_limits< double >::quiet_NaN();
54 const double rightMargin = format.hasProperty( QTextFormat::BlockRightMargin ) ? format.rightMargin() : std::numeric_limits< double >::quiet_NaN();
55 const double bottomMargin = format.hasProperty( QTextFormat::BlockBottomMargin ) ? format.bottomMargin() : std::numeric_limits< double >::quiet_NaN();
56 mMargins = QgsMargins( leftMargin, topMargin, rightMargin, bottomMargin );
57}
58
60{
61 if ( mHasHorizontalAlignSet && other.hasHorizontalAlignmentSet() )
62 {
63 mHorizontalAlign = other.mHorizontalAlign;
64 mHasHorizontalAlignSet = true;
65 }
66
67 if ( std::isnan( mLineHeight ) )
68 mLineHeight = other.mLineHeight;
69 if ( std::isnan( mLineHeightPercentage ) )
70 mLineHeightPercentage = other.mLineHeightPercentage;
71
72 if ( std::isnan( mMargins.left() ) )
73 mMargins.setLeft( other.mMargins.left() );
74 if ( std::isnan( mMargins.right() ) )
75 mMargins.setRight( other.mMargins.right() );
76 if ( std::isnan( mMargins.top() ) )
77 mMargins.setTop( other.mMargins.top() );
78 if ( std::isnan( mMargins.bottom() ) )
79 mMargins.setBottom( other.mMargins.bottom() );
80}
81
83{
84 return mLineHeight;
85}
86
88{
89 mLineHeight = height;
90}
91
93{
94 return mLineHeightPercentage;
95}
96
98{
99 mLineHeightPercentage = height;
100}
101
102void QgsTextBlockFormat::updateFontForFormat( QFont &, const QgsRenderContext &, const double ) const
103{
104
105}
TextHorizontalAlignment
Text horizontal alignment.
Definition qgis.h:2740
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.
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...
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.
Qgis::TextHorizontalAlignment convertTextBlockFormatAlign(const QTextBlockFormat &format, bool &set)