QGIS API Documentation  3.18.1-Zürich (202f1bf7e5)
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 #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
81 #else
82  enum class ConfigurationFlag : int
83 #endif
84  {
85  None = 0,
86  NotSearchable = 1 << 1,
87  HideFromWms = 1 << 2,
88  HideFromWfs = 1 << 3,
89  };
90  Q_ENUM( ConfigurationFlag )
91  Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
92 #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
93  // https://bugreports.qt.io/browse/QTBUG-47652
94  Q_ENUM( ConfigurationFlags )
95 #else
96  Q_FLAG( ConfigurationFlags )
97 #endif
98 #endif
99 
115  QgsField( const QString &name = QString(),
116  QVariant::Type type = QVariant::Invalid,
117  const QString &typeName = QString(),
118  int len = 0,
119  int prec = 0,
120  const QString &comment = QString(),
121  QVariant::Type subType = QVariant::Invalid );
122 
126  QgsField( const QgsField &other );
127 
131  QgsField &operator =( const QgsField &other ) SIP_SKIP;
132 
133  virtual ~QgsField();
134 
135  bool operator==( const QgsField &other ) const;
136  bool operator!=( const QgsField &other ) const;
137 
143  QString name() const;
144 
152  QString displayName() const;
153 
165  QString displayNameWithAlias() const;
166 
167 
176  QString displayType( bool showConstraints = false ) const;
177 
179  QVariant::Type type() const;
180 
187  QVariant::Type subType() const;
188 
195  QString typeName() const;
196 
201  int length() const;
202 
207  int precision() const;
208 
212  QString comment() const;
213 
220  bool isNumeric() const;
221 
227  bool isDateOrTime() const;
228 
233  void setName( const QString &name );
234 
238  void setType( QVariant::Type type );
239 
246  void setSubType( QVariant::Type subType );
247 
252  void setTypeName( const QString &typeName );
253 
258  void setLength( int len );
259 
264  void setPrecision( int precision );
265 
269  void setComment( const QString &comment );
270 
278  QgsDefaultValue defaultValueDefinition() const;
279 
287  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
288 
294  const QgsFieldConstraints &constraints() const;
295 
301  void setConstraints( const QgsFieldConstraints &constraints );
302 
309  QString alias() const;
310 
317  void setAlias( const QString &alias );
318 
323  QgsField::ConfigurationFlags configurationFlags() const SIP_SKIP;
324 
329  void setConfigurationFlags( QgsField::ConfigurationFlags configurationFlags ) SIP_SKIP;
330 
332  QString displayString( const QVariant &v ) const;
333 
338  static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;
339 
340 #ifndef SIP_RUN
341 
350  bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
351 #else
352 
360  bool convertCompatible( QVariant &v ) const;
361  % MethodCode
362  PyObject *sipParseErr = NULL;
363 
364  {
365  QVariant *a0;
366  int a0State = 0;
367  const QgsField *sipCpp;
368 
369  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
370  {
371  bool sipRes;
372  QString errorMessage;
373 
374  Py_BEGIN_ALLOW_THREADS
375  try
376  {
377  sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
378  }
379  catch ( ... )
380  {
381  Py_BLOCK_THREADS
382 
383  sipReleaseType( a0, sipType_QVariant, a0State );
384  sipRaiseUnknownException();
385  return NULL;
386  }
387 
388  Py_END_ALLOW_THREADS
389 
390  if ( !sipRes )
391  {
392  PyErr_SetString( PyExc_ValueError,
393  QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
394  sipIsErr = 1;
395  }
396  else
397  {
398  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
399  sipReleaseType( a0, sipType_QVariant, a0State );
400  return res;
401  }
402  }
403  else
404  {
405  // Raise an exception if the arguments couldn't be parsed.
406  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
407 
408  return nullptr;
409  }
410  }
411 
412  % End
413 #endif
414 
416  operator QVariant() const
417  {
418  return QVariant::fromValue( *this );
419  }
420 
426  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
427 
436  QgsEditorWidgetSetup editorWidgetSetup() const;
437 
443  void setReadOnly( bool readOnly );
444 
450  bool isReadOnly() const;
451 
452 #ifdef SIP_RUN
453  SIP_PYOBJECT __repr__();
454  % MethodCode
455  QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
456  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
457  % End
458 #endif
459 
460  private:
461 
462  QSharedDataPointer<QgsFieldPrivate> d;
463 
464 
465 }; // class QgsField
466 
468 
470 
472 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
474 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
475 
476 
477 #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:84
#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:472
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