23 #include <QDataStream> 24 #include <QtCore/qmath.h> 36 : mName( nam ), mType( typ ), mLength( len ), mPrecision( prec ), mNumeric( num )
47 const QString& typeName,
int len,
int prec,
const QString& comment )
49 d =
new QgsFieldPrivate( name, type, typeName, len, prec, comment );
76 return *( other.d ) == *d;
81 return !( *
this == other );
160 return settings.
value(
"qgis/nullValue",
"NULL" ).
toString();
163 if ( d->type == QVariant::Double && d->precision > 0 )
191 if ( d->type == QVariant::Int && v.
canConvert( QVariant::Double ) )
203 if ( round > INT_MAX || round < -INT_MAX )
219 if ( d->type == QVariant::Double && d->precision > 0 )
221 double s = qPow( 10, d->precision );
223 v =
QVariant(( d < 0 ? ceil( d - 0.5 ) : floor( d + 0.5 ) ) / s );
227 if ( d->type == QVariant::String && d->length > 0 && v.
toString().
length() > d->length )
245 out << static_cast< quint32 >( field.
type() );
257 in >> name >> type >> typeName >> length >> precision >>
comment;
259 field.
setType( static_cast< QVariant::Type >( type ) );
261 field.
setLength( static_cast< int >( length ) );
278 d =
new QgsFieldsPrivate();
300 d->nameToIndex.clear();
311 if ( d->nameToIndex.contains( field.
name() ) )
315 originIndex = d->fields.count();
316 d->fields.append(
Field( field, origin, originIndex ) );
318 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
324 if ( d->nameToIndex.contains( field.
name() ) )
329 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
335 if ( !
exists( fieldIdx ) )
338 d->fields.remove( fieldIdx );
339 d->nameToIndex.clear();
340 for (
int idx = 0; idx <
count(); ++idx )
342 d->nameToIndex.insert( d->fields.at( idx ).field.name(), idx );
348 for (
int i = 0; i < other.
count(); ++i )
362 return d->fields.isEmpty();
367 return d->fields.count();
372 return d->fields.count();
377 return i >= 0 && i < d->fields.count();
382 return d->fields[i].field;
387 return d->fields[i].field;
392 return d->fields[fieldIdx].field;
408 return d->fields[i].field;
413 if ( !
exists( fieldIdx ) )
416 return d->fields[fieldIdx].origin;
421 return d->fields[fieldIdx].originIndex;
426 return d->nameToIndex.value( name, -1 );
432 for (
int i = 0; i < d->fields.count(); ++i )
433 lst.
append( d->fields[i].field );
439 return d->fields == other.d->fields;
444 static QIcon intIcon;
447 static QIcon floatIcon;
450 static QIcon stringIcon;
451 if ( stringIcon.
isNull() )
453 static QIcon dateIcon;
456 static QIcon dateTimeIcon;
457 if ( dateTimeIcon.
isNull() )
459 static QIcon timeIcon;
463 switch ( d->fields.at( fieldIdx ).field.type() )
467 case QVariant::LongLong:
468 case QVariant::ULongLong:
472 case QVariant::Double:
476 case QVariant::String:
484 case QVariant::DateTime:
505 for (
int idx = 0; idx <
count(); ++idx )
507 if ( d->fields[idx].field.name() == fieldName )
511 for (
int idx = 0; idx <
count(); ++idx )
513 if (
QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
523 for (
int i = 0; i < d->fields.count(); ++i )
536 out << static_cast< quint32 >( fields.
size() );
537 for (
int i = 0; i < fields.
size(); i++ )
539 out << fields.
field( i );
549 for ( quint32 i = 0; i <
size; i++ )
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
bool canConvert(Type t) const
qlonglong toLongLong(bool *ok) const
const QgsField & operator[](int i) const
Get field at particular index (must be in range 0..N-1)
QgsField & operator=(const QgsField &other)
Assignment operator.
bool operator==(const QgsField &other) const
const QgsField & field(int fieldIdx) const
Get field at particular index (must be in range 0..N-1)
QIcon iconForField(int fieldIdx) const
Returns an icon corresponding to a field index, based on the field's type and source.
struct QgsFields::Field Field
QgsFields()
Constructor for an empty field container.
virtual ~QgsField()
Destructor.
QgsFields & operator=(const QgsFields &other)
Assignment operator.
QString displayString(const QVariant &v) const
Formats string for display.
void setPrecision(int precision)
Set the field precision.
int fieldNameIndex(const QString &fieldName) const
Look up field's index from name also looks up case-insensitive if there is no match otherwise...
static QIcon getThemeIcon(const QString &theName)
Helper to get a theme icon.
QgsField(const QString &name=QString(), QVariant::Type type=QVariant::Invalid, const QString &typeName=QString(), int len=0, int prec=0, const QString &comment=QString())
Constructor.
int precision() const
Gets the precision of the field.
QDataStream & operator>>(QDataStream &in, QgsField &field)
Reads a field from stream in into field.
Container of fields for a vector layer.
void setName(const QString &name)
Set the field name.
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.
QString comment() const
Returns the field comment.
field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
it has not been specified where the field comes from
void setLength(int len)
Set the field length.
double qgsRound(double x)
bool exists(int i) const
Return if a field index is valid.
QString number(int n, int base)
void append(const T &value)
QString typeName() const
Gets the field type.
int toInt(bool *ok) const
void clear()
Remove all fields.
void setTypeName(const QString &typeName)
Set the field type.
QString name() const
Gets the name of the field.
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)
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.
const QgsField & at(int i) const
Get field at particular index (must be in range 0..N-1)
void remove(int fieldIdx)
Remove a field with the given index.
int indexFromName(const QString &name) const
Look up field's index from name. Returns -1 on error.
QVariant value(const QString &key, const QVariant &defaultValue) const
void setType(QVariant::Type type)
Set variant type.
bool convertCompatible(QVariant &v) const
Converts the provided variant to a compatible format.
int length() const
Gets the length of the field.
int size() const
Return number of items.
FieldOrigin fieldOrigin(int fieldIdx) const
Get field's origin (value from an enumeration)
QString left(int n) const
double toDouble(bool *ok) const
bool isEmpty() const
Check whether the container is empty.
int compare(const QString &other) const
field is calculated from an expression
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
void setComment(const QString &comment)
Set the field comment.
QDataStream & operator<<(QDataStream &out, const QgsField &field)
Writes the field to stream out.
bool operator==(const QgsFields &other) const
QVariant::Type type() const
Gets variant type of the field as it will be retrieved from data source.