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