26#include "moc_qgsfields.cpp"
28using namespace Qt::StringLiterals;
38 d =
new QgsFieldsPrivate();
54 d =
new QgsFieldsPrivate();
67 d->nameToIndex.clear();
78 if ( d->nameToIndex.contains(
field.name() ) )
82 originIndex = d->fields.count();
83 d->fields.append(
Field(
field, origin, originIndex ) );
85 d->nameToIndex.insert(
field.name(), d->fields.count() - 1 );
93 if ( d->nameToIndex.contains(
field.name() ) )
108 if ( d->nameToIndex.contains(
field.name() ) )
112 for (
int i = 0; i < fields.
size(); ++ i )
121 if ( !
exists( fieldIdx ) )
124 if ( name.isEmpty() )
127 if ( d->nameToIndex.contains( name ) )
130 const QString oldName = d->fields[ fieldIdx ].field.name();
131 d->fields[ fieldIdx ].field.setName( name );
132 d->nameToIndex.remove( oldName );
133 d->nameToIndex.insert( name, fieldIdx );
139 if ( d->nameToIndex.contains(
field.name() ) )
144 d->nameToIndex.insert(
field.name(), d->fields.count() - 1 );
150 if ( !
exists( fieldIdx ) )
153 d->fields.remove( fieldIdx );
154 d->nameToIndex.clear();
155 for (
int idx = 0; idx <
count(); ++idx )
157 d->nameToIndex.insert( d->fields.at( idx ).field.name(), idx );
163 for (
int i = 0; i < other.
count(); ++i )
177 return d->fields.isEmpty();
182 return d->fields.count();
187 return d->fields.count();
193 for (
int i = 0; i < d->fields.count(); ++i )
195 lst.append( d->fields[i].field.name() );
202 return i >= 0 && i < d->fields.count();
207 return d->fields[i].field;
212 return d->fields[i].field;
217 return d->fields[fieldIdx].field;
233 return d->fields[i].field;
238 if ( !
exists( fieldIdx ) )
241 return d->fields[fieldIdx].origin;
246 return d->fields[fieldIdx].originIndex;
251 return d->nameToIndex.value( fieldName, -1 );
256 return d->nameToIndex.value( fieldName, -1 );
262 for (
int i = 0; i < d->fields.count(); ++i )
263 lst.append( d->fields[i].field );
269 return d->fields == other.d->fields;
274 if ( d->fields.isEmpty() )
275 return const_iterator();
277 return const_iterator( &d->fields.first() );
282 if ( d->fields.isEmpty() )
283 return const_iterator();
285 return const_iterator( &d->fields.last() + 1 );
290 if ( d->fields.isEmpty() )
291 return const_iterator();
293 return const_iterator( &d->fields.first() );
298 if ( d->fields.isEmpty() )
299 return const_iterator();
301 return const_iterator( &d->fields.last() + 1 );
306 if ( d->fields.isEmpty() )
310 return iterator( &d->fields.first() );
315 if ( d->fields.isEmpty() )
319 return iterator( &d->fields.last() + 1 );
324 if ( considerOrigin )
335 return iconForFieldType( d->fields.at( fieldIdx ).field.type(), d->fields.at( fieldIdx ).field.subType(), d->fields.at( fieldIdx ).field.typeName() );
338 return iconForFieldType( d->fields.at( fieldIdx ).field.type(), d->fields.at( fieldIdx ).field.subType(), d->fields.at( fieldIdx ).field.typeName() );
345 case QMetaType::Type::Bool:
347 case QMetaType::Type::Int:
348 case QMetaType::Type::UInt:
349 case QMetaType::Type::LongLong:
350 case QMetaType::Type::ULongLong:
352 case QMetaType::Type::Double:
354 case QMetaType::Type::QString:
356 case QMetaType::Type::QDate:
358 case QMetaType::Type::QDateTime:
360 case QMetaType::Type::QTime:
362 case QMetaType::Type::QByteArray:
364 case QMetaType::Type::QVariantList:
368 case QMetaType::Type::Int:
369 case QMetaType::Type::UInt:
370 case QMetaType::Type::LongLong:
371 case QMetaType::Type::ULongLong:
373 case QMetaType::Type::Double:
375 case QMetaType::Type::QString:
381 case QMetaType::Type::QStringList:
383 case QMetaType::Type::QVariantMap:
385 case QMetaType::Type::User:
386 if ( typeString.compare(
"geometry"_L1 ) == 0 )
413 for (
int idx = 0; idx <
count(); ++idx )
415 if ( d->fields[idx].field.name() == fieldName )
419 if ( fieldName.isEmpty() )
422 for (
int idx = 0; idx <
count(); ++idx )
424 if ( QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
428 for (
int idx = 0; idx <
count(); ++idx )
430 const QString alias = d->fields[idx].field.alias();
431 if ( !alias.isEmpty() && QString::compare( alias, fieldName, Qt::CaseInsensitive ) == 0 )
440 const int count = d->fields.count();
442 lst.reserve(
count );
443 for (
int i = 0; i <
count; ++i )
456 out << static_cast< quint32 >( fields.
size() );
457 for (
int i = 0; i < fields.
size(); i++ )
459 out << fields.
field( i );
469 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.
struct QgsFields::Field Field
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.