QGIS API Documentation  3.0.2-Girona (307d082)
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( QVariant::Type type READ type WRITE setType )
56  Q_PROPERTY( QString comment READ comment WRITE setComment )
57  Q_PROPERTY( QString name READ name WRITE setName )
58  Q_PROPERTY( QString alias READ alias WRITE setAlias )
59  Q_PROPERTY( QgsDefaultValue defaultValueDefinition READ defaultValueDefinition WRITE setDefaultValueDefinition )
60  Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
61 
62  public:
63 
79  QgsField( const QString &name = QString(),
80  QVariant::Type type = QVariant::Invalid,
81  const QString &typeName = QString(),
82  int len = 0,
83  int prec = 0,
84  const QString &comment = QString(),
85  QVariant::Type subType = QVariant::Invalid );
86 
90  QgsField( const QgsField &other );
91 
95  QgsField &operator =( const QgsField &other ) SIP_SKIP;
96 
97  virtual ~QgsField() = default;
98 
99  bool operator==( const QgsField &other ) const;
100  bool operator!=( const QgsField &other ) const;
101 
107  QString name() const;
108 
116  QString displayName() const;
117 
119  QVariant::Type type() const;
120 
127  QVariant::Type subType() const;
128 
135  QString typeName() const;
136 
141  int length() const;
142 
147  int precision() const;
148 
152  QString comment() const;
153 
160  bool isNumeric() const;
161 
166  void setName( const QString &name );
167 
171  void setType( QVariant::Type type );
172 
179  void setSubType( QVariant::Type subType );
180 
185  void setTypeName( const QString &typeName );
186 
191  void setLength( int len );
192 
197  void setPrecision( int precision );
198 
202  void setComment( const QString &comment );
203 
211  QgsDefaultValue defaultValueDefinition() const;
212 
220  void setDefaultValueDefinition( const QgsDefaultValue &defaultValueDefinition );
221 
227  const QgsFieldConstraints &constraints() const;
228 
234  void setConstraints( const QgsFieldConstraints &constraints );
235 
242  QString alias() const;
243 
250  void setAlias( const QString &alias );
251 
253  QString displayString( const QVariant &v ) const;
254 
262  bool convertCompatible( QVariant &v ) const;
263 #ifdef SIP_RUN
264  % MethodCode
265  PyObject *sipParseErr = NULL;
266 
267  {
268  QVariant *a0;
269  int a0State = 0;
270  const QgsField *sipCpp;
271 
272  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
273  {
274  bool sipRes;
275 
276  Py_BEGIN_ALLOW_THREADS
277  try
278  {
279  QgsDebugMsg( a0->toString() );
280  sipRes = sipCpp->convertCompatible( *a0 );
281  QgsDebugMsg( a0->toString() );
282  }
283  catch ( ... )
284  {
285  Py_BLOCK_THREADS
286 
287  sipReleaseType( a0, sipType_QVariant, a0State );
288  sipRaiseUnknownException();
289  return NULL;
290  }
291 
292  Py_END_ALLOW_THREADS
293 
294  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
295  sipReleaseType( a0, sipType_QVariant, a0State );
296 
297  if ( !sipRes )
298  {
299  PyErr_SetString( PyExc_ValueError,
300  QString( "Value %1 (%2) could not be converted to field type %3." ).arg( a0->toString(), a0->typeName() ).arg( sipCpp->type() ).toUtf8().constData() );
301  sipError = sipErrorFail;
302  }
303 
304  return res;
305  }
306  }
307 
308  // Raise an exception if the arguments couldn't be parsed.
309  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
310 
311  return nullptr;
312  % End
313 #endif
314 
316  operator QVariant() const
317  {
318  return QVariant::fromValue( *this );
319  }
320 
326  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
327 
336  QgsEditorWidgetSetup editorWidgetSetup() const;
337 
338  private:
339 
340  QSharedDataPointer<QgsFieldPrivate> d;
341 
342 
343 }; // class QgsField
344 
346 
347 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
350 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
351 
352 
353 #endif
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:223
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:300
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:324
QVariant::Type type
Definition: qgsfield.h:55