QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 "qgsfield.h"
19 #include "qgsfields.h"
20 #include "qgsvectorlayer.h"
21 #include "qgsvectordataprovider.h"
22 
23 
24 QString QgsFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
25 {
26  Q_UNUSED( config )
27  Q_UNUSED( cache )
28 
29  QString defVal;
30  if ( layer->fields().fieldOrigin( fieldIndex ) == QgsFields::OriginProvider && layer->dataProvider() )
31  defVal = layer->dataProvider()->defaultValueClause( layer->fields().fieldOriginIndex( fieldIndex ) );
32 
33  if ( ! layer->fields().exists( fieldIndex ) )
34  {
35  return defVal;
36  }
37  else
38  {
39  return layer->fields().at( fieldIndex ).displayString( value.isNull() ? defVal : value );
40  }
41 }
42 
43 QVariant QgsFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
44 {
45  Q_UNUSED( layer )
46  Q_UNUSED( fieldIndex )
47  Q_UNUSED( config )
48  Q_UNUSED( cache )
49 
50  return value;
51 }
52 
53 Qt::AlignmentFlag QgsFieldFormatter::alignmentFlag( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const
54 {
55  Q_UNUSED( config )
56 
57  QgsField field = layer->fields().at( fieldIndex );
58  if ( field.isNumeric() || field.isDateOrTime() )
59  return Qt::AlignRight;
60  else
61  return Qt::AlignLeft;
62 }
63 
64 QVariant QgsFieldFormatter::createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const
65 {
66  Q_UNUSED( layer )
67  Q_UNUSED( fieldIndex )
68  Q_UNUSED( config )
69 
70  return QVariant();
71 }
72 
73 QList<QgsVectorLayerRef> QgsFieldFormatter::layerDependencies( const QVariantMap &config ) const
74 {
75  Q_UNUSED( config )
76  return QList<QgsVectorLayerRef>();
77 }
78 
79 QVariantList QgsFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext &context ) const
80 {
81  Q_UNUSED( config )
82  Q_UNUSED( countLimit )
83  Q_UNUSED( context )
84 
85  return QVariantList();
86 }
87 
88 void QgsFieldFormatter::setFlags( const Flags &flags )
89 {
90  mFlags = flags;
91 }
QgsFields::OriginProvider
@ OriginProvider
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
Definition: qgsfields.h:51
qgsfields.h
QgsFieldFormatter::flags
Flags flags() const
Returns the flags.
Definition: qgsfieldformatter.h:170
QgsField::isNumeric
bool isNumeric
Definition: qgsfield.h:53
QgsFieldFormatter::createCache
virtual QVariant createCache(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const
Create a cache for a given field.
Definition: qgsfieldformatter.cpp:64
QgsVectorLayer::dataProvider
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.
Definition: qgsvectorlayer.cpp:627
QgsField::displayString
QString displayString(const QVariant &v) const
Formats string for display.
Definition: qgsfield.cpp:245
QgsField::isDateOrTime
bool isDateOrTime
Definition: qgsfield.h:54
QgsFieldFormatter::layerDependencies
virtual QList< QgsVectorLayerRef > layerDependencies(const QVariantMap &config) const
Returns a list of weak layer references to other layers required by this formatter for the given conf...
Definition: qgsfieldformatter.cpp:73
QgsFieldFormatterContext
Definition: qgsfieldformatter.h:33
QgsFieldFormatter::setFlags
void setFlags(const Flags &flags)
Sets the flags.
Definition: qgsfieldformatter.cpp:88
QgsFieldFormatter::sortValue
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.
Definition: qgsfieldformatter.cpp:43
QgsVectorLayer::fields
QgsFields fields() const FINAL
Returns the list of fields of this layer.
Definition: qgsvectorlayer.cpp:3280
QgsFieldFormatter::alignmentFlag
virtual Qt::AlignmentFlag alignmentFlag(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const
Returns the alignment for a particular field.
Definition: qgsfieldformatter.cpp:53
QgsFields::exists
bool exists(int i) const
Returns if a field index is valid.
Definition: qgsfields.cpp:153
QgsFields::fieldOrigin
FieldOrigin fieldOrigin(int fieldIdx) const
Gets field's origin (value from an enumeration)
Definition: qgsfields.cpp:189
qgsvectordataprovider.h
QgsFieldFormatter::availableValues
virtual QVariantList availableValues(const QVariantMap &config, int countLimit, const QgsFieldFormatterContext &context) const
Returns a list of the values that would be possible to select with this widget type On a RelationRefe...
Definition: qgsfieldformatter.cpp:79
qgsvectorlayer.h
QgsFieldFormatter::representValue
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.
Definition: qgsfieldformatter.cpp:24
QgsVectorLayer
Definition: qgsvectorlayer.h:385
qgsfield.h
QgsFields::fieldOriginIndex
int fieldOriginIndex(int fieldIdx) const
Gets field's origin index (its meaning is specific to each type of origin)
Definition: qgsfields.cpp:197
QgsVectorDataProvider::defaultValueClause
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index.
Definition: qgsvectordataprovider.cpp:142
QgsFields::at
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
Definition: qgsfields.cpp:163
qgsfieldformatter.h
QgsField
Definition: qgsfield.h:49