QGIS API Documentation  3.20.0-Odense (decaadbb31)
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 
26 typedef QList<int> QgsAttributeList SIP_SKIP;
27 
28 /***************************************************************************
29  * This class is considered CRITICAL and any change MUST be accompanied with
30  * full unit tests in testqgsfield.cpp.
31  * See details in QEP #17
32  ****************************************************************************/
33 
34 #include "qgseditorwidgetsetup.h"
35 #include "qgsfieldconstraints.h"
36 #include "qgsdefaultvalue.h"
37 
38 class QgsFieldPrivate;
39 
50 class CORE_EXPORT QgsField
51 {
52  Q_GADGET
53 
54  Q_PROPERTY( bool isNumeric READ isNumeric )
55  Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
56  Q_PROPERTY( int length READ length WRITE setLength )
57  Q_PROPERTY( int precision READ precision WRITE setPrecision )
58  Q_PROPERTY( QVariant::Type type READ type WRITE setType )
59  Q_PROPERTY( QString comment READ comment WRITE setComment )
60  Q_PROPERTY( QString name READ name WRITE setName )
61  Q_PROPERTY( QString alias READ alias WRITE setAlias )
62  Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
63  Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
64  Q_PROPERTY( ConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
65  Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
66 
67 
68  public:
69 
70 #ifndef SIP_RUN
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 #endif
90 
106  QgsField( const QString &name = QString(),
107  QVariant::Type type = QVariant::Invalid,
108  const QString &typeName = QString(),
109  int len = 0,
110  int prec = 0,
111  const QString &comment = QString(),
112  QVariant::Type subType = QVariant::Invalid );
113 
117  QgsField( const QgsField &other );
118 
122  QgsField &operator =( const QgsField &other ) SIP_SKIP;
123 
124  virtual ~QgsField();
125 
126  bool operator==( const QgsField &other ) const;
127  bool operator!=( const QgsField &other ) const;
128 
134  QString name() const;
135 
143  QString displayName() const;
144 
156  QString displayNameWithAlias() const;
157 
158 
167  QString displayType( bool showConstraints = false ) const;
168 
170  QVariant::Type type() const;
171 
178  QVariant::Type subType() const;
179 
186  QString typeName() const;
187 
192  int length() const;
193 
198  int precision() const;
199 
203  QString comment() const;
204 
211  bool isNumeric() const;
212 
218  bool isDateOrTime() const;
219 
224  void setName( const QString &name );
225 
229  void setType( QVariant::Type type );
230 
237  void setSubType( QVariant::Type subType );
238 
243  void setTypeName( const QString &typeName );
244 
249  void setLength( int len );
250 
255  void setPrecision( int precision );
256 
260  void setComment( const QString &comment );
261 
269  QgsDefaultValue defaultValueDefinition() const;
270 
278  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
279 
285  const QgsFieldConstraints &constraints() const;
286 
292  void setConstraints( const QgsFieldConstraints &constraints );
293 
300  QString alias() const;
301 
308  void setAlias( const QString &alias );
309 
314  QgsField::ConfigurationFlags configurationFlags() const SIP_SKIP;
315 
320  void setConfigurationFlags( QgsField::ConfigurationFlags configurationFlags ) SIP_SKIP;
321 
323  QString displayString( const QVariant &v ) const;
324 
329  static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;
330 
331 #ifndef SIP_RUN
332 
341  bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
342 #else
343 
351  bool convertCompatible( QVariant &v ) const;
352  % MethodCode
353  PyObject *sipParseErr = NULL;
354 
355  {
356  QVariant *a0;
357  int a0State = 0;
358  const QgsField *sipCpp;
359 
360  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
361  {
362  bool sipRes;
363  QString errorMessage;
364 
365  Py_BEGIN_ALLOW_THREADS
366  try
367  {
368  sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
369  }
370  catch ( ... )
371  {
372  Py_BLOCK_THREADS
373 
374  sipReleaseType( a0, sipType_QVariant, a0State );
375  sipRaiseUnknownException();
376  return NULL;
377  }
378 
379  Py_END_ALLOW_THREADS
380 
381  if ( !sipRes )
382  {
383  PyErr_SetString( PyExc_ValueError,
384  QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
385  sipIsErr = 1;
386  }
387  else
388  {
389  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
390  sipReleaseType( a0, sipType_QVariant, a0State );
391  return res;
392  }
393  }
394  else
395  {
396  // Raise an exception if the arguments couldn't be parsed.
397  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
398 
399  return nullptr;
400  }
401  }
402 
403  % End
404 #endif
405 
407  operator QVariant() const
408  {
409  return QVariant::fromValue( *this );
410  }
411 
417  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
418 
427  QgsEditorWidgetSetup editorWidgetSetup() const;
428 
434  void setReadOnly( bool readOnly );
435 
441  bool isReadOnly() const;
442 
443 #ifdef SIP_RUN
444  SIP_PYOBJECT __repr__();
445  % MethodCode
446  QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
447  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
448  % End
449 #endif
450 
451  private:
452 
453  QSharedDataPointer<QgsFieldPrivate> d;
454 
455 
456 }; // class QgsField
457 
459 
461 
463 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
465 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
466 
467 
468 #endif
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:51
QString typeName() const
Gets the field type.
Definition: qgsfield.cpp:138
QString name
Definition: qgsfield.h:60
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:371
QVariant::Type type
Definition: qgsfield.h:58
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:37
#define SIP_SKIP
Definition: qgis_sip.h:126
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
QList< int > QgsAttributeList
Definition: qgsfield.h:26
const QgsField & field
Definition: qgsfield.h:463
Q_DECLARE_OPERATORS_FOR_FLAGS(QgsField::ConfigurationFlags) CORE_EXPORT QDataStream &operator<<(QDataStream &out
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
const QString & typeName
int precision