QGIS API Documentation  2.99.0-Master (53aba61)
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 
46 class CORE_EXPORT QgsField
47 {
48  Q_GADGET
49 
50  Q_PROPERTY( bool isNumeric READ isNumeric )
51  Q_PROPERTY( int length READ length WRITE setLength )
52  Q_PROPERTY( int precision READ precision WRITE setPrecision )
53  Q_PROPERTY( QString comment READ comment WRITE setComment )
54  Q_PROPERTY( QString name READ name WRITE setName )
55  Q_PROPERTY( QString alias READ alias WRITE setAlias )
56  Q_PROPERTY( QString defaultValueExpression READ defaultValueExpression WRITE setDefaultValueExpression )
57  Q_PROPERTY( QgsFieldConstraints constraints READ constraints WRITE setConstraints )
58 
59  public:
60 
75  QgsField( const QString &name = QString(),
76  QVariant::Type type = QVariant::Invalid,
77  const QString &typeName = QString(),
78  int len = 0,
79  int prec = 0,
80  const QString &comment = QString(),
81  QVariant::Type subType = QVariant::Invalid );
82 
85  QgsField( const QgsField &other );
86 
89  QgsField &operator =( const QgsField &other ) SIP_SKIP;
90 
91  virtual ~QgsField() = default;
92 
93  bool operator==( const QgsField &other ) const;
94  bool operator!=( const QgsField &other ) const;
95 
100  QString name() const;
101 
108  QString displayName() const;
109 
111  QVariant::Type type() const;
112 
119  QVariant::Type subType() const;
120 
127  QString typeName() const;
128 
133  int length() const;
134 
139  int precision() const;
140 
144  QString comment() const;
145 
152  bool isNumeric() const;
153 
158  void setName( const QString &name );
159 
163  void setType( QVariant::Type type );
164 
171  void setSubType( QVariant::Type subType );
172 
177  void setTypeName( const QString &typeName );
178 
183  void setLength( int len );
184 
189  void setPrecision( int precision );
190 
194  void setComment( const QString &comment );
195 
202  QString defaultValueExpression() const;
203 
210  void setDefaultValueExpression( const QString &expression );
211 
217  const QgsFieldConstraints &constraints() const;
218 
224  void setConstraints( const QgsFieldConstraints &constraints );
225 
231  QString alias() const;
232 
238  void setAlias( const QString &alias );
239 
241  QString displayString( const QVariant &v ) const;
242 
250  bool convertCompatible( QVariant &v ) const;
251 #ifdef SIP_RUN
252  % MethodCode
253  PyObject *sipParseErr = NULL;
254 
255  {
256  QVariant *a0;
257  int a0State = 0;
258  const QgsField *sipCpp;
259 
260  if ( sipParseArgs( &sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant, &a0, &a0State ) )
261  {
262  bool sipRes;
263 
264  Py_BEGIN_ALLOW_THREADS
265  try
266  {
267  QgsDebugMsg( a0->toString() );
268  sipRes = sipCpp->convertCompatible( *a0 );
269  QgsDebugMsg( a0->toString() );
270  }
271  catch ( ... )
272  {
273  Py_BLOCK_THREADS
274 
275  sipReleaseType( a0, sipType_QVariant, a0State );
276  sipRaiseUnknownException();
277  return NULL;
278  }
279 
280  Py_END_ALLOW_THREADS
281 
282  PyObject *res = sipConvertFromType( a0, sipType_QVariant, NULL );
283  sipReleaseType( a0, sipType_QVariant, a0State );
284 
285  if ( !sipRes )
286  {
287  PyErr_SetString( PyExc_ValueError,
288  QString( "Value %1 (%2) could not be converted to field type %3." ).arg( a0->toString(), a0->typeName() ).arg( sipCpp->type() ).toUtf8().constData() );
289  sipError = sipErrorFail;
290  }
291 
292  return res;
293  }
294  }
295 
296  // Raise an exception if the arguments couldn't be parsed.
297  sipNoMethod( sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible );
298 
299  return nullptr;
300  % End
301 #endif
302 
304  operator QVariant() const
305  {
306  return QVariant::fromValue( *this );
307  }
308 
314  void setEditorWidgetSetup( const QgsEditorWidgetSetup &v );
315 
324  QgsEditorWidgetSetup editorWidgetSetup() const;
325 
326  private:
327 
328  QSharedDataPointer<QgsFieldPrivate> d;
329 
330 
331 }; // class QgsField
332 
334 
335 CORE_EXPORT QDataStream &operator<<( QDataStream &out, const QgsField &field );
338 CORE_EXPORT QDataStream &operator>>( QDataStream &in, QgsField &field );
339 
340 
341 #endif
#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:46
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:324