23
23
#include " qgsproject.h"
24
24
#include " qgscsexception.h"
25
25
#include " qgslogger.h"
26
+ #include " qgsfieldformatterregistry.h"
27
+ #include " qgsfieldformatter.h"
26
28
27
29
#include < QJsonDocument>
28
30
#include < QJsonArray>
29
31
30
- QgsJSONExporter::QgsJSONExporter ( const QgsVectorLayer *vectorLayer, int precision )
32
+ QgsJsonExporter::QgsJsonExporter ( QgsVectorLayer *vectorLayer, int precision )
31
33
: mPrecision( precision )
32
34
, mIncludeGeometry( true )
33
35
, mIncludeAttributes( true )
34
36
, mIncludeRelatedAttributes( false )
35
- , mLayerId ( vectorLayer ? vectorLayer-> id () : QString() )
37
+ , mLayer ( vectorLayer )
36
38
{
37
39
if ( vectorLayer )
38
40
{
@@ -42,39 +44,39 @@ QgsJSONExporter::QgsJSONExporter( const QgsVectorLayer *vectorLayer, int precisi
42
44
mTransform .setDestinationCrs ( QgsCoordinateReferenceSystem ( 4326 , QgsCoordinateReferenceSystem::EpsgCrsId ) );
43
45
}
44
46
45
- void QgsJSONExporter ::setVectorLayer ( const QgsVectorLayer *vectorLayer )
47
+ void QgsJsonExporter ::setVectorLayer ( QgsVectorLayer *vectorLayer )
46
48
{
47
- mLayerId = vectorLayer ? vectorLayer-> id () : QString () ;
49
+ mLayer = vectorLayer;
48
50
if ( vectorLayer )
49
51
{
50
52
mCrs = vectorLayer->crs ();
51
53
mTransform .setSourceCrs ( mCrs );
52
54
}
53
55
}
54
56
55
- QgsVectorLayer *QgsJSONExporter ::vectorLayer () const
57
+ QgsVectorLayer *QgsJsonExporter ::vectorLayer () const
56
58
{
57
- return qobject_cast< QgsVectorLayer * >( QgsProject::instance ()-> mapLayer ( mLayerId ) );
59
+ return mLayer . data ( );
58
60
}
59
61
60
- void QgsJSONExporter ::setSourceCrs ( const QgsCoordinateReferenceSystem &crs )
62
+ void QgsJsonExporter ::setSourceCrs ( const QgsCoordinateReferenceSystem &crs )
61
63
{
62
64
mCrs = crs;
63
65
mTransform .setSourceCrs ( mCrs );
64
66
}
65
67
66
- QgsCoordinateReferenceSystem QgsJSONExporter ::sourceCrs () const
68
+ QgsCoordinateReferenceSystem QgsJsonExporter ::sourceCrs () const
67
69
{
68
70
return mCrs ;
69
71
}
70
72
71
- QString QgsJSONExporter ::exportFeature ( const QgsFeature &feature, const QVariantMap &extraProperties,
73
+ QString QgsJsonExporter ::exportFeature ( const QgsFeature &feature, const QVariantMap &extraProperties,
72
74
const QVariant &id ) const
73
75
{
74
76
QString s = QStringLiteral ( " {\n \" type\" :\" Feature\" ,\n " );
75
77
76
78
// ID
77
- s += QStringLiteral ( " \" id\" :%1,\n " ).arg ( !id.isValid () ? QString::number ( feature.id () ) : QgsJSONUtils ::encodeValue ( id ) );
79
+ s += QStringLiteral ( " \" id\" :%1,\n " ).arg ( !id.isValid () ? QString::number ( feature.id () ) : QgsJsonUtils ::encodeValue ( id ) );
78
80
79
81
QgsGeometry geom = feature.geometry ();
80
82
if ( !geom.isNull () && mIncludeGeometry )
@@ -119,7 +121,7 @@ QString QgsJSONExporter::exportFeature( const QgsFeature &feature, const QVarian
119
121
120
122
if ( mIncludeAttributes )
121
123
{
122
- QgsFields fields = feature.fields ();
124
+ QgsFields fields = mLayer ? mLayer -> fields () : feature.fields ();
123
125
124
126
for ( int i = 0 ; i < fields.count (); ++i )
125
127
{
@@ -130,7 +132,15 @@ QString QgsJSONExporter::exportFeature( const QgsFeature &feature, const QVarian
130
132
properties += QLatin1String ( " ,\n " );
131
133
QVariant val = feature.attributes ().at ( i );
132
134
133
- properties += QStringLiteral ( " \" %1\" :%2" ).arg ( fields.at ( i ).name (), QgsJSONUtils::encodeValue ( val ) );
135
+ if ( mLayer )
136
+ {
137
+ QgsEditorWidgetSetup setup = fields.at ( i ).editorWidgetSetup ();
138
+ QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
139
+ if ( fieldFormatter != QgsApplication::fieldFormatterRegistry ()->fallbackFieldFormatter () )
140
+ val = fieldFormatter->representValue ( mLayer .data (), i, setup.config (), QVariant (), val );
141
+ }
142
+
143
+ properties += QStringLiteral ( " \" %1\" :%2" ).arg ( fields.at ( i ).name (), QgsJsonUtils::encodeValue ( val ) );
134
144
135
145
++attributeCounter;
136
146
}
@@ -144,17 +154,16 @@ QString QgsJSONExporter::exportFeature( const QgsFeature &feature, const QVarian
144
154
if ( attributeCounter > 0 )
145
155
properties += QLatin1String ( " ,\n " );
146
156
147
- properties += QStringLiteral ( " \" %1\" :%2" ).arg ( it.key (), QgsJSONUtils ::encodeValue ( it.value () ) );
157
+ properties += QStringLiteral ( " \" %1\" :%2" ).arg ( it.key (), QgsJsonUtils ::encodeValue ( it.value () ) );
148
158
149
159
++attributeCounter;
150
160
}
151
161
}
152
162
153
163
// related attributes
154
- QgsVectorLayer *vl = vectorLayer ();
155
- if ( vl && mIncludeRelatedAttributes )
164
+ if ( mLayer && mIncludeRelatedAttributes )
156
165
{
157
- QList< QgsRelation > relations = QgsProject::instance ()->relationManager ()->referencedRelations ( vl );
166
+ QList< QgsRelation > relations = QgsProject::instance ()->relationManager ()->referencedRelations ( mLayer . data () );
158
167
Q_FOREACH ( const QgsRelation &relation , relations )
159
168
{
160
169
if ( attributeCounter > 0 )
@@ -167,14 +176,24 @@ QString QgsJSONExporter::exportFeature( const QgsFeature &feature, const QVarian
167
176
if ( childLayer )
168
177
{
169
178
QgsFeatureIterator it = childLayer->getFeatures ( req );
179
+ QVector<QVariant> attributeWidgetCaches;
180
+ int fieldIndex = 0 ;
181
+ Q_FOREACH ( const QgsField &field, childLayer->fields () )
182
+ {
183
+ QgsEditorWidgetSetup setup = field.editorWidgetSetup ();
184
+ QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
185
+ attributeWidgetCaches.append ( fieldFormatter->createCache ( childLayer, fieldIndex, setup.config () ) );
186
+ fieldIndex++;
187
+ }
188
+
170
189
QgsFeature relatedFet;
171
190
int relationFeatures = 0 ;
172
191
while ( it.nextFeature ( relatedFet ) )
173
192
{
174
193
if ( relationFeatures > 0 )
175
194
relatedFeatureAttributes += QLatin1String ( " ,\n " );
176
195
177
- relatedFeatureAttributes += QgsJSONUtils ::exportAttributes ( relatedFet );
196
+ relatedFeatureAttributes += QgsJsonUtils ::exportAttributes ( relatedFet, childLayer, attributeWidgetCaches );
178
197
relationFeatures++;
179
198
}
180
199
}
@@ -204,7 +223,7 @@ QString QgsJSONExporter::exportFeature( const QgsFeature &feature, const QVarian
204
223
return s;
205
224
}
206
225
207
- QString QgsJSONExporter ::exportFeatures ( const QgsFeatureList &features ) const
226
+ QString QgsJsonExporter ::exportFeatures ( const QgsFeatureList &features ) const
208
227
{
209
228
QStringList featureJSON;
210
229
Q_FOREACH ( const QgsFeature &feature, features )
@@ -218,20 +237,20 @@ QString QgsJSONExporter::exportFeatures( const QgsFeatureList &features ) const
218
237
219
238
220
239
//
221
- // QgsJSONUtils
240
+ // QgsJsonUtils
222
241
//
223
242
224
- QgsFeatureList QgsJSONUtils ::stringToFeatureList ( const QString &string, const QgsFields &fields, QTextCodec *encoding )
243
+ QgsFeatureList QgsJsonUtils ::stringToFeatureList ( const QString &string, const QgsFields &fields, QTextCodec *encoding )
225
244
{
226
245
return QgsOgrUtils::stringToFeatureList ( string, fields, encoding );
227
246
}
228
247
229
- QgsFields QgsJSONUtils ::stringToFields ( const QString &string, QTextCodec *encoding )
248
+ QgsFields QgsJsonUtils ::stringToFields ( const QString &string, QTextCodec *encoding )
230
249
{
231
250
return QgsOgrUtils::stringToFields ( string, encoding );
232
251
}
233
252
234
- QString QgsJSONUtils ::encodeValue ( const QVariant &value )
253
+ QString QgsJsonUtils ::encodeValue ( const QVariant &value )
235
254
{
236
255
if ( value.isNull () )
237
256
return QStringLiteral ( " null" );
@@ -268,7 +287,7 @@ QString QgsJSONUtils::encodeValue( const QVariant &value )
268
287
}
269
288
}
270
289
271
- QString QgsJSONUtils ::exportAttributes ( const QgsFeature &feature )
290
+ QString QgsJsonUtils ::exportAttributes ( const QgsFeature &feature, QgsVectorLayer *layer, QVector<QVariant> attributeWidgetCaches )
272
291
{
273
292
QgsFields fields = feature.fields ();
274
293
QString attrs;
@@ -278,12 +297,21 @@ QString QgsJSONUtils::exportAttributes( const QgsFeature &feature )
278
297
attrs += QLatin1String ( " ,\n " );
279
298
280
299
QVariant val = feature.attributes ().at ( i );
300
+
301
+ if ( layer )
302
+ {
303
+ QgsEditorWidgetSetup setup = layer->fields ().at ( i ).editorWidgetSetup ();
304
+ QgsFieldFormatter *fieldFormatter = QgsApplication::fieldFormatterRegistry ()->fieldFormatter ( setup.type () );
305
+ if ( fieldFormatter != QgsApplication::fieldFormatterRegistry ()->fallbackFieldFormatter () )
306
+ val = fieldFormatter->representValue ( layer, i, setup.config (), attributeWidgetCaches.count () >= i ? attributeWidgetCaches.at ( i ) : QVariant (), val );
307
+ }
308
+
281
309
attrs += encodeValue ( fields.at ( i ).name () ) + ' :' + encodeValue ( val );
282
310
}
283
311
return attrs.prepend ( ' {' ).append ( ' }' );
284
312
}
285
313
286
- QVariantList QgsJSONUtils ::parseArray ( const QString &json, QVariant::Type type )
314
+ QVariantList QgsJsonUtils ::parseArray ( const QString &json, QVariant::Type type )
287
315
{
288
316
QJsonParseError error;
289
317
const QJsonDocument jsonDoc = QJsonDocument::fromJson ( json.toUtf8 (), &error );
0 commit comments