QGIS API Documentation  3.7.0-Master (1205fbf)
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 "qgsfield_p.h"
24 #include "qgis_core.h"
25 #include "qgis_sip.h"
26 
27 typedef QList<int> QgsAttributeList SIP_SKIP;
28 
29 /***************************************************************************
30  * This class is considered CRITICAL and any change MUST be accompanied with
31  * full unit tests in testqgsfield.cpp.
32  * See details in QEP #17
33  ****************************************************************************/
34 
35 #include "qgseditorwidgetsetup.h"
36 #include "qgsfieldconstraints.h"
37 #include "qgsdefaultvalue.h"
38 
48 class CORE_EXPORT QgsField
49 {
50  Q_GADGET
51 
52  Q_PROPERTY( bool isNumeric READ isNumeric )
53  Q_PROPERTY( bool isDateOrTime READ isDateOrTime )
54  Q_PROPERTY( int length READ length WRITE setLength )
55  Q_PROPERTY( int precision READ precision WRITE setPrecision )
56  Q_PROPERTY( QVariant::Type type READ type WRITE setType )
57  Q_PROPERTY( QString comment READ comment WRITE setComment )
58  Q_PROPERTY( QString name READ name WRITE setName )
59  Q_PROPERTY( QString alias READ alias WRITE setAlias )
60  Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
61  Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
62 
63  public:
64 
80  QgsField( const QString &name = QString(),
81  QVariant::Type type = QVariant::Invalid,
82  const QString &typeName = QString(),
83  int len = 0,
84  int prec = 0,
85  const QString &comment = QString(),
86  QVariant::Type subType = QVariant::Invalid );
87 
91  QgsField( const QgsField &other );
92 
96  QgsField &operator =( const QgsField &other ) SIP_SKIP;
97 
98  virtual ~QgsField() = default;
99 
100  bool operator==( const QgsField &other ) const;
101  bool operator!=( const QgsField &other ) const;
102 
108  QString name() const;
109 
117  QString displayName() const;
118 
120  QVariant::Type type() const;
121 
128  QVariant::Type subType() const;
129 
136  QString typeName() const;
137 
142  int length() const;
143 
148  int precision() const;
149 
153  QString comment() const;
154 
161  bool isNumeric() const;
162 
168  bool isDateOrTime() const;
169 
174  void setName( const QString &name );
175 
179  void setType( QVariant::Type type );
180 
187  void setSubType( QVariant::Type subType );
188 
193  void setTypeName( const QString &typeName );
194 
199  void setLength( int len );
200 
205  void setPrecision( int precision );
206 
210  void setComment( const QString &comment );
211 
219  QgsDefaultValue defaultValueDefinition() const;
220 
228  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
229 
235  const QgsFieldConstraints &constraints() const;
236 
242  void setConstraints( const QgsFieldConstraints &constraints );
243 
250  QString alias() const;
251 
258  void setAlias( const QString &alias );
259 
261  QString displayString( const QVariant &v ) const;
262 
270  bool convertCompatible( QVariant &v ) const;
271 #ifdef SIP_RUN
272  % MethodCode
273  PyObject *sipParseErr = NULL;
274 
275  {
276  QVariant *a0;
277  int a0State = 0;
278  const QgsField *sipCpp;
279 
280  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
281  {
282  bool sipRes;
283 
284  Py_BEGIN_ALLOW_THREADS
285  try
286  {
287  QgsDebugMsg( a0->toString() );
288  sipRes = sipCpp->convertCompatible( *a0 );
289  QgsDebugMsg( a0->toString() );
290  }
291  catch ( ... )
292  {
293  Py_BLOCK_THREADS
294 
295  sipReleaseType( a0, sipType_QVariant, a0State );
296  sipRaiseUnknownException();
297  return NULL;
298  }
299 
300  Py_END_ALLOW_THREADS
301 
302  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
303  sipReleaseType( a0, sipType_QVariant, a0State );
304 
305  if ( !sipRes )
306  {
307  PyErr_SetString( PyExc_ValueError,
308  QString( "Value %1 (%2) could not be converted to field type %3." ).arg( a0->toString(), a0->typeName() ).arg( sipCpp->type() ).toUtf8().constData() );
309  sipError = sipErrorFail;
310  }
311 
312  return res;
313  }
314  }
315 
316  // Raise an exception if the arguments couldn't be parsed.
317  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
318 
319  return nullptr;
320  % End
321 #endif
322 
324  operator QVariant() const
325  {
326  return QVariant::fromValue( *this );
327  }
328 
334  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
335 
344  QgsEditorWidgetSetup editorWidgetSetup() const;
345 
346 #ifdef SIP_RUN
347  SIP_PYOBJECT __repr__();
348  % MethodCode
349  QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
350  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
351  % End
352 #endif
353 
354  private:
355 
356  QSharedDataPointer<QgsFieldPrivate> d;
357 
358 
359 }; // class QgsField
360 
362 
363 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
366 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
367 
368 
369 #endif
int precision
The QgsDefaultValue class provides a container for managing client side default values for fields...
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:281
Stores information about constraints which may be present on a field.
#define SIP_SKIP
Definition: qgis_sip.h:119
const QString & typeName
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfield.cpp:421
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
Encapsulate a field in an attribute table or data source.
Definition: qgsfield.h:48
Holder for the widget type and its configuration for a field.
QList< int > QgsAttributeList
Definition: qgsfield.h:27
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:445
QVariant::Type type
Definition: qgsfield.h:56