QGIS API Documentation  3.17.0-Master (fba5093f18)
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  Q_PROPERTY( bool isReadOnly READ isReadOnly WRITE setReadOnly )
65 
66 
67  public:
68 
69 #ifndef SIP_RUN
70 
78 #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
80 #else
81  enum class ConfigurationFlag : int
82 #endif
83  {
84  None = 0,
85  NotSearchable = 1 << 1,
86  HideFromWms = 1 << 2,
87  HideFromWfs = 1 << 3,
88  };
89  Q_ENUM( ConfigurationFlag )
90  Q_DECLARE_FLAGS( ConfigurationFlags, ConfigurationFlag )
91 #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0)
92  // https://bugreports.qt.io/browse/QTBUG-47652
93  Q_ENUM( ConfigurationFlags )
94 #else
95  Q_FLAG( ConfigurationFlags )
96 #endif
97 #endif
98 
114  QgsField( const QString &name = QString(),
115  QVariant::Type type = QVariant::Invalid,
116  const QString &typeName = QString(),
117  int len = 0,
118  int prec = 0,
119  const QString &comment = QString(),
120  QVariant::Type subType = QVariant::Invalid );
121 
125  QgsField( const QgsField &other );
126 
130  QgsField &operator =( const QgsField &other ) SIP_SKIP;
131 
132  virtual ~QgsField();
133 
134  bool operator==( const QgsField &other ) const;
135  bool operator!=( const QgsField &other ) const;
136 
142  QString name() const;
143 
151  QString displayName() const;
152 
164  QString displayNameWithAlias() const;
165 
166 
175  QString displayType( bool showConstraints = false ) const;
176 
178  QVariant::Type type() const;
179 
186  QVariant::Type subType() const;
187 
194  QString typeName() const;
195 
200  int length() const;
201 
206  int precision() const;
207 
211  QString comment() const;
212 
219  bool isNumeric() const;
220 
226  bool isDateOrTime() const;
227 
232  void setName( const QString &name );
233 
237  void setType( QVariant::Type type );
238 
245  void setSubType( QVariant::Type subType );
246 
251  void setTypeName( const QString &typeName );
252 
257  void setLength( int len );
258 
263  void setPrecision( int precision );
264 
268  void setComment( const QString &comment );
269 
277  QgsDefaultValue defaultValueDefinition() const;
278 
286  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
287 
293  const QgsFieldConstraints &constraints() const;
294 
300  void setConstraints( const QgsFieldConstraints &constraints );
301 
308  QString alias() const;
309 
316  void setAlias( const QString &alias );
317 
322  QgsField::ConfigurationFlags configurationFlags() const SIP_SKIP;
323 
328  void setConfigurationFlags( QgsField::ConfigurationFlags configurationFlags ) SIP_SKIP;
329 
331  QString displayString( const QVariant &v ) const;
332 
337  static QString readableConfigurationFlag( QgsField::ConfigurationFlag flag ) SIP_SKIP;
338 
339 #ifndef SIP_RUN
340 
349  bool convertCompatible( QVariant &v, QString *errorMessage = nullptr ) const;
350 #else
351 
359  bool convertCompatible( QVariant &v ) const;
360  % MethodCode
361  PyObject *sipParseErr = NULL;
362 
363  {
364  QVariant *a0;
365  int a0State = 0;
366  const QgsField *sipCpp;
367 
368  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
369  {
370  bool sipRes;
371  QString errorMessage;
372 
373  Py_BEGIN_ALLOW_THREADS
374  try
375  {
376  sipRes = sipCpp->convertCompatible( *a0, &errorMessage );
377  }
378  catch ( ... )
379  {
380  Py_BLOCK_THREADS
381 
382  sipReleaseType( a0, sipType_QVariant, a0State );
383  sipRaiseUnknownException();
384  return NULL;
385  }
386 
387  Py_END_ALLOW_THREADS
388 
389  if ( !sipRes )
390  {
391  PyErr_SetString( PyExc_ValueError,
392  QString( "Value could not be converted to field type %1: %2" ).arg( QMetaType::typeName( sipCpp->type() ), errorMessage ).toUtf8().constData() );
393  sipIsErr = 1;
394  }
395  else
396  {
397  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
398  sipReleaseType( a0, sipType_QVariant, a0State );
399  return res;
400  }
401  }
402  else
403  {
404  // Raise an exception if the arguments couldn't be parsed.
405  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
406 
407  return nullptr;
408  }
409  }
410 
411  % End
412 #endif
413 
415  operator QVariant() const
416  {
417  return QVariant::fromValue( *this );
418  }
419 
425  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
426 
435  QgsEditorWidgetSetup editorWidgetSetup() const;
436 
442  void setReadOnly( bool readOnly );
443 
449  bool isReadOnly() const;
450 
451 #ifdef SIP_RUN
452  SIP_PYOBJECT __repr__();
453  % MethodCode
454  QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
455  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
456  % End
457 #endif
458 
459  private:
460 
461  QSharedDataPointer<QgsFieldPrivate> d;
462 
463 
464 }; // class QgsField
465 
467 
468 Q_DECLARE_OPERATORS_FOR_FLAGS( QgsField::ConfigurationFlags ) SIP_SKIP
469 
471 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
473 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
474 
475 
476 #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:81
#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:471
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:605
QVariant::Type type
Definition: qgsfield.h:57