30 d =
new QgsFieldsPrivate();
50 d->nameToIndex.clear();
61 if ( d->nameToIndex.contains( field.
name() ) )
65 originIndex = d->fields.count();
66 d->fields.append(
Field( field, origin, originIndex ) );
68 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
80 if ( d->nameToIndex.contains( name ) )
83 const QString oldName = d->fields[ fieldIdx ].field.name();
84 d->fields[ fieldIdx ].field.setName( name );
85 d->nameToIndex.remove( oldName );
86 d->nameToIndex.insert( name, fieldIdx );
92 if ( d->nameToIndex.contains( field.
name() ) )
97 d->nameToIndex.insert( field.
name(), d->fields.count() - 1 );
103 if ( !
exists( fieldIdx ) )
106 d->fields.remove( fieldIdx );
107 d->nameToIndex.clear();
108 for (
int idx = 0; idx <
count(); ++idx )
110 d->nameToIndex.insert( d->fields.at( idx ).field.name(), idx );
116 for (
int i = 0; i < other.
count(); ++i )
130 return d->fields.isEmpty();
135 return d->fields.count();
140 return d->fields.count();
146 for (
int i = 0; i < d->fields.count(); ++i )
148 lst.append( d->fields[i].field.name() );
155 return i >= 0 && i < d->fields.count();
160 return d->fields[i].field;
165 return d->fields[i].field;
170 return d->fields[fieldIdx].field;
186 return d->fields[i].field;
191 if ( !
exists( fieldIdx ) )
194 return d->fields[fieldIdx].origin;
199 return d->fields[fieldIdx].originIndex;
204 return d->nameToIndex.value( fieldName, -1 );
209 return d->nameToIndex.value( fieldName, -1 );
215 for (
int i = 0; i < d->fields.count(); ++i )
216 lst.append( d->fields[i].field );
222 return d->fields == other.d->fields;
227 if ( d->fields.isEmpty() )
228 return const_iterator();
230 return const_iterator( &d->fields.first() );
235 if ( d->fields.isEmpty() )
236 return const_iterator();
238 return const_iterator( &d->fields.last() + 1 );
243 if ( d->fields.isEmpty() )
244 return const_iterator();
246 return const_iterator( &d->fields.first() );
251 if ( d->fields.isEmpty() )
252 return const_iterator();
254 return const_iterator( &d->fields.last() + 1 );
259 if ( d->fields.isEmpty() )
263 return iterator( &d->fields.first() );
268 if ( d->fields.isEmpty() )
272 return iterator( &d->fields.last() + 1 );
277 switch ( d->fields.at( fieldIdx ).field.type() )
284 case QVariant::LongLong:
285 case QVariant::ULongLong:
289 case QVariant::Double:
293 case QVariant::String:
301 case QVariant::DateTime:
322 if ( fieldName.isEmpty() )
325 for (
int idx = 0; idx <
count(); ++idx )
327 if ( d->fields[idx].field.name() == fieldName )
331 for (
int idx = 0; idx <
count(); ++idx )
333 if ( QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
337 for (
int idx = 0; idx <
count(); ++idx )
339 QString alias = d->fields[idx].field.alias();
340 if ( !alias.isEmpty() && QString::compare( alias, fieldName, Qt::CaseInsensitive ) == 0 )
350 for (
int i = 0; i < d->fields.count(); ++i )
363 out << static_cast< quint32 >( fields.
size() );
364 for (
int i = 0; i < fields.
size(); i++ )
366 out << fields.
field( i );
376 for ( quint32 i = 0; i <
size; i++ )
QList< QgsField > toList() const
Utility function to return a list of QgsField instances.
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.
QgsFields & operator=(const QgsFields &other)
Assignment operator.
const_iterator constEnd() const noexcept
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Container of fields for a vector layer.
static QIcon getThemeIcon(const QString &name)
Helper to get a theme icon.
bool appendExpressionField(const QgsField &field, int originIndex)
Appends an expression field. The field must have unique name, otherwise it is rejected (returns false...
void extend(const QgsFields &other)
Extends with fields from another QgsFields container.
Field comes from the underlying data provider of the vector layer (originIndex = index in provider's ...
bool rename(int fieldIdx, const QString &name)
Renames a name of field.
QDataStream & operator<<(QDataStream &out, const QgsFields &fields)
Writes the fields to stream out. QGIS version compatibility is not guaranteed.
QgsField at(int i) const
Gets field at particular index (must be in range 0..N-1)
QStringList names() const
Returns a list with field names.
const_iterator end() const noexcept
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
It has not been specified where the field comes from.
bool exists(int i) const
Returns if a field index is valid.
void clear()
Removes all fields.
int fieldOriginIndex(int fieldIdx) const
Gets field's origin index (its meaning is specific to each type of origin)
bool append(const QgsField &field, FieldOrigin origin=OriginProvider, int originIndex=-1)
Appends a field. The field must have unique name, otherwise it is rejected (returns false) ...
int count() const
Returns number of items.
Encapsulate a field in an attribute table or data source.
int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
int indexOf(const QString &fieldName) const
Gets the field index from the field name.
QgsField field(int fieldIdx) const
Gets field at particular index (must be in range 0..N-1)
void remove(int fieldIdx)
Removes a field with the given index.
int indexFromName(const QString &fieldName) const
Gets the field index from the field name.
const_iterator begin() const noexcept
Returns a const STL-style iterator pointing to the first item in the list.
QDataStream & operator>>(QDataStream &in, QgsFields &fields)
Reads fields from stream in into fields. QGIS version compatibility is not guaranteed.
const_iterator constBegin() const noexcept
Returns a const STL-style iterator pointing to the first item in the list.
int size() const
Returns number of items.
FieldOrigin fieldOrigin(int fieldIdx) const
Gets field's origin (value from an enumeration)
QList< int > QgsAttributeList
bool isEmpty() const
Checks whether the container is empty.
Field is calculated from an expression.
QgsAttributeList allAttributesList() const
Utility function to get list of attribute indexes.
bool operator==(const QgsFields &other) const
QgsField operator[](int i) const
Gets field at particular index (must be in range 0..N-1)