QGIS API Documentation 3.99.0-Master (26c88405ac0)
Loading...
Searching...
No Matches
qgsfield.cpp
Go to the documentation of this file.
1#include "moc_qgsfield.cpp"
2
3/***************************************************************************
4 qgsfield.cpp - Describes a field in a layer or table
5 --------------------------------------
6 Date : 01-Jan-2004
7 Copyright : (C) 2004 by Gary E.Sherman
8 email : sherman at mrcc.com
9
10 ***************************************************************************
11 * *
12 * This program is free software; you can redistribute it and/or modify *
13 * it under the terms of the GNU General Public License as published by *
14 * the Free Software Foundation; either version 2 of the License, or *
15 * (at your option) any later version. *
16 * *
17 ***************************************************************************/
18
19#include "qgsfield_p.h"
20#include "qgis.h"
21#include "qgsapplication.h"
23#include "qgsvariantutils.h"
25
26#include <QDataStream>
27#include <QIcon>
28#include <QLocale>
29#include <QJsonDocument>
30
31/***************************************************************************
32 * This class is considered CRITICAL and any change MUST be accompanied with
33 * full unit tests in testqgsfield.cpp.
34 * See details in QEP #17
35 ****************************************************************************/
36
37#if 0
38QgsField::QgsField( QString nam, QString typ, int len, int prec, bool num,
39 QString comment )
40 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
41 , mComment( comment )
42{
43 // This function used to lower case the field name since some stores
44 // use upper case (e.g., shapefiles), but that caused problems with
45 // attribute actions getting confused between uppercase and
46 // lowercase versions of the attribute names, so just leave the
47 // names how they are now.
48}
49#endif
50QgsField::QgsField( const QString &name, QMetaType::Type type,
51 const QString &typeName, int len, int prec, const QString &comment, QMetaType::Type subType )
52{
53 d = new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
54}
55
56QgsField::QgsField( const QString &name, QVariant::Type type,
57 const QString &typeName, int len, int prec, const QString &comment, QVariant::Type subType )
58 : QgsField( name, QgsVariantUtils::variantTypeToMetaType( type ), typeName, len, prec, comment, QgsVariantUtils::variantTypeToMetaType( subType ) )
59{
60}
61
62QgsField::QgsField( const QgsField &other ) //NOLINT
63 : d( other.d )
64{
65
66}
67
68QgsField::~QgsField() = default;
69
70/***************************************************************************
71 * This class is considered CRITICAL and any change MUST be accompanied with
72 * full unit tests in testqgsfield.cpp.
73 * See details in QEP #17
74 ****************************************************************************/
75
76QgsField &QgsField::operator =( const QgsField &other ) //NOLINT
77{
78 d = other.d;
79 return *this;
80}
81
82bool QgsField::operator==( const QgsField &other ) const
83{
84 return *( other.d ) == *d;
85}
86
87bool QgsField::operator!=( const QgsField &other ) const
88{
89 return !( *this == other );
90}
91
92QString QgsField::name() const
93{
94 return d->name;
95}
96
97QString QgsField::displayName() const
98{
99 if ( !d->alias.isEmpty() )
100 return d->alias;
101 else
102 return d->name;
103}
104
106{
107 if ( alias().isEmpty() )
108 {
109 return name();
110 }
111 return QStringLiteral( "%1 (%2)" ).arg( name(), alias() );
112}
113
114QString QgsField::displayType( const bool showConstraints ) const
115{
116 QString typeStr = typeName();
117 if ( typeStr.isEmpty() )
118 {
120 }
121
122 if ( length() > 0 && precision() > 0 )
123 typeStr += QStringLiteral( "(%1, %2)" ).arg( length() ).arg( precision() );
124 else if ( length() > 0 )
125 typeStr += QStringLiteral( "(%1)" ).arg( length() );
126
127 if ( showConstraints )
128 {
130 ? QStringLiteral( " NOT NULL" )
131 : QStringLiteral( " NULL" );
132
134 ? QStringLiteral( " UNIQUE" )
135 : QString();
136 }
137
138 return typeStr;
139}
140
142{
143 if ( d->type == QMetaType::Type::User )
144 {
145 if ( d->typeName.compare( QLatin1String( "geometry" ), Qt::CaseInsensitive ) == 0 )
146 {
147 return QObject::tr( "Geometry" );
148 }
149 }
150 return QgsVariantUtils::typeToDisplayString( d->type, d->subType );
151}
152
153QMetaType::Type QgsField::type() const
154{
155 return d->type;
156}
157
158QMetaType::Type QgsField::subType() const
159{
160 return d->subType;
161}
162
163QString QgsField::typeName() const
164{
165 return d->typeName;
166}
167
169{
170 return d->length;
171}
172
174{
175 return d->precision;
176}
177
178QString QgsField::comment() const
179{
180 return d->comment;
181}
182
183QVariant QgsField::metadata( int property ) const
184{
185 return d->metadata.value( property );
186}
187
188QMap<int, QVariant> QgsField::metadata() const
189{
190 return d->metadata;
191}
192
194{
195 return d->metadata.value( static_cast< int >( property ) );
196}
197
198void QgsField::setMetadata( const QMap<int, QVariant> metadata )
199{
200 d->metadata = metadata;
201}
202
203void QgsField::setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value )
204{
205 d->metadata[ static_cast< int >( property )] = value;
206}
207
208void QgsField::setMetadata( int property, const QVariant &value )
209{
210 d->metadata[ property ] = value;
211}
212
214{
215 return d->type == QMetaType::Type::Double || d->type == QMetaType::Type::Int || d->type == QMetaType::Type::UInt || d->type == QMetaType::Type::LongLong || d->type == QMetaType::Type::ULongLong;
216}
217
219{
220 return d->type == QMetaType::Type::QDate || d->type == QMetaType::Type::QTime || d->type == QMetaType::Type::QDateTime;
221}
222
223/***************************************************************************
224 * This class is considered CRITICAL and any change MUST be accompanied with
225 * full unit tests in testqgsfield.cpp.
226 * See details in QEP #17
227 ****************************************************************************/
228
229void QgsField::setName( const QString &name )
230{
231 d->name = name;
232}
233
234void QgsField::setType( QMetaType::Type type )
235{
236 d->type = type;
237}
238
243
244void QgsField::setSubType( QMetaType::Type subType )
245{
246 d->subType = subType;
247}
248
253
254void QgsField::setTypeName( const QString &typeName )
255{
256 d->typeName = typeName;
257}
258
259void QgsField::setLength( int len )
260{
261 d->length = len;
262}
264{
265 d->precision = precision;
266}
267
268void QgsField::setComment( const QString &comment )
269{
270 d->comment = comment;
271}
272
274{
275 return d->defaultValueDefinition;
276}
277
282
284{
285 d->constraints = constraints;
286}
287
289{
290 return d->constraints;
291}
292
293QString QgsField::alias() const
294{
295 return d->alias;
296}
297
298void QgsField::setAlias( const QString &alias )
299{
300 d->alias = alias;
301}
302
304{
305 return d->flags;
306}
307
309{
310 d->flags = flags;
311}
312
313/***************************************************************************
314 * This class is considered CRITICAL and any change MUST be accompanied with
315 * full unit tests in testqgsfield.cpp.
316 * See details in QEP #17
317 ****************************************************************************/
318
319QString QgsField::displayString( const QVariant &v ) const
320{
321 if ( QgsVariantUtils::isNull( v ) )
322 {
324 }
325
326 if ( v.userType() == qMetaTypeId<QgsReferencedGeometry>() )
327 {
328 QgsReferencedGeometry geom = qvariant_cast<QgsReferencedGeometry>( v );
329 if ( geom.isNull() )
331 else
332 {
333 QString wkt = geom.asWkt();
334 if ( wkt.length() >= 1050 )
335 {
336 wkt = wkt.left( MAX_WKT_LENGTH ) + QChar( 0x2026 );
337 }
338 QString formattedText = QStringLiteral( "%1 [%2]" ).arg( wkt, geom.crs().userFriendlyIdentifier() );
339 return formattedText;
340 }
341 }
342
343 // Special treatment for numeric types if group separator is set or decimalPoint is not a dot
344 if ( d->type == QMetaType::Type::Double )
345 {
346 // if value doesn't contain a double (a default value expression for instance),
347 // apply no transformation
348 bool ok;
349 v.toDouble( &ok );
350 if ( !ok )
351 return v.toString();
352
353 // Locales with decimal point != '.' or that require group separator: use QLocale
354 if ( QLocale().decimalPoint() != '.' ||
355 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
356 {
357 if ( d->precision > 0 )
358 {
359 if ( -1 < v.toDouble() && v.toDouble() < 1 )
360 {
361 return QLocale().toString( v.toDouble(), 'g', d->precision );
362 }
363 else
364 {
365 return QLocale().toString( v.toDouble(), 'f', d->precision );
366 }
367 }
368 else
369 {
370 // Precision is not set, let's guess it from the
371 // standard conversion to string
372 const QString s( v.toString() );
373 const int dotPosition( s.indexOf( '.' ) );
374 int precision;
375 if ( dotPosition < 0 && s.indexOf( 'e' ) < 0 )
376 {
377 precision = 0;
378 return QLocale().toString( v.toDouble(), 'f', precision );
379 }
380 else
381 {
382 if ( dotPosition < 0 ) precision = 0;
383 else precision = s.length() - dotPosition - 1;
384
385 if ( -1 < v.toDouble() && v.toDouble() < 1 )
386 {
387 return QLocale().toString( v.toDouble(), 'g', precision );
388 }
389 else
390 {
391 return QLocale().toString( v.toDouble(), 'f', precision );
392 }
393 }
394 }
395 }
396 // Default for doubles with precision
397 else if ( d->precision > 0 )
398 {
399 if ( -1 < v.toDouble() && v.toDouble() < 1 )
400 {
401 return QString::number( v.toDouble(), 'g', d->precision );
402 }
403 else
404 {
405 return QString::number( v.toDouble(), 'f', d->precision );
406 }
407 }
408 else
409 {
410 const double vDouble = v.toDouble();
411 // mimic Qt 5 handling of when to switch to exponential forms
412 if ( std::fabs( vDouble ) < 1e-04 )
413 return QString::number( vDouble, 'g', QLocale::FloatingPointShortest );
414 else
415 return QString::number( vDouble, 'f', QLocale::FloatingPointShortest );
416 }
417 }
418 // Other numeric types than doubles
419 else if ( isNumeric() &&
420 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
421 {
422 bool ok;
423 const qlonglong converted( v.toLongLong( &ok ) );
424 if ( ok )
425 return QLocale().toString( converted );
426 }
427 else if ( d->typeName.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || d->typeName == QLatin1String( "jsonb" ) )
428 {
429 const QJsonDocument doc = QJsonDocument::fromVariant( v );
430 return QString::fromUtf8( doc.toJson().constData() );
431 }
432 else if ( d->type == QMetaType::Type::QByteArray )
433 {
434 return QObject::tr( "BLOB" );
435 }
436 else if ( d->type == QMetaType::Type::QStringList || d->type == QMetaType::Type::QVariantList )
437 {
438 QString result;
439 const QVariantList list = v.toList();
440 for ( const QVariant &var : list )
441 {
442 if ( !result.isEmpty() )
443 result.append( QStringLiteral( ", " ) );
444 result.append( var.toString() );
445 }
446 return result;
447 }
448
449 // Fallback if special rules do not apply
450 return v.toString();
451}
452
454{
455 switch ( flag )
456 {
458 return QObject::tr( "None" );
460 return QObject::tr( "Not searchable" );
462 return QObject::tr( "Do not expose via WMS" );
464 return QObject::tr( "Do not expose via WFS" );
465 }
466 return QString();
467}
468
469/***************************************************************************
470 * This class is considered CRITICAL and any change MUST be accompanied with
471 * full unit tests in testqgsfield.cpp.
472 * See details in QEP #17
473 ****************************************************************************/
474
475bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
476{
477 const QVariant original = v;
478 if ( errorMessage )
479 errorMessage->clear();
480
481 if ( QgsVariantUtils::isNull( v ) )
482 {
483 v.convert( d->type );
484 return true;
485 }
486
488 {
489 return true;
490 }
491
492 if ( d->type == QMetaType::Type::Int && v.toInt() != v.toLongLong() )
493 {
495 if ( errorMessage )
496 *errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toLongLong() );
497 return false;
498 }
499
500 // Give it a chance to convert to double since for not '.' locales
501 // we accept both comma and dot as decimal point
502 if ( d->type == QMetaType::Type::Double && v.userType() == QMetaType::Type::QString )
503 {
504 QVariant tmp( v );
505 if ( !tmp.convert( d->type ) )
506 {
507 // This might be a string with thousand separator: use locale to convert
508 bool ok = false;
509 double d = qgsPermissiveToDouble( v.toString(), ok );
510 if ( ok )
511 {
512 v = QVariant( d );
513 return true;
514 }
515 // For not 'dot' locales, we also want to accept '.'
516 if ( QLocale().decimalPoint() != '.' )
517 {
518 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
519 if ( ok )
520 {
521 v = QVariant( d );
522 return true;
523 }
524 }
525 }
526 }
527
528 // For string representation of an int we also might have thousand separator
529 if ( d->type == QMetaType::Type::Int && v.userType() == QMetaType::Type::QString )
530 {
531 QVariant tmp( v );
532 if ( !tmp.convert( d->type ) )
533 {
534 // This might be a string with thousand separator: use locale to convert
535 bool ok;
536 const int i = qgsPermissiveToInt( v.toString(), ok );
537 if ( ok )
538 {
539 v = QVariant( i );
540 return true;
541 }
542 }
543 }
544
545 // For string representation of a long we also might have thousand separator
546 if ( d->type == QMetaType::Type::LongLong && v.userType() == QMetaType::Type::QString )
547 {
548 QVariant tmp( v );
549 if ( !tmp.convert( d->type ) )
550 {
551 // This might be a string with thousand separator: use locale to convert
552 bool ok;
553 const qlonglong l = qgsPermissiveToLongLong( v.toString(), ok );
554 if ( ok )
555 {
556 v = QVariant( l );
557 return true;
558 }
559 }
560 }
561
562 //String representations of doubles in QVariant will return false to convert( QVariant::Int )
563 //work around this by first converting to double, and then checking whether the double is convertible to int
564 if ( d->type == QMetaType::Type::Int && v.canConvert( QMetaType::Type::Double ) )
565 {
566 bool ok = false;
567 const double dbl = v.toDouble( &ok );
568 if ( !ok )
569 {
570 //couldn't convert to number
572
573 if ( errorMessage )
574 *errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
575
576 return false;
577 }
578
579 const double round = std::round( dbl );
580 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
581 {
582 //double too large to fit in int
584
585 if ( errorMessage )
586 *errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toDouble() );
587
588 return false;
589 }
590 v = QVariant( static_cast< int >( std::round( dbl ) ) );
591 return true;
592 }
593
594 //String representations of doubles in QVariant will return false to convert( QVariant::LongLong )
595 //work around this by first converting to double, and then checking whether the double is convertible to longlong
596 if ( d->type == QMetaType::Type::LongLong && v.canConvert( QMetaType::Type::Double ) )
597 {
598 //firstly test the conversion to longlong because conversion to double will rounded the value
599 QVariant tmp( v );
600 if ( !tmp.convert( d->type ) )
601 {
602 bool ok = false;
603 const double dbl = v.toDouble( &ok );
604 if ( !ok )
605 {
606 //couldn't convert to number
608
609 if ( errorMessage )
610 *errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
611
612 return false;
613 }
614
615 const double round = std::round( dbl );
616 if ( round > static_cast<double>( std::numeric_limits<long long>::max() ) || round < static_cast<double>( -std::numeric_limits<long long>::max() ) )
617 {
618 //double too large to fit in longlong
620
621 if ( errorMessage )
622 *errorMessage = QObject::tr( "Value \"%1\" is too large for long long field" ).arg( original.toDouble() );
623
624 return false;
625 }
626 v = QVariant( static_cast< long long >( std::round( dbl ) ) );
627 return true;
628 }
629 }
630
631 if ( d->typeName.compare( QLatin1String( "json" ), Qt::CaseInsensitive ) == 0 || d->typeName.compare( QLatin1String( "jsonb" ), Qt::CaseInsensitive ) == 0 )
632 {
633 if ( d->type == QMetaType::Type::QString )
634 {
635 const QJsonDocument doc = QJsonDocument::fromVariant( v );
636 if ( !doc.isNull() )
637 {
638 v = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).constData() );
639 return true;
640 }
642 return false;
643 }
644 else if ( d->type == QMetaType::Type::QVariantMap )
645 {
646 if ( v.userType() == QMetaType::Type::QStringList || v.userType() == QMetaType::Type::QVariantList || v.userType() == QMetaType::Type::QVariantMap )
647 {
648 return true;
649 }
651 return false;
652 }
653 }
654
655 if ( ( d->type == QMetaType::Type::QStringList || ( d->type == QMetaType::Type::QVariantList && d->subType == QMetaType::Type::QString ) )
656 && ( v.userType() == QMetaType::Type::QString ) )
657 {
658 v = QStringList( { v.toString() } );
659 return true;
660 }
661
662 if ( ( d->type == QMetaType::Type::QStringList || d->type == QMetaType::Type::QVariantList ) && !( v.userType() == QMetaType::Type::QStringList || v.userType() == QMetaType::Type::QVariantList ) )
663 {
665
666 if ( errorMessage )
667 *errorMessage = QObject::tr( "Could not convert value \"%1\" to target list type" ).arg( original.toString() );
668
669 return false;
670 }
671
672 // Handle referenced geometries (e.g. from additional geometry fields)
673 if ( d->type == QMetaType::Type::QString && v.userType() == qMetaTypeId<QgsReferencedGeometry>() )
674 {
675 const QgsReferencedGeometry geom { v.value<QgsReferencedGeometry>( ) };
676 if ( geom.isNull() )
677 {
679 }
680 else
681 {
682 v = QVariant( geom.asWkt() );
683 }
684 return true;
685 }
686 else if ( d->type == QMetaType::Type::User && d->typeName.compare( QLatin1String( "geometry" ), Qt::CaseInsensitive ) == 0 )
687 {
688 if ( v.userType() == qMetaTypeId<QgsReferencedGeometry>() || v.userType() == qMetaTypeId< QgsGeometry>() )
689 {
690 return true;
691 }
692 else if ( v.userType() == QMetaType::Type::QString )
693 {
694 const QgsGeometry geom = QgsGeometry::fromWkt( v.toString() );
695 if ( !geom.isNull() )
696 {
697 v = QVariant::fromValue( geom );
698 return true;
699 }
700 }
701 return false;
702 }
703 else if ( !v.convert( d->type ) )
704 {
706
707 if ( errorMessage )
708 *errorMessage = QObject::tr( "Could not convert value \"%1\" to target type \"%2\"" )
709 .arg( original.toString(),
710 d->typeName );
711
712 return false;
713 }
714
715 if ( d->type == QMetaType::Type::Double && d->precision > 0 )
716 {
717 const double s = std::pow( 10, d->precision );
718 const double d = v.toDouble() * s;
719 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
720 return true;
721 }
722
723 if ( d->type == QMetaType::Type::QString && d->length > 0 && v.toString().length() > d->length )
724 {
725 const int length = v.toString().length();
726 v = v.toString().left( d->length );
727
728 if ( errorMessage )
729 *errorMessage = QObject::tr( "String of length %1 exceeds maximum field length (%2)" ).arg( length ).arg( d->length );
730
731 return false;
732 }
733
734 return true;
735}
736
737QgsField::operator QVariant() const
738{
739 return QVariant::fromValue( *this );
740}
741
743{
744 d->editorWidgetSetup = v;
745}
746
748{
749 return d->editorWidgetSetup;
750}
751
752void QgsField::setReadOnly( bool readOnly )
753{
754 d->isReadOnly = readOnly;
755}
756
758{
759 return d->isReadOnly;
760}
761
763{
764 return d->splitPolicy;
765}
766
768{
769 d->splitPolicy = policy;
770}
771
773{
774 return d->duplicatePolicy;
775}
776
778{
779 d->duplicatePolicy = policy;
780}
781
783{
784 return d->mergePolicy;
785}
786
788{
789 d->mergePolicy = policy;
790}
791
792/***************************************************************************
793 * This class is considered CRITICAL and any change MUST be accompanied with
794 * full unit tests in testqgsfield.cpp.
795 * See details in QEP #17
796 ****************************************************************************/
797
798QDataStream &operator<<( QDataStream &out, const QgsField &field )
799{
800 out << field.name();
801 out << static_cast< quint32 >( field.type() );
802 out << field.typeName();
803 out << field.length();
804 out << field.precision();
805 out << field.comment();
806 out << field.alias();
807 out << field.defaultValueDefinition().expression();
808 out << field.defaultValueDefinition().applyOnUpdate();
809 out << field.constraints().constraints();
810 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) );
811 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ) );
812 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintExpression ) );
813 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
814 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
815 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
816 out << field.constraints().constraintExpression();
817 out << field.constraints().constraintDescription();
818 out << static_cast< quint32 >( field.subType() );
819 out << static_cast< int >( field.splitPolicy() );
820 out << static_cast< int >( field.duplicatePolicy() );
821 out << field.metadata();
822 return out;
823}
824
825QDataStream &operator>>( QDataStream &in, QgsField &field )
826{
827 quint32 type;
828 quint32 subType;
829 quint32 length;
830 quint32 precision;
831 quint32 constraints;
832 quint32 originNotNull;
833 quint32 originUnique;
834 quint32 originExpression;
835 quint32 strengthNotNull;
836 quint32 strengthUnique;
837 quint32 strengthExpression;
838 int splitPolicy;
839 int duplicatePolicy;
840
841 bool applyOnUpdate;
842
843 QString name;
844 QString typeName;
845 QString comment;
846 QString alias;
847 QString defaultValueExpression;
848 QString constraintExpression;
849 QString constraintDescription;
850 QMap< int, QVariant > metadata;
851
852 in >> name >> type >> typeName >> length >> precision >> comment >> alias
853 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
854 constraintExpression >> constraintDescription >> subType >> splitPolicy >> duplicatePolicy >> metadata;
855 field.setName( name );
856 field.setType( static_cast< QMetaType::Type >( type ) );
857 field.setTypeName( typeName );
858 field.setLength( static_cast< int >( length ) );
859 field.setPrecision( static_cast< int >( precision ) );
860 field.setComment( comment );
861 field.setAlias( alias );
862 field.setDefaultValueDefinition( QgsDefaultValue( defaultValueExpression, applyOnUpdate ) );
863 field.setSplitPolicy( static_cast< Qgis::FieldDomainSplitPolicy >( splitPolicy ) );
864 field.setDuplicatePolicy( static_cast< Qgis::FieldDuplicatePolicy >( duplicatePolicy ) );
865 QgsFieldConstraints fieldConstraints;
866 if ( constraints & QgsFieldConstraints::ConstraintNotNull )
867 {
868 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
870 }
871 else
873 if ( constraints & QgsFieldConstraints::ConstraintUnique )
874 {
875 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
877 }
878 else
881 {
882 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
884 }
885 else
887 fieldConstraints.setConstraintExpression( constraintExpression, constraintDescription );
888 field.setConstraints( fieldConstraints );
889 field.setSubType( static_cast< QMetaType::Type >( subType ) );
890 field.setMetadata( metadata );
891 return in;
892}
FieldDomainMergePolicy
Merge policy for field domains.
Definition qgis.h:3923
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:3906
FieldDuplicatePolicy
Duplicate policy for fields.
Definition qgis.h:3943
FieldMetadataProperty
Standard field metadata values.
Definition qgis.h:1745
FieldConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1721
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
Definition qgis.h:1725
@ NoFlag
No flag is defined.
Definition qgis.h:1722
@ NotSearchable
Defines if the field is searchable (used in the locator search for instance).
Definition qgis.h:1723
@ HideFromWms
Field is not available if layer is served as WMS from QGIS server.
Definition qgis.h:1724
QFlags< FieldConfigurationFlag > FieldConfigurationFlags
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1736
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
QString userFriendlyIdentifier(Qgis::CrsIdentifierType type=Qgis::CrsIdentifierType::MediumString) const
Returns a user friendly identifier for the CRS.
Provides a container for managing client side default values for fields.
Holder for the widget type and its configuration for a field.
Stores information about constraints which may be present on a field.
ConstraintStrength
Strength of constraints.
void setConstraintStrength(Constraint constraint, ConstraintStrength strength)
Sets the strength of a constraint.
void setConstraintExpression(const QString &expression, const QString &description=QString())
Set the constraint expression for the field.
ConstraintOrigin
Origin of constraints.
ConstraintStrength constraintStrength(Constraint constraint) const
Returns the strength of a field constraint, or ConstraintStrengthNotSet if the constraint is not pres...
ConstraintOrigin constraintOrigin(Constraint constraint) const
Returns the origin of a field constraint, or ConstraintOriginNotSet if the constraint is not present ...
QString constraintExpression() const
Returns the constraint expression for the field, if set.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
@ ConstraintExpression
Field has an expression constraint set. See constraintExpression().
void removeConstraint(Constraint constraint)
Removes a constraint from the field.
QString constraintDescription() const
Returns the descriptive name for the constraint expression.
void setConstraint(Constraint constraint, ConstraintOrigin origin=ConstraintOriginLayer)
Sets a constraint on the field.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:54
void setSplitPolicy(Qgis::FieldDomainSplitPolicy policy)
Sets the field's split policy, which indicates how field values should be handled during a split oper...
Definition qgsfield.cpp:767
QMetaType::Type type
Definition qgsfield.h:61
bool isDateOrTime
Definition qgsfield.h:58
void setEditorWidgetSetup(const QgsEditorWidgetSetup &v)
Set the editor widget setup for the field.
Definition qgsfield.cpp:742
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:163
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
Definition qgsfield.cpp:283
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:298
QString name
Definition qgsfield.h:63
bool operator!=(const QgsField &other) const
Definition qgsfield.cpp:87
bool operator==(const QgsField &other) const
Definition qgsfield.cpp:82
int precision
Definition qgsfield.h:60
int length
Definition qgsfield.h:59
QgsField & operator=(const QgsField &other)
Definition qgsfield.cpp:76
QString displayString(const QVariant &v) const
Formats string for display.
Definition qgsfield.cpp:319
void setPrecision(int precision)
Set the field precision.
Definition qgsfield.cpp:263
void setDuplicatePolicy(Qgis::FieldDuplicatePolicy policy)
Sets the field's duplicate policy, which indicates how field values should be handled during a duplic...
Definition qgsfield.cpp:777
QString displayNameWithAlias() const
Returns the name to use when displaying this field and adds the alias in parenthesis if it is defined...
Definition qgsfield.cpp:105
QgsField(const QString &name=QString(), QMetaType::Type type=QMetaType::Type::UnknownType, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString(), QMetaType::Type subType=QMetaType::Type::UnknownType)
Constructor.
Definition qgsfield.cpp:50
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:475
void setMergePolicy(Qgis::FieldDomainMergePolicy policy)
Sets the field's merge policy, which indicates how field values should be handled during a merge oper...
Definition qgsfield.cpp:787
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:244
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:229
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:268
void setType(QMetaType::Type type)
Set variant type.
Definition qgsfield.cpp:234
QString displayType(bool showConstraints=false) const
Returns the type to use when displaying this field, including the length and precision of the datatyp...
Definition qgsfield.cpp:114
void setConfigurationFlags(Qgis::FieldConfigurationFlags flags)
Sets the Flags for the field (searchable, …).
Definition qgsfield.cpp:308
Qgis::FieldDomainSplitPolicy splitPolicy() const
Returns the field's split policy, which indicates how field values should be handled during a split o...
Definition qgsfield.cpp:762
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:259
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:97
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
Definition qgsfield.cpp:278
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
Definition qgsfield.cpp:141
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
Definition qgsfield.cpp:752
QMap< int, QVariant > metadata() const
Returns the map of field metadata.
Definition qgsfield.cpp:188
static QString readableConfigurationFlag(Qgis::FieldConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
Definition qgsfield.cpp:453
Qgis::FieldConfigurationFlags configurationFlags
Definition qgsfield.h:67
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:158
QString alias
Definition qgsfield.h:64
Qgis::FieldDuplicatePolicy duplicatePolicy() const
Returns the field's duplicate policy, which indicates how field values should be handled during a dup...
Definition qgsfield.cpp:772
static constexpr int MAX_WKT_LENGTH
Definition qgsfield.h:578
QgsDefaultValue defaultValueDefinition
Definition qgsfield.h:65
bool isNumeric
Definition qgsfield.h:57
Qgis::FieldDomainMergePolicy mergePolicy() const
Returns the field's merge policy, which indicates how field values should be handled during a merge o...
Definition qgsfield.cpp:782
void setMetadata(const QMap< int, QVariant > metadata)
Sets the map of field metadata.
Definition qgsfield.cpp:198
QString comment
Definition qgsfield.h:62
virtual ~QgsField()
QgsFieldConstraints constraints
Definition qgsfield.h:66
QgsEditorWidgetSetup editorWidgetSetup() const
Gets the editor widget setup for the field.
Definition qgsfield.cpp:747
bool isReadOnly
Definition qgsfield.h:68
void setTypeName(const QString &typeName)
Set the field type.
Definition qgsfield.cpp:254
A geometry is the spatial representation of a feature.
static Q_INVOKABLE QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
Q_INVOKABLE QString asWkt(int precision=17) const
Exports the geometry to WKT.
QgsCoordinateReferenceSystem crs() const
Returns the associated coordinate reference system, or an invalid CRS if no reference system is set.
A QgsGeometry with associated coordinate reference system.
Contains utility functions for working with QVariants and QVariant types.
static QMetaType::Type variantTypeToMetaType(QVariant::Type variantType)
Converts a QVariant::Type to a QMetaType::Type.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
static QString typeToDisplayString(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType)
Returns a user-friendly translated string representing a QVariant type.
static QVariant createNullVariant(QMetaType::Type metaType)
Helper method to properly create a null QVariant from a metaType Returns the created QVariant.
static bool isUnsetAttributeValue(const QVariant &variant)
Check if the variant is a QgsUnsetAttributeValue.
qlonglong qgsPermissiveToLongLong(QString string, bool &ok)
Converts a string to an qlonglong in a permissive way, e.g., allowing for incorrect numbers of digits...
Definition qgis.cpp:97
double qgsPermissiveToDouble(QString string, bool &ok)
Converts a string to a double in a permissive way, e.g., allowing for incorrect numbers of digits bet...
Definition qgis.cpp:83
int qgsPermissiveToInt(QString string, bool &ok)
Converts a string to an integer in a permissive way, e.g., allowing for incorrect numbers of digits b...
Definition qgis.cpp:90
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:825
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:798