/
qgsfield.cpp
655 lines (571 loc) · 19.8 KB
/
qgsfield.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
/***************************************************************************
qgsfield.cpp - Describes a field in a layer or table
--------------------------------------
Date : 01-Jan-2004
Copyright : (C) 2004 by Gary E.Sherman
email : sherman at mrcc.com
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsfields.h"
#include "qgsfield_p.h"
#include "qgis.h"
#include "qgsapplication.h"
#include "qgssettings.h"
#include <QDataStream>
#include <QIcon>
#include <QLocale>
#include <QJsonDocument>
/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
* See details in QEP #17
****************************************************************************/
#if 0
QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
QString comment )
: mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
, mComment( comment )
{
// This function used to lower case the field name since some stores
// use upper case (e.g., shapefiles), but that caused problems with
// attribute actions getting confused between uppercase and
// lowercase versions of the attribute names, so just leave the
// names how they are now.
}
#endif
QgsField::QgsField( const QString &name, QVariant::Type type,
const QString &typeName, int len, int prec, const QString &comment, QVariant::Type subType )
{
d = new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
}
QgsField::QgsField( const QgsField &other ) //NOLINT
: d( other.d )
{
}
QgsField::~QgsField() = default;
/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
* See details in QEP #17
****************************************************************************/
QgsField &QgsField::operator =( const QgsField &other ) //NOLINT
{
d = other.d;
return *this;
}
bool QgsField::operator==( const QgsField &other ) const
{
return *( other.d ) == *d;
}
bool QgsField::operator!=( const QgsField &other ) const
{
return !( *this == other );
}
QString QgsField::name() const
{
return d->name;
}
QString QgsField::displayName() const
{
if ( !d->alias.isEmpty() )
return d->alias;
else
return d->name;
}
QString QgsField::displayNameWithAlias() const
{
if ( alias().isEmpty() )
{
return name();
}
return QStringLiteral( "%1 (%2)" ).arg( name() ).arg( alias() );
}
QString QgsField::displayType( const bool showConstraints ) const
{
QString typeStr = typeName();
if ( length() > 0 && precision() > 0 )
typeStr += QStringLiteral( "(%1, %2)" ).arg( length() ).arg( precision() );
else if ( length() > 0 )
typeStr += QStringLiteral( "(%1)" ).arg( length() );
if ( showConstraints )
{
typeStr += ( constraints().constraints() & QgsFieldConstraints::ConstraintNotNull )
? QStringLiteral( " NOT NULL" )
: QStringLiteral( " NULL" );
typeStr += ( constraints().constraints() & QgsFieldConstraints::ConstraintUnique )
? QStringLiteral( " UNIQUE" )
: QString();
}
return typeStr;
}
QVariant::Type QgsField::type() const
{
return d->type;
}
QVariant::Type QgsField::subType() const
{
return d->subType;
}
QString QgsField::typeName() const
{
return d->typeName;
}
int QgsField::length() const
{
return d->length;
}
int QgsField::precision() const
{
return d->precision;
}
QString QgsField::comment() const
{
return d->comment;
}
bool QgsField::isNumeric() const
{
return d->type == QVariant::Double || d->type == QVariant::Int || d->type == QVariant::UInt || d->type == QVariant::LongLong || d->type == QVariant::ULongLong;
}
bool QgsField::isDateOrTime() const
{
return d->type == QVariant::Date || d->type == QVariant::Time || d->type == QVariant::DateTime;
}
/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
* See details in QEP #17
****************************************************************************/
void QgsField::setName( const QString &name )
{
d->name = name;
}
void QgsField::setType( QVariant::Type type )
{
d->type = type;
}
void QgsField::setSubType( QVariant::Type subType )
{
d->subType = subType;
}
void QgsField::setTypeName( const QString &typeName )
{
d->typeName = typeName;
}
void QgsField::setLength( int len )
{
d->length = len;
}
void QgsField::setPrecision( int precision )
{
d->precision = precision;
}
void QgsField::setComment( const QString &comment )
{
d->comment = comment;
}
QgsDefaultValue QgsField::defaultValueDefinition() const
{
return d->defaultValueDefinition;
}
void QgsField::setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition )
{
d->defaultValueDefinition = defaultValueDefinition;
}
void QgsField::setConstraints( const QgsFieldConstraints &constraints )
{
d->constraints = constraints;
}
const QgsFieldConstraints &QgsField::constraints() const
{
return d->constraints;
}
QString QgsField::alias() const
{
return d->alias;
}
void QgsField::setAlias( const QString &alias )
{
d->alias = alias;
}
QgsField::ConfigurationFlags QgsField::configurationFlags() const
{
return d->flags;
}
void QgsField::setConfigurationFlags( QgsField::ConfigurationFlags flags )
{
d->flags = flags;
}
/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
* See details in QEP #17
****************************************************************************/
QString QgsField::displayString( const QVariant &v ) const
{
if ( v.isNull() )
{
return QgsApplication::nullRepresentation();
}
// Special treatment for numeric types if group separator is set or decimalPoint is not a dot
if ( d->type == QVariant::Double )
{
// if value doesn't contain a double (a default value expression for instance),
// apply no transformation
bool ok;
v.toDouble( &ok );
if ( !ok )
return v.toString();
// Locales with decimal point != '.' or that require group separator: use QLocale
if ( QLocale().decimalPoint() != '.' ||
!( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
{
if ( d->precision > 0 )
{
if ( -1 < v.toDouble() && v.toDouble() < 1 )
{
return QLocale().toString( v.toDouble(), 'g', d->precision );
}
else
{
return QLocale().toString( v.toDouble(), 'f', d->precision );
}
}
else
{
// Precision is not set, let's guess it from the
// standard conversion to string
QString s( v.toString() );
int dotPosition( s.indexOf( '.' ) );
int precision;
if ( dotPosition < 0 && s.indexOf( 'e' ) < 0 )
{
precision = 0;
return QLocale().toString( v.toDouble(), 'f', precision );
}
else
{
if ( dotPosition < 0 ) precision = 0;
else precision = s.length() - dotPosition - 1;
if ( -1 < v.toDouble() && v.toDouble() < 1 )
{
return QLocale().toString( v.toDouble(), 'g', precision );
}
else
{
return QLocale().toString( v.toDouble(), 'f', precision );
}
}
}
}
// Default for doubles with precision
else if ( d->precision > 0 )
{
if ( -1 < v.toDouble() && v.toDouble() < 1 )
{
return QString::number( v.toDouble(), 'g', d->precision );
}
else
{
return QString::number( v.toDouble(), 'f', d->precision );
}
}
}
// Other numeric types than doubles
else if ( isNumeric() &&
!( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
{
bool ok;
qlonglong converted( v.toLongLong( &ok ) );
if ( ok )
return QLocale().toString( converted );
}
else if ( d->typeName.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String( "jsonb" ) )
{
QJsonDocument doc = QJsonDocument::fromVariant( v );
return QString::fromUtf8( doc.toJson().data() );
}
else if ( d->type == QVariant::ByteArray )
{
return QObject::tr( "BLOB" );
}
// Fallback if special rules do not apply
return v.toString();
}
QString QgsField::readableConfigurationFlag( QgsField::ConfigurationFlag flag )
{
switch ( flag )
{
case ConfigurationFlag::None:
return QObject::tr( "None" );
case ConfigurationFlag::NotSearchable:
return QObject::tr( "Not searchable" );
case ConfigurationFlag::HideFromWms:
return QObject::tr( "Do not expose via WMS" );
case ConfigurationFlag::HideFromWfs:
return QObject::tr( "Do not expose via WFS" );
}
return QString();
}
/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
* See details in QEP #17
****************************************************************************/
bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
{
const QVariant original = v;
if ( errorMessage )
errorMessage->clear();
if ( v.isNull() )
{
v.convert( d->type );
return true;
}
if ( d->type == QVariant::Int && v.toInt() != v.toLongLong() )
{
v = QVariant( d->type );
if ( errorMessage )
*errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toLongLong() );
return false;
}
// Give it a chance to convert to double since for not '.' locales
// we accept both comma and dot as decimal point
if ( d->type == QVariant::Double && v.type() == QVariant::String )
{
QVariant tmp( v );
if ( !tmp.convert( d->type ) )
{
// This might be a string with thousand separator: use locale to convert
bool ok = false;
double d = qgsPermissiveToDouble( v.toString(), ok );
if ( ok )
{
v = QVariant( d );
return true;
}
// For not 'dot' locales, we also want to accept '.'
if ( QLocale().decimalPoint() != '.' )
{
d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
if ( ok )
{
v = QVariant( d );
return true;
}
}
}
}
// For string representation of an int we also might have thousand separator
if ( d->type == QVariant::Int && v.type() == QVariant::String )
{
QVariant tmp( v );
if ( !tmp.convert( d->type ) )
{
// This might be a string with thousand separator: use locale to convert
bool ok;
int i = qgsPermissiveToInt( v.toString(), ok );
if ( ok )
{
v = QVariant( i );
return true;
}
}
}
// For string representation of a long we also might have thousand separator
if ( d->type == QVariant::LongLong && v.type() == QVariant::String )
{
QVariant tmp( v );
if ( !tmp.convert( d->type ) )
{
// This might be a string with thousand separator: use locale to convert
bool ok;
qlonglong l = qgsPermissiveToLongLong( v.toString(), ok );
if ( ok )
{
v = QVariant( l );
return true;
}
}
}
//String representations of doubles in QVariant will return false to convert( QVariant::Int )
//work around this by first converting to double, and then checking whether the double is convertible to int
if ( d->type == QVariant::Int && v.canConvert( QVariant::Double ) )
{
bool ok = false;
double dbl = v.toDouble( &ok );
if ( !ok )
{
//couldn't convert to number
v = QVariant( d->type );
if ( errorMessage )
*errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
return false;
}
double round = std::round( dbl );
if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
{
//double too large to fit in int
v = QVariant( d->type );
if ( errorMessage )
*errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toDouble() );
return false;
}
v = QVariant( static_cast< int >( std::round( dbl ) ) );
return true;
}
//String representations of doubles in QVariant will return false to convert( QVariant::LongLong )
//work around this by first converting to double, and then checking whether the double is convertible to longlong
if ( d->type == QVariant::LongLong && v.canConvert( QVariant::Double ) )
{
//firstly test the conversion to longlong because conversion to double will rounded the value
QVariant tmp( v );
if ( !tmp.convert( d->type ) )
{
bool ok = false;
double dbl = v.toDouble( &ok );
if ( !ok )
{
//couldn't convert to number
v = QVariant( d->type );
if ( errorMessage )
*errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
return false;
}
double round = std::round( dbl );
if ( round > static_cast<double>( std::numeric_limits<long long>::max() ) || round < static_cast<double>( -std::numeric_limits<long long>::max() ) )
{
//double too large to fit in longlong
v = QVariant( d->type );
if ( errorMessage )
*errorMessage = QObject::tr( "Value \"%1\" is too large for long long field" ).arg( original.toDouble() );
return false;
}
v = QVariant( static_cast< long long >( std::round( dbl ) ) );
return true;
}
}
if ( !v.convert( d->type ) )
{
v = QVariant( d->type );
if ( errorMessage )
*errorMessage = QObject::tr( "Could not convert value \"%1\" to target type" ).arg( original.toString() );
return false;
}
if ( d->type == QVariant::Double && d->precision > 0 )
{
double s = std::pow( 10, d->precision );
double d = v.toDouble() * s;
v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
return true;
}
if ( d->type == QVariant::String && d->length > 0 && v.toString().length() > d->length )
{
const int length = v.toString().length();
v = v.toString().left( d->length );
if ( errorMessage )
*errorMessage = QObject::tr( "String of length %1 exceeds maximum field length (%2)" ).arg( length ).arg( d->length );
return false;
}
return true;
}
void QgsField::setEditorWidgetSetup( const QgsEditorWidgetSetup &v )
{
d->editorWidgetSetup = v;
}
QgsEditorWidgetSetup QgsField::editorWidgetSetup() const
{
return d->editorWidgetSetup;
}
/***************************************************************************
* This class is considered CRITICAL and any change MUST be accompanied with
* full unit tests in testqgsfield.cpp.
* See details in QEP #17
****************************************************************************/
QDataStream &operator<<( QDataStream &out, const QgsField &field )
{
out << field.name();
out << static_cast< quint32 >( field.type() );
out << field.typeName();
out << field.length();
out << field.precision();
out << field.comment();
out << field.alias();
out << field.defaultValueDefinition().expression();
out << field.defaultValueDefinition().applyOnUpdate();
out << field.constraints().constraints();
out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) );
out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ) );
out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintExpression ) );
out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
out << field.constraints().constraintExpression();
out << field.constraints().constraintDescription();
out << static_cast< quint32 >( field.subType() );
return out;
}
QDataStream &operator>>( QDataStream &in, QgsField &field )
{
quint32 type;
quint32 subType;
quint32 length;
quint32 precision;
quint32 constraints;
quint32 originNotNull;
quint32 originUnique;
quint32 originExpression;
quint32 strengthNotNull;
quint32 strengthUnique;
quint32 strengthExpression;
bool applyOnUpdate;
QString name;
QString typeName;
QString comment;
QString alias;
QString defaultValueExpression;
QString constraintExpression;
QString constraintDescription;
in >> name >> type >> typeName >> length >> precision >> comment >> alias
>> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
constraintExpression >> constraintDescription >> subType;
field.setName( name );
field.setType( static_cast< QVariant::Type >( type ) );
field.setTypeName( typeName );
field.setLength( static_cast< int >( length ) );
field.setPrecision( static_cast< int >( precision ) );
field.setComment( comment );
field.setAlias( alias );
field.setDefaultValueDefinition( QgsDefaultValue( defaultValueExpression, applyOnUpdate ) );
QgsFieldConstraints fieldConstraints;
if ( constraints & QgsFieldConstraints::ConstraintNotNull )
{
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthNotNull ) );
}
else
fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintNotNull );
if ( constraints & QgsFieldConstraints::ConstraintUnique )
{
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthUnique ) );
}
else
fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintUnique );
if ( constraints & QgsFieldConstraints::ConstraintExpression )
{
fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
fieldConstraints.setConstraintStrength( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintStrength>( strengthExpression ) );
}
else
fieldConstraints.removeConstraint( QgsFieldConstraints::ConstraintExpression );
fieldConstraints.setConstraintExpression( constraintExpression, constraintDescription );
field.setConstraints( fieldConstraints );
field.setSubType( static_cast< QVariant::Type >( subType ) );
return in;
}