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 );
199 mNameToIndex.clear();
204 if ( mNameToIndex.contains( field.
name() ) )
207 if ( originIndex == -1 && origin == OriginProvider )
208 originIndex = mFields.count();
209 mFields.append(
Field( field, origin, originIndex ) );
211 mNameToIndex.insert( field.
name(), mFields.count() - 1 );
217 if ( mNameToIndex.contains( field.
name() ) )
220 mFields.append(
Field( field, OriginExpression, originIndex ) );
222 mNameToIndex.insert( field.
name(), mFields.count() - 1 );
228 if ( !exists( fieldIdx ) )
231 mFields.remove( fieldIdx );
232 mNameToIndex.clear();
233 for (
int idx = 0; idx < count(); ++idx )
235 mNameToIndex.insert( mFields[idx].field.name(), idx );
241 for (
int i = 0; i < other.
count(); ++i )
249 if ( !exists( fieldIdx ) )
250 return OriginUnknown;
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 )
267 if ( mFields[idx].field.name() == fieldName )
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
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.
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)
void setLength(int len)
Set the field length.
double qgsRound(double x)
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
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.
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.
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.
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.