2015-05-03 17:03:37 +10:00
/** \class QgsField
* \ingroup core
* Encapsulate a field in an attribute table or data source.
* QgsField stores metadata about an attribute field, including name, type
* length, and if applicable, precision.
* \note QgsField objects are implicitly shared.
2007-01-09 02:39:15 +00:00
*/
class QgsField
{
%TypeHeaderCode
#include <qgsfield.h>
%End
2015-10-11 22:42:28 +02:00
public:
2007-01-09 02:39:15 +00:00
/** Constructor. Constructs a new QgsField object.
2012-09-24 02:28:15 +02:00
* @param name Field name
2007-03-24 22:40:10 +00:00
* @param type Field variant type, currently supported: String / Int / Double
2012-09-24 02:28:15 +02:00
* @param typeName Field type (eg. char, varchar, text, int, serial, double).
2016-02-14 03:50:23 +01:00
* Field types are usually unique to the source and are stored exactly
* as returned from the data store.
2007-01-09 02:39:15 +00:00
* @param len Field length
* @param prec Field precision. Usually decimal places but may also be
* used in conjunction with other fields types (eg. variable character fields)
2007-03-24 22:40:10 +00:00
* @param comment Comment for the field
2016-09-07 14:30:10 +02:00
* @param subType If the field is a collection, its element's type. When
* all the elements don't need to have the same type, leave
* this to QVariant::Invalid.
2007-01-09 02:39:15 +00:00
*/
2015-10-07 11:55:34 +11:00
QgsField( const QString& name = QString(),
2012-09-24 02:28:15 +02:00
QVariant::Type type = QVariant::Invalid,
2015-10-07 11:55:34 +11:00
const QString& typeName = QString(),
2012-09-24 02:28:15 +02:00
int len = 0,
int prec = 0,
2016-09-06 15:03:16 +02:00
const QString& comment = QString(),
QVariant::Type subType = QVariant::Invalid );
2007-01-09 02:39:15 +00:00
2015-05-01 20:28:39 +10:00
/** Copy constructor
*/
QgsField( const QgsField& other );
2012-09-24 02:28:15 +02:00
//! Destructor
2015-05-03 17:03:37 +10:00
virtual ~QgsField();
2007-01-09 02:39:15 +00:00
2012-09-24 02:28:15 +02:00
bool operator==( const QgsField& other ) const;
2012-12-17 21:22:42 +01:00
bool operator!=( const QgsField& other ) const;
2007-01-09 02:39:15 +00:00
2016-08-30 12:12:47 +10:00
/** Returns the name of the field.
* @see setName()
* @see displayName()
*/
2015-10-27 14:03:00 +11:00
QString name() const;
2007-01-09 02:39:15 +00:00
2016-08-30 12:12:47 +10:00
/** Returns the name to use when displaying this field. This will be the
* field alias if set, otherwise the field name.
* @see name()
* @see alias()
* @note added in QGIS 3.0
*/
QString displayName() const;
2012-09-24 02:28:15 +02:00
//! Gets variant type of the field as it will be retrieved from data source
QVariant::Type type() const;
2016-09-06 15:03:16 +02:00
/**
2016-09-07 14:30:10 +02:00
* If the field is a collection, gets its element's type.
* When all the elements don't need to have the same type, this returns
* QVariant::Invalid.
2016-09-06 15:03:16 +02:00
* @note added in QGIS 3.0
*/
QVariant::Type subType() const;
2012-09-24 02:28:15 +02:00
/**
2016-02-14 03:50:23 +01:00
* Gets the field type. Field types vary depending on the data source. Examples
* are char, int, double, blob, geometry, etc. The type is stored exactly as
* the data store reports it, with no attempt to standardize the value.
* @return QString containing the field type
2007-01-09 02:39:15 +00:00
*/
2015-10-27 14:03:00 +11:00
QString typeName() const;
2007-01-09 02:39:15 +00:00
/**
2016-02-14 03:50:23 +01:00
* Gets the length of the field.
* @return int containing the length of the field
2007-01-09 02:39:15 +00:00
*/
2012-09-24 02:28:15 +02:00
int length() const;
2007-01-09 02:39:15 +00:00
/**
2016-02-14 03:50:23 +01:00
* Gets the precision of the field. Not all field types have a related precision.
* @return int containing the precision or zero if not applicable to the field type.
2007-01-09 02:39:15 +00:00
*/
2012-09-24 02:28:15 +02:00
int precision() const;
2007-01-09 02:39:15 +00:00
2012-09-24 02:28:15 +02:00
/**
2016-02-14 03:50:23 +01:00
* Returns the field comment
*/
2015-10-27 14:03:00 +11:00
QString comment() const;
2007-01-09 02:39:15 +00:00
2016-08-16 16:22:29 +02:00
/**
* Returns if this field is numeric. Any integer or floating point type
* will return true for this.
*
* @note added in QGIS 2.18
*/
bool isNumeric() const;
2007-01-09 02:39:15 +00:00
/**
2016-02-14 03:50:23 +01:00
* Set the field name.
* @param name Name of the field
2007-01-09 02:39:15 +00:00
*/
2015-05-01 20:28:39 +10:00
void setName( const QString& name );
2012-09-24 02:28:15 +02:00
2007-03-24 22:40:10 +00:00
/**
2016-02-14 03:50:23 +01:00
* Set variant type.
2007-03-24 22:40:10 +00:00
*/
2012-09-24 02:28:15 +02:00
void setType( QVariant::Type type );
2007-01-09 02:39:15 +00:00
2016-09-06 15:03:16 +02:00
/**
* If the field is a collection, set its element's type.
2016-09-07 14:30:10 +02:00
* When all the elements don't need to have the same type, set this to
* QVariant::Invalid.
2016-09-06 15:03:16 +02:00
* @note added in QGIS 3.0
*/
void setSubType( QVariant::Type subType );
2007-01-09 02:39:15 +00:00
/**
2016-02-14 03:50:23 +01:00
* Set the field type.
* @param typeName Field type
2007-01-09 02:39:15 +00:00
*/
2015-05-01 20:28:39 +10:00
void setTypeName( const QString& typeName );
2007-01-09 02:39:15 +00:00
/**
2016-02-14 03:50:23 +01:00
* Set the field length.
* @param len Length of the field
2007-01-09 02:39:15 +00:00
*/
2012-09-24 02:28:15 +02:00
void setLength( int len );
2007-01-09 02:39:15 +00:00
/**
2016-02-14 03:50:23 +01:00
* Set the field precision.
* @param precision Precision of the field
2007-01-09 02:39:15 +00:00
*/
2015-05-01 20:28:39 +10:00
void setPrecision( int precision );
2007-01-09 02:39:15 +00:00
/**
2016-02-14 03:50:23 +01:00
* Set the field comment
*/
2015-05-01 20:28:39 +10:00
void setComment( const QString& comment );
2007-01-09 02:39:15 +00:00
2016-08-30 12:12:47 +10:00
/** Returns the expression used when calculating the default value for the field.
* @returns expression evaluated when calculating default values for field, or an
* empty string if no default is set
* @note added in QGIS 3.0
* @see setDefaultValueExpression()
*/
QString defaultValueExpression() const;
/** Sets an expression to use when calculating the default value for the field.
* @param expression expression to evaluate when calculating default values for field. Pass
* an empty expression to clear the default.
* @note added in QGIS 3.0
* @see defaultValueExpression()
*/
void setDefaultValueExpression( const QString& expression );
/** Returns the alias for the field (the friendly displayed name of the field ),
* or an empty string if there is no alias.
* @see setAlias()
* @note added in QGIS 3.0
*/
QString alias() const;
/** Sets the alias for the field (the friendly displayed name of the field ).
* @param alias field alias, or empty string to remove an existing alias
* @see alias()
* @note added in QGIS 3.0
*/
void setAlias( const QString& alias );
2015-02-03 02:21:52 +01:00
/** Formats string for display*/
2013-06-23 16:00:16 +02:00
QString displayString( const QVariant& v ) const;
2014-10-06 10:17:05 +02:00
/**
* Converts the provided variant to a compatible format
*
* @param v The value to convert
*
* @return True if the conversion was successful
*/
QVariant convertCompatible( QVariant& v ) const;
%MethodCode
PyObject *sipParseErr = NULL;
{
QVariant * a0;
int a0State = 0;
const QgsField *sipCpp;
if (sipParseArgs(&sipParseErr, sipArgs, "BJ1", &sipSelf, sipType_QgsField, &sipCpp, sipType_QVariant,&a0, &a0State))
{
bool sipRes;
Py_BEGIN_ALLOW_THREADS
try
{
QgsDebugMsg( a0->toString() );
sipRes = sipCpp->convertCompatible(*a0);
QgsDebugMsg( a0->toString() );
}
catch (...)
{
Py_BLOCK_THREADS
sipReleaseType(a0,sipType_QVariant,a0State);
sipRaiseUnknownException();
return NULL;
}
Py_END_ALLOW_THREADS
PyObject* res = sipConvertFromType( a0, sipType_QVariant, NULL );
sipReleaseType(a0,sipType_QVariant,a0State);
if ( !sipRes )
{
PyErr_SetString(PyExc_ValueError,
2015-10-15 17:50:17 +11:00
QString( "Value %1 (%2) could not be converted to field type %3." ).arg( a0->toString(), a0->typeName() ).arg( sipCpp->type() ).toUtf8().constData() );
2014-10-06 10:17:05 +02:00
sipError = sipErrorFail;
}
return res;
}
}
/* Raise an exception if the arguments couldn't be parsed. */
sipNoMethod(sipParseErr, sipName_QgsField, sipName_convertCompatible, doc_QgsField_convertCompatible);
2016-03-14 12:15:04 +01:00
return nullptr;
2014-10-06 10:17:05 +02:00
%End
2016-05-20 09:48:18 +10:00
//! Allows direct construction of QVariants from fields.
operator QVariant() const;
2016-07-15 12:11:43 +02:00
/**
* Set the editor widget setup for the field.
*
* @param v The value to set
*/
void setEditorWidgetSetup( const QgsEditorWidgetSetup& v );
/**
* Get the editor widget setup for the field.
*
* @return the value
*/
const QgsEditorWidgetSetup& editorWidgetSetup() const;
2007-01-09 02:39:15 +00:00
}; // class QgsField