QGIS API Documentation  3.14.0-Pi (9f7028fd23)
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 
64  public:
65 
81  QgsField( const QString &name = QString(),
82  QVariant::Type type = QVariant::Invalid,
83  const QString &typeName = QString(),
84  int len = 0,
85  int prec = 0,
86  const QString &comment = QString(),
87  QVariant::Type subType = QVariant::Invalid );
88 
92  QgsField( const QgsField &other );
93 
97  QgsField &operator =( const QgsField &other ) SIP_SKIP;
98 
99  virtual ~QgsField();
100 
101  bool operator==( const QgsField &other ) const;
102  bool operator!=( const QgsField &other ) const;
103 
109  QString name() const;
110 
118  QString displayName() const;
119 
131  QString displayNameWithAlias() const;
132 
133 
142  QString displayType( bool showConstraints = false ) const;
143 
145  QVariant::Type type() const;
146 
153  QVariant::Type subType() const;
154 
161  QString typeName() const;
162 
167  int length() const;
168 
173  int precision() const;
174 
178  QString comment() const;
179 
186  bool isNumeric() const;
187 
193  bool isDateOrTime() const;
194 
199  void setName( const QString &name );
200 
204  void setType( QVariant::Type type );
205 
212  void setSubType( QVariant::Type subType );
213 
218  void setTypeName( const QString &typeName );
219 
224  void setLength( int len );
225 
230  void setPrecision( int precision );
231 
235  void setComment( const QString &comment );
236 
244  QgsDefaultValue defaultValueDefinition() const;
245 
253  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
254 
260  const QgsFieldConstraints &constraints() const;
261 
267  void setConstraints( const QgsFieldConstraints &constraints );
268 
275  QString alias() const;
276 
283  void setAlias( const QString &alias );
284 
286  QString displayString( const QVariant &v ) const;
287 
295  bool convertCompatible( QVariant &v ) const;
296 #ifdef SIP_RUN
297  % MethodCode
298  PyObject *sipParseErr = NULL;
299 
300  {
301  QVariant *a0;
302  int a0State = 0;
303  const QgsField *sipCpp;
304 
305  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
306  {
307  bool sipRes;
308 
309  Py_BEGIN_ALLOW_THREADS
310  try
311  {
312  sipRes = sipCpp->convertCompatible( *a0 );
313  }
314  catch ( ... )
315  {
316  Py_BLOCK_THREADS
317 
318  sipReleaseType( a0, sipType_QVariant, a0State );
319  sipRaiseUnknownException();
320  return NULL;
321  }
322 
323  Py_END_ALLOW_THREADS
324 
325  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
326  sipReleaseType( a0, sipType_QVariant, a0State );
327 
328  if ( !sipRes )
329  {
330  PyErr_SetString( PyExc_ValueError,
331  QString( "Value %1 (%2) could not be converted to field type %3." ).arg( a0->toString(), a0->typeName() ).arg( sipCpp->type() ).toUtf8().constData() );
332  sipError = sipErrorFail;
333  }
334 
335  return res;
336  }
337  }
338 
339  // Raise an exception if the arguments couldn't be parsed.
340  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
341 
342  return nullptr;
343  % End
344 #endif
345 
347  operator QVariant() const
348  {
349  return QVariant::fromValue( *this );
350  }
351 
357  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
358 
367  QgsEditorWidgetSetup editorWidgetSetup() const;
368 
369 #ifdef SIP_RUN
370  SIP_PYOBJECT __repr__();
371  % MethodCode
372  QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
373  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
374  % End
375 #endif
376 
377  private:
378 
379  QSharedDataPointer<QgsFieldPrivate> d;
380 
381 
382 }; // class QgsField
383 
385 
386 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
389 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
390 
391 
392 #endif
QgsDefaultValue
The QgsDefaultValue class provides a container for managing client side default values for fields.
Definition: qgsdefaultvalue.h:48
QgsEditorWidgetSetup
Definition: qgseditorwidgetsetup.h:28
qgsdefaultvalue.h
QgsFieldConstraints
Definition: qgsfieldconstraints.h:32
QgsAttributeList
QList< int > QgsAttributeList
Definition: qgsfield.h:26
Q_DECLARE_METATYPE
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
precision
int precision
Definition: qgswfsgetfeature.cpp:103
SIP_SKIP
#define SIP_SKIP
Definition: qgis_sip.h:126
QgsField::convertCompatible
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:339
qgis_sip.h
qgseditorwidgetsetup.h
typeName
const QString & typeName
Definition: qgswfsgetfeature.cpp:109
operator>>
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:531
qgsfieldconstraints.h
operator<<
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfield.cpp:507
QgsField::type
QVariant::Type type
Definition: qgsfield.h:57
QgsField
Definition: qgsfield.h:49