QGIS API Documentation  3.12.1-BucureČ™ti (121cc00ff0)
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 
134  QVariant::Type type() const;
135 
142  QVariant::Type subType() const;
143 
150  QString typeName() const;
151 
156  int length() const;
157 
162  int precision() const;
163 
167  QString comment() const;
168 
175  bool isNumeric() const;
176 
182  bool isDateOrTime() const;
183 
188  void setName( const QString &name );
189 
193  void setType( QVariant::Type type );
194 
201  void setSubType( QVariant::Type subType );
202 
207  void setTypeName( const QString &typeName );
208 
213  void setLength( int len );
214 
219  void setPrecision( int precision );
220 
224  void setComment( const QString &comment );
225 
233  QgsDefaultValue defaultValueDefinition() const;
234 
242  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
243 
249  const QgsFieldConstraints &constraints() const;
250 
256  void setConstraints( const QgsFieldConstraints &constraints );
257 
264  QString alias() const;
265 
272  void setAlias( const QString &alias );
273 
275  QString displayString( const QVariant &v ) const;
276 
284  bool convertCompatible( QVariant &v ) const;
285 #ifdef SIP_RUN
286  % MethodCode
287  PyObject *sipParseErr = NULL;
288 
289  {
290  QVariant *a0;
291  int a0State = 0;
292  const QgsField *sipCpp;
293 
294  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
295  {
296  bool sipRes;
297 
298  Py_BEGIN_ALLOW_THREADS
299  try
300  {
301  sipRes = sipCpp->convertCompatible( *a0 );
302  }
303  catch ( ... )
304  {
305  Py_BLOCK_THREADS
306 
307  sipReleaseType( a0, sipType_QVariant, a0State );
308  sipRaiseUnknownException();
309  return NULL;
310  }
311 
312  Py_END_ALLOW_THREADS
313 
314  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
315  sipReleaseType( a0, sipType_QVariant, a0State );
316 
317  if ( !sipRes )
318  {
319  PyErr_SetString( PyExc_ValueError,
320  QString( "Value %1 (%2) could not be converted to field type %3." ).arg( a0->toString(), a0->typeName() ).arg( sipCpp->type() ).toUtf8().constData() );
321  sipError = sipErrorFail;
322  }
323 
324  return res;
325  }
326  }
327 
328  // Raise an exception if the arguments couldn't be parsed.
329  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
330 
331  return nullptr;
332  % End
333 #endif
334 
336  operator QVariant() const
337  {
338  return QVariant::fromValue( *this );
339  }
340 
346  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
347 
356  QgsEditorWidgetSetup editorWidgetSetup() const;
357 
358 #ifdef SIP_RUN
359  SIP_PYOBJECT __repr__();
360  % MethodCode
361  QString str = QStringLiteral( "<QgsField: %1 (%2)>" ).arg( sipCpp->name() ).arg( sipCpp->typeName() );
362  sipRes = PyUnicode_FromString( str.toUtf8().constData() );
363  % End
364 #endif
365 
366  private:
367 
368  QSharedDataPointer<QgsFieldPrivate> d;
369 
370 
371 }; // class QgsField
372 
374 
375 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
378 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
379 
380 
381 #endif
int precision
The QgsDefaultValue class provides a container for managing client side default values for fields...
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:316
Stores information about constraints which may be present on a field.
#define SIP_SKIP
Definition: qgis_sip.h:126
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:484
Q_DECLARE_METATYPE(QgsMeshTimeSettings)
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.
QList< int > QgsAttributeList
Definition: qgsfield.h:26
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:508
QVariant::Type type
Definition: qgsfield.h:57