QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsnumericformatregistry.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsnumericformatregistry.cpp
3 ----------------------------
4 begin : January 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 ***************************************************************************/
16
24#include "qgsnumericformat.h"
27#include "qgsxmlutils.h"
28
29#include <QString>
30
31using namespace Qt::StringLiterals;
32
45
47{
48 qDeleteAll( mFormats );
49}
50
52{
53 return mFormats.keys();
54}
55
57{
58 if ( !format )
59 return;
60
61 mFormats.insert( format->id(), format );
62}
63
65{
66 if ( QgsNumericFormat *format = mFormats.take( id ) )
67 {
68 delete format;
69 }
70}
71
73{
74 if ( mFormats.contains( id ) )
75 return mFormats.value( id )->clone();
76
77 return new QgsFallbackNumericFormat();
78}
79
80QgsNumericFormat *QgsNumericFormatRegistry::create( const QString &id, const QVariantMap &configuration, const QgsReadWriteContext &context ) const
81{
82 if ( mFormats.contains( id ) )
83 return mFormats.value( id )->create( configuration, context );
84
85 return new QgsFallbackNumericFormat();
86}
87
88QgsNumericFormat *QgsNumericFormatRegistry::createFromXml( const QDomElement &element, const QgsReadWriteContext &context ) const
89{
90 const QVariantMap configuration = QgsXmlUtils::readVariant( element.firstChildElement() ).toMap();
91 const QString id = element.attribute( u"id"_s );
92
93 if ( mFormats.contains( id ) )
94 return mFormats.value( id )->create( configuration, context );
95
96 return new QgsFallbackNumericFormat();
97}
98
103
104QString QgsNumericFormatRegistry::visibleName( const QString &id ) const
105{
106 if ( mFormats.contains( id ) )
107 return mFormats.value( id )->visibleName();
108
109 return QString();
110}
111
112int QgsNumericFormatRegistry::sortKey( const QString &id ) const
113{
114 if ( mFormats.contains( id ) )
115 return mFormats.value( id )->sortKey();
116
117 return 0;
118}
A numeric formatter which returns a simple text representation of a value.
A numeric formatter which returns a text representation of a direction/bearing.
A numeric formatter which returns a text representation of a currency value.
A numeric formatter which uses a QgsExpression to calculate the text representation of a value.
A basic numeric formatter which returns a simple text representation of a value.
A numeric formatter which returns a vulgar fractional representation of a decimal value (e....
A numeric formatter which returns a text representation of a geographic coordinate (latitude or longi...
int sortKey(const QString &id) const
Returns the sorting key for the format with matching id.
QgsNumericFormat * fallbackFormat() const
Returns a basic numeric formatter which can be used to represent any number in an default manner.
void removeFormat(const QString &id)
Removes the format with matching id from the registry.
QString visibleName(const QString &id) const
Returns the translated, user-visible name for the format with matching id.
QgsNumericFormat * format(const QString &id) const
Creates a new numeric format by id.
QStringList formats() const
Returns a list of the format IDs currently contained in the registry.
QgsNumericFormat * createFromXml(const QDomElement &element, const QgsReadWriteContext &context) const
Creates a new numeric format from an XML element.
QgsNumericFormatRegistry()
You should not normally need to create your own numeric format registry.
void addFormat(QgsNumericFormat *format)
Adds a new format to the registry.
QgsNumericFormat * create(const QString &id, const QVariantMap &configuration, const QgsReadWriteContext &context) const
Creates a new numeric format by id, using the supplied configuration.
Abstract base class for numeric formatters, which allow for formatting a numeric value for display.
virtual QgsNumericFormat * create(const QVariantMap &configuration, const QgsReadWriteContext &context) const =0
Creates a new copy of the format, using the supplied configuration.
virtual QgsNumericFormat * clone() const =0
Clones the format, returning a new object.
A numeric formatter which returns a text representation of a percentage value.
A container for the context for various read/write operations on objects.
A numeric formatter which returns a scientific notation representation of a value.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.