QGIS API Documentation 3.99.0-Master (d270888f95f)
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,
55 const QString &typeName, int len, int prec, const QString &comment, QMetaType::Type subType )
56{
57 d = new QgsFieldPrivate( name, type, subType, typeName, len, prec, comment );
58}
59
60QgsField::QgsField( const QString &name, QVariant::Type type,
61 const QString &typeName, int len, int prec, const QString &comment, QVariant::Type subType )
62 : QgsField( name, QgsVariantUtils::variantTypeToMetaType( type ), typeName, len, prec, comment, QgsVariantUtils::variantTypeToMetaType( subType ) )
63{
64}
65
66QgsField::QgsField( const QgsField &other ) //NOLINT
67 : d( other.d )
68{
69
70}
71
72QgsField::~QgsField() = default;
73
74/***************************************************************************
75 * This class is considered CRITICAL and any change MUST be accompanied with
76 * full unit tests in testqgsfield.cpp.
77 * See details in QEP #17
78 ****************************************************************************/
79
80QgsField &QgsField::operator =( const QgsField &other ) //NOLINT
81{
82 d = other.d;
83 return *this;
84}
85
86bool QgsField::operator==( const QgsField &other ) const
87{
88 return *( other.d ) == *d;
89}
90
91bool QgsField::operator!=( const QgsField &other ) const
92{
93 return !( *this == other );
94}
95
96QString QgsField::name() const
97{
98 return d->name;
99}
100
102{
103 if ( !d->alias.isEmpty() )
104 return d->alias;
105 else
106 return d->name;
107}
108
110{
111 if ( alias().isEmpty() )
112 {
113 return name();
114 }
115 return u"%1 (%2)"_s.arg( name(), alias() );
116}
117
118QString QgsField::displayType( const bool showConstraints ) const
119{
120 QString typeStr = typeName();
121 if ( typeStr.isEmpty() )
122 {
124 }
125
126 if ( length() > 0 && precision() > 0 )
127 typeStr += u"(%1, %2)"_s.arg( length() ).arg( precision() );
128 else if ( length() > 0 )
129 typeStr += u"(%1)"_s.arg( length() );
130
131 if ( showConstraints )
132 {
134 ? u" NOT NULL"_s
135 : u" NULL"_s;
136
138 ? u" UNIQUE"_s
139 : QString();
140 }
141
142 return typeStr;
143}
144
146{
147 if ( d->type == QMetaType::Type::User )
148 {
149 if ( d->typeName.compare( "geometry"_L1, Qt::CaseInsensitive ) == 0 )
150 {
151 return QObject::tr( "Geometry" );
152 }
153 }
154 return QgsVariantUtils::typeToDisplayString( d->type, d->subType );
155}
156
157QMetaType::Type QgsField::type() const
158{
159 return d->type;
160}
161
162QMetaType::Type QgsField::subType() const
163{
164 return d->subType;
165}
166
167QString QgsField::typeName() const
168{
169 return d->typeName;
170}
171
173{
174 return d->length;
175}
176
178{
179 return d->precision;
180}
181
182QString QgsField::comment() const
183{
184 return d->comment;
185}
186
187QVariant QgsField::metadata( int property ) const
188{
189 return d->metadata.value( property );
190}
191
192QMap<int, QVariant> QgsField::metadata() const
193{
194 return d->metadata;
195}
196
198{
199 return d->metadata.value( static_cast< int >( property ) );
200}
201
202void QgsField::setMetadata( const QMap<int, QVariant> metadata )
203{
204 d->metadata = metadata;
205}
206
207void QgsField::setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value )
208{
209 d->metadata[ static_cast< int >( property )] = value;
210}
211
212void QgsField::setMetadata( int property, const QVariant &value )
213{
214 d->metadata[ property ] = value;
215}
216
218{
219 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;
220}
221
223{
224 return d->type == QMetaType::Type::QDate || d->type == QMetaType::Type::QTime || d->type == QMetaType::Type::QDateTime;
225}
226
227/***************************************************************************
228 * This class is considered CRITICAL and any change MUST be accompanied with
229 * full unit tests in testqgsfield.cpp.
230 * See details in QEP #17
231 ****************************************************************************/
232
233void QgsField::setName( const QString &name )
234{
235 d->name = name;
236}
237
238void QgsField::setType( QMetaType::Type type )
239{
240 d->type = type;
241}
242
247
248void QgsField::setSubType( QMetaType::Type subType )
249{
250 d->subType = subType;
251}
252
257
258void QgsField::setTypeName( const QString &typeName )
259{
260 d->typeName = typeName;
261}
262
263void QgsField::setLength( int len )
264{
265 d->length = len;
266}
268{
269 d->precision = precision;
270}
271
272void QgsField::setComment( const QString &comment )
273{
274 d->comment = comment;
275}
276
278{
279 return d->defaultValueDefinition;
280}
281
286
288{
289 d->constraints = constraints;
290}
291
293{
294 return d->constraints;
295}
296
297QString QgsField::alias() const
298{
299 return d->alias;
300}
301
302void QgsField::setAlias( const QString &alias )
303{
304 d->alias = alias;
305}
306
308{
309 return d->flags;
310}
311
313{
314 d->flags = flags;
315}
316
317/***************************************************************************
318 * This class is considered CRITICAL and any change MUST be accompanied with
319 * full unit tests in testqgsfield.cpp.
320 * See details in QEP #17
321 ****************************************************************************/
322
323QString QgsField::displayString( const QVariant &v ) const
324{
325 if ( QgsVariantUtils::isNull( v ) )
326 {
328 }
329
330 if ( v.userType() == qMetaTypeId<QgsReferencedGeometry>() )
331 {
332 QgsReferencedGeometry geom = qvariant_cast<QgsReferencedGeometry>( v );
333 if ( geom.isNull() )
335 else
336 {
337 QString wkt = geom.asWkt();
338 if ( wkt.length() >= 1050 )
339 {
340 wkt = wkt.left( MAX_WKT_LENGTH ) + QChar( 0x2026 );
341 }
342 QString formattedText = u"%1 [%2]"_s.arg( wkt, geom.crs().userFriendlyIdentifier() );
343 return formattedText;
344 }
345 }
346
347 // Special treatment for numeric types if group separator is set or decimalPoint is not a dot
348 if ( d->type == QMetaType::Type::Double )
349 {
350 // if value doesn't contain a double (a default value expression for instance),
351 // apply no transformation
352 bool ok;
353 v.toDouble( &ok );
354 if ( !ok )
355 return v.toString();
356
357 // Locales with decimal point != '.' or that require group separator: use QLocale
358 if ( QLocale().decimalPoint() != '.' ||
359 !( 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 ) precision = 0;
387 else precision = s.length() - dotPosition - 1;
388
389 if ( -1 < v.toDouble() && v.toDouble() < 1 )
390 {
391 return QLocale().toString( v.toDouble(), 'g', precision );
392 }
393 else
394 {
395 return QLocale().toString( v.toDouble(), 'f', precision );
396 }
397 }
398 }
399 }
400 // Default for doubles with precision
401 else if ( d->precision > 0 )
402 {
403 if ( -1 < v.toDouble() && v.toDouble() < 1 )
404 {
405 return QString::number( v.toDouble(), 'g', d->precision );
406 }
407 else
408 {
409 return QString::number( v.toDouble(), 'f', d->precision );
410 }
411 }
412 else
413 {
414 const double vDouble = v.toDouble();
415 // mimic Qt 5 handling of when to switch to exponential forms
416 if ( std::fabs( vDouble ) < 1e-04 )
417 return QString::number( vDouble, 'g', QLocale::FloatingPointShortest );
418 else
419 return QString::number( vDouble, 'f', QLocale::FloatingPointShortest );
420 }
421 }
422 // Other numeric types than doubles
423 else if ( isNumeric() &&
424 !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
425 {
426 bool ok;
427 const qlonglong converted( v.toLongLong( &ok ) );
428 if ( ok )
429 return QLocale().toString( converted );
430 }
431 else if ( d->typeName.compare( "json"_L1, Qt::CaseInsensitive ) == 0 || d->typeName == "jsonb"_L1 )
432 {
433 const QJsonDocument doc = QJsonDocument::fromVariant( v );
434 return QString::fromUtf8( doc.toJson().constData() );
435 }
436 else if ( d->type == QMetaType::Type::QByteArray )
437 {
438 return QObject::tr( "BLOB" );
439 }
440 else if ( d->type == QMetaType::Type::QStringList || d->type == QMetaType::Type::QVariantList )
441 {
442 QString result;
443 const QVariantList list = v.toList();
444 for ( const QVariant &var : list )
445 {
446 if ( !result.isEmpty() )
447 result.append( u", "_s );
448 result.append( var.toString() );
449 }
450 return result;
451 }
452
453 // Fallback if special rules do not apply
454 return v.toString();
455}
456
458{
459 switch ( flag )
460 {
462 return QObject::tr( "None" );
464 return QObject::tr( "Not searchable" );
466 return QObject::tr( "Do not expose via WMS" );
468 return QObject::tr( "Do not expose via WFS" );
469 }
470 return QString();
471}
472
473/***************************************************************************
474 * This class is considered CRITICAL and any change MUST be accompanied with
475 * full unit tests in testqgsfield.cpp.
476 * See details in QEP #17
477 ****************************************************************************/
478
479bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
480{
481 const QVariant original = v;
482 if ( errorMessage )
483 errorMessage->clear();
484
485 if ( QgsVariantUtils::isNull( v ) )
486 {
487 ( void )v.convert( d->type );
488 return true;
489 }
490
492 {
493 return true;
494 }
495
496 if ( d->type == QMetaType::Type::Int && v.toInt() != v.toLongLong() )
497 {
499 if ( errorMessage )
500 *errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toLongLong() );
501 return false;
502 }
503
504 // Give it a chance to convert to double since for not '.' locales
505 // we accept both comma and dot as decimal point
506 if ( d->type == QMetaType::Type::Double && v.userType() == QMetaType::Type::QString )
507 {
508 QVariant tmp( v );
509 if ( !tmp.convert( d->type ) )
510 {
511 // This might be a string with thousand separator: use locale to convert
512 bool ok = false;
513 double d = qgsPermissiveToDouble( v.toString(), ok );
514 if ( ok )
515 {
516 v = QVariant( d );
517 return true;
518 }
519 // For not 'dot' locales, we also want to accept '.'
520 if ( QLocale().decimalPoint() != '.' )
521 {
522 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
523 if ( ok )
524 {
525 v = QVariant( d );
526 return true;
527 }
528 }
529 }
530 }
531
532 // For string representation of an int we also might have thousand separator
533 if ( d->type == QMetaType::Type::Int && v.userType() == QMetaType::Type::QString )
534 {
535 QVariant tmp( v );
536 if ( !tmp.convert( d->type ) )
537 {
538 // This might be a string with thousand separator: use locale to convert
539 bool ok;
540 const int i = qgsPermissiveToInt( v.toString(), ok );
541 if ( ok )
542 {
543 v = QVariant( i );
544 return true;
545 }
546 }
547 }
548
549 // For string representation of a long we also might have thousand separator
550 if ( d->type == QMetaType::Type::LongLong && v.userType() == QMetaType::Type::QString )
551 {
552 QVariant tmp( v );
553 if ( !tmp.convert( d->type ) )
554 {
555 // This might be a string with thousand separator: use locale to convert
556 bool ok;
557 const qlonglong l = qgsPermissiveToLongLong( v.toString(), ok );
558 if ( ok )
559 {
560 v = QVariant( l );
561 return true;
562 }
563 }
564 }
565
566 //String representations of doubles in QVariant will return false to convert( QVariant::Int )
567 //work around this by first converting to double, and then checking whether the double is convertible to int
568 if ( d->type == QMetaType::Type::Int && v.canConvert( QMetaType::Type::Double ) )
569 {
570 bool ok = false;
571 const double dbl = v.toDouble( &ok );
572 if ( !ok )
573 {
574 //couldn't convert to number
576
577 if ( errorMessage )
578 *errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
579
580 return false;
581 }
582
583 const double round = std::round( dbl );
584 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
585 {
586 //double too large to fit in int
588
589 if ( errorMessage )
590 *errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toDouble() );
591
592 return false;
593 }
594 v = QVariant( static_cast< int >( std::round( dbl ) ) );
595 return true;
596 }
597
598 //String representations of doubles in QVariant will return false to convert( QVariant::LongLong )
599 //work around this by first converting to double, and then checking whether the double is convertible to longlong
600 if ( d->type == QMetaType::Type::LongLong && v.canConvert( QMetaType::Type::Double ) )
601 {
602 //firstly test the conversion to longlong because conversion to double will rounded the value
603 QVariant tmp( v );
604 if ( !tmp.convert( d->type ) )
605 {
606 bool ok = false;
607 const double dbl = v.toDouble( &ok );
608 if ( !ok )
609 {
610 //couldn't convert to number
612
613 if ( errorMessage )
614 *errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
615
616 return false;
617 }
618
619 const double round = std::round( dbl );
620 if ( round > static_cast<double>( std::numeric_limits<long long>::max() ) || round < static_cast<double>( -std::numeric_limits<long long>::max() ) )
621 {
622 //double too large to fit in longlong
624
625 if ( errorMessage )
626 *errorMessage = QObject::tr( "Value \"%1\" is too large for long long field" ).arg( original.toDouble() );
627
628 return false;
629 }
630 v = QVariant( static_cast< long long >( std::round( dbl ) ) );
631 return true;
632 }
633 }
634
635 if ( d->typeName.compare( "json"_L1, Qt::CaseInsensitive ) == 0 || d->typeName.compare( "jsonb"_L1, Qt::CaseInsensitive ) == 0 )
636 {
637 if ( d->type == QMetaType::Type::QString )
638 {
639 const QJsonDocument doc = QJsonDocument::fromVariant( v );
640 if ( !doc.isNull() )
641 {
642 v = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).constData() );
643 return true;
644 }
646 return false;
647 }
648 else if ( d->type == QMetaType::Type::QVariantMap )
649 {
650 if ( v.userType() == QMetaType::Type::QStringList || v.userType() == QMetaType::Type::QVariantList || v.userType() == QMetaType::Type::QVariantMap )
651 {
652 return true;
653 }
655 return false;
656 }
657 }
658
659 if ( ( d->type == QMetaType::Type::QStringList || ( d->type == QMetaType::Type::QVariantList && d->subType == QMetaType::Type::QString ) )
660 && ( v.userType() == QMetaType::Type::QString ) )
661 {
662 v = QStringList( { v.toString() } );
663 return true;
664 }
665
666 if ( ( d->type == QMetaType::Type::QStringList || d->type == QMetaType::Type::QVariantList ) && !( v.userType() == QMetaType::Type::QStringList || v.userType() == QMetaType::Type::QVariantList ) )
667 {
669
670 if ( errorMessage )
671 *errorMessage = QObject::tr( "Could not convert value \"%1\" to target list type" ).arg( original.toString() );
672
673 return false;
674 }
675
676 // Handle referenced geometries (e.g. from additional geometry fields)
677 if ( d->type == QMetaType::Type::QString && v.userType() == qMetaTypeId<QgsReferencedGeometry>() )
678 {
679 const QgsReferencedGeometry geom { v.value<QgsReferencedGeometry>( ) };
680 if ( geom.isNull() )
681 {
683 }
684 else
685 {
686 v = QVariant( geom.asWkt() );
687 }
688 return true;
689 }
690 else if ( d->type == QMetaType::Type::User && d->typeName.compare( "geometry"_L1, Qt::CaseInsensitive ) == 0 )
691 {
692 if ( v.userType() == qMetaTypeId<QgsReferencedGeometry>() || v.userType() == qMetaTypeId< QgsGeometry>() )
693 {
694 return true;
695 }
696 else if ( v.userType() == QMetaType::Type::QString )
697 {
698 const QgsGeometry geom = QgsGeometry::fromWkt( v.toString() );
699 if ( !geom.isNull() )
700 {
701 v = QVariant::fromValue( geom );
702 return true;
703 }
704 }
705 return false;
706 }
707 else if ( !v.convert( d->type ) )
708 {
710
711 if ( errorMessage )
712 *errorMessage = QObject::tr( "Could not convert value \"%1\" to target type \"%2\"" )
713 .arg( original.toString(),
714 d->typeName );
715
716 return false;
717 }
718
719 if ( d->type == QMetaType::Type::Double && d->precision > 0 )
720 {
721 const double s = std::pow( 10, d->precision );
722 const double d = v.toDouble() * s;
723 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
724 return true;
725 }
726
727 if ( d->type == QMetaType::Type::QString && d->length > 0 && v.toString().length() > d->length )
728 {
729 const int length = v.toString().length();
730 v = v.toString().left( d->length );
731
732 if ( errorMessage )
733 *errorMessage = QObject::tr( "String of length %1 exceeds maximum field length (%2)" ).arg( length ).arg( d->length );
734
735 return false;
736 }
737
738 return true;
739}
740
741QgsField::operator QVariant() const
742{
743 return QVariant::fromValue( *this );
744}
745
747{
748 d->editorWidgetSetup = v;
749}
750
752{
753 return d->editorWidgetSetup;
754}
755
756void QgsField::setReadOnly( bool readOnly )
757{
758 d->isReadOnly = readOnly;
759}
760
762{
763 return d->isReadOnly;
764}
765
767{
768 return d->splitPolicy;
769}
770
772{
773 d->splitPolicy = policy;
774}
775
777{
778 return d->duplicatePolicy;
779}
780
782{
783 d->duplicatePolicy = policy;
784}
785
787{
788 return d->mergePolicy;
789}
790
792{
793 d->mergePolicy = policy;
794}
795
796/***************************************************************************
797 * This class is considered CRITICAL and any change MUST be accompanied with
798 * full unit tests in testqgsfield.cpp.
799 * See details in QEP #17
800 ****************************************************************************/
801
802QDataStream &operator<<( QDataStream &out, const QgsField &field )
803{
804 out << field.name();
805 out << static_cast< quint32 >( field.type() );
806 out << field.typeName();
807 out << field.length();
808 out << field.precision();
809 out << field.comment();
810 out << field.alias();
811 out << field.defaultValueDefinition().expression();
812 out << field.defaultValueDefinition().applyOnUpdate();
813 out << field.constraints().constraints();
814 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) );
815 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ) );
816 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintExpression ) );
817 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
818 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
819 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
820 out << field.constraints().constraintExpression();
821 out << field.constraints().constraintDescription();
822 out << static_cast< quint32 >( field.subType() );
823 out << static_cast< int >( field.splitPolicy() );
824 out << static_cast< int >( field.duplicatePolicy() );
825 out << field.metadata();
826 return out;
827}
828
829QDataStream &operator>>( QDataStream &in, QgsField &field )
830{
831 quint32 type;
832 quint32 subType;
833 quint32 length;
834 quint32 precision;
835 quint32 constraints;
836 quint32 originNotNull;
837 quint32 originUnique;
838 quint32 originExpression;
839 quint32 strengthNotNull;
840 quint32 strengthUnique;
841 quint32 strengthExpression;
842 int splitPolicy;
843 int duplicatePolicy;
844
845 bool applyOnUpdate;
846
847 QString name;
848 QString typeName;
849 QString comment;
850 QString alias;
851 QString defaultValueExpression;
852 QString constraintExpression;
853 QString constraintDescription;
854 QMap< int, QVariant > metadata;
855
856 in >> name >> type >> typeName >> length >> precision >> comment >> alias
857 >> defaultValueExpression >> applyOnUpdate >> constraints >> originNotNull >> originUnique >> originExpression >> strengthNotNull >> strengthUnique >> strengthExpression >>
858 constraintExpression >> constraintDescription >> subType >> splitPolicy >> duplicatePolicy >> metadata;
859 field.setName( name );
860 field.setType( static_cast< QMetaType::Type >( type ) );
861 field.setTypeName( typeName );
862 field.setLength( static_cast< int >( length ) );
863 field.setPrecision( static_cast< int >( precision ) );
864 field.setComment( comment );
865 field.setAlias( alias );
866 field.setDefaultValueDefinition( QgsDefaultValue( defaultValueExpression, applyOnUpdate ) );
867 field.setSplitPolicy( static_cast< Qgis::FieldDomainSplitPolicy >( splitPolicy ) );
868 field.setDuplicatePolicy( static_cast< Qgis::FieldDuplicatePolicy >( duplicatePolicy ) );
869 QgsFieldConstraints fieldConstraints;
870 if ( constraints & QgsFieldConstraints::ConstraintNotNull )
871 {
872 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
874 }
875 else
877 if ( constraints & QgsFieldConstraints::ConstraintUnique )
878 {
879 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
881 }
882 else
885 {
886 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
888 }
889 else
891 fieldConstraints.setConstraintExpression( constraintExpression, constraintDescription );
892 field.setConstraints( fieldConstraints );
893 field.setSubType( static_cast< QMetaType::Type >( subType ) );
894 field.setMetadata( metadata );
895 return in;
896}
FieldDomainMergePolicy
Merge policy for field domains.
Definition qgis.h:3982
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:3965
FieldDuplicatePolicy
Duplicate policy for fields.
Definition qgis.h:4002
FieldMetadataProperty
Standard field metadata values.
Definition qgis.h:1803
FieldConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1779
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
Definition qgis.h:1783
@ NoFlag
No flag is defined.
Definition qgis.h:1780
@ NotSearchable
Defines if the field is searchable (used in the locator search for instance).
Definition qgis.h:1781
@ HideFromWms
Field is not available if layer is served as WMS from QGIS server.
Definition qgis.h:1782
QFlags< FieldConfigurationFlag > FieldConfigurationFlags
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1794
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:771
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:746
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:167
void setConstraints(const QgsFieldConstraints &constraints)
Sets constraints which are present for the field.
Definition qgsfield.cpp:287
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:302
QString name
Definition qgsfield.h:65
bool operator!=(const QgsField &other) const
Definition qgsfield.cpp:91
bool operator==(const QgsField &other) const
Definition qgsfield.cpp:86
int precision
Definition qgsfield.h:62
int length
Definition qgsfield.h:61
QgsField & operator=(const QgsField &other)
Definition qgsfield.cpp:80
QString displayString(const QVariant &v) const
Formats string for display.
Definition qgsfield.cpp:323
void setPrecision(int precision)
Set the field precision.
Definition qgsfield.cpp:267
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:781
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:109
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:479
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:791
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:248
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:233
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:272
void setType(QMetaType::Type type)
Set variant type.
Definition qgsfield.cpp:238
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:118
void setConfigurationFlags(Qgis::FieldConfigurationFlags flags)
Sets the Flags for the field (searchable, …).
Definition qgsfield.cpp:312
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:766
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:263
QString displayName() const
Returns the name to use when displaying this field.
Definition qgsfield.cpp:101
void setDefaultValueDefinition(const QgsDefaultValue &defaultValueDefinition)
Sets an expression to use when calculating the default value for the field.
Definition qgsfield.cpp:282
QString friendlyTypeString() const
Returns a user friendly, translated representation of the field type.
Definition qgsfield.cpp:145
void setReadOnly(bool readOnly)
Make field read-only if readOnly is set to true.
Definition qgsfield.cpp:756
QMap< int, QVariant > metadata() const
Returns the map of field metadata.
Definition qgsfield.cpp:192
static QString readableConfigurationFlag(Qgis::FieldConfigurationFlag flag)
Returns the readable and translated value of the configuration flag.
Definition qgsfield.cpp:457
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:162
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:776
static constexpr int MAX_WKT_LENGTH
Definition qgsfield.h:580
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:786
void setMetadata(const QMap< int, QVariant > metadata)
Sets the map of field metadata.
Definition qgsfield.cpp:202
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:751
bool isReadOnly
Definition qgsfield.h:70
void setTypeName(const QString &typeName)
Set the field type.
Definition qgsfield.cpp:258
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:829
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:802