QGIS API Documentation 3.41.0-Master (af5edcb665c)
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#include "qgsnumericformat.h"
17
27#include "qgsxmlutils.h"
28
41
43{
44 qDeleteAll( mFormats );
45}
46
48{
49 return mFormats.keys();
50}
51
53{
54 if ( !format )
55 return;
56
57 mFormats.insert( format->id(), format );
58}
59
61{
62 if ( QgsNumericFormat *format = mFormats.take( id ) )
63 {
64 delete format;
65 }
66}
67
69{
70 if ( mFormats.contains( id ) )
71 return mFormats.value( id )->clone();
72
73 return new QgsFallbackNumericFormat();
74}
75
76QgsNumericFormat *QgsNumericFormatRegistry::create( const QString &id, const QVariantMap &configuration, const QgsReadWriteContext &context ) const
77{
78 if ( mFormats.contains( id ) )
79 return mFormats.value( id )->create( configuration, context );
80
81 return new QgsFallbackNumericFormat();
82}
83
84QgsNumericFormat *QgsNumericFormatRegistry::createFromXml( const QDomElement &element, const QgsReadWriteContext &context ) const
85{
86 const QVariantMap configuration = QgsXmlUtils::readVariant( element.firstChildElement() ).toMap();
87 const QString id = element.attribute( QStringLiteral( "id" ) );
88
89 if ( mFormats.contains( id ) )
90 return mFormats.value( id )->create( configuration, context );
91
92 return new QgsFallbackNumericFormat();
93}
94
99
100QString QgsNumericFormatRegistry::visibleName( const QString &id ) const
101{
102 if ( mFormats.contains( id ) )
103 return mFormats.value( id )->visibleName();
104
105 return QString();
106}
107
108int QgsNumericFormatRegistry::sortKey( const QString &id ) const
109{
110 if ( mFormats.contains( id ) )
111 return mFormats.value( id )->sortKey();
112
113 return 0;
114}
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.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
virtual QString id() const =0
Returns a unique id for this numeric format.
A numeric formatter which returns a text representation of a percentage value.
The class is used as a container of context for various read/write operations on other 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.