QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgscolorramplegendnodesettings.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscolorramplegendnode.h
3  --------------------------------------
4  Date : December 2020
5  Copyright : (C) 2020 by 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 
17 #include "qgsnumericformat.h"
18 #include "qgsbasicnumericformat.h"
19 #include "qgsapplication.h"
21 #include "qgis.h"
22 
24  : mNumericFormat( std::make_unique< QgsBasicNumericFormat >() )
25 {
26 }
27 
29  : mUseContinuousLegend( other.mUseContinuousLegend )
30  , mMinimumLabel( other.mMinimumLabel )
31  , mMaximumLabel( other.mMaximumLabel )
32  , mPrefix( other.mPrefix )
33  , mSuffix( other.mSuffix )
34  , mDirection( other.mDirection )
35  , mNumericFormat( other.numericFormat()->clone() )
36  , mTextFormat( other.textFormat() )
37  , mOrientation( other.mOrientation )
38 {
39 
40 }
41 
43 {
44  mUseContinuousLegend = other.mUseContinuousLegend;
45  mMinimumLabel = other.mMinimumLabel;
46  mMaximumLabel = other.mMaximumLabel;
47  mPrefix = other.mPrefix;
48  mSuffix = other.mSuffix;
49  mDirection = other.mDirection;
50  mNumericFormat.reset( other.numericFormat()->clone() );
51  mTextFormat = other.mTextFormat;
52  mOrientation = other.mOrientation;
53  return *this;
54 }
55 
57 
59 {
60  return mDirection;
61 }
62 
64 {
65  mDirection = direction;
66 }
67 
69 {
70  return mMinimumLabel;
71 }
72 
74 {
75  mMinimumLabel = label;
76 }
77 
79 {
80  return mMaximumLabel;
81 }
82 
84 {
85  mMaximumLabel = label;
86 }
87 
89 {
90  return mNumericFormat.get();
91 }
92 
94 {
95  mNumericFormat.reset( format );
96 }
97 
98 void QgsColorRampLegendNodeSettings::writeXml( QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context ) const
99 {
100  QDomElement settingsElement = doc.createElement( QStringLiteral( "rampLegendSettings" ) );
101 
102  settingsElement.setAttribute( QStringLiteral( "useContinuousLegend" ), mUseContinuousLegend );
103  settingsElement.setAttribute( QStringLiteral( "minimumLabel" ), mMinimumLabel );
104  settingsElement.setAttribute( QStringLiteral( "maximumLabel" ), mMaximumLabel );
105  settingsElement.setAttribute( QStringLiteral( "prefix" ), mPrefix );
106  settingsElement.setAttribute( QStringLiteral( "suffix" ), mSuffix );
107  settingsElement.setAttribute( QStringLiteral( "direction" ), static_cast< int >( mDirection ) );
108  settingsElement.setAttribute( QStringLiteral( "orientation" ), static_cast< int >( mOrientation ) );
109 
110  QDomElement numericFormatElem = doc.createElement( QStringLiteral( "numericFormat" ) );
111  mNumericFormat->writeXml( numericFormatElem, doc, context );
112  settingsElement.appendChild( numericFormatElem );
113 
114  if ( mTextFormat.isValid() )
115  {
116  settingsElement.appendChild( mTextFormat.writeXml( doc, context ) );
117  }
118 
119  element.appendChild( settingsElement );
120 }
121 
122 void QgsColorRampLegendNodeSettings::readXml( const QDomElement &element, const QgsReadWriteContext &context )
123 {
124  const QDomElement settingsElement = element.firstChildElement( QStringLiteral( "rampLegendSettings" ) );
125  if ( !settingsElement.isNull() )
126  {
127  mUseContinuousLegend = settingsElement.attribute( QStringLiteral( "useContinuousLegend" ), QStringLiteral( "1" ) ).toInt( );
128  mMinimumLabel = settingsElement.attribute( QStringLiteral( "minimumLabel" ) );
129  mMaximumLabel = settingsElement.attribute( QStringLiteral( "maximumLabel" ) );
130  mPrefix = settingsElement.attribute( QStringLiteral( "prefix" ) );
131  mSuffix = settingsElement.attribute( QStringLiteral( "suffix" ) );
132  mDirection = static_cast< QgsColorRampLegendNodeSettings::Direction >( settingsElement.attribute( QStringLiteral( "direction" ) ).toInt() );
133  mOrientation = static_cast< Qt::Orientation >( settingsElement.attribute( QStringLiteral( "orientation" ), QString::number( Qt::Vertical ) ).toInt() );
134 
135  const QDomNodeList numericFormatNodeList = settingsElement.elementsByTagName( QStringLiteral( "numericFormat" ) );
136  if ( !numericFormatNodeList.isEmpty() )
137  {
138  const QDomElement numericFormatElem = numericFormatNodeList.at( 0 ).toElement();
139  mNumericFormat.reset( QgsApplication::numericFormatRegistry()->createFromXml( numericFormatElem, context ) );
140  }
141 
142  if ( !settingsElement.firstChildElement( QStringLiteral( "text-style" ) ).isNull() )
143  {
144  mTextFormat.readXml( settingsElement, context );
145  }
146  else
147  {
148  mTextFormat = QgsTextFormat();
149  }
150  }
151 }
152 
154 {
155  return mPrefix;
156 }
157 
158 void QgsColorRampLegendNodeSettings::setPrefix( const QString &prefix )
159 {
160  mPrefix = prefix;
161 }
162 
164 {
165  return mSuffix;
166 }
167 
168 void QgsColorRampLegendNodeSettings::setSuffix( const QString &suffix )
169 {
170  mSuffix = suffix;
171 }
172 
174 {
175  return mTextFormat;
176 }
177 
179 {
180  mTextFormat = format;
181 }
182 
184 {
185  return mOrientation;
186 }
187 
188 void QgsColorRampLegendNodeSettings::setOrientation( Qt::Orientation orientation )
189 {
190  mOrientation = orientation;
191 }
192 
194 {
195  return mUseContinuousLegend;
196 }
197 
199 {
200  mUseContinuousLegend = useContinuousLegend;
201 }
QgsColorRampLegendNodeSettings::setOrientation
void setOrientation(Qt::Orientation orientation)
Sets the ramp orientation (i.e.
Definition: qgscolorramplegendnodesettings.cpp:188
qgsnumericformatregistry.h
QgsColorRampLegendNodeSettings::setUseContinuousLegend
void setUseContinuousLegend(bool useContinuousLegend)
Sets the flag to use a continuos gradient legend to useContinuousLegend.
Definition: qgscolorramplegendnodesettings.cpp:198
QgsColorRampLegendNodeSettings::textFormat
QgsTextFormat textFormat() const
Returns the text format used to render text in the legend item.
Definition: qgscolorramplegendnodesettings.cpp:173
QgsColorRampLegendNodeSettings::QgsColorRampLegendNodeSettings
QgsColorRampLegendNodeSettings()
Definition: qgscolorramplegendnodesettings.cpp:23
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
qgis.h
QgsNumericFormat
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
Definition: qgsnumericformat.h:259
QgsBasicNumericFormat
A numeric formatter which returns a simple text representation of a value.
Definition: qgsbasicnumericformat.h:31
QgsColorRampLegendNodeSettings::Direction
Direction
Ramp directions.
Definition: qgscolorramplegendnodesettings.h:44
QgsColorRampLegendNodeSettings::setTextFormat
void setTextFormat(const QgsTextFormat &format)
Sets the text format used to render text in the legend item.
Definition: qgscolorramplegendnodesettings.cpp:178
QgsColorRampLegendNodeSettings::numericFormat
const QgsNumericFormat * numericFormat() const
Returns the numeric format used for numbers in the scalebar.
Definition: qgscolorramplegendnodesettings.cpp:88
qgsapplication.h
QgsTextFormat
Container for all settings relating to text rendering.
Definition: qgstextformat.h:40
QgsColorRampLegendNodeSettings::operator=
QgsColorRampLegendNodeSettings & operator=(const QgsColorRampLegendNodeSettings &other)
Definition: qgscolorramplegendnodesettings.cpp:42
QgsColorRampLegendNodeSettings::setSuffix
void setSuffix(const QString &suffix)
Sets the suffix to show after legend text.
Definition: qgscolorramplegendnodesettings.cpp:168
QgsColorRampLegendNodeSettings::minimumLabel
QString minimumLabel() const
Returns the label for the minimum value on the ramp.
Definition: qgscolorramplegendnodesettings.cpp:68
QgsColorRampLegendNodeSettings::writeXml
void writeXml(QDomDocument &doc, QDomElement &element, const QgsReadWriteContext &context) const
Writes settings to an XML element.
Definition: qgscolorramplegendnodesettings.cpp:98
QgsColorRampLegendNodeSettings::setPrefix
void setPrefix(const QString &prefix)
Sets the prefix to show before legend text.
Definition: qgscolorramplegendnodesettings.cpp:158
QgsColorRampLegendNodeSettings::prefix
QString prefix() const
Returns the prefix to show before legend text.
Definition: qgscolorramplegendnodesettings.cpp:153
QgsTextFormat::readXml
void readXml(const QDomElement &elem, const QgsReadWriteContext &context)
Read settings from a DOM element.
Definition: qgstextformat.cpp:488
qgscolorramplegendnodesettings.h
QgsColorRampLegendNodeSettings::~QgsColorRampLegendNodeSettings
~QgsColorRampLegendNodeSettings()
QgsTextFormat::isValid
bool isValid() const
Returns true if the format is valid.
Definition: qgstextformat.cpp:102
QgsColorRampLegendNodeSettings::useContinuousLegend
bool useContinuousLegend() const
Returns true if a continuous gradient legend will be used.
Definition: qgscolorramplegendnodesettings.cpp:193
qgsnumericformat.h
qgsbasicnumericformat.h
QgsColorRampLegendNodeSettings::direction
QgsColorRampLegendNodeSettings::Direction direction() const
Returns the direction of the ramp.
Definition: qgscolorramplegendnodesettings.cpp:58
QgsColorRampLegendNodeSettings::setDirection
void setDirection(QgsColorRampLegendNodeSettings::Direction direction)
Sets the direction of the ramp.
Definition: qgscolorramplegendnodesettings.cpp:63
QgsColorRampLegendNodeSettings::setMinimumLabel
void setMinimumLabel(const QString &label)
Sets the label for the minimum value on the ramp.
Definition: qgscolorramplegendnodesettings.cpp:73
QgsApplication::numericFormatRegistry
static QgsNumericFormatRegistry * numericFormatRegistry()
Gets the registry of available numeric formats.
Definition: qgsapplication.cpp:2475
QgsColorRampLegendNodeSettings::maximumLabel
QString maximumLabel() const
Returns the label for the maximum value on the ramp.
Definition: qgscolorramplegendnodesettings.cpp:78
QgsColorRampLegendNodeSettings::readXml
void readXml(const QDomElement &element, const QgsReadWriteContext &context)
Reads settings from an XML element.
Definition: qgscolorramplegendnodesettings.cpp:122
QgsNumericFormat::clone
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
QgsColorRampLegendNodeSettings::suffix
QString suffix() const
Returns the suffix to show after legend text.
Definition: qgscolorramplegendnodesettings.cpp:163
QgsColorRampLegendNodeSettings::setMaximumLabel
void setMaximumLabel(const QString &label)
Sets the label for the maximum value on the ramp.
Definition: qgscolorramplegendnodesettings.cpp:83
QgsTextFormat::writeXml
QDomElement writeXml(QDomDocument &doc, const QgsReadWriteContext &context) const
Write settings into a DOM element.
Definition: qgstextformat.cpp:671
QgsNumericFormatRegistry::createFromXml
QgsNumericFormat * createFromXml(const QDomElement &element, const QgsReadWriteContext &context) const
Creates a new numeric format from an XML element.
Definition: qgsnumericformatregistry.cpp:82
QgsColorRampLegendNodeSettings::setNumericFormat
void setNumericFormat(QgsNumericFormat *format)
Sets the numeric format used for numbers in the scalebar.
Definition: qgscolorramplegendnodesettings.cpp:93
QgsColorRampLegendNodeSettings
Settings for a color ramp legend node.
Definition: qgscolorramplegendnodesettings.h:37
QgsColorRampLegendNodeSettings::orientation
Qt::Orientation orientation() const
Returns the ramp orientation (i.e.
Definition: qgscolorramplegendnodesettings.cpp:183