QGIS API Documentation 4.3.0-Master (91d4a333d5d)
Loading...
Searching...
No Matches
qgsfields.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfields.cpp - QgsFields
3
4 ---------------------
5 begin : 22.9.2016
6 copyright : (C) 2016 by Matthias Kuhn
7 email : matthias@opengis.ch
8 ***************************************************************************
9 * *
10 * This program is free software; you can redistribute it and/or modify *
11 * it under the terms of the GNU General Public License as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
16
17#include "qgsfields.h"
18
19#include "qgsapplication.h"
20#include "qgsfields_p.h"
21#include "qgsvariantutils.h"
22
23#include <QIcon>
24#include <QString>
25
26#include "moc_qgsfields.cpp"
27
28using namespace Qt::StringLiterals;
29
30/***************************************************************************
31 * This class is considered CRITICAL and any change MUST be accompanied with
32 * full unit tests in testqgsfields.cpp.
33 * See details in QEP #17
34 ****************************************************************************/
35
37{
38 d = new QgsFieldsPrivate();
39}
40
41QgsFields::QgsFields( const QgsFields &other ) //NOLINT
42 : d( other.d )
43{}
44
45QgsFields &QgsFields::operator=( const QgsFields &other ) //NOLINT
46{
47 d = other.d;
48 return *this;
49}
50
51QgsFields::QgsFields( const QList<QgsField> &fields )
52{
53 d = new QgsFieldsPrivate();
54 for ( const QgsField &field : fields )
55 {
56 append( field );
57 }
58}
59
61{}
62
64{
65 d->fields.clear();
66 d->nameToIndex.clear();
67}
68
69/***************************************************************************
70 * This class is considered CRITICAL and any change MUST be accompanied with
71 * full unit tests in testqgsfields.cpp.
72 * See details in QEP #17
73 ****************************************************************************/
74
75bool QgsFields::append( const QgsField &field, Qgis::FieldOrigin origin, int originIndex )
76{
77 if ( d->nameToIndex.contains( field.name() ) )
78 return false;
79
80 if ( originIndex == -1 && origin == Qgis::FieldOrigin::Provider )
81 originIndex = d->fields.count();
82 d->fields.append( Field( field, origin, originIndex ) );
83
84 d->nameToIndex.insert( field.name(), d->fields.count() - 1 );
85 return true;
86}
87
88bool QgsFields::append( const QList<QgsField> &fields, Qgis::FieldOrigin origin )
89{
90 for ( const QgsField &field : fields )
91 {
92 if ( d->nameToIndex.contains( field.name() ) )
93 return false;
94 }
95
96 for ( const QgsField &field : fields )
97 {
98 append( field, origin );
99 }
100 return true;
101}
102
103bool QgsFields::append( const QgsFields &fields )
104{
105 for ( const QgsField &field : fields )
106 {
107 if ( d->nameToIndex.contains( field.name() ) )
108 return false;
109 }
110
111 for ( int i = 0; i < fields.size(); ++i )
112 {
113 append( fields.at( i ), fields.fieldOrigin( i ), fields.fieldOriginIndex( i ) );
114 }
115 return true;
116}
117
118bool QgsFields::rename( int fieldIdx, const QString &name )
119{
120 if ( !exists( fieldIdx ) )
121 return false;
122
123 if ( name.isEmpty() )
124 return false;
125
126 if ( d->nameToIndex.contains( name ) )
127 return false;
128
129 const QString oldName = d->fields[fieldIdx].field.name();
130 d->fields[fieldIdx].field.setName( name );
131 d->nameToIndex.remove( oldName );
132 d->nameToIndex.insert( name, fieldIdx );
133 return true;
134}
135
136bool QgsFields::appendExpressionField( const QgsField &field, int originIndex )
137{
138 if ( d->nameToIndex.contains( field.name() ) )
139 return false;
140
141 d->fields.append( Field( field, Qgis::FieldOrigin::Expression, originIndex ) );
142
143 d->nameToIndex.insert( field.name(), d->fields.count() - 1 );
144 return true;
145}
146
147void QgsFields::remove( int fieldIdx )
148{
149 if ( !exists( fieldIdx ) )
150 return;
151
152 d->fields.remove( fieldIdx );
153 d->nameToIndex.clear();
154 for ( int idx = 0; idx < count(); ++idx )
155 {
156 d->nameToIndex.insert( d->fields.at( idx ).field.name(), idx );
157 }
158}
159
160void QgsFields::extend( const QgsFields &other )
161{
162 for ( int i = 0; i < other.count(); ++i )
163 {
164 append( other.at( i ), other.fieldOrigin( i ), other.fieldOriginIndex( i ) );
165 }
166}
167
168/***************************************************************************
169 * This class is considered CRITICAL and any change MUST be accompanied with
170 * full unit tests in testqgsfields.cpp.
171 * See details in QEP #17
172 ****************************************************************************/
173
175{
176 return d->fields.isEmpty();
177}
178
180{
181 return d->fields.count();
182}
183
185{
186 return d->fields.count();
187}
188
189QStringList QgsFields::names() const
190{
191 QStringList lst;
192 for ( int i = 0; i < d->fields.count(); ++i )
193 {
194 lst.append( d->fields[i].field.name() );
195 }
196 return lst;
197}
198
199bool QgsFields::exists( int i ) const
200{
201 return i >= 0 && i < d->fields.count();
202}
203
205{
206 return d->fields[i].field;
207}
208
210{
211 return d->fields[i].field;
212}
213
214QgsField QgsFields::field( int fieldIdx ) const
215{
216 return d->fields[fieldIdx].field;
217}
218
219QgsField QgsFields::field( const QString &name ) const
220{
221 return d->fields[indexFromName( name )].field;
222}
223
224/***************************************************************************
225 * This class is considered CRITICAL and any change MUST be accompanied with
226 * full unit tests in testqgsfields.cpp.
227 * See details in QEP #17
228 ****************************************************************************/
229
231{
232 return d->fields[i].field;
233}
234
236{
237 if ( !exists( fieldIdx ) )
239
240 return d->fields[fieldIdx].origin;
241}
242
243int QgsFields::fieldOriginIndex( int fieldIdx ) const
244{
245 if ( !exists( fieldIdx ) )
246 return -1;
247
248 return d->fields[fieldIdx].originIndex;
249}
250
251int QgsFields::indexFromName( const QString &fieldName ) const
252{
253 return d->nameToIndex.value( fieldName, -1 );
254}
255
256int QgsFields::indexOf( const QString &fieldName ) const
257{
258 return d->nameToIndex.value( fieldName, -1 );
259}
260
261QList<QgsField> QgsFields::toList() const
262{
263 QList<QgsField> lst;
264 for ( int i = 0; i < d->fields.count(); ++i )
265 lst.append( d->fields[i].field );
266 return lst;
267}
268
269bool QgsFields::operator==( const QgsFields &other ) const
270{
271 return d->fields == other.d->fields;
272}
273
274QgsFields::const_iterator QgsFields::constBegin() const noexcept
275{
276 if ( d->fields.isEmpty() )
277 return const_iterator();
278
279 return const_iterator( &d->fields.first() );
280}
281
282QgsFields::const_iterator QgsFields::constEnd() const noexcept
283{
284 if ( d->fields.isEmpty() )
285 return const_iterator();
286
287 return const_iterator( &d->fields.last() + 1 );
288}
289
290QgsFields::const_iterator QgsFields::begin() const noexcept
291{
292 if ( d->fields.isEmpty() )
293 return const_iterator();
294
295 return const_iterator( &d->fields.first() );
296}
297
298QgsFields::const_iterator QgsFields::end() const noexcept
299{
300 if ( d->fields.isEmpty() )
301 return const_iterator();
302
303 return const_iterator( &d->fields.last() + 1 );
304}
305
306QgsFields::iterator QgsFields::begin()
307{
308 if ( d->fields.isEmpty() )
309 return iterator();
310
311 d.detach();
312 return iterator( &d->fields.first() );
313}
314
315QgsFields::iterator QgsFields::end()
316{
317 if ( d->fields.isEmpty() )
318 return iterator();
319
320 d.detach();
321 return iterator( &d->fields.last() + 1 );
322}
323
324QIcon QgsFields::iconForField( int fieldIdx, bool considerOrigin ) const
325{
326 if ( !exists( fieldIdx ) )
327 return {};
328
329 if ( considerOrigin )
330 {
331 switch ( fieldOrigin( fieldIdx ) )
332 {
334 return QgsApplication::getThemeIcon( u"/mIconExpression.svg"_s );
335
337 return QgsApplication::getThemeIcon( u"/propertyicons/join.svg"_s );
338
339 default:
340 return iconForFieldType( d->fields.at( fieldIdx ).field.type(), d->fields.at( fieldIdx ).field.subType(), d->fields.at( fieldIdx ).field.typeName() );
341 }
342 }
343 return iconForFieldType( d->fields.at( fieldIdx ).field.type(), d->fields.at( fieldIdx ).field.subType(), d->fields.at( fieldIdx ).field.typeName() );
344}
345
346QIcon QgsFields::iconForFieldType( QMetaType::Type type, QMetaType::Type subType, const QString &typeString )
347{
348 switch ( type )
349 {
350 case QMetaType::Type::Bool:
351 return QgsApplication::getThemeIcon( u"/mIconFieldBool.svg"_s );
352 case QMetaType::Type::Int:
353 case QMetaType::Type::UInt:
354 case QMetaType::Type::LongLong:
355 case QMetaType::Type::ULongLong:
356 return QgsApplication::getThemeIcon( u"/mIconFieldInteger.svg"_s );
357 case QMetaType::Type::Double:
358 return QgsApplication::getThemeIcon( u"/mIconFieldFloat.svg"_s );
359 case QMetaType::Type::QString:
360 return QgsApplication::getThemeIcon( u"/mIconFieldText.svg"_s );
361 case QMetaType::Type::QDate:
362 return QgsApplication::getThemeIcon( u"/mIconFieldDate.svg"_s );
363 case QMetaType::Type::QDateTime:
364 return QgsApplication::getThemeIcon( u"/mIconFieldDateTime.svg"_s );
365 case QMetaType::Type::QTime:
366 return QgsApplication::getThemeIcon( u"/mIconFieldTime.svg"_s );
367 case QMetaType::Type::QByteArray:
368 return QgsApplication::getThemeIcon( u"/mIconFieldBinary.svg"_s );
369 case QMetaType::Type::QVariantList:
370 {
371 switch ( subType )
372 {
373 case QMetaType::Type::Int:
374 case QMetaType::Type::UInt:
375 case QMetaType::Type::LongLong:
376 case QMetaType::Type::ULongLong:
377 return QgsApplication::getThemeIcon( u"/mIconFieldArrayInteger.svg"_s );
378 case QMetaType::Type::Double:
379 return QgsApplication::getThemeIcon( u"/mIconFieldArrayFloat.svg"_s );
380 case QMetaType::Type::QString:
381 return QgsApplication::getThemeIcon( u"/mIconFieldArrayString.svg"_s );
382 default:
383 return QgsApplication::getThemeIcon( u"/mIconFieldArray.svg"_s );
384 }
385 }
386 case QMetaType::Type::QStringList:
387 return QgsApplication::getThemeIcon( u"/mIconFieldArrayString.svg"_s );
388 case QMetaType::Type::QVariantMap:
389 return QgsApplication::getThemeIcon( u"/mIconFieldJson.svg"_s );
390 case QMetaType::Type::User:
391 if ( typeString.compare( "geometry"_L1 ) == 0 )
392 {
393 return QgsApplication::getThemeIcon( u"/mIconFieldGeometry.svg"_s );
394 }
395 else
396 {
397 return QIcon();
398 }
399
400 default:
401 return QIcon();
402 }
403}
404
405QIcon QgsFields::iconForFieldType( QVariant::Type type, QVariant::Type subType, const QString &typeString )
406{
408}
409
410/***************************************************************************
411 * This class is considered CRITICAL and any change MUST be accompanied with
412 * full unit tests in testqgsfields.cpp.
413 * See details in QEP #17
414 ****************************************************************************/
415
416int QgsFields::lookupField( const QString &fieldName ) const
417{
418 for ( int idx = 0; idx < count(); ++idx )
419 {
420 if ( d->fields[idx].field.name() == fieldName )
421 return idx;
422 }
423
424 if ( fieldName.isEmpty() )
425 return -1;
426
427 for ( int idx = 0; idx < count(); ++idx )
428 {
429 if ( QString::compare( d->fields[idx].field.name(), fieldName, Qt::CaseInsensitive ) == 0 )
430 return idx;
431 }
432
433 for ( int idx = 0; idx < count(); ++idx )
434 {
435 const QString alias = d->fields[idx].field.alias();
436 if ( !alias.isEmpty() && QString::compare( alias, fieldName, Qt::CaseInsensitive ) == 0 )
437 return idx;
438 }
439
440 return -1;
441}
442
444{
445 const int count = d->fields.count();
447 lst.reserve( count );
448 for ( int i = 0; i < count; ++i )
449 lst.append( i );
450 return lst;
451}
452
453/***************************************************************************
454 * This class is considered CRITICAL and any change MUST be accompanied with
455 * full unit tests in testqgsfields.cpp.
456 * See details in QEP #17
457 ****************************************************************************/
458
459QDataStream &operator<<( QDataStream &out, const QgsFields &fields )
460{
461 out << static_cast< quint32 >( fields.size() );
462 for ( int i = 0; i < fields.size(); i++ )
463 {
464 out << fields.field( i );
465 }
466 return out;
467}
468
469QDataStream &operator>>( QDataStream &in, QgsFields &fields )
470{
471 fields.clear();
472 quint32 size;
473 in >> size;
474 for ( quint32 i = 0; i < size; i++ )
475 {
476 QgsField field;
477 in >> field;
478 fields.append( field );
479 }
480 return in;
481}
FieldOrigin
Field origin.
Definition qgis.h:1874
@ Provider
Field originates from the underlying data provider of the vector layer.
Definition qgis.h:1876
@ Unknown
The field origin has not been specified.
Definition qgis.h:1875
@ Expression
Field is calculated from an expression.
Definition qgis.h:1879
@ Join
Field originates from a joined layer.
Definition qgis.h:1877
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.
Definition qgsfield.h:56
Container of fields for a vector layer.
Definition qgsfields.h:45
bool append(const QgsField &field, Qgis::FieldOrigin origin=Qgis::FieldOrigin::Provider, int originIndex=-1)
Appends a field.
Definition qgsfields.cpp:75
void extend(const QgsFields &other)
Extends with fields from another QgsFields container.
int count
Definition qgsfields.h:49
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 isEmpty
Definition qgsfields.h:48
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)
Definition qgsfields.cpp:45
QgsField operator[](int i) const
Gets field at particular index (must be in range 0..N-1).
QgsFields()
Constructor for an empty field container.
Definition qgsfields.cpp:36
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.
Definition qgsfields.cpp:63
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) or -1 if no field e...
Q_INVOKABLE int lookupField(const QString &fieldName) const
Looks up field's index from the field name.
virtual ~QgsFields()
Definition qgsfields.cpp:60
QIcon iconForField(int fieldIdx, bool considerOrigin=false) const
Returns an icon corresponding to a field index, based on the field's type and source.
QStringList names
Definition qgsfields.h:50
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
Definition qgsfield.h:30
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.