QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
19 #include "qgsbasicnumericformat.h"
26 #include "qgsxmlutils.h"
27 
29 {
38 }
39 
41 {
42  qDeleteAll( mFormats );
43 }
44 
46 {
47  return mFormats.keys();
48 }
49 
51 {
52  if ( !format )
53  return;
54 
55  mFormats.insert( format->id(), format );
56 }
57 
58 void QgsNumericFormatRegistry::removeFormat( const QString &id )
59 {
60  if ( QgsNumericFormat *format = mFormats.take( id ) )
61  {
62  delete format;
63  }
64 }
65 
67 {
68  if ( mFormats.contains( id ) )
69  return mFormats.value( id )->clone();
70 
71  return new QgsFallbackNumericFormat();
72 }
73 
74 QgsNumericFormat *QgsNumericFormatRegistry::create( const QString &id, const QVariantMap &configuration, const QgsReadWriteContext &context ) const
75 {
76  if ( mFormats.contains( id ) )
77  return mFormats.value( id )->create( configuration, context );
78 
79  return new QgsFallbackNumericFormat();
80 }
81 
82 QgsNumericFormat *QgsNumericFormatRegistry::createFromXml( const QDomElement &element, const QgsReadWriteContext &context ) const
83 {
84  const QVariantMap configuration = QgsXmlUtils::readVariant( element.firstChildElement() ).toMap();
85  const QString id = element.attribute( QStringLiteral( "id" ) );
86 
87  if ( mFormats.contains( id ) )
88  return mFormats.value( id )->create( configuration, context );
89 
90  return new QgsFallbackNumericFormat();
91 }
92 
94 {
95  return new QgsFallbackNumericFormat();
96 }
97 
98 QString QgsNumericFormatRegistry::visibleName( const QString &id ) const
99 {
100  if ( mFormats.contains( id ) )
101  return mFormats.value( id )->visibleName();
102 
103  return QString();
104 }
105 
106 int QgsNumericFormatRegistry::sortKey( const QString &id ) const
107 {
108  if ( mFormats.contains( id ) )
109  return mFormats.value( id )->sortKey();
110 
111  return 0;
112 }
qgsnumericformatregistry.h
qgsfallbacknumericformat.h
QgsReadWriteContext
The class is used as a container of context for various read/write operations on other objects.
Definition: qgsreadwritecontext.h:34
QgsGeographicCoordinateNumericFormat
A numeric formatter which returns a text representation of a geographic coordinate (latitude or longi...
Definition: qgscoordinatenumericformat.h:28
QgsFallbackNumericFormat
A basic numeric formatter which returns a simple text representation of a value.
Definition: qgsfallbacknumericformat.h:28
QgsNumericFormatRegistry::formats
QStringList formats() const
Returns a list of the format IDs currently contained in the registry.
Definition: qgsnumericformatregistry.cpp:45
qgsfractionnumericformat.h
QgsNumericFormat
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
Definition: qgsnumericformat.h:259
qgscurrencynumericformat.h
QgsNumericFormatRegistry::~QgsNumericFormatRegistry
~QgsNumericFormatRegistry()
Definition: qgsnumericformatregistry.cpp:40
QgsBasicNumericFormat
A numeric formatter which returns a simple text representation of a value.
Definition: qgsbasicnumericformat.h:31
QgsNumericFormatRegistry::addFormat
void addFormat(QgsNumericFormat *format)
Adds a new format to the registry.
Definition: qgsnumericformatregistry.cpp:50
qgsbearingnumericformat.h
QgsPercentageNumericFormat
A numeric formatter which returns a text representation of a percentage value.
Definition: qgspercentagenumericformat.h:28
QgsNumericFormat::id
virtual QString id() const =0
Returns a unique id for this numeric format.
QgsXmlUtils::readVariant
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
Definition: qgsxmlutils.cpp:251
QgsNumericFormatRegistry::create
QgsNumericFormat * create(const QString &id, const QVariantMap &configuration, const QgsReadWriteContext &context) const
Creates a new numeric format by id, using the supplied configuration.
Definition: qgsnumericformatregistry.cpp:74
QgsNumericFormatRegistry::QgsNumericFormatRegistry
QgsNumericFormatRegistry()
You should not normally need to create your own numeric format registry.
Definition: qgsnumericformatregistry.cpp:28
QgsNumericFormatRegistry::sortKey
int sortKey(const QString &id) const
Returns the sorting key for the format with matching id.
Definition: qgsnumericformatregistry.cpp:106
qgsscientificnumericformat.h
qgsxmlutils.h
qgsnumericformat.h
QgsFractionNumericFormat
A numeric formatter which returns a vulgar fractional representation of a decimal value (e....
Definition: qgsfractionnumericformat.h:30
qgsbasicnumericformat.h
QgsScientificNumericFormat
A numeric formatter which returns a scientific notation representation of a value.
Definition: qgsscientificnumericformat.h:28
qgscoordinatenumericformat.h
QgsCurrencyNumericFormat
A numeric formatter which returns a text representation of a currency value.
Definition: qgscurrencynumericformat.h:28
QgsNumericFormatRegistry::fallbackFormat
QgsNumericFormat * fallbackFormat() const
Returns a basic numeric formatter which can be used to represent any number in an default manner.
Definition: qgsnumericformatregistry.cpp:93
QgsNumericFormatRegistry::visibleName
QString visibleName(const QString &id) const
Returns the translated, user-visible name for the format with matching id.
Definition: qgsnumericformatregistry.cpp:98
QgsNumericFormatRegistry::removeFormat
void removeFormat(const QString &id)
Removes the format with matching id from the registry.
Definition: qgsnumericformatregistry.cpp:58
QgsBearingNumericFormat
A numeric formatter which returns a text representation of a direction/bearing.
Definition: qgsbearingnumericformat.h:28
qgspercentagenumericformat.h
QgsNumericFormatRegistry::format
QgsNumericFormat * format(const QString &id) const
Creates a new numeric format by id.
Definition: qgsnumericformatregistry.cpp:66
QgsNumericFormatRegistry::createFromXml
QgsNumericFormat * createFromXml(const QDomElement &element, const QgsReadWriteContext &context) const
Creates a new numeric format from an XML element.
Definition: qgsnumericformatregistry.cpp:82