QGIS API Documentation 3.41.0-Master (25ec5511245)
Loading...
Searching...
No Matches
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
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"
22#include "qgsapplication.h"
23
24QString 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 if ( ! layer->fields().exists( fieldIndex ) )
30 return QString();
31
32 QString defVal;
33 if ( layer->fields().fieldOrigin( fieldIndex ) == Qgis::FieldOrigin::Provider && layer->dataProvider() )
34 defVal = layer->dataProvider()->defaultValueClause( layer->fields().fieldOriginIndex( fieldIndex ) );
35
36 if ( !defVal.isNull() && defVal == value )
37 {
38 return defVal;
39 }
40 else if ( QgsVariantUtils::isNull( value ) )
41 {
43 }
44 else
45 {
46 return layer->fields().at( fieldIndex ).displayString( value );
47 }
48}
49
50QVariant QgsFieldFormatter::sortValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
51{
52 Q_UNUSED( layer )
53 Q_UNUSED( fieldIndex )
54 Q_UNUSED( config )
55 Q_UNUSED( cache )
56
57 return value;
58}
59
60Qt::AlignmentFlag QgsFieldFormatter::alignmentFlag( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const
61{
62 Q_UNUSED( config )
63
64 const QgsField field = layer->fields().at( fieldIndex );
65 if ( field.isNumeric() || field.isDateOrTime() )
66 return Qt::AlignRight;
67 else
68 return Qt::AlignLeft;
69}
70
71QVariant QgsFieldFormatter::createCache( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config ) const
72{
73 Q_UNUSED( layer )
74 Q_UNUSED( fieldIndex )
75 Q_UNUSED( config )
76
77 return QVariant();
78}
79
80QList<QgsVectorLayerRef> QgsFieldFormatter::layerDependencies( const QVariantMap &config ) const
81{
82 Q_UNUSED( config )
83 return QList<QgsVectorLayerRef>();
84}
85
86QVariantList QgsFieldFormatter::availableValues( const QVariantMap &config, int countLimit, const QgsFieldFormatterContext &context ) const
87{
88 Q_UNUSED( config )
89 Q_UNUSED( countLimit )
90 Q_UNUSED( context )
91
92 return QVariantList();
93}
94
96{
97 mFlags = flags;
98}
@ Provider
Field originates from the underlying data provider of the vector layer.
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
A context for field formatter containing information like the project.
virtual QVariant createCache(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const
Create a cache for a given field.
Flags flags() const
Returns the flags.
virtual Qt::AlignmentFlag alignmentFlag(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config) const
Returns the alignment for a particular field.
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...
QFlags< Flag > Flags
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.
virtual QList< QgsVectorLayerRef > layerDependencies(const QVariantMap &config) const SIP_SKIP
Returns a list of weak layer references to other layers required by this formatter for the given conf...
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.
void setFlags(const Flags &flags)
Sets the flags.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
bool isDateOrTime
Definition qgsfield.h:57
QString displayString(const QVariant &v) const
Formats string for display.
Definition qgsfield.cpp:317
bool isNumeric
Definition qgsfield.h:56
Qgis::FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
Q_INVOKABLE bool exists(int i) const
Returns if a field index is valid.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
virtual QString defaultValueClause(int fieldIndex) const
Returns any default value clauses which are present at the provider for a specified field index.
Represents a vector layer which manages a vector based data sets.
QgsVectorDataProvider * dataProvider() FINAL
Returns the layer's data provider, it may be nullptr.