QGIS API Documentation 3.33.0-Master (4676af859f)
Loading...
Searching...
No Matches
qgsfield.h
Go to the documentation of this file.
1/***************************************************************************
2 qgsfield.h - Describes a field in a layer or table
3 --------------------------------------
4 Date : 01-Jan-2004
5 Copyright : (C) 2004 by Gary E.Sherman
6 email : sherman at mrcc.com
7 ***************************************************************************
8 * *
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
13 * *
14 ***************************************************************************/
15
16#ifndef QGSFIELD_H
17#define QGSFIELD_H
18
19#include <QString>
20#include <QVariant>
21#include <QVector>
22#include <QSharedDataPointer>
23#include "qgis_core.h"
24#include "qgis_sip.h"
25#include "qgis.h"
26
27typedef QList<int> QgsAttributeList SIP_SKIP;
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
36#include "qgsfieldconstraints.h"
37#include "qgsdefaultvalue.h"
38#include "qgis.h"
39
40class QgsFieldPrivate;
41
52class CORE_EXPORT QgsField
53{
54 Q_GADGET
55
56 Q_PROPERTY( bool isNumeric READ isNumeric )
57 Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
58 Q_PROPERTY( int length READ length WRITE setLength )
59 Q_PROPERTY( int precision READ precision WRITE setPrecision )
60 Q_PROPERTY( QVariant::Type type READ type WRITE setType )
61 Q_PROPERTY( QString comment READ comment WRITE setComment )
62 Q_PROPERTY( QString name READ name WRITE setName )
63 Q_PROPERTY( QString alias READ alias WRITE setAlias )
64 Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
65 Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
66 Q_PROPERTY( ConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
67 Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
68
69
70 public:
71
79 enum class ConfigurationFlag : int
80 {
81 None = 0,
82 NotSearchable = 1 << 1,
83 HideFromWms = 1 << 2,
84 HideFromWfs = 1 << 3,
85 };
86 Q_ENUM( ConfigurationFlag )
87 Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
88 Q_FLAG( ConfigurationFlags )
89
90
105 QgsField( const QString &name = QString(),
106 QVariant::Type type = QVariant::Invalid,
107 const QString &typeName = QString(),
108 int len = 0,
109 int prec = 0,
110 const QString &comment = QString(),
111 QVariant::Type subType = QVariant::Invalid );
112
116 QgsField( const QgsField &other );
117
121 QgsField &operator =( const QgsField &other ) SIP_SKIP;
122
123 virtual ~QgsField();
124
125 bool operator==( const QgsField &other ) const;
126 bool operator!=( const QgsField &other ) const;
127
133 QString name() const;
134
142 QString displayName() const;
143
155 QString displayNameWithAlias() const;
156
167 QString displayType( bool showConstraints = false ) const;
168
179 QString friendlyTypeString() const;
180
182 QVariant::Type type() const;
183
190 QVariant::Type subType() const;
191
198 QString typeName() const;
199
204 int length() const;
205
210 int precision() const;
211
215 QString comment() const;
216
225 QMap< int, QVariant > metadata() const;
226
233 QVariant metadata( Qgis::FieldMetadataProperty property ) const SIP_SKIP;
234
241 QVariant metadata( int property ) const;
242
251 void setMetadata( const QMap< int, QVariant > metadata );
252
259 void setMetadata( Qgis::FieldMetadataProperty property, const QVariant &value ) SIP_SKIP;
260
267 void setMetadata( int property, const QVariant &value );
268
275 bool isNumeric() const;
276
282 bool isDateOrTime() const;
283
288 void setName( const QString &name );
289
293 void setType( QVariant::Type type );
294
301 void setSubType( QVariant::Type subType );
302
307 void setTypeName( const QString &typeName );
308
313 void setLength( int len );
314
319 void setPrecision( int precision );
320
324 void setComment( const QString &comment );
325
333 QgsDefaultValue defaultValueDefinition() const;
334
342 void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
343
349 const QgsFieldConstraints &constraints() const;
350
356 void setConstraints( const QgsFieldConstraints &constraints );
357
364 QString alias() const;
365
372 void setAlias( const QString &alias );
373
378 QgsField::ConfigurationFlags configurationFlags() const;
379
384 void setConfigurationFlags( QgsField::ConfigurationFlags configurationFlags );
385
387 QString displayString( const QVariant &v ) const;
388
393 static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag );
394
395#ifndef SIP_RUN
396
405 bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
406#else
407
415 bool convertCompatible( QVariant &v ) const;
416 % MethodCode
417 PyObject *sipParseErr = NULL;
418
419 {
420 QVariant *a0;
421 int a0State = 0;
422 const QgsField *sipCpp;
423
424 if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
425 {
426 bool sipRes;
427 QString errorMessage;
428
429 Py_BEGIN_ALLOW_THREADS
430 try
431 {
432 sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
433 }
434 catch ( ... )
435 {
436 Py_BLOCK_THREADS
437
438 sipReleaseType( a0, sipType_QVariant, a0State );
439 sipRaiseUnknownException();
440 return NULL;
441 }
442
443 Py_END_ALLOW_THREADS
444
445 if ( !sipRes )
446 {
447 PyErr_SetString( PyExc_ValueError,
448 QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
449 sipIsErr = 1;
450 }
451 else
452 {
453 PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
454 sipReleaseType( a0, sipType_QVariant, a0State );
455 return res;
456 }
457 }
458 else
459 {
460 // Raise an exception if the arguments couldn't be parsed.
461 sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
462
463 return nullptr;
464 }
465 }
466
467 % End
468#endif
469
471 operator QVariant() const
472 {
473 return QVariant::fromValue( *this );
474 }
475
481 void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
482
491 QgsEditorWidgetSetup editorWidgetSetup() const;
492
498 void setReadOnly( bool readOnly );
499
505 bool isReadOnly() const;
506
515 Qgis::FieldDomainSplitPolicy splitPolicy() const;
516
525 void setSplitPolicy( Qgis::FieldDomainSplitPolicy policy );
526
527#ifdef SIP_RUN
528 SIP_PYOBJECT __repr__();
529 % MethodCode
530 QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
531 sipRes = PyUnicode_FromString( str.toUtf8().constData() );
532 % End
533#endif
534
535#ifndef SIP_RUN
536 static constexpr int MAX_WKT_LENGTH = 999;
537#endif
538
539 private:
540
541 QSharedDataPointer<QgsFieldPrivate> d;
542
543
544}; // class QgsField
545
547
548Q_DECLARE_OPERATORS_FOR_FLAGS( QgsField::ConfigurationFlags )
549
550
551CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
553CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
554
555
556#endif
FieldDomainSplitPolicy
Split policy for field domains.
Definition qgis.h:2579
FieldMetadataProperty
Standard field metadata values.
Definition qgis.h:1184
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.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:53
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:150
QString name
Definition qgsfield.h:62
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition qgsfield.cpp:452
QVariant::Type type
Definition qgsfield.h:60
virtual ~QgsField()
ConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition qgsfield.h:80
#define str(x)
Definition qgis.cpp:38
#define SIP_SKIP
Definition qgis_sip.h:126
Q_DECLARE_METATYPE(QgsDatabaseQueryLogEntry)
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QList< int > QgsAttributeList
Definition qgsfield.h:27
CORE_EXPORT QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:771
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition qgsfield.cpp:745
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsTextRendererUtils::CurvedTextFlags)
const QString & typeName
int precision