QGIS API Documentation 3.43.0-Master (3ee7834ace6)
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#include "moc_qgsfieldmappingmodel.cpp"
21#include "qgsvariantutils.h"
22
23QgsFieldMappingModel::QgsFieldMappingModel( const QgsFields &sourceFields, const QgsFields &destinationFields, const QMap<QString, QString> &expressions, QObject *parent )
24 : QAbstractTableModel( parent )
25 , mNativeTypes( supportedDataTypes() )
26 , mSourceFields( sourceFields )
27 , mExpressionContextGenerator( new ExpressionContextGenerator( mSourceFields ) )
28{
29 setDestinationFields( destinationFields, expressions );
30}
31
32void QgsFieldMappingModel::setNativeTypes( const QList<QgsVectorDataProvider::NativeType> &nativeTypes )
33{
34 mNativeTypes = nativeTypes.isEmpty() ? supportedDataTypes() : nativeTypes;
35}
36
37QVariant QgsFieldMappingModel::headerData( int section, Qt::Orientation orientation, int role ) const
38{
39 if ( role == Qt::DisplayRole )
40 {
41 switch ( orientation )
42 {
43 case Qt::Horizontal:
44 {
45 switch ( static_cast<ColumnDataIndex>( section ) )
46 {
48 {
49 return tr( "Source Expression" );
50 }
52 {
53 return tr( "Name" );
54 }
56 {
57 return tr( "Type" );
58 }
60 {
61 return tr( "Length" );
62 }
64 {
65 return tr( "Precision" );
66 }
68 {
69 return tr( "Constraints" );
70 }
72 {
73 return tr( "Alias" );
74 }
76 {
77 return tr( "Comment" );
78 }
79 }
80 break;
81 }
82 case Qt::Vertical:
83 {
84 return section;
85 }
86 }
87 }
88 return QVariant();
89}
90
92{
93 return mSourceFields;
94}
95
96int QgsFieldMappingModel::rowCount( const QModelIndex &parent ) const
97{
98 if ( parent.isValid() )
99 return 0;
100 return mMapping.count();
101}
102
103int QgsFieldMappingModel::columnCount( const QModelIndex &parent ) const
104{
105 if ( parent.isValid() )
106 return 0;
107 return 8;
108}
109
110QVariant QgsFieldMappingModel::data( const QModelIndex &index, int role ) const
111{
112 if ( index.isValid() )
113 {
114 const ColumnDataIndex col { static_cast<ColumnDataIndex>( index.column() ) };
115 const Field &f { mMapping.at( index.row() ) };
116
117 const QgsFieldConstraints::Constraints constraints { fieldConstraints( f.field ) };
118
119 switch ( role )
120 {
121 case Qt::DisplayRole:
122 case Qt::EditRole:
123 {
124 switch ( col )
125 {
127 {
128 return f.expression;
129 }
131 {
132 return f.field.displayNameWithAlias();
133 }
135 {
136 return f.field.typeName();
137 }
139 {
140 return f.field.length();
141 }
143 {
144 return f.field.precision();
145 }
147 {
148 return constraints != 0 ? tr( "Constraints active" ) : QString();
149 }
151 {
152 return f.field.alias();
153 }
155 {
156 return f.field.comment();
157 }
158 }
159 break;
160 }
161 case Qt::ToolTipRole:
162 {
163 if ( col == ColumnDataIndex::DestinationConstraints && constraints != 0 )
164 {
165 QStringList constraintDescription;
166 if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintUnique ) )
167 {
168 constraintDescription.push_back( tr( "Unique" ) );
169 }
170 if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
171 {
172 constraintDescription.push_back( tr( "Not null" ) );
173 }
174 if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintExpression ) )
175 {
176 constraintDescription.push_back( tr( "Expression" ) );
177 }
178 return constraintDescription.join( QLatin1String( "<br>" ) );
179 }
180 break;
181 }
182 case Qt::BackgroundRole:
183 {
184 if ( constraints != 0 )
185 {
186 return QBrush( QColor( 255, 224, 178 ) );
187 }
188 break;
189 }
190
191 default:
192 break;
193 }
194 }
195 return QVariant();
196}
197
198Qt::ItemFlags QgsFieldMappingModel::flags( const QModelIndex &index ) const
199{
200 if ( index.isValid() && index.column() != static_cast<int>( ColumnDataIndex::DestinationConstraints ) && ( index.column() == static_cast<int>( ColumnDataIndex::SourceExpression ) || destinationEditable() ) )
201 {
202 return Qt::ItemFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
203 }
204 return Qt::ItemFlags();
205}
206
207bool QgsFieldMappingModel::setData( const QModelIndex &index, const QVariant &value, int role )
208{
209 if ( index.isValid() )
210 {
211 if ( role == Qt::EditRole )
212 {
213 Field &f = mMapping[index.row()];
214 switch ( static_cast<ColumnDataIndex>( index.column() ) )
215 {
217 {
218 const QgsExpression exp { value.toString() };
219 f.expression = exp;
220 break;
221 }
223 {
224 f.field.setName( value.toString() );
225 break;
226 }
228 {
229 setFieldTypeFromName( f.field, value.toString() );
230 break;
231 }
233 {
234 bool ok;
235 const int length { value.toInt( &ok ) };
236 if ( ok )
237 f.field.setLength( length );
238 break;
239 }
241 {
242 bool ok;
243 const int precision { value.toInt( &ok ) };
244 if ( ok )
246 break;
247 }
249 {
250 // Not editable: do nothing
251 break;
252 }
254 {
255 f.field.setAlias( value.toString() );
256 break;
257 }
259 {
260 f.field.setComment( value.toString() );
261 break;
262 }
263 }
264 emit dataChanged( index, index );
265 }
266 return true;
267 }
268 else
269 {
270 return false;
271 }
272}
273
274QgsFieldConstraints::Constraints QgsFieldMappingModel::fieldConstraints( const QgsField &field ) const
275{
277
278 const QgsFieldConstraints fieldConstraints { field.constraints() };
279
280 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintNotNull && fieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintNotNull ) & QgsFieldConstraints::ConstraintStrengthHard )
281 constraints.setFlag( QgsFieldConstraints::ConstraintNotNull );
282
283 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintUnique && fieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintUnique ) & QgsFieldConstraints::ConstraintStrengthHard )
284 constraints.setFlag( QgsFieldConstraints::ConstraintUnique );
285
286 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintExpression && fieldConstraints.constraintStrength( QgsFieldConstraints::ConstraintExpression ) & QgsFieldConstraints::ConstraintStrengthHard )
287 constraints.setFlag( QgsFieldConstraints::ConstraintExpression );
288
289 return constraints;
290}
291
292bool QgsFieldMappingModel::moveUpOrDown( const QModelIndex &index, bool up )
293{
294 if ( !index.isValid() && index.model() == this )
295 return false;
296
297 // Always swap down
298 const int row { up ? index.row() - 1 : index.row() };
299 // Range checking
300 if ( row < 0 || row + 1 >= rowCount( QModelIndex() ) )
301 {
302 return false;
303 }
304 beginMoveRows( QModelIndex(), row, row, QModelIndex(), row + 2 );
305 mMapping.swapItemsAt( row, row + 1 );
306 endMoveRows();
307 return true;
308}
309
310QString QgsFieldMappingModel::findExpressionForDestinationField( const QgsFieldMappingModel::Field &f, QStringList &excludedFieldNames )
311{
312 // Search for fields in the source
313 // 1. match by name
314 for ( const QgsField &sf : std::as_const( mSourceFields ) )
315 {
316 if ( sf.name() == f.field.name() )
317 {
318 excludedFieldNames.push_back( sf.name() );
319 return QgsExpression::quotedColumnRef( sf.name() );
320 }
321 }
322 // 2. match by type
323 for ( const QgsField &sf : std::as_const( mSourceFields ) )
324 {
325 if ( excludedFieldNames.contains( sf.name() ) || sf.type() != f.field.type() )
326 continue;
327 excludedFieldNames.push_back( sf.name() );
328 return QgsExpression::quotedColumnRef( sf.name() );
329 }
330 return QString();
331}
332
334{
335 mSourceFields = sourceFields;
336 if ( mExpressionContextGenerator )
337 mExpressionContextGenerator->setSourceFields( mSourceFields );
338 QStringList usedFields;
339 beginResetModel();
340 for ( const Field &f : std::as_const( mMapping ) )
341 {
342 if ( QgsExpression( f.expression ).isField() )
343 {
344 usedFields.push_back( f.expression.mid( 1, f.expression.length() - 2 ) );
345 }
346 }
347 for ( auto it = mMapping.begin(); it != mMapping.end(); ++it )
348 {
349 if ( it->expression.isEmpty() )
350 {
351 const QString expression { findExpressionForDestinationField( *it, usedFields ) };
352 if ( !expression.isEmpty() )
353 it->expression = expression;
354 }
355 }
356 endResetModel();
357}
358
360{
361 return mExpressionContextGenerator.get();
362}
363
365{
366 mExpressionContextGenerator->setBaseExpressionContextGenerator( generator );
367}
368
369void QgsFieldMappingModel::setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions )
370{
371 beginResetModel();
372 mMapping.clear();
373 // Prepare the model data
374 QStringList usedFields;
375 for ( const QgsField &df : destinationFields )
376 {
377 Field f;
378 f.field = df;
379 f.field.setTypeName( qgsFieldToTypeName( df ) );
380 f.originalName = df.name();
381 if ( expressions.contains( f.field.name() ) )
382 {
383 f.expression = expressions.value( f.field.name() );
384 const QgsExpression exp { f.expression };
385 // if it's source field
386 if ( exp.isField() && mSourceFields.names().contains( qgis::setToList( exp.referencedColumns() ).constFirst() ) )
387 {
388 usedFields.push_back( qgis::setToList( exp.referencedColumns() ).constFirst() );
389 }
390 }
391 else
392 {
393 const QString expression { findExpressionForDestinationField( f, usedFields ) };
394 if ( !expression.isEmpty() )
395 f.expression = expression;
396 }
397 mMapping.push_back( f );
398 }
399 endResetModel();
400}
401
403{
404 return mDestinationEditable;
405}
406
407void QgsFieldMappingModel::setDestinationEditable( bool destinationEditable )
408{
409 mDestinationEditable = destinationEditable;
410}
411
412const QMap<QMetaType::Type, QString> QgsFieldMappingModel::dataTypes()
413{
414 static const QMap<QMetaType::Type, QString> sDataTypes {
415 { QMetaType::Type::Int, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Int ) },
416 { QMetaType::Type::LongLong, QgsVariantUtils::typeToDisplayString( QMetaType::Type::LongLong ) },
417 { QMetaType::Type::Double, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ) },
418 { QMetaType::Type::QString, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QString ) },
419 { QMetaType::Type::QDate, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDate ) },
420 { QMetaType::Type::QTime, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QTime ) },
421 { QMetaType::Type::QDateTime, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDateTime ) },
422 { QMetaType::Type::Bool, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Bool ) },
423 { QMetaType::Type::QByteArray, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QByteArray ) },
424 };
425 return sDataTypes;
426}
427
428const QList<QgsVectorDataProvider::NativeType> QgsFieldMappingModel::supportedDataTypes()
429{
430 static const QList<QgsVectorDataProvider::NativeType> sDataTypes = QList<QgsVectorDataProvider::NativeType>() << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Int ), QStringLiteral( "integer" ), QMetaType::Type::Int )
431 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::LongLong ), QStringLiteral( "int8" ), QMetaType::Type::LongLong )
432 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ), QStringLiteral( "double precision" ), QMetaType::Type::Double )
433 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QString ), QStringLiteral( "text" ), QMetaType::Type::QString )
434 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDate ), QStringLiteral( "date" ), QMetaType::Type::QDate )
435 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QTime ), QStringLiteral( "time" ), QMetaType::Type::QTime )
436 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDateTime ), QStringLiteral( "datetime" ), QMetaType::Type::QDateTime )
437 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Bool ), QStringLiteral( "boolean" ), QMetaType::Type::Bool )
438 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QByteArray ), QStringLiteral( "binary" ), QMetaType::Type::QByteArray )
439 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QStringList ), QStringLiteral( "stringlist" ), QMetaType::Type::QStringList, 0, 0, 0, 0, QMetaType::Type::QString )
440 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::Int ), QStringLiteral( "integerlist" ), QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::Int )
441 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::Double ), QStringLiteral( "doublelist" ), QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::Double )
442 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::LongLong ), QStringLiteral( "integer64list" ), QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::LongLong );
443 return sDataTypes;
444}
445
446QString QgsFieldMappingModel::qgsFieldToTypeName( const QgsField &field ) const
447{
448 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
449 {
450 if ( type.mType == field.type() && type.mSubType == field.subType() )
451 {
452 return type.mTypeName;
453 }
454 }
455
456 // no exact type name matches, try to match metatype as a fallback
457 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
458 {
459 if ( type.mType == field.type() && type.mSubType == field.subType() )
460 {
461 return type.mTypeName;
462 }
463 }
464
465 // next chance -- try promoting numeric types
466
467 // promote int -> long long
468 if ( field.type() == QMetaType::Type::Int )
469 {
470 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
471 {
472 if ( type.mType == QMetaType::Type::LongLong )
473 {
474 return type.mTypeName;
475 }
476 }
477 }
478 // promote uint -> unsigned long long or long long
479 if ( field.type() == QMetaType::Type::UInt )
480 {
481 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
482 {
483 if ( type.mType == QMetaType::Type::ULongLong || type.mType == QMetaType::Type::LongLong )
484 {
485 return type.mTypeName;
486 }
487 }
488 }
489 // promote float or int -> double
490 if ( field.type() == QMetaType::Type::Float || field.type() == QMetaType::Type::Int )
491 {
492 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
493 {
494 if ( type.mType == QMetaType::Type::Double )
495 {
496 return type.mTypeName;
497 }
498 }
499 }
500 // last resort -- promote certain types to string
501 if ( field.type() == QMetaType::Type::Float
502 || field.type() == QMetaType::Type::Double
503 || field.type() == QMetaType::Type::Int
504 || field.type() == QMetaType::Type::UInt
505 || field.type() == QMetaType::Type::LongLong
506 || field.type() == QMetaType::Type::ULongLong
507 || field.type() == QMetaType::Type::QDate
508 || field.type() == QMetaType::Type::QTime
509 || field.type() == QMetaType::Type::QDateTime )
510 {
511 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
512 {
513 if ( type.mType == QMetaType::Type::QString )
514 {
515 return type.mTypeName;
516 }
517 }
518 }
519
520 return QString();
521}
522
523void QgsFieldMappingModel::setFieldTypeFromName( QgsField &field, const QString &name ) const
524{
525 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
526 {
527 if ( type.mTypeName == name )
528 {
529 field.setType( type.mType );
530 field.setTypeName( type.mTypeName );
531 field.setSubType( type.mSubType );
532 return;
533 }
534 }
535}
536
537QList<QgsFieldMappingModel::Field> QgsFieldMappingModel::mapping() const
538{
539 return mMapping;
540}
541
542QMap<QString, QgsProperty> QgsFieldMappingModel::fieldPropertyMap() const
543{
544 QMap<QString, QgsProperty> fieldMap;
545 for ( const QgsFieldMappingModel::Field &field : mMapping )
546 {
547 const QgsExpression exp( field.expression );
548 const bool isField = exp.isField();
549 fieldMap.insert( field.originalName, isField ? QgsProperty::fromField( static_cast<const QgsExpressionNodeColumnRef *>( exp.rootNode() )->name() ) : QgsProperty::fromExpression( field.expression ) );
550 }
551 return fieldMap;
552}
553
554void QgsFieldMappingModel::setFieldPropertyMap( const QMap<QString, QgsProperty> &map )
555{
556 beginResetModel();
557 for ( int i = 0; i < mMapping.count(); ++i )
558 {
559 Field &f = mMapping[i];
560 if ( map.contains( f.field.name() ) )
561 {
562 const QgsProperty prop = map.value( f.field.name() );
563 switch ( prop.propertyType() )
564 {
567 break;
568
570 f.expression = prop.field();
571 break;
572
574 f.expression = prop.expressionString();
575 break;
576
578 f.expression.clear();
579 break;
580 }
581 }
582 else
583 {
584 f.expression.clear();
585 }
586 }
587 endResetModel();
588}
589
590void QgsFieldMappingModel::appendField( const QgsField &field, const QString &expression )
591{
592 const int lastRow { rowCount( QModelIndex() ) };
593 beginInsertRows( QModelIndex(), lastRow, lastRow );
594 Field f;
595 f.field = field;
596 f.field.setTypeName( qgsFieldToTypeName( field ) );
597 f.expression = expression;
598 f.originalName = field.name();
599 mMapping.push_back( f );
600 endInsertRows();
601}
602
603bool QgsFieldMappingModel::removeField( const QModelIndex &index )
604{
605 if ( index.isValid() && index.model() == this && index.row() < rowCount( QModelIndex() ) )
606 {
607 beginRemoveRows( QModelIndex(), index.row(), index.row() );
608 mMapping.removeAt( index.row() );
609 endRemoveRows();
610 return true;
611 }
612 else
613 {
614 return false;
615 }
616}
617
618bool QgsFieldMappingModel::moveUp( const QModelIndex &index )
619{
620 return moveUpOrDown( index );
621}
622
623bool QgsFieldMappingModel::moveDown( const QModelIndex &index )
624{
625 return moveUpOrDown( index, false );
626}
627
628QgsFieldMappingModel::ExpressionContextGenerator::ExpressionContextGenerator( const QgsFields &sourceFields )
629 : mSourceFields( sourceFields )
630{
631}
632
633QgsExpressionContext QgsFieldMappingModel::ExpressionContextGenerator::createExpressionContext() const
634{
635 if ( mBaseGenerator )
636 {
637 QgsExpressionContext ctx = mBaseGenerator->createExpressionContext();
638 auto fieldMappingScope = std::make_unique<QgsExpressionContextScope>( tr( "Field Mapping" ) );
639 fieldMappingScope->setFields( mSourceFields );
640 ctx.appendScope( fieldMappingScope.release() );
641 return ctx;
642 }
643 else
644 {
647 ctx.setFields( mSourceFields );
648 QgsFeature feature { mSourceFields };
649 feature.setValid( true );
650 ctx.setFeature( feature );
651 return ctx;
652 }
653}
654
655void QgsFieldMappingModel::ExpressionContextGenerator::setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator )
656{
657 mBaseGenerator = generator;
658}
659
660void QgsFieldMappingModel::ExpressionContextGenerator::setSourceFields( const QgsFields &fields )
661{
662 mSourceFields = fields;
663}
@ Invalid
Invalid (not set) property.
@ Field
Field based property.
@ Static
Static property.
@ Expression
Expression based property.
Abstract interface for generating an expression context.
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 it value from a feature's field.
QString name() const
The name of the column.
Class for 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.
static QString quotedColumnRef(QString name)
Returns a quoted column reference (in double quotes)
const QgsExpressionNode * rootNode() const
Returns the root node of the expression.
The feature class encapsulates a single feature including its unique ID, geometry and a list of field...
Definition qgsfeature.h:58
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.
@ DestinationComment
Destination comment.
@ DestinationConstraints
Destination field constraints.
@ DestinationName
Destination field name.
@ 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:53
QMetaType::Type type
Definition qgsfield.h:60
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:297
QString name
Definition qgsfield.h:62
void setPrecision(int precision)
Set the field precision.
Definition qgsfield.cpp:262
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:243
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:228
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:267
void setType(QMetaType::Type type)
Set variant type.
Definition qgsfield.cpp:233
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:258
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:157
QgsFieldConstraints constraints
Definition qgsfield.h:65
void setTypeName(const QString &typeName)
Set the field type.
Definition qgsfield.cpp:253
Container of fields for a vector layer.
Definition qgsfields.h:46
QStringList names
Definition qgsfields.h:51
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.
int precision
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.