QGIS API Documentation  3.14.0-Pi (9f7028fd23)
qgsrangefieldformatter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrangefieldformatter.cpp - QgsRangeFieldFormatter
3 
4  ---------------------
5  begin : 01/02/2018
6  copyright : (C) 2018 by Alessandro Pasotti
7  email : elpaso at itopen dot it
8  ***************************************************************************
9  * *
10  * This program is free software; you can redistribute it and/or modify *
11  * it under the terms of the GNU General Public License as published by *
12  * the Free Software Foundation; either version 2 of the License, or *
13  * (at your option) any later version. *
14  * *
15  ***************************************************************************/
16 
17 #include <QLocale>
18 
19 #include "qgsrangefieldformatter.h"
20 
21 #include "qgssettings.h"
22 #include "qgsfield.h"
23 #include "qgsvectorlayer.h"
24 #include "qgsapplication.h"
25 
26 
28 {
29  return QStringLiteral( "Range" );
30 }
31 
32 QString QgsRangeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
33 {
34  Q_UNUSED( cache )
35  Q_UNUSED( config )
36 
37  if ( value.isNull() )
38  {
40  }
41 
42  QString result;
43 
44  const QgsField field = layer->fields().at( fieldIndex );
45 
46  if ( field.type() == QVariant::Double &&
47  config.contains( QStringLiteral( "Precision" ) ) &&
48  value.isValid( ) )
49  {
50  bool ok;
51  double val( value.toDouble( &ok ) );
52  if ( ok )
53  {
54  int precision( config[ QStringLiteral( "Precision" ) ].toInt( &ok ) );
55  if ( ok )
56  {
57  // TODO: make the format configurable!
58  result = QLocale().toString( val, 'f', precision );
59  }
60  }
61  }
62  else if ( ( field.type() == QVariant::Int ) &&
63  value.isValid( ) )
64  {
65  bool ok;
66  double val( value.toInt( &ok ) );
67  if ( ok )
68  {
69  result = QLocale().toString( val, 'f', 0 );
70  }
71  }
72  else if ( ( field.type() == QVariant::LongLong ) &&
73  value.isValid( ) )
74  {
75  bool ok;
76  double val( value.toLongLong( &ok ) );
77  if ( ok )
78  {
79  result = QLocale().toString( val, 'f', 0 );
80  }
81  }
82  else
83  {
84  result = value.toString();
85  }
86  return result;
87 }
QgsRangeFieldFormatter::id
QString id() const override
Returns a unique id for this field formatter.
Definition: qgsrangefieldformatter.cpp:27
qgsapplication.h
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
precision
int precision
Definition: qgswfsgetfeature.cpp:103
QgsRangeFieldFormatter::representValue
QString representValue(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value) const override
Create a pretty String representation of the value.
Definition: qgsrangefieldformatter.cpp:32
QgsApplication::nullRepresentation
static QString nullRepresentation()
This string is used to represent the value NULL throughout QGIS.
Definition: qgsapplication.cpp:1802
qgsrangefieldformatter.h
qgsvectorlayer.h
QgsVectorLayer
Definition: qgsvectorlayer.h:385
qgsfield.h
qgssettings.h
QgsFields::at
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
QgsField::type
QVariant::Type type
Definition: qgsfield.h:57
QgsField
Definition: qgsfield.h:49