QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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"
24 #include "qgsxmlutils.h"
25 
27 {
34 }
35 
37 {
38  qDeleteAll( mFormats );
39 }
40 
42 {
43  return mFormats.keys();
44 }
45 
47 {
48  if ( !format )
49  return;
50 
51  mFormats.insert( format->id(), format );
52 }
53 
54 void QgsNumericFormatRegistry::removeFormat( const QString &id )
55 {
56  if ( QgsNumericFormat *format = mFormats.take( id ) )
57  {
58  delete format;
59  }
60 }
61 
63 {
64  if ( mFormats.contains( id ) )
65  return mFormats.value( id )->clone();
66 
67  return new QgsFallbackNumericFormat();
68 }
69 
70 QgsNumericFormat *QgsNumericFormatRegistry::create( const QString &id, const QVariantMap &configuration, const QgsReadWriteContext &context ) const
71 {
72  if ( mFormats.contains( id ) )
73  return mFormats.value( id )->create( configuration, context );
74 
75  return new QgsFallbackNumericFormat();
76 }
77 
78 QgsNumericFormat *QgsNumericFormatRegistry::createFromXml( const QDomElement &element, const QgsReadWriteContext &context ) const
79 {
80  const QVariantMap configuration = QgsXmlUtils::readVariant( element.firstChildElement() ).toMap();
81  const QString id = element.attribute( QStringLiteral( "id" ) );
82 
83  if ( mFormats.contains( id ) )
84  return mFormats.value( id )->create( configuration, context );
85 
86  return new QgsFallbackNumericFormat();
87 }
88 
90 {
91  return new QgsFallbackNumericFormat();
92 }
93 
94 QString QgsNumericFormatRegistry::visibleName( const QString &id ) const
95 {
96  if ( mFormats.contains( id ) )
97  return mFormats.value( id )->visibleName();
98 
99  return QString();
100 }
101 
102 int QgsNumericFormatRegistry::sortKey( const QString &id ) const
103 {
104  if ( mFormats.contains( id ) )
105  return mFormats.value( id )->sortKey();
106 
107  return 0;
108 }
A numeric formatter which returns a text representation of a percentage value.
QgsNumericFormat * createFromXml(const QDomElement &element, const QgsReadWriteContext &context) const
Creates a new numeric format from an XML element.
The class is used as a container of context for various read/write operations on other objects...
int sortKey(const QString &id) const
Returns the sorting key for the format with matching id.
A numeric formatter which returns a scientific notation representation of a value.
QStringList formats() const
Returns a list of the format IDs currently contained in the registry.
A numeric formatter which returns a text representation of a direction/bearing.
A numeric formatter which returns a simple text representation of a value.
virtual QString id() const =0
Returns a unique id for this numeric format.
A numeric formatter allows for formatting a numeric value for display, using a variety of different f...
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
QgsNumericFormat * create(const QString &id, const QVariantMap &configuration, const QgsReadWriteContext &context) const
Creates a new numeric format by id, using the supplied configuration.
QgsNumericFormat * format(const QString &id) const
Creates a new numeric format by id.
void removeFormat(const QString &id)
Removes the format with matching id from the registry.
A numeric formatter which returns a text representation of a currency value.
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 * fallbackFormat() const
Returns a basic numeric formatter which can be used to represent any number in an default manner...
QString visibleName(const QString &id) const
Returns the translated, user-visible name for the format with matching id.
A basic numeric formatter which returns a simple text representation of a value.