18#include "moc_qgsfields.cpp"
32 d =
new QgsFieldsPrivate();
48 d =
new QgsFieldsPrivate();
61 d->nameToIndex.clear();
72 if ( d->nameToIndex.contains(
field.
name() ) )
76 originIndex = d->fields.count();
77 d->fields.append(
Field(
field, origin, originIndex ) );
79 d->nameToIndex.insert(
field.
name(), d->fields.count() - 1 );
87 if ( d->nameToIndex.contains(
field.
name() ) )
102 if ( d->nameToIndex.contains(
field.
name() ) )
106 for (
int i = 0; i < fields.
size(); ++ i )
115 if ( !
exists( fieldIdx ) )
118 if ( name.isEmpty() )
121 if ( d->nameToIndex.contains( name ) )
124 const QString oldName = d->fields[ fieldIdx ].field.name();
125 d->fields[ fieldIdx ].field.setName( name );
126 d->nameToIndex.remove( oldName );
127 d->nameToIndex.insert( name, fieldIdx );
133 if ( d->nameToIndex.contains(
field.
name() ) )
138 d->nameToIndex.insert(
field.
name(), d->fields.count() - 1 );
144 if ( !
exists( fieldIdx ) )
147 d->fields.remove( fieldIdx );
148 d->nameToIndex.clear();
149 for (
int idx = 0; idx <
count(); ++idx )
151 d->nameToIndex.insert( d->fields.at( idx ).field.name(), idx );
157 for (
int i = 0; i < other.
count(); ++i )
171 return d->fields.isEmpty();
176 return d->fields.count();
181 return d->fields.count();
187 for (
int i = 0; i < d->fields.count(); ++i )
189 lst.append( d->fields[i].field.name() );
196 return i >= 0 && i < d->fields.count();
201 return d->fields[i].field;
206 return d->fields[i].field;
211 return d->fields[fieldIdx].field;
227 return d->fields[i].field;
232 if ( !
exists( fieldIdx ) )
235 return d->fields[fieldIdx].origin;
240 return d->fields[fieldIdx].originIndex;
245 return d->nameToIndex.value( fieldName, -1 );
250 return d->nameToIndex.value( fieldName, -1 );
256 for (
int i = 0; i < d->fields.count(); ++i )
257 lst.append( d->fields[i].field );
263 return d->fields == other.d->fields;
268 if ( d->fields.isEmpty() )
269 return const_iterator();
271 return const_iterator( &d->fields.first() );
276 if ( d->fields.isEmpty() )
277 return const_iterator();
279 return const_iterator( &d->fields.last() + 1 );
284 if ( d->fields.isEmpty() )
285 return const_iterator();
287 return const_iterator( &d->fields.first() );
292 if ( d->fields.isEmpty() )
293 return const_iterator();
295 return const_iterator( &d->fields.last() + 1 );
300 if ( d->fields.isEmpty() )
304 return iterator( &d->fields.first() );
309 if ( d->fields.isEmpty() )
313 return iterator( &d->fields.last() + 1 );
318 if ( considerOrigin )
329 return iconForFieldType( d->fields.at( fieldIdx ).field.type(), d->fields.at( fieldIdx ).field.subType(), d->fields.at( fieldIdx ).field.typeName() );
332 return iconForFieldType( d->fields.at( fieldIdx ).field.type(), d->fields.at( fieldIdx ).field.subType(), d->fields.at( fieldIdx ).field.typeName() );
339 case QMetaType::Type::Bool:
341 case QMetaType::Type::Int:
342 case QMetaType::Type::UInt:
343 case QMetaType::Type::LongLong:
344 case QMetaType::Type::ULongLong:
346 case QMetaType::Type::Double:
348 case QMetaType::Type::QString:
350 case QMetaType::Type::QDate:
352 case QMetaType::Type::QDateTime:
354 case QMetaType::Type::QTime:
356 case QMetaType::Type::QByteArray:
358 case QMetaType::Type::QVariantList:
362 case QMetaType::Type::Int:
363 case QMetaType::Type::UInt:
364 case QMetaType::Type::LongLong:
365 case QMetaType::Type::ULongLong:
367 case QMetaType::Type::Double:
369 case QMetaType::Type::QString:
375 case QMetaType::Type::QStringList:
377 case QMetaType::Type::QVariantMap:
379 case QMetaType::Type::User:
380 if ( typeString.compare( QLatin1String(
"geometry" ) ) == 0 )
407 for (
int idx = 0; idx <
count(); ++idx )
409 if ( d->fields[idx].field.name() == fieldName )
413 if ( fieldName.isEmpty() )
416 for (
int idx = 0; idx <
count(); ++idx )
418 if ( QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
422 for (
int idx = 0; idx <
count(); ++idx )
424 const QString alias = d->fields[idx].field.alias();
425 if ( !alias.isEmpty() && QString::compare( alias, fieldName, Qt::CaseInsensitive ) == 0 )
434 const int count = d->fields.count();
436 lst.reserve(
count );
437 for (
int i = 0; i <
count; ++i )
450 out << static_cast< quint32 >( fields.
size() );
451 for (
int i = 0; i < fields.
size(); i++ )
453 out << fields.
field( i );
463 for ( quint32 i = 0; i < size; i++ )
@ Provider
Field originates from the underlying data provider of the vector layer.
@ Unknown
The field origin has not been specified.
@ Expression
Field is calculated from an expression.
@ Join
Field originates from a joined layer.
static QIcon getThemeIcon(const QString &name, const QColor &fillColor=QColor(), const QColor &strokeColor=QColor())
Helper to get a theme icon.
Encapsulate a field in an attribute table or data source.
Container of fields for a vector layer.
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
void extend(const QgsFields &other)
Extends with fields from another QgsFields container.
const_iterator constEnd() const noexcept
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
bool operator==(const QgsFields &other) const
bool appendExpressionField(const QgsField &field, int originIndex)
Appends an expression field. The field must have unique name, otherwise it is rejected (returns false...
Q_INVOKABLE int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
Q_INVOKABLE int indexOf(const QString &fieldName) const
Gets the field index from the field name.
void remove(int fieldIdx)
Removes the field with the given index.
const_iterator begin() const noexcept
Returns a const STL-style iterator pointing to the first item in the list.
static QIcon iconForFieldType(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType, const QString &typeString=QString())
Returns an icon corresponding to a field type.
QgsField field(int fieldIdx) const
Returns the field at particular index (must be in range 0..N-1).
QgsFields & operator=(const QgsFields &other)
QgsField operator[](int i) const
Gets field at particular index (must be in range 0..N-1)
QgsFields()
Constructor for an empty field container.
Qgis::FieldOrigin fieldOrigin(int fieldIdx) const
Returns the field's origin (value from an enumeration).
Q_INVOKABLE bool exists(int i) const
Returns if a field index is valid.
int size() const
Returns number of items.
void clear()
Removes all fields.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
int fieldOriginIndex(int fieldIdx) const
Returns the field's origin index (its meaning is specific to each type of origin).
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
QIcon iconForField(int fieldIdx, bool considerOrigin=false) const
Returns an icon corresponding to a field index, based on the field's type and source.
const_iterator end() const noexcept
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list.
bool rename(int fieldIdx, const QString &name)
Renames a name of field.
const_iterator constBegin() const noexcept
Returns a const STL-style iterator pointing to the first item in the list.
static QMetaType::Type variantTypeToMetaType(QVariant::Type variantType)
Converts a QVariant::Type to a QMetaType::Type.
QList< int > QgsAttributeList
QDataStream & operator>>(QDataStream &in, QgsFields &fields)
Reads fields from stream in into fields. QGIS version compatibility is not guaranteed.
QDataStream & operator<<(QDataStream &out, const QgsFields &fields)
Writes the fields to stream out. QGIS version compatibility is not guaranteed.