20 #include <QtCore/qmath.h>
26 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
37 QgsField::QgsField( QString name, QVariant::Type type, QString typeName,
int len,
int prec, QString comment )
38 : mName( name ), mType( type ), mTypeName( typeName )
39 , mLength( len ), mPrecision( prec ), mComment( comment )
50 return (( mName == other.mName ) && ( mType == other.mType )
51 && ( mLength == other.mLength ) && ( mPrecision == other.mPrecision ) );
56 return !( *
this == other );
124 return settings.value(
"qgis/nullValue",
"NULL" ).toString();
127 if ( mType == QVariant::Double && mPrecision > 0 )
128 return QString::number( v.toDouble(),
'f', mPrecision );
141 if ( mType == QVariant::Int && v.toInt() != v.toLongLong() )
143 v = QVariant( mType );
149 if ( mType == QVariant::Int && v.canConvert( QVariant::Double ) )
152 double dbl = v.toDouble( &ok );
156 v = QVariant( mType );
161 if ( round > INT_MAX || round < -INT_MAX )
164 v = QVariant( mType );
167 v = QVariant( qRound( dbl ) );
171 if ( !v.convert( mType ) )
173 v = QVariant( mType );
177 if ( mType == QVariant::Double && mPrecision > 0 )
179 double s = qPow( 10, mPrecision );
180 double d = v.toDouble() * s;
181 v = QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
185 if ( mType == QVariant::String && mLength > 0 && v.toString().length() > mLength )
187 v = v.toString().left( mLength );
228 if ( !
exists( fieldIdx ) )
233 for (
int idx = 0; idx <
count(); ++idx )
241 for (
int i = 0; i < other.
count(); ++i )
249 if ( !
exists( fieldIdx ) )
252 return mFields[fieldIdx].origin;
258 for (
int i = 0; i <
mFields.count(); ++i )
259 lst.append(
mFields[i].field );
265 for (
int idx = 0; idx <
count(); ++idx )
271 for (
int idx = 0; idx <
count(); ++idx )
273 if ( QString::compare(
mFields[idx].
field.
name(), fieldName, Qt::CaseInsensitive ) == 0 )
283 for (
int i = 0; i <
mFields.count(); ++i )
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
void setPrecision(int prec)
Set the field precision.
const QString & name() const
Gets the name of the field.
bool operator==(const QgsField &other) const
struct QgsFields::Field Field
QgsField(QString name=QString(), QVariant::Type type=QVariant::Invalid, QString typeName=QString(), int len=0, int prec=0, QString comment=QString())
Constructor.
void setTypeName(const QString &typ)
Set the field type.
QString displayString(const QVariant &v) const
Formats string for display.
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name also looks up case-insensitive if there is no match otherwise...
int precision() const
Gets the precision of the field.
Container of fields for a vector layer.
bool appendExpressionField(const QgsField &field, int originIndex)
Append an expression field. The field must have unique name, otherwise it is rejected (returns false)...
void extend(const QgsFields &other)
Extend with fields from another QgsFields container.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
void setLength(int len)
Set the field length.
double qgsRound(double x)
bool exists(int i) const
Return if a field index is valid.
void clear()
Remove all fields.
bool operator!=(const QgsField &other) const
int fieldOriginIndex(int fieldIdx) const
Get field's origin index (its meaning is specific to each type of origin)
QList< int > QgsAttributeList
QHash< QString, int > mNameToIndex
map for quick resolution of name to index
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Append a field. The field must have unique name, otherwise it is rejected (returns false) ...
int count() const
Return number of items.
field is calculated from an expression
Encapsulate a field in an attribute table or data source.
void remove(int fieldIdx)
Remove a field with the given index.
void setName(const QString &nam)
Set the field name.
void setType(QVariant::Type type)
Set variant type.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
const QString & typeName() const
Gets the field type.
it has not been specified where the field comes from
int length() const
Gets the length of the field.
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
const QString & comment() const
Returns the field comment.
QVector< Field > mFields
internal storage of the container
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
void setComment(const QString &comment)
Set the field comment.
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.