QGIS API Documentation  3.17.0-Master (7a14973234)
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 
49 class CORE_EXPORT QgsField
50 {
51  Q_GADGET
52 
53  Q_PROPERTY( bool isNumeric READ isNumeric )
54  Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
55  Q_PROPERTY( int length READ length WRITE setLength )
56  Q_PROPERTY( int precision READ precision WRITE setPrecision )
57  Q_PROPERTY( QVariant::Type type READ type WRITE setType )
58  Q_PROPERTY( QString comment READ comment WRITE setComment )
59  Q_PROPERTY( QString name READ name WRITE setName )
60  Q_PROPERTY( QString alias READ alias WRITE setAlias )
61  Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
62  Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
63  Q_PROPERTY( ConfigurationFlags configurationFlags READ configurationFlags WRITE setConfigurationFlags )
64 
65 
66  public:
67 
68 #ifndef SIP_RUN
69 
77 #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
79 #else
80  enum class ConfigurationFlag : int
81 #endif
82  {
83  None = 0,
84  NotSearchable = 1 << 1,
85  HideFromWms = 1 << 2,
86  HideFromWfs = 1 << 3,
87  };
88  Q_ENUM( ConfigurationFlag )
89  Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
90 #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
91  // https://bugreports.qt.io/browse/QTBUG-47652
92  Q_ENUM( ConfigurationFlags )
93 #else
94  Q_FLAG( ConfigurationFlags )
95 #endif
96 #endif
97 
113  QgsField( const QString &name = QString(),
114  QVariant::Type type = QVariant::Invalid,
115  const QString &typeName = QString(),
116  int len = 0,
117  int prec = 0,
118  const QString &comment = QString(),
119  QVariant::Type subType = QVariant::Invalid );
120 
124  QgsField( const QgsField &other );
125 
129  QgsField &operator =( const QgsField &other ) SIP_SKIP;
130 
131  virtual ~QgsField();
132 
133  bool operator==( const QgsField &other ) const;
134  bool operator!=( const QgsField &other ) const;
135 
141  QString name() const;
142 
150  QString displayName() const;
151 
163  QString displayNameWithAlias() const;
164 
165 
174  QString displayType( bool showConstraints = false ) const;
175 
177  QVariant::Type type() const;
178 
185  QVariant::Type subType() const;
186 
193  QString typeName() const;
194 
199  int length() const;
200 
205  int precision() const;
206 
210  QString comment() const;
211 
218  bool isNumeric() const;
219 
225  bool isDateOrTime() const;
226 
231  void setName( const QString &name );
232 
236  void setType( QVariant::Type type );
237 
244  void setSubType( QVariant::Type subType );
245 
250  void setTypeName( const QString &typeName );
251 
256  void setLength( int len );
257 
262  void setPrecision( int precision );
263 
267  void setComment( const QString &comment );
268 
276  QgsDefaultValue defaultValueDefinition() const;
277 
285  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
286 
292  const QgsFieldConstraints &constraints() const;
293 
299  void setConstraints( const QgsFieldConstraints &constraints );
300 
307  QString alias() const;
308 
315  void setAlias( const QString &alias );
316 
321  QgsField::ConfigurationFlags configurationFlags() const SIP_SKIP;
322 
327  void setConfigurationFlags( QgsField::ConfigurationFlags configurationFlags ) SIP_SKIP;
328 
330  QString displayString( const QVariant &v ) const;
331 
336  static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;
337 
338 #ifndef SIP_RUN
339 
348  bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
349 #else
350 
358  bool convertCompatible( QVariant &v ) const;
359  % MethodCode
360  PyObject *sipParseErr = NULL;
361 
362  {
363  QVariant *a0;
364  int a0State = 0;
365  const QgsField *sipCpp;
366 
367  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
368  {
369  bool sipRes;
370  QString errorMessage;
371 
372  Py_BEGIN_ALLOW_THREADS
373  try
374  {
375  sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
376  }
377  catch ( ... )
378  {
379  Py_BLOCK_THREADS
380 
381  sipReleaseType( a0, sipType_QVariant, a0State );
382  sipRaiseUnknownException();
383  return NULL;
384  }
385 
386  Py_END_ALLOW_THREADS
387 
388  if ( !sipRes )
389  {
390  PyErr_SetString( PyExc_ValueError,
391  QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
392  sipIsErr = 1;
393  }
394  else
395  {
396  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
397  sipReleaseType( a0, sipType_QVariant, a0State );
398  return res;
399  }
400  }
401  else
402  {
403  // Raise an exception if the arguments couldn't be parsed.
404  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
405 
406  return nullptr;
407  }
408  }
409 
410  % End
411 #endif
412 
414  operator QVariant() const
415  {
416  return QVariant::fromValue( *this );
417  }
418 
424  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
425 
434  QgsEditorWidgetSetup editorWidgetSetup() const;
435 
436 #ifdef SIP_RUN
437  SIP_PYOBJECT __repr__();
438  % MethodCode
439  QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
440  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
441  % End
442 #endif
443 
444  private:
445 
446  QSharedDataPointer<QgsFieldPrivate> d;
447 
448 
449 }; // class QgsField
450 
452 
453 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsField::ConfigurationFlags ) SIP_SKIP
454 
456 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
458 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
459 
460 
461 #endif
int precision
The QgsDefaultValue class provides a container for managing client side default values for fields...
bool operator==(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
bool operator!=(const QgsFeatureIterator &fi1, const QgsFeatureIterator &fi2)
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.
Stores information about constraints which may be present on a field.
ConfigurationFlag
Configuration flags for fields These flags are meant to be user-configurable and are not describing a...
Definition: qgsfield.h:80
#define SIP_SKIP
Definition: qgis_sip.h:126
const QString & typeName
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
std::ostream & operator<<(std::ostream &os, const QgsCoordinateReferenceSystem &r)
Output stream operator.
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:49
Holder for the widget type and its configuration for a field.
bool convertCompatible(QVariant &v, QString *errorMessage=nullptr) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:371
QList< int > QgsAttributeList
Definition: qgsfield.h:26
const QgsField & field
Definition: qgsfield.h:456
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:594
QVariant::Type type
Definition: qgsfield.h:57