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