QGIS API Documentation 4.1.0-Master (5bf3c20f3c9)
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()
207 && index.column() != static_cast<int>( ColumnDataIndex::DestinationConstraints )
208 && ( index.column() == static_cast<int>( ColumnDataIndex::SourceExpression ) || destinationEditable() ) )
209 {
210 return Qt::ItemFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
211 }
212 return Qt::ItemFlags();
213}
214
215bool QgsFieldMappingModel::setData( const QModelIndex &index, const QVariant &value, int role )
216{
217 if ( index.isValid() )
218 {
219 if ( role == Qt::EditRole )
220 {
221 Field &f = mMapping[index.row()];
222 switch ( static_cast<ColumnDataIndex>( index.column() ) )
223 {
225 {
226 const QgsExpression exp { value.toString() };
227 f.expression = exp;
228 break;
229 }
231 {
232 f.field.setName( value.toString() );
233 break;
234 }
236 {
237 setFieldTypeFromName( f.field, value.toString() );
238 break;
239 }
241 {
242 bool ok;
243 const int length { value.toInt( &ok ) };
244 if ( ok )
245 f.field.setLength( length );
246 break;
247 }
249 {
250 bool ok;
251 const int precision { value.toInt( &ok ) };
252 if ( ok )
253 f.field.setPrecision( precision );
254 break;
255 }
257 {
258 // Not editable: do nothing
259 break;
260 }
262 {
263 f.field.setAlias( value.toString() );
264 break;
265 }
267 {
268 f.field.setComment( value.toString() );
269 break;
270 }
271 }
272 emit dataChanged( index, index );
273 }
274 return true;
275 }
276 else
277 {
278 return false;
279 }
280}
281
282QgsFieldConstraints::Constraints QgsFieldMappingModel::fieldConstraints( const QgsField &field ) const
283{
285
286 const QgsFieldConstraints fieldConstraints { field.constraints() };
287
288 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintNotNull
290 constraints.setFlag( QgsFieldConstraints::ConstraintNotNull );
291
292 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintUnique
294 constraints.setFlag( QgsFieldConstraints::ConstraintUnique );
295
296 if ( fieldConstraints.constraints() & QgsFieldConstraints::ConstraintExpression
298 constraints.setFlag( QgsFieldConstraints::ConstraintExpression );
299
300 return constraints;
301}
302
303bool QgsFieldMappingModel::moveUpOrDown( const QModelIndex &index, bool up )
304{
305 if ( !index.isValid() && index.model() == this )
306 return false;
307
308 // Always swap down
309 const int row { up ? index.row() - 1 : index.row() };
310 // Range checking
311 if ( row < 0 || row + 1 >= rowCount( QModelIndex() ) )
312 {
313 return false;
314 }
315 beginMoveRows( QModelIndex(), row, row, QModelIndex(), row + 2 );
316 mMapping.swapItemsAt( row, row + 1 );
317 endMoveRows();
318 return true;
319}
320
321QString QgsFieldMappingModel::findExpressionForDestinationField( const QgsFieldMappingModel::Field &f, QStringList &excludedFieldNames )
322{
323 // Search for fields in the source
324 // 1. match by name
325 for ( const QgsField &sf : std::as_const( mSourceFields ) )
326 {
327 if ( sf.name() == f.field.name() )
328 {
329 excludedFieldNames.push_back( sf.name() );
330 return QgsExpression::quotedColumnRef( sf.name() );
331 }
332 }
333 // 2. match by type
334 for ( const QgsField &sf : std::as_const( mSourceFields ) )
335 {
336 if ( excludedFieldNames.contains( sf.name() ) || sf.type() != f.field.type() )
337 continue;
338 excludedFieldNames.push_back( sf.name() );
339 return QgsExpression::quotedColumnRef( sf.name() );
340 }
341 return QString();
342}
343
345{
346 mSourceFields = sourceFields;
347 if ( mExpressionContextGenerator )
348 mExpressionContextGenerator->setSourceFields( mSourceFields );
349 QStringList usedFields;
350 beginResetModel();
351 for ( const Field &f : std::as_const( mMapping ) )
352 {
353 if ( QgsExpression( f.expression ).isField() )
354 {
355 usedFields.push_back( f.expression.mid( 1, f.expression.length() - 2 ) );
356 }
357 }
358 for ( auto it = mMapping.begin(); it != mMapping.end(); ++it )
359 {
360 if ( it->expression.isEmpty() )
361 {
362 const QString expression { findExpressionForDestinationField( *it, usedFields ) };
363 if ( !expression.isEmpty() )
364 it->expression = expression;
365 }
366 }
367 endResetModel();
368}
369
371{
372 return mExpressionContextGenerator.get();
373}
374
376{
377 mExpressionContextGenerator->setBaseExpressionContextGenerator( generator );
378}
379
380void QgsFieldMappingModel::setDestinationFields( const QgsFields &destinationFields, const QMap<QString, QString> &expressions )
381{
382 beginResetModel();
383 mMapping.clear();
384 // Prepare the model data
385 QStringList usedFields;
386 for ( const QgsField &df : destinationFields )
387 {
388 Field f;
389 f.field = df;
390 f.field.setTypeName( qgsFieldToTypeName( df ) );
391 f.originalName = df.name();
392 if ( expressions.contains( f.field.name() ) )
393 {
394 f.expression = expressions.value( f.field.name() );
395 const QgsExpression exp { f.expression };
396 // if it's source field
397 if ( exp.isField() && mSourceFields.names().contains( qgis::setToList( exp.referencedColumns() ).constFirst() ) )
398 {
399 usedFields.push_back( qgis::setToList( exp.referencedColumns() ).constFirst() );
400 }
401 }
402 else
403 {
404 const QString expression { findExpressionForDestinationField( f, usedFields ) };
405 if ( !expression.isEmpty() )
406 f.expression = expression;
407 }
408 mMapping.push_back( f );
409 }
410 endResetModel();
411}
412
414{
415 return mDestinationEditable;
416}
417
422
423const QMap<QMetaType::Type, QString> QgsFieldMappingModel::dataTypes()
424{
425 static const QMap<QMetaType::Type, QString> sDataTypes {
426 { QMetaType::Type::Int, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Int ) },
427 { QMetaType::Type::LongLong, QgsVariantUtils::typeToDisplayString( QMetaType::Type::LongLong ) },
428 { QMetaType::Type::Double, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ) },
429 { QMetaType::Type::QString, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QString ) },
430 { QMetaType::Type::QDate, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDate ) },
431 { QMetaType::Type::QTime, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QTime ) },
432 { QMetaType::Type::QDateTime, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDateTime ) },
433 { QMetaType::Type::Bool, QgsVariantUtils::typeToDisplayString( QMetaType::Type::Bool ) },
434 { QMetaType::Type::QByteArray, QgsVariantUtils::typeToDisplayString( QMetaType::Type::QByteArray ) },
435 };
436 return sDataTypes;
437}
438
439const QList<QgsVectorDataProvider::NativeType> QgsFieldMappingModel::supportedDataTypes()
440{
441 static const QList<QgsVectorDataProvider::NativeType> sDataTypes
442 = QList<QgsVectorDataProvider::NativeType>()
443 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Int ), u"integer"_s, QMetaType::Type::Int )
444 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::LongLong ), u"int8"_s, QMetaType::Type::LongLong )
445 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Double ), u"double precision"_s, QMetaType::Type::Double )
446 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QString ), u"text"_s, QMetaType::Type::QString )
447 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDate ), u"date"_s, QMetaType::Type::QDate )
448 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QTime ), u"time"_s, QMetaType::Type::QTime )
449 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QDateTime ), u"datetime"_s, QMetaType::Type::QDateTime )
450 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::Bool ), u"boolean"_s, QMetaType::Type::Bool )
451 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QByteArray ), u"binary"_s, QMetaType::Type::QByteArray )
452 << QgsVectorDataProvider::NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QStringList ), u"stringlist"_s, QMetaType::Type::QStringList, 0, 0, 0, 0, QMetaType::Type::QString )
453 << QgsVectorDataProvider::
454 NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::Int ), u"integerlist"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::Int )
455 << QgsVectorDataProvider::
456 NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::Double ), u"doublelist"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::Double )
457 << QgsVectorDataProvider::
458 NativeType( QgsVariantUtils::typeToDisplayString( QMetaType::Type::QVariantList, QMetaType::Type::LongLong ), u"integer64list"_s, QMetaType::Type::QVariantList, 0, 0, 0, 0, QMetaType::Type::LongLong );
459 return sDataTypes;
460}
461
462QString QgsFieldMappingModel::qgsFieldToTypeName( const QgsField &field ) const
463{
464 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
465 {
466 if ( type.mType == field.type() && type.mSubType == field.subType() )
467 {
468 return type.mTypeName;
469 }
470 }
471
472 // no exact type name matches, try to match metatype as a fallback
473 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
474 {
475 if ( type.mType == field.type() && type.mSubType == field.subType() )
476 {
477 return type.mTypeName;
478 }
479 }
480
481 // next chance -- try promoting numeric types
482
483 // promote int -> long long
484 if ( field.type() == QMetaType::Type::Int )
485 {
486 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
487 {
488 if ( type.mType == QMetaType::Type::LongLong )
489 {
490 return type.mTypeName;
491 }
492 }
493 }
494 // promote uint -> unsigned long long or long long
495 if ( field.type() == QMetaType::Type::UInt )
496 {
497 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
498 {
499 if ( type.mType == QMetaType::Type::ULongLong || type.mType == QMetaType::Type::LongLong )
500 {
501 return type.mTypeName;
502 }
503 }
504 }
505 // promote float or int -> double
506 if ( field.type() == QMetaType::Type::Float || field.type() == QMetaType::Type::Int )
507 {
508 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
509 {
510 if ( type.mType == QMetaType::Type::Double )
511 {
512 return type.mTypeName;
513 }
514 }
515 }
516 // last resort -- promote certain types to string
517 if ( field.type() == QMetaType::Type::Float
518 || field.type() == QMetaType::Type::Double
519 || field.type() == QMetaType::Type::Int
520 || field.type() == QMetaType::Type::UInt
521 || field.type() == QMetaType::Type::LongLong
522 || field.type() == QMetaType::Type::ULongLong
523 || field.type() == QMetaType::Type::QDate
524 || field.type() == QMetaType::Type::QTime
525 || field.type() == QMetaType::Type::QDateTime )
526 {
527 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
528 {
529 if ( type.mType == QMetaType::Type::QString )
530 {
531 return type.mTypeName;
532 }
533 }
534 }
535
536 return QString();
537}
538
539void QgsFieldMappingModel::setFieldTypeFromName( QgsField &field, const QString &name ) const
540{
541 for ( const QgsVectorDataProvider::NativeType &type : mNativeTypes )
542 {
543 if ( type.mTypeName == name )
544 {
545 field.setType( type.mType );
546 field.setTypeName( type.mTypeName );
547 field.setSubType( type.mSubType );
548 return;
549 }
550 }
551}
552
553QList<QgsFieldMappingModel::Field> QgsFieldMappingModel::mapping() const
554{
555 return mMapping;
556}
557
558QMap<QString, QgsProperty> QgsFieldMappingModel::fieldPropertyMap() const
559{
560 QMap<QString, QgsProperty> fieldMap;
561 for ( const QgsFieldMappingModel::Field &field : mMapping )
562 {
563 const QgsExpression exp( field.expression );
564 const bool isField = exp.isField();
565 fieldMap.insert( field.originalName, isField ? QgsProperty::fromField( static_cast<const QgsExpressionNodeColumnRef *>( exp.rootNode() )->name() ) : QgsProperty::fromExpression( field.expression ) );
566 }
567 return fieldMap;
568}
569
570void QgsFieldMappingModel::setFieldPropertyMap( const QMap<QString, QgsProperty> &map )
571{
572 beginResetModel();
573 for ( int i = 0; i < mMapping.count(); ++i )
574 {
575 Field &f = mMapping[i];
576 if ( map.contains( f.field.name() ) )
577 {
578 const QgsProperty prop = map.value( f.field.name() );
579 switch ( prop.propertyType() )
580 {
583 break;
584
586 f.expression = prop.field();
587 break;
588
590 f.expression = prop.expressionString();
591 break;
592
594 f.expression.clear();
595 break;
596 }
597 }
598 else
599 {
600 f.expression.clear();
601 }
602 }
603 endResetModel();
604}
605
606void QgsFieldMappingModel::appendField( const QgsField &field, const QString &expression )
607{
608 const int lastRow { rowCount( QModelIndex() ) };
609 beginInsertRows( QModelIndex(), lastRow, lastRow );
610 Field f;
611 f.field = field;
612 f.field.setTypeName( qgsFieldToTypeName( field ) );
613 f.expression = expression;
614 f.originalName = field.name();
615 mMapping.push_back( f );
616 endInsertRows();
617}
618
619bool QgsFieldMappingModel::removeField( const QModelIndex &index )
620{
621 if ( index.isValid() && index.model() == this && index.row() < rowCount( QModelIndex() ) )
622 {
623 beginRemoveRows( QModelIndex(), index.row(), index.row() );
624 mMapping.removeAt( index.row() );
625 endRemoveRows();
626 return true;
627 }
628 else
629 {
630 return false;
631 }
632}
633
634bool QgsFieldMappingModel::moveUp( const QModelIndex &index )
635{
636 return moveUpOrDown( index );
637}
638
639bool QgsFieldMappingModel::moveDown( const QModelIndex &index )
640{
641 return moveUpOrDown( index, false );
642}
643
644QgsFieldMappingModel::ExpressionContextGenerator::ExpressionContextGenerator( const QgsFields &sourceFields )
645 : mSourceFields( sourceFields )
646{}
647
649{
650 if ( mBaseGenerator )
651 {
652 QgsExpressionContext ctx = mBaseGenerator->createExpressionContext();
653 auto fieldMappingScope = std::make_unique<QgsExpressionContextScope>( tr( "Field Mapping" ) );
654 fieldMappingScope->setFields( mSourceFields );
655 ctx.appendScope( fieldMappingScope.release() );
656 return ctx;
657 }
658 else
659 {
660 QgsExpressionContext ctx;
662 ctx.setFields( mSourceFields );
663 QgsFeature feature { mSourceFields };
664 feature.setValid( true );
665 ctx.setFeature( feature );
666 return ctx;
667 }
668}
669
670void QgsFieldMappingModel::ExpressionContextGenerator::setBaseExpressionContextGenerator( const QgsExpressionContextGenerator *generator )
671{
672 mBaseGenerator = generator;
673}
674
675void QgsFieldMappingModel::ExpressionContextGenerator::setSourceFields( const QgsFields &fields )
676{
677 mSourceFields = fields;
678}
@ Invalid
Invalid (not set) property.
Definition qgis.h:709
@ Field
Field based property.
Definition qgis.h:711
@ Static
Static property.
Definition qgis.h:710
@ Expression
Expression based property.
Definition qgis.h:712
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:158
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
Definition qgsfield.cpp:293
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:258
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
Definition qgsfield.cpp:239
void setName(const QString &name)
Set the field name.
Definition qgsfield.cpp:224
void setComment(const QString &comment)
Set the field comment.
Definition qgsfield.cpp:263
void setType(QMetaType::Type type)
Set variant type.
Definition qgsfield.cpp:229
void setLength(int len)
Set the field length.
Definition qgsfield.cpp:254
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
Definition qgsfield.cpp:153
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:249
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.