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