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