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