QGIS API Documentation 4.1.0-Master (60fea48833c)
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->flags;
301}
302
304{
305 d->flags = flags;
306}
307
308/***************************************************************************
309 * This class is considered CRITICAL and any change MUST be accompanied with
310 * full unit tests in testqgsfield.cpp.
311 * See details in QEP #17
312 ****************************************************************************/
313
314QString QgsField::displayString( const QVariant &v ) const
315{
316 if ( QgsVariantUtils::isNull( v ) )
317 {
319 }
320
321 if ( v.userType() == qMetaTypeId<QgsReferencedGeometry>() )
322 {
323 QgsReferencedGeometry geom = qvariant_cast<QgsReferencedGeometry>( v );
324 if ( geom.isNull() )
326 else
327 {
328 QString wkt = geom.asWkt();
329 if ( wkt.length() >= 1050 )
330 {
331 wkt = wkt.left( MAX_WKT_LENGTH ) + QChar( 0x2026 );
332 }
333 QString formattedText = u"%1 [%2]"_s.arg( wkt, geom.crs().userFriendlyIdentifier() );
334 return formattedText;
335 }
336 }
337
338 // Special treatment for numeric types if group separator is set or decimalPoint is not a dot
339 if ( d->type == QMetaType::Type::Double )
340 {
341 // if value doesn't contain a double (a default value expression for instance),
342 // apply no transformation
343 bool ok;
344 v.toDouble( &ok );
345 if ( !ok )
346 return v.toString();
347
348 // Locales with decimal point != '.' or that require group separator: use QLocale
349 if ( QLocale().decimalPoint() != '.' || !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
350 {
351 if ( d->precision > 0 )
352 {
353 if ( -1 < v.toDouble() && v.toDouble() < 1 )
354 {
355 return QLocale().toString( v.toDouble(), 'g', d->precision );
356 }
357 else
358 {
359 return QLocale().toString( v.toDouble(), 'f', d->precision );
360 }
361 }
362 else
363 {
364 // Precision is not set, let's guess it from the
365 // standard conversion to string
366 const QString s( v.toString() );
367 const int dotPosition( s.indexOf( '.' ) );
368 int precision;
369 if ( dotPosition < 0 && s.indexOf( 'e' ) < 0 )
370 {
371 precision = 0;
372 return QLocale().toString( v.toDouble(), 'f', precision );
373 }
374 else
375 {
376 if ( dotPosition < 0 )
377 precision = 0;
378 else
379 precision = s.length() - dotPosition - 1;
380
381 if ( -1 < v.toDouble() && v.toDouble() < 1 )
382 {
383 return QLocale().toString( v.toDouble(), 'g', precision );
384 }
385 else
386 {
387 return QLocale().toString( v.toDouble(), 'f', precision );
388 }
389 }
390 }
391 }
392 // Default for doubles with precision
393 else if ( d->precision > 0 )
394 {
395 if ( -1 < v.toDouble() && v.toDouble() < 1 )
396 {
397 return QString::number( v.toDouble(), 'g', d->precision );
398 }
399 else
400 {
401 return QString::number( v.toDouble(), 'f', d->precision );
402 }
403 }
404 else
405 {
406 const double vDouble = v.toDouble();
407 // mimic Qt 5 handling of when to switch to exponential forms
408 if ( std::fabs( vDouble ) < 1e-04 )
409 return QString::number( vDouble, 'g', QLocale::FloatingPointShortest );
410 else
411 return QString::number( vDouble, 'f', QLocale::FloatingPointShortest );
412 }
413 }
414 // Other numeric types than doubles
415 else if ( isNumeric() && !( QLocale().numberOptions() & QLocale::NumberOption::OmitGroupSeparator ) )
416 {
417 bool ok;
418 const qlonglong converted( v.toLongLong( &ok ) );
419 if ( ok )
420 return QLocale().toString( converted );
421 }
422 else if ( d->typeName.compare( "json"_L1, Qt::CaseInsensitive ) == 0 || d->typeName == "jsonb"_L1 )
423 {
424 const QJsonDocument doc = QJsonDocument::fromVariant( v );
425 return QString::fromUtf8( doc.toJson().constData() );
426 }
427 else if ( d->type == QMetaType::Type::QByteArray )
428 {
429 return QObject::tr( "BLOB" );
430 }
431 else if ( d->type == QMetaType::Type::QStringList || d->type == QMetaType::Type::QVariantList )
432 {
433 QString result;
434 const QVariantList list = v.toList();
435 for ( const QVariant &var : list )
436 {
437 if ( !result.isEmpty() )
438 result.append( u", "_s );
439 result.append( var.toString() );
440 }
441 return result;
442 }
443
444 // Fallback if special rules do not apply
445 return v.toString();
446}
447
449{
450 switch ( flag )
451 {
453 return QObject::tr( "None" );
455 return QObject::tr( "Not searchable" );
457 return QObject::tr( "Do not expose via WMS" );
459 return QObject::tr( "Do not expose via WFS" );
460 }
461 return QString();
462}
463
464/***************************************************************************
465 * This class is considered CRITICAL and any change MUST be accompanied with
466 * full unit tests in testqgsfield.cpp.
467 * See details in QEP #17
468 ****************************************************************************/
469
470bool QgsField::convertCompatible( QVariant &v, QString *errorMessage ) const
471{
472 const QVariant original = v;
473 if ( errorMessage )
474 errorMessage->clear();
475
476 if ( QgsVariantUtils::isNull( v ) )
477 {
478 ( void ) v.convert( d->type );
479 return true;
480 }
481
483 {
484 return true;
485 }
486
487 if ( d->type == QMetaType::Type::Int && v.toInt() != v.toLongLong() )
488 {
490 if ( errorMessage )
491 *errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toLongLong() );
492 return false;
493 }
494
495 // Give it a chance to convert to double since for not '.' locales
496 // we accept both comma and dot as decimal point
497 if ( d->type == QMetaType::Type::Double && v.userType() == QMetaType::Type::QString )
498 {
499 QVariant tmp( v );
500 if ( !tmp.convert( d->type ) )
501 {
502 // This might be a string with thousand separator: use locale to convert
503 bool ok = false;
504 double d = qgsPermissiveToDouble( v.toString(), ok );
505 if ( ok )
506 {
507 v = QVariant( d );
508 return true;
509 }
510 // For not 'dot' locales, we also want to accept '.'
511 if ( QLocale().decimalPoint() != '.' )
512 {
513 d = QLocale( QLocale::C ).toDouble( v.toString(), &ok );
514 if ( ok )
515 {
516 v = QVariant( d );
517 return true;
518 }
519 }
520 }
521 }
522
523 // For string representation of an int we also might have thousand separator
524 if ( d->type == QMetaType::Type::Int && v.userType() == QMetaType::Type::QString )
525 {
526 QVariant tmp( v );
527 if ( !tmp.convert( d->type ) )
528 {
529 // This might be a string with thousand separator: use locale to convert
530 bool ok;
531 const int i = qgsPermissiveToInt( v.toString(), ok );
532 if ( ok )
533 {
534 v = QVariant( i );
535 return true;
536 }
537 }
538 }
539
540 // For string representation of a long we also might have thousand separator
541 if ( d->type == QMetaType::Type::LongLong && v.userType() == QMetaType::Type::QString )
542 {
543 QVariant tmp( v );
544 if ( !tmp.convert( d->type ) )
545 {
546 // This might be a string with thousand separator: use locale to convert
547 bool ok;
548 const qlonglong l = qgsPermissiveToLongLong( v.toString(), ok );
549 if ( ok )
550 {
551 v = QVariant( l );
552 return true;
553 }
554 }
555 }
556
557 //String representations of doubles in QVariant will return false to convert( QVariant::Int )
558 //work around this by first converting to double, and then checking whether the double is convertible to int
559 if ( d->type == QMetaType::Type::Int && v.canConvert( QMetaType::Type::Double ) )
560 {
561 bool ok = false;
562 const double dbl = v.toDouble( &ok );
563 if ( !ok )
564 {
565 //couldn't convert to number
567
568 if ( errorMessage )
569 *errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
570
571 return false;
572 }
573
574 const double round = std::round( dbl );
575 if ( round > std::numeric_limits<int>::max() || round < -std::numeric_limits<int>::max() )
576 {
577 //double too large to fit in int
579
580 if ( errorMessage )
581 *errorMessage = QObject::tr( "Value \"%1\" is too large for integer field" ).arg( original.toDouble() );
582
583 return false;
584 }
585 v = QVariant( static_cast< int >( std::round( dbl ) ) );
586 return true;
587 }
588
589 //String representations of doubles in QVariant will return false to convert( QVariant::LongLong )
590 //work around this by first converting to double, and then checking whether the double is convertible to longlong
591 if ( d->type == QMetaType::Type::LongLong && v.canConvert( QMetaType::Type::Double ) )
592 {
593 //firstly test the conversion to longlong because conversion to double will rounded the value
594 QVariant tmp( v );
595 if ( !tmp.convert( d->type ) )
596 {
597 bool ok = false;
598 const double dbl = v.toDouble( &ok );
599 if ( !ok )
600 {
601 //couldn't convert to number
603
604 if ( errorMessage )
605 *errorMessage = QObject::tr( "Value \"%1\" is not a number" ).arg( original.toString() );
606
607 return false;
608 }
609
610 const double round = std::round( dbl );
611 if ( round > static_cast<double>( std::numeric_limits<long long>::max() ) || round < static_cast<double>( -std::numeric_limits<long long>::max() ) )
612 {
613 //double too large to fit in longlong
615
616 if ( errorMessage )
617 *errorMessage = QObject::tr( "Value \"%1\" is too large for long long field" ).arg( original.toDouble() );
618
619 return false;
620 }
621 v = QVariant( static_cast< long long >( std::round( dbl ) ) );
622 return true;
623 }
624 }
625
626 if ( d->typeName.compare( "json"_L1, Qt::CaseInsensitive ) == 0 || d->typeName.compare( "jsonb"_L1, Qt::CaseInsensitive ) == 0 )
627 {
628 if ( d->type == QMetaType::Type::QString )
629 {
630 const QJsonDocument doc = QJsonDocument::fromVariant( v );
631 if ( !doc.isNull() )
632 {
633 v = QString::fromUtf8( doc.toJson( QJsonDocument::Compact ).constData() );
634 return true;
635 }
637 return false;
638 }
639 else if ( d->type == QMetaType::Type::QVariantMap )
640 {
641 if ( v.userType() == QMetaType::Type::QStringList || v.userType() == QMetaType::Type::QVariantList || v.userType() == QMetaType::Type::QVariantMap )
642 {
643 return true;
644 }
646 return false;
647 }
648 }
649
650 if ( ( d->type == QMetaType::Type::QStringList || ( d->type == QMetaType::Type::QVariantList && d->subType == QMetaType::Type::QString ) ) && ( v.userType() == QMetaType::Type::QString ) )
651 {
652 v = QStringList( { v.toString() } );
653 return true;
654 }
655
656 if ( ( d->type == QMetaType::Type::QStringList || d->type == QMetaType::Type::QVariantList ) && !( v.userType() == QMetaType::Type::QStringList || v.userType() == QMetaType::Type::QVariantList ) )
657 {
659
660 if ( errorMessage )
661 *errorMessage = QObject::tr( "Could not convert value \"%1\" to target list type" ).arg( original.toString() );
662
663 return false;
664 }
665
666 // Handle referenced geometries (e.g. from additional geometry fields)
667 if ( d->type == QMetaType::Type::QString && v.userType() == qMetaTypeId<QgsReferencedGeometry>() )
668 {
669 const QgsReferencedGeometry geom { v.value<QgsReferencedGeometry>() };
670 if ( geom.isNull() )
671 {
673 }
674 else
675 {
676 v = QVariant( geom.asWkt() );
677 }
678 return true;
679 }
680 else if ( d->type == QMetaType::Type::User && d->typeName.compare( "geometry"_L1, Qt::CaseInsensitive ) == 0 )
681 {
682 if ( v.userType() == qMetaTypeId<QgsReferencedGeometry>() || v.userType() == qMetaTypeId< QgsGeometry>() )
683 {
684 return true;
685 }
686 else if ( v.userType() == QMetaType::Type::QString )
687 {
688 const QgsGeometry geom = QgsGeometry::fromWkt( v.toString() );
689 if ( !geom.isNull() )
690 {
691 v = QVariant::fromValue( geom );
692 return true;
693 }
694 }
695 return false;
696 }
697 else if ( !v.convert( d->type ) )
698 {
700
701 if ( errorMessage )
702 *errorMessage = QObject::tr( "Could not convert value \"%1\" to target type \"%2\"" ).arg( original.toString(), d->typeName );
703
704 return false;
705 }
706
707 if ( d->type == QMetaType::Type::Double && d->precision > 0 )
708 {
709 const double s = std::pow( 10, d->precision );
710 const double d = v.toDouble() * s;
711 v = QVariant( ( d < 0 ? std::ceil( d - 0.5 ) : std::floor( d + 0.5 ) ) / s );
712 return true;
713 }
714
715 if ( d->type == QMetaType::Type::QString && d->length > 0 && v.toString().length() > d->length )
716 {
717 const int length = v.toString().length();
718 v = v.toString().left( d->length );
719
720 if ( errorMessage )
721 *errorMessage = QObject::tr( "String of length %1 exceeds maximum field length (%2)" ).arg( length ).arg( d->length );
722
723 return false;
724 }
725
726 return true;
727}
728
729QgsField::operator QVariant() const
730{
731 return QVariant::fromValue( *this );
732}
733
735{
736 d->editorWidgetSetup = v;
737}
738
740{
741 return d->editorWidgetSetup;
742}
743
744void QgsField::setReadOnly( bool readOnly )
745{
746 d->isReadOnly = readOnly;
747}
748
750{
751 return d->isReadOnly;
752}
753
755{
756 return d->splitPolicy;
757}
758
760{
761 d->splitPolicy = policy;
762}
763
765{
766 return d->duplicatePolicy;
767}
768
770{
771 d->duplicatePolicy = policy;
772}
773
775{
776 return d->mergePolicy;
777}
778
780{
781 d->mergePolicy = policy;
782}
783
784/***************************************************************************
785 * This class is considered CRITICAL and any change MUST be accompanied with
786 * full unit tests in testqgsfield.cpp.
787 * See details in QEP #17
788 ****************************************************************************/
789
790QDataStream &operator<<( QDataStream &out, const QgsField &field )
791{
792 out << field.name();
793 out << static_cast< quint32 >( field.type() );
794 out << field.typeName();
795 out << field.length();
796 out << field.precision();
797 out << field.comment();
798 out << field.alias();
799 out << field.defaultValueDefinition().expression();
800 out << field.defaultValueDefinition().applyOnUpdate();
801 out << field.constraints().constraints();
802 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintNotNull ) );
803 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintUnique ) );
804 out << static_cast< quint32 >( field.constraints().constraintOrigin( QgsFieldConstraints::ConstraintExpression ) );
805 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintNotNull ) );
806 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintUnique ) );
807 out << static_cast< quint32 >( field.constraints().constraintStrength( QgsFieldConstraints::ConstraintExpression ) );
808 out << field.constraints().constraintExpression();
809 out << field.constraints().constraintDescription();
810 out << static_cast< quint32 >( field.subType() );
811 out << static_cast< int >( field.splitPolicy() );
812 out << static_cast< int >( field.duplicatePolicy() );
813 out << field.metadata();
814 return out;
815}
816
817QDataStream &operator>>( QDataStream &in, QgsField &field )
818{
819 quint32 type;
820 quint32 subType;
821 quint32 length;
822 quint32 precision;
823 quint32 constraints;
824 quint32 originNotNull;
825 quint32 originUnique;
826 quint32 originExpression;
827 quint32 strengthNotNull;
828 quint32 strengthUnique;
829 quint32 strengthExpression;
830 int splitPolicy;
831 int duplicatePolicy;
832
833 bool applyOnUpdate;
834
835 QString name;
836 QString typeName;
837 QString comment;
838 QString alias;
839 QString defaultValueExpression;
840 QString constraintExpression;
841 QString constraintDescription;
842 QMap< int, QVariant > metadata;
843
844 in
845 >> name
846 >> type
847 >> typeName
848 >> length
849 >> precision
850 >> comment
851 >> alias
852 >> defaultValueExpression
853 >> applyOnUpdate
854 >> constraints
855 >> originNotNull
856 >> originUnique
857 >> originExpression
858 >> strengthNotNull
859 >> strengthUnique
860 >> strengthExpression
861 >> constraintExpression
862 >> constraintDescription
863 >> subType
864 >> splitPolicy
865 >> duplicatePolicy
866 >> metadata;
867 field.setName( name );
868 field.setType( static_cast< QMetaType::Type >( type ) );
869 field.setTypeName( typeName );
870 field.setLength( static_cast< int >( length ) );
871 field.setPrecision( static_cast< int >( precision ) );
872 field.setComment( comment );
873 field.setAlias( alias );
874 field.setDefaultValueDefinition( QgsDefaultValue( defaultValueExpression, applyOnUpdate ) );
875 field.setSplitPolicy( static_cast< Qgis::FieldDomainSplitPolicy >( splitPolicy ) );
876 field.setDuplicatePolicy( static_cast< Qgis::FieldDuplicatePolicy >( duplicatePolicy ) );
877 QgsFieldConstraints fieldConstraints;
878 if ( constraints & QgsFieldConstraints::ConstraintNotNull )
879 {
880 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, static_cast< QgsFieldConstraints::ConstraintOrigin>( originNotNull ) );
882 }
883 else
885 if ( constraints & QgsFieldConstraints::ConstraintUnique )
886 {
887 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintUnique, static_cast< QgsFieldConstraints::ConstraintOrigin>( originUnique ) );
889 }
890 else
893 {
894 fieldConstraints.setConstraint( QgsFieldConstraints::ConstraintExpression, static_cast< QgsFieldConstraints::ConstraintOrigin>( originExpression ) );
896 }
897 else
899 fieldConstraints.setConstraintExpression( constraintExpression, constraintDescription );
900 field.setConstraints( fieldConstraints );
901 field.setSubType( static_cast< QMetaType::Type >( subType ) );
902 field.setMetadata( metadata );
903 return in;
904}
FieldDomainMergePolicy
Merge policy for field domains.
Definition qgis.h:4040
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:4023
FieldDuplicatePolicy
Duplicate policy for fields.
Definition qgis.h:4060
FieldMetadataProperty
Standard field metadata values.
Definition qgis.h:1824
FieldConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1800
@ HideFromWfs
Field is not available if layer is served as WFS from QGIS server.
Definition qgis.h:1804
@ NoFlag
No flag is defined.
Definition qgis.h:1801
@ NotSearchable
Defines if the field is searchable (used in the locator search for instance).
Definition qgis.h:1802
@ HideFromWms
Field is not available if layer is served as WMS from QGIS server.
Definition qgis.h:1803
QFlags< FieldConfigurationFlag > FieldConfigurationFlags
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgis.h:1815
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:759
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:734
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:314
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:769
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:470
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:779
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:303
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:754
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:744
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:448
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:764
static constexpr int MAX_WKT_LENGTH
Definition qgsfield.h:579
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:774
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:739
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:817
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:790