QGIS API Documentation  3.0.2-Girona (307d082)
qgsfieldformatter.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsfieldformatter.cpp - QgsFieldFormatter
3 
4  ---------------------
5  begin : 2.12.2016
6  copyright : (C) 2016 by Matthias Kuhn
7  email : [email protected]
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 #include "qgsfieldformatter.h"
17 
18 #include "qgsfields.h"
19 #include "qgsvectorlayer.h"
20 #include "qgsvectordataprovider.h"
21 
23 {
24 }
25 
26 QString QgsFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
27 {
28  Q_UNUSED( layer )
29  Q_UNUSED( fieldIndex )
30  Q_UNUSED( config )
31  Q_UNUSED( cache )
32  Q_UNUSED( value )
33 
34  QString defVal;
35  if ( layer->fields().fieldOrigin( fieldIndex ) == QgsFields::OriginProvider && layer->dataProvider() )
36  defVal = layer->dataProvider()->defaultValueClause( layer->fields().fieldOriginIndex( fieldIndex ) );
37 
38  return value == defVal ? defVal : layer->fields().at( fieldIndex ).displayString( value );
39 }
40 
41 QVariant QgsFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
42 {
43  Q_UNUSED( layer )
44  Q_UNUSED( fieldIndex )
45  Q_UNUSED( config )
46  Q_UNUSED( cache )
47 
48  return value;
49 }
50 
51 Qt::AlignmentFlag QgsFieldFormatter::alignmentFlag( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const
52 {
53  Q_UNUSED( config );
54 
55  QVariant::Type fldType = layer->fields().at( fieldIndex ).type();
56  bool alignRight = ( fldType == QVariant::Int || fldType == QVariant::Double || fldType == QVariant::LongLong
57  || fldType == QVariant::DateTime || fldType == QVariant::Date || fldType == QVariant::Time );
58 
59  if ( alignRight )
60  return Qt::AlignRight;
61  else
62  return Qt::AlignLeft;
63 }
64 
65 QVariant QgsFieldFormatter::createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const
66 {
67  Q_UNUSED( layer )
68  Q_UNUSED( fieldIndex )
69  Q_UNUSED( config )
70 
71  return QVariant();
72 }
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
Definition: qgsfields.cpp:171
virtual QVariant createCache(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const
Create a cache for a given field.
virtual QVariant sortValue(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value) const
If the default sort order should be overwritten for this widget, you can transform the value in here...
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfields.h:49
QgsField at(int i) const
Get field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:145
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
Definition: qgsfields.cpp:179
QgsFields fields() const override
Returns the list of fields of this layer.
virtual Qt::AlignmentFlag alignmentFlag(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const
Return the alignment for a particular field.
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index...
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:204
virtual QString representValue(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value) const
Create a pretty String representation of the value.
QgsVectorDataProvider * dataProvider() override
Returns the layer's data provider.
Represents a vector layer which manages a vector based data sets.
QVariant::Type type
Definition: qgsfield.h:55