QGIS API Documentation 3.99.0-Master (e9821da5c6b)
Loading...
Searching...
No Matches
qgsfieldmappingmodel.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsfieldmappingmodel.cpp - QgsFieldMappingModel
3
4 ---------------------
5 begin : 17.3.2020
6 copyright : (C) 2020 by Alessandro Pasotti
7 email : elpaso at itopen dot it
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
18
21#include "qgsvariantutils.h"
22
23#include <QString>
24
25#include "moc_qgsfieldmappingmodel.cpp"
26
27using namespace Qt::StringLiterals;
28
29QgsFieldMappingModel::QgsFieldMappingModel( const QgsFields &sourceFields, const QgsFields &destinationFields, const QMap<QString, QString> &expressions, QObject *parent )
30 : QAbstractTableModel( parent )
31 , mNativeTypes( supportedDataTypes() )
32 , mSourceFields( sourceFields )
33 , mExpressionContextGenerator( new ExpressionContextGenerator( mSourceFields ) )
34{
35 setDestinationFields( destinationFields, expressions );
36}
37
38void QgsFieldMappingModel::setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes )
39{
40 mNativeTypes = nativeTypes.isEmpty() ? supportedDataTypes() : nativeTypes;
41}
42
43QVariant QgsFieldMappingModel::headerData( int section, Qt::Orientation orientation, int role ) const
44{
45 if ( role == Qt::DisplayRole )
46 {
47 switch ( orientation )
48 {
49 case Qt::Horizontal:
50 {
51 switch ( static_cast<ColumnDataIndex>( section ) )
52 {
54 {
55 return tr( "Source Expression" );
56 }
58 {
59 return tr( "Name" );
60 }
62 {
63 return tr( "Type" );
64 }
66 {
67 return tr( "Length" );
68 }
70 {
71 return tr( "Precision" );
72 }
74 {
75 return tr( "Constraints" );
76 }
78 {
79 return tr( "Alias" );
80 }
82 {
83 return tr( "Comment" );
84 }
85 }
86 break;
87 }
88 case Qt::Vertical:
89 {
90 return section;
91 }
92 }
93 }
94 return QVariant();
95}
96
98{
99 return mSourceFields;
100}
101
102int QgsFieldMappingModel::rowCount( const QModelIndex &parent ) const
103{
104 if ( parent.isValid() )
105 return 0;
106 return mMapping.count();
107}
108
109int QgsFieldMappingModel::columnCount( const QModelIndex &parent ) const
110{
111 if ( parent.isValid() )
112 return 0;
113 return 8;
114}
115
116QVariant QgsFieldMappingModel::data( const QModelIndex &index, int role ) const
117{
118 if ( index.isValid() )
119 {
120 const ColumnDataIndex col { static_cast<ColumnDataIndex>( index.column() ) };
121 const Field &f { mMapping.at( index.row() ) };
122
123 const QgsFieldConstraints::Constraints constraints { fieldConstraints( f.field ) };
124
125 switch ( role )
126 {
127 case Qt::DisplayRole:
128 case Qt::EditRole:
129 {
130 switch ( col )
131 {
133 {
134 return f.expression;
135 }
137 {
138 return f.field.name();
139 }
141 {
142 return f.field.typeName();
143 }
145 {
146 return f.field.length();
147 }
149 {
150 return f.field.precision();
151 }
153 {
154 return constraints != 0 ? tr( "Constraints active" ) : QString();
155 }
157 {
158 return f.field.alias();
159 }
161 {
162 return f.field.comment();
163 }
164 }
165 break;
166 }
167 case Qt::ToolTipRole:
168 {
169 if ( col == ColumnDataIndex::DestinationConstraints && constraints != 0 )
170 {
171 QStringList constraintDescription;
172 if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintUnique ) )
173 {
174 constraintDescription.push_back( tr( "Unique" ) );
175 }
176 if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
177 {
178 constraintDescription.push_back( tr( "Not null" ) );
179 }
180 if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintExpression ) )
181 {
182 constraintDescription.push_back( tr( "Expression" ) );
183 }
184 return constraintDescription.join( "<br>"_L1 );
185 }
186 break;
187 }
188 case Qt::BackgroundRole:
189 {
190 if ( constraints != 0 )
191 {
192 return QBrush( QColor( 255, 224, 178 ) );
193 }
194 break;
195 }
196
197 default:
198 break;
199 }
200 }
201 return QVariant();
202}
203
204Qt::ItemFlags QgsFieldMappingModel::flags( const QModelIndex &index ) const
205{
206 if ( index.isValid() && index.column() != static_cast<int>( ColumnDataIndex::DestinationConstraints ) && ( index.column() == static_cast<int>( ColumnDataIndex::SourceExpression ) || destinationEditable() ) )
207 {
208 return Qt::ItemFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
209 }
210 return Qt::ItemFlags();
211}
212
213bool QgsFieldMappingModel::setData( const QModelIndex &index, const QVariant &value, int role )
214{
215 if ( index.isValid() )
216 {
217 if ( role == Qt::EditRole )
218 {
219 Field &f = mMapping[index.row()];
220 switch ( static_cast<ColumnDataIndex>( index.column() ) )
221 {
223 {
224 const QgsExpression exp { value.toString() };
225 f.expression = exp;
226 break;
227 }
229 {
230 f.field.setName( value.toString() );
231 break;
232 }
234 {
235 setFieldTypeFromName( f.field, value.toString() );
236 break;
237 }
239 {
240 bool ok;
241 const int length { value.toInt( &ok ) };
242 if ( ok )
243 f.field.setLength( length );
244 break;
245 }
247 {
248 bool ok;
249 const int precision { value.toInt( &ok ) };
250 if ( ok )
251 f.field.setPrecision( precision );
252 break;
253 }
255 {
256 // Not editable: do nothing
257 break;
258 }
260 {
261 f.field.setAlias( value.toString() );
262 break;
263 }
265 {
266 f.field.setComment( value.toString() );
267 break;
268 }
269 }
270 emit dataChanged( index, index );
271 }
272 return true;
273 }
274 else
275 {
276 return false;
277 }
278}
279
280QgsFieldConstraints::Constraints QgsFieldMappingModel::fieldConstraints( const QgsField &field ) const
281{
283
284 const QgsFieldConstraints fieldConstraints { field.constraints() };
285
286 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintNotNull && fieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintNotNull ) & QgsFieldConstraints::ConstraintStrengthHard )
287 constraints.setFlag( QgsFieldConstraints::ConstraintNotNull );
288
289 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintUnique && fieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintUnique ) & QgsFieldConstraints::ConstraintStrengthHard )
290 constraints.setFlag( QgsFieldConstraints::ConstraintUnique );
291
292 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintExpression && fieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintExpression ) & QgsFieldConstraints::ConstraintStrengthHard )
293 constraints.setFlag( QgsFieldConstraints::ConstraintExpression );
294
295 return constraints;
296}
297
298bool QgsFieldMappingModel::moveUpOrDown( const QModelIndex &index, bool up )
299{
300 if ( !index.isValid() && index.model() == this )
301 return false;
302
303 // Always swap down
304 const int row { up ? index.row() - 1 : index.row() };
305 // Range checking
306 if ( row < 0 || row + 1 >= rowCount( QModelIndex() ) )
307 {
308 return false;
309 }
310 beginMoveRows( QModelIndex(), row, row, QModelIndex(), row + 2 );
311 mMapping.swapItemsAt( row, row + 1 );
312 endMoveRows();
313 return true;
314}
315
316QString QgsFieldMappingModel::findExpressionForDestinationField( const QgsFieldMappingModel::Field &f, QStringList &excludedFieldNames )
317{
318 // Search for fields in the source
319 // 1. match by name
320 for ( const QgsField &sf : std::as_const( mSourceFields ) )
321 {
322 if ( sf.name() == f.field.name() )
323 {
324 excludedFieldNames.push_back( sf.name() );
325 return QgsExpression::quotedColumnRef( sf.name() );
326 }
327 }
328 // 2. match by type
329 for ( const QgsField &sf : std::as_const( mSourceFields ) )
330 {
331 if ( excludedFieldNames.contains( sf.name() ) || sf.type() != f.field.type() )
332 continue;
333 excludedFieldNames.push_back( sf.name() );
334 return QgsExpression::quotedColumnRef( sf.name() );
335 }
336 return QString();
337}
338
340{
341 mSourceFields = sourceFields;
342 if ( mExpressionContextGenerator )
343 mExpressionContextGenerator->setSourceFields( mSourceFields );
344 QStringList usedFields;
345 beginResetModel();
346 for ( const Field &f : std::as_const( mMapping ) )
347 {
348 if ( QgsExpression( f.expression ).isField() )
349 {
350 usedFields.push_back( f.expression.mid( 1, f.expression.length() - 2 ) );
351 }
352 }
353 for ( auto it = mMapping.begin(); it != mMapping.end(); ++it )
354 {
355 if ( it->expression.isEmpty() )
356 {
357 const QString expression { findExpressionForDestinationField( *it, usedFields ) };
358 if ( !expression.isEmpty() )
359 it->expression = expression;
360 }
361 }
362 endResetModel();
363}
364
366{
367 return mExpressionContextGenerator.get();
368}
369
371{
372 mExpressionContextGenerator->setBaseExpressionContextGenerator( generator );
373}
374
375void QgsFieldMappingModel::setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions )
376{
377 beginResetModel();
378 mMapping.clear();
379 // Prepare the model data
380 QStringList usedFields;
381 for ( const QgsField &df : destinationFields )
382 {
383 Field f;
384 f.field = df;
385 f.field.setTypeName( qgsFieldToTypeName( df ) );
386 f.originalName = df.name();
387 if ( expressions.contains( f.field.name() ) )
388 {
389 f.expression = expressions.value( f.field.name() );
390 const QgsExpression exp { f.expression };
391 // if it's source field
392 if ( exp.isField() && mSourceFields.names().contains( qgis::setToList( exp.referencedColumns() ).constFirst() ) )
393 {
394 usedFields.push_back( qgis::setToList( exp.referencedColumns() ).constFirst() );
395 }
396 }
397 else
398 {
399 const QString expression { findExpressionForDestinationField( f, usedFields ) };
400 if ( !expression.isEmpty() )
401 f.expression = expression;
402 }
403 mMapping.push_back( f );
404 }
405 endResetModel();
406}
407
409{
410 return mDestinationEditable;
411}
412
417
418const QMap<QMetaType::Type, QString> QgsFieldMappingModel::dataTypes()
419{
420 static const QMap<QMetaType::Type, QString> sDataTypes {
421 { QMetaType::Type::Int, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Int ) },
422 { QMetaType::Type::LongLong, QgsVariantUtils::typeToDisplayString( QMetaType::Type::LongLong ) },
423 { QMetaType::Type::Double, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ) },
424 { QMetaType::Type::QString, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QString ) },
425 { QMetaType::Type::QDate, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDate ) },
426 { QMetaType::Type::QTime, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QTime ) },
427 { QMetaType::Type::QDateTime, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDateTime ) },
428 { QMetaType::Type::Bool, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Bool ) },
429 { QMetaType::Type::QByteArray, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QByteArray ) },
430 };
431 return sDataTypes;
432}
433
434const QList<QgsVectorDataProvider::NativeType> QgsFieldMappingModel::supportedDataTypes()
435{
436 static const QList<QgsVectorDataProvider::NativeType> sDataTypes = QList<QgsVectorDataProvider::NativeType>() << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Int ), u"integer"_s, QMetaType::Type::Int )
437 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::LongLong ), u"int8"_s, QMetaType::Type::LongLong )
438 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ), u"double precision"_s, QMetaType::Type::Double )
439 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QString ), u"text"_s, QMetaType::Type::QString )
440 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDate ), u"date"_s, QMetaType::Type::QDate )
441 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QTime ), u"time"_s, QMetaType::Type::QTime )
442 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDateTime ), u"datetime"_s, QMetaType::Type::QDateTime )
443 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Bool ), u"boolean"_s, QMetaType::Type::Bool )
444 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QByteArray ), u"binary"_s, QMetaType::Type::QByteArray )
445 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QStringList ), u"stringlist"_s, QMetaType::Type::QStringList, 0, 0, 0, 0, QMetaType::Type::QString )
446 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::Int ), u"integerlist"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::Int )
447 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::Double ), u"doublelist"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::Double )
448 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::LongLong ), u"integer64list"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::LongLong );
449 return sDataTypes;
450}
451
452QString QgsFieldMappingModel::qgsFieldToTypeName( const QgsField &field ) const
453{
454 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
455 {
456 if ( type.mType == field.type() && type.mSubType == field.subType() )
457 {
458 return type.mTypeName;
459 }
460 }
461
462 // no exact type name matches, try to match metatype as a fallback
463 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
464 {
465 if ( type.mType == field.type() && type.mSubType == field.subType() )
466 {
467 return type.mTypeName;
468 }
469 }
470
471 // next chance -- try promoting numeric types
472
473 // promote int -> long long
474 if ( field.type() == QMetaType::Type::Int )
475 {
476 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
477 {
478 if ( type.mType == QMetaType::Type::LongLong )
479 {
480 return type.mTypeName;
481 }
482 }
483 }
484 // promote uint -> unsigned long long or long long
485 if ( field.type() == QMetaType::Type::UInt )
486 {
487 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
488 {
489 if ( type.mType == QMetaType::Type::ULongLong || type.mType == QMetaType::Type::LongLong )
490 {
491 return type.mTypeName;
492 }
493 }
494 }
495 // promote float or int -> double
496 if ( field.type() == QMetaType::Type::Float || field.type() == QMetaType::Type::Int )
497 {
498 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
499 {
500 if ( type.mType == QMetaType::Type::Double )
501 {
502 return type.mTypeName;
503 }
504 }
505 }
506 // last resort -- promote certain types to string
507 if ( field.type() == QMetaType::Type::Float
508 || field.type() == QMetaType::Type::Double
509 || field.type() == QMetaType::Type::Int
510 || field.type() == QMetaType::Type::UInt
511 || field.type() == QMetaType::Type::LongLong
512 || field.type() == QMetaType::Type::ULongLong
513 || field.type() == QMetaType::Type::QDate
514 || field.type() == QMetaType::Type::QTime
515 || field.type() == QMetaType::Type::QDateTime )
516 {
517 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
518 {
519 if ( type.mType == QMetaType::Type::QString )
520 {
521 return type.mTypeName;
522 }
523 }
524 }
525
526 return QString();
527}
528
529void QgsFieldMappingModel::setFieldTypeFromName( QgsField &field, const QString &name ) const
530{
531 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
532 {
533 if ( type.mTypeName == name )
534 {
535 field.setType( type.mType );
536 field.setTypeName( type.mTypeName );
537 field.setSubType( type.mSubType );
538 return;
539 }
540 }
541}
542
543QList<QgsFieldMappingModel::Field> QgsFieldMappingModel::mapping() const
544{
545 return mMapping;
546}
547
548QMap<QString, QgsProperty> QgsFieldMappingModel::fieldPropertyMap() const
549{
550 QMap<QString, QgsProperty> fieldMap;
551 for ( const QgsFieldMappingModel::Field &field : mMapping )
552 {
553 const QgsExpression exp( field.expression );
554 const bool isField = exp.isField();
555 fieldMap.insert( field.originalName, isField ? QgsProperty::fromField( static_cast<const QgsExpressionNodeColumnRef *>( exp.rootNode() )->name() ) : QgsProperty::fromExpression( field.expression ) );
556 }
557 return fieldMap;
558}
559
560void QgsFieldMappingModel::setFieldPropertyMap( const QMap<QString, QgsProperty> &map )
561{
562 beginResetModel();
563 for ( int i = 0; i < mMapping.count(); ++i )
564 {
565 Field &f = mMapping[i];
566 if ( map.contains( f.field.name() ) )
567 {
568 const QgsProperty prop = map.value( f.field.name() );
569 switch ( prop.propertyType() )
570 {
573 break;
574
576 f.expression = prop.field();
577 break;
578
580 f.expression = prop.expressionString();
581 break;
582
584 f.expression.clear();
585 break;
586 }
587 }
588 else
589 {
590 f.expression.clear();
591 }
592 }
593 endResetModel();
594}
595
596void QgsFieldMappingModel::appendField( const QgsField &field, const QString &expression )
597{
598 const int lastRow { rowCount( QModelIndex() ) };
599 beginInsertRows( QModelIndex(), lastRow, lastRow );
600 Field f;
601 f.field = field;
602 f.field.setTypeName( qgsFieldToTypeName( field ) );
603 f.expression = expression;
604 f.originalName = field.name();
605 mMapping.push_back( f );
606 endInsertRows();
607}
608
609bool QgsFieldMappingModel::removeField( const QModelIndex &index )
610{
611 if ( index.isValid() && index.model() == this && index.row() < rowCount( QModelIndex() ) )
612 {
613 beginRemoveRows( QModelIndex(), index.row(), index.row() );
614 mMapping.removeAt( index.row() );
615 endRemoveRows();
616 return true;
617 }
618 else
619 {
620 return false;
621 }
622}
623
624bool QgsFieldMappingModel::moveUp( const QModelIndex &index )
625{
626 return moveUpOrDown( index );
627}
628
629bool QgsFieldMappingModel::moveDown( const QModelIndex &index )
630{
631 return moveUpOrDown( index, false );
632}
633
634QgsFieldMappingModel::ExpressionContextGenerator::ExpressionContextGenerator( const QgsFields &sourceFields )
635 : mSourceFields( sourceFields )
636{
637}
638
640{
641 if ( mBaseGenerator )
642 {
643 QgsExpressionContext ctx = mBaseGenerator->createExpressionContext();
644 auto fieldMappingScope = std::make_unique<QgsExpressionContextScope>( tr( "Field Mapping" ) );
645 fieldMappingScope->setFields( mSourceFields );
646 ctx.appendScope( fieldMappingScope.release() );
647 return ctx;
648 }
649 else
650 {
651 QgsExpressionContext ctx;
653 ctx.setFields( mSourceFields );
654 QgsFeature feature { mSourceFields };
655 feature.setValid( true );
656 ctx.setFeature( feature );
657 return ctx;
658 }
659}
660
661void QgsFieldMappingModel::ExpressionContextGenerator::setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator )
662{
663 mBaseGenerator = generator;
664}
665
666void QgsFieldMappingModel::ExpressionContextGenerator::setSourceFields( const QgsFields &fields )
667{
668 mSourceFields = fields;
669}
@ Invalid
Invalid (not set) property.
Definition qgis.h:702
@ Field
Field based property.
Definition qgis.h:704
@ Static
Static property.
Definition qgis.h:703
@ Expression
Expression based property.
Definition qgis.h:705
Abstract interface for generating an expression context.
virtual QgsExpressionContext createExpressionContext() const =0
This method needs to be reimplemented in all classes which implement this interface and return an exp...
static QgsExpressionContextScope * globalScope()
Creates a new scope which contains variables and functions relating to the global QGIS context.
Expression contexts are used to encapsulate the parameters around which a QgsExpression should be eva...
void appendScope(QgsExpressionContextScope *scope)
Appends a scope to the end of the context.
void setFeature(const QgsFeature &feature)
Convenience function for setting a feature for the context.
void setFields(const QgsFields &fields)
Convenience function for setting a fields for the context.
An expression node which takes its value from a feature's field.
QString name() const
The name of the column.
Handles parsing and evaluation of expressions (formerly called "search strings").
static QString quotedValue(const QVariant &value)
Returns a string representation of a literal value, including appropriate quotations where required.
bool isField() const
Checks whether an expression consists only of a single field reference.
QSet< QString > referencedColumns() const
Gets list of columns referenced by the expression.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes).
const QgsExpressionNode * rootNode() const
Returns the root node of the expression.
void setValid(bool validity)
Sets the validity of the feature.
Stores information about constraints which may be present on a field.
@ ConstraintStrengthHard
Constraint must be honored before feature can be accepted.
@ ConstraintNotNull
Field may not be null.
@ ConstraintUnique
Field must have a unique value.
@ ConstraintExpression
Field has an expression constraint set. See constraintExpression().
QFlags< Constraint > Constraints
void setDestinationEditable(bool editable)
Sets the destination fields editable state to editable.
QVariant headerData(int section, Qt::Orientation orientation, int role) const override
ColumnDataIndex
The ColumnDataIndex enum represents the column index for the view.
@ DestinationPrecision
Destination field precision.
@ DestinationConstraints
Destination field constraints.
@ DestinationType
Destination field type string.
@ DestinationLength
Destination field length.
Qt::ItemFlags flags(const QModelIndex &index) const override
QgsFields sourceFields() const
Returns a list of source fields.
void setNativeTypes(const QList< QgsVectorDataProvider::NativeType > &nativeTypes)
Sets the list of nativeTypes supported by a data provider.
int columnCount(const QModelIndex &parent=QModelIndex()) const override
bool removeField(const QModelIndex &index)
Removes the field at index from the model, returns true on success.
void appendField(const QgsField &field, const QString &expression=QString())
Appends a new field to the model, with an optional expression.
QMap< QString, QgsProperty > fieldPropertyMap() const
Returns a map of destination field name to QgsProperty definition for field value,...
QList< QgsFieldMappingModel::Field > mapping() const
Returns a list of Field objects representing the current status of the model.
QgsFieldMappingModel(const QgsFields &sourceFields=QgsFields(), const QgsFields &destinationFields=QgsFields(), const QMap< QString, QString > &expressions=QMap< QString, QString >(), QObject *parent=nullptr)
Constructs a QgsFieldMappingModel from a set of sourceFields and destinationFields,...
bool moveDown(const QModelIndex &index)
Moves up the field at index.
bool moveUp(const QModelIndex &index)
Moves down the field at index.
int rowCount(const QModelIndex &parent=QModelIndex()) const override
QVariant data(const QModelIndex &index, int role) const override
static Q_DECL_DEPRECATED const QMap< QMetaType::Type, QString > dataTypes()
Returns a static map of supported data types.
void setBaseExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Sets the base expression context generator, which will generate the expression contexts for expressio...
static const QList< QgsVectorDataProvider::NativeType > supportedDataTypes()
Returns a static list of supported data types.
QgsExpressionContextGenerator * contextGenerator() const
Returns the context generator with the source fields.
void setDestinationFields(const QgsFields &destinationFields, const QMap< QString, QString > &expressions=QMap< QString, QString >())
Set destination fields to destinationFields, initial values for the expressions can be optionally spe...
void setSourceFields(const QgsFields &sourceFields)
Set source fields to sourceFields.
bool setData(const QModelIndex &index, const QVariant &value, int role) override
bool destinationEditable() const
Returns true if the destination fields are editable.
void setFieldPropertyMap(const QMap< QString, QgsProperty > &map)
Sets a map of destination field name to QgsProperty definition for field value.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
QMetaType::Type type
Definition qgsfield.h:63
QString typeName() const
Gets the field type.
Definition qgsfield.cpp:167
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:302
QString name
Definition qgsfield.h:65
int precision
Definition qgsfield.h:62
int length
Definition qgsfield.h:61
void setPrecision(int precision)
Set the field precision.
Definition qgsfield.cpp:267
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:248
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:233
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:272
void setType(QMetaType::Type type)
Set variant type.
Definition qgsfield.cpp:238
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:263
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:162
QString alias
Definition qgsfield.h:66
QString comment
Definition qgsfield.h:64
QgsFieldConstraints constraints
Definition qgsfield.h:68
void setTypeName(const QString &typeName)
Set the field type.
Definition qgsfield.cpp:258
Container of fields for a vector layer.
Definition qgsfields.h:46
int count
Definition qgsfields.h:50
A store for object properties.
QString expressionString() const
Returns the expression used for the property value.
Qgis::PropertyType propertyType() const
Returns the property type.
QString field() const
Returns the current field name the property references.
QVariant value(const QgsExpressionContext &context, const QVariant &defaultValue=QVariant(), bool *ok=nullptr) const
Calculates the current value of the property, including any transforms which are set for the property...
static QgsProperty fromExpression(const QString &expression, bool isActive=true)
Returns a new ExpressionBasedProperty created from the specified expression.
QVariant staticValue() const
Returns the current static value for the property.
static QgsProperty fromField(const QString &fieldName, bool isActive=true)
Returns a new FieldBasedProperty created from the specified field name.
static QString typeToDisplayString(QMetaType::Type type, QMetaType::Type subType=QMetaType::Type::UnknownType)
Returns a user-friendly translated string representing a QVariant type.
The Field struct holds information about a mapped field.
QgsField field
The field in its current status (it might have been renamed).
QString expression
The expression for the mapped field from the source fields.
QString originalName
The original name of the field.