QGIS API Documentation  2.99.0-Master (f1c3692)
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.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( int length READ length WRITE setLength )
54  Q_PROPERTY( int precision READ precision WRITE setPrecision )
55  Q_PROPERTY( QString comment READ comment WRITE setComment )
56  Q_PROPERTY( QString name READ name WRITE setName )
57  Q_PROPERTY( QString alias READ alias WRITE setAlias )
58  Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
59  Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
60 
61  public:
62 
78  QgsField( const QString &name = QString(),
79  QVariant::Type type = QVariant::Invalid,
80  const QString &typeName = QString(),
81  int len = 0,
82  int prec = 0,
83  const QString &comment = QString(),
84  QVariant::Type subType = QVariant::Invalid );
85 
89  QgsField( const QgsField &other );
90 
94  QgsField &operator =( const QgsField &other ) SIP_SKIP;
95 
96  virtual ~QgsField() = default;
97 
98  bool operator==( const QgsField &other ) const;
99  bool operator!=( const QgsField &other ) const;
100 
106  QString name() const;
107 
115  QString displayName() const;
116 
118  QVariant::Type type() const;
119 
126  QVariant::Type subType() const;
127 
134  QString typeName() const;
135 
140  int length() const;
141 
146  int precision() const;
147 
151  QString comment() const;
152 
159  bool isNumeric() const;
160 
165  void setName( const QString &name );
166 
170  void setType( QVariant::Type type );
171 
178  void setSubType( QVariant::Type subType );
179 
184  void setTypeName( const QString &typeName );
185 
190  void setLength( int len );
191 
196  void setPrecision( int precision );
197 
201  void setComment( const QString &comment );
202 
210  QgsDefaultValue defaultValueDefinition() const;
211 
219  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
220 
226  const QgsFieldConstraints &constraints() const;
227 
233  void setConstraints( const QgsFieldConstraints &constraints );
234 
241  QString alias() const;
242 
249  void setAlias( const QString &alias );
250 
252  QString displayString( const QVariant &v ) const;
253 
261  bool convertCompatible( QVariant &v ) const;
262 #ifdef SIP_RUN
263  % MethodCode
264  PyObject *sipParseErr = NULL;
265 
266  {
267  QVariant *a0;
268  int a0State = 0;
269  const QgsField *sipCpp;
270 
271  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
272  {
273  bool sipRes;
274 
275  Py_BEGIN_ALLOW_THREADS
276  try
277  {
278  QgsDebugMsg( a0->toString() );
279  sipRes = sipCpp->convertCompatible( *a0 );
280  QgsDebugMsg( a0->toString() );
281  }
282  catch ( ... )
283  {
284  Py_BLOCK_THREADS
285 
286  sipReleaseType( a0, sipType_QVariant, a0State );
287  sipRaiseUnknownException();
288  return NULL;
289  }
290 
291  Py_END_ALLOW_THREADS
292 
293  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
294  sipReleaseType( a0, sipType_QVariant, a0State );
295 
296  if ( !sipRes )
297  {
298  PyErr_SetString( PyExc_ValueError,
299  QString( "Value %1 (%2) could not be converted to field type %3." ).arg( a0->toString(), a0->typeName() ).arg( sipCpp->type() ).toUtf8().constData() );
300  sipError = sipErrorFail;
301  }
302 
303  return res;
304  }
305  }
306 
307  // Raise an exception if the arguments couldn't be parsed.
308  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
309 
310  return nullptr;
311  % End
312 #endif
313 
315  operator QVariant() const
316  {
317  return QVariant::fromValue( *this );
318  }
319 
325  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
326 
335  QgsEditorWidgetSetup editorWidgetSetup() const;
336 
337  private:
338 
339  QSharedDataPointer<QgsFieldPrivate> d;
340 
341 
342 }; // class QgsField
343 
345 
346 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
349 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
350 
351 
352 #endif
The QgsDefaultValue class provides a container for managing client side default values for fields...
#define QgsDebugMsg(str)
Definition: qgslogger.h:37
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
Definition: qgsfield.cpp:224
Stores information about constraints which may be present on a field.
Q_DECLARE_METATYPE(QModelIndex)
#define SIP_SKIP
Definition: qgis_sip.h:119
CORE_EXPORT QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out. QGIS version compatibility is not guaranteed.
Definition: qgsfield.cpp:301
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
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.
Definition: qgsfield.cpp:93
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:325