25#include "moc_qgsfieldmappingmodel.cpp"
27using namespace Qt::StringLiterals;
30 : QAbstractTableModel( parent )
33 , mExpressionContextGenerator( new ExpressionContextGenerator( mSourceFields ) )
45 if ( role == Qt::DisplayRole )
47 switch ( orientation )
55 return tr(
"Source Expression" );
67 return tr(
"Length" );
71 return tr(
"Precision" );
75 return tr(
"Constraints" );
83 return tr(
"Comment" );
104 if ( parent.isValid() )
106 return mMapping.
count();
111 if ( parent.isValid() )
118 if ( index.isValid() )
121 const Field &f { mMapping.at( index.row() ) };
127 case Qt::DisplayRole:
154 return constraints != 0 ? tr(
"Constraints active" ) : QString();
167 case Qt::ToolTipRole:
171 QStringList constraintDescription;
174 constraintDescription.push_back( tr(
"Unique" ) );
178 constraintDescription.push_back( tr(
"Not null" ) );
182 constraintDescription.push_back( tr(
"Expression" ) );
184 return constraintDescription.join(
"<br>"_L1 );
188 case Qt::BackgroundRole:
190 if ( constraints != 0 )
192 return QBrush( QColor( 255, 224, 178 ) );
210 return Qt::ItemFlags( Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled );
212 return Qt::ItemFlags();
217 if ( index.isValid() )
219 if ( role == Qt::EditRole )
221 Field &f = mMapping[index.row()];
237 setFieldTypeFromName( f.
field, value.toString() );
243 const int length { value.toInt( &ok ) };
251 const int precision { value.toInt( &ok ) };
272 emit dataChanged( index, index );
303bool QgsFieldMappingModel::moveUpOrDown(
const QModelIndex &index,
bool up )
305 if ( !index.isValid() && index.model() ==
this )
309 const int row { up ? index.row() - 1 : index.row() };
311 if ( row < 0 || row + 1 >=
rowCount( QModelIndex() ) )
315 beginMoveRows( QModelIndex(), row, row, QModelIndex(), row + 2 );
316 mMapping.swapItemsAt( row, row + 1 );
321QString QgsFieldMappingModel::findExpressionForDestinationField(
const QgsFieldMappingModel::Field &f, QStringList &excludedFieldNames )
325 for (
const QgsField &sf : std::as_const( mSourceFields ) )
329 excludedFieldNames.push_back( sf.name() );
334 for (
const QgsField &sf : std::as_const( mSourceFields ) )
336 if ( excludedFieldNames.contains( sf.name() ) || sf.type() != f.
field.
type() )
338 excludedFieldNames.push_back( sf.name() );
347 if ( mExpressionContextGenerator )
348 mExpressionContextGenerator->setSourceFields( mSourceFields );
349 QStringList usedFields;
351 for (
const Field &f : std::as_const( mMapping ) )
358 for (
auto it = mMapping.begin(); it != mMapping.end(); ++it )
360 if ( it->expression.isEmpty() )
362 const QString expression { findExpressionForDestinationField( *it, usedFields ) };
363 if ( !expression.isEmpty() )
364 it->expression = expression;
372 return mExpressionContextGenerator.get();
377 mExpressionContextGenerator->setBaseExpressionContextGenerator( generator );
385 QStringList usedFields;
386 for (
const QgsField &df : destinationFields )
392 if ( expressions.contains( f.
field.
name() ) )
404 const QString expression { findExpressionForDestinationField( f, usedFields ) };
405 if ( !expression.isEmpty() )
408 mMapping.push_back( f );
415 return mDestinationEditable;
425 static const QMap<QMetaType::Type, QString> sDataTypes {
441 static const QList<QgsVectorDataProvider::NativeType> sDataTypes
442 = QList<QgsVectorDataProvider::NativeType>()
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 );
462QString QgsFieldMappingModel::qgsFieldToTypeName(
const QgsField &field )
const
466 if ( type.mType == field.
type() && type.mSubType == field.
subType() )
468 return type.mTypeName;
473 for (
const QgsVectorDataProvider::NativeType &type : mNativeTypes )
475 if ( type.mType == field.
type() && type.mSubType == field.
subType() )
477 return type.mTypeName;
484 if ( field.
type() == QMetaType::Type::Int )
486 for (
const QgsVectorDataProvider::NativeType &type : mNativeTypes )
488 if ( type.mType == QMetaType::Type::LongLong )
490 return type.mTypeName;
495 if ( field.
type() == QMetaType::Type::UInt )
497 for (
const QgsVectorDataProvider::NativeType &type : mNativeTypes )
499 if ( type.mType == QMetaType::Type::ULongLong || type.mType == QMetaType::Type::LongLong )
501 return type.mTypeName;
506 if ( field.
type() == QMetaType::Type::Float || field.
type() == QMetaType::Type::Int )
508 for (
const QgsVectorDataProvider::NativeType &type : mNativeTypes )
510 if ( type.mType == QMetaType::Type::Double )
512 return type.mTypeName;
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 )
527 for (
const QgsVectorDataProvider::NativeType &type : mNativeTypes )
529 if ( type.mType == QMetaType::Type::QString )
531 return type.mTypeName;
539void QgsFieldMappingModel::setFieldTypeFromName(
QgsField &field,
const QString &name )
const
541 for (
const QgsVectorDataProvider::NativeType &type : mNativeTypes )
543 if ( type.mTypeName == name )
560 QMap<QString, QgsProperty> fieldMap;
564 const bool isField = exp.
isField();
573 for (
int i = 0; i < mMapping.count(); ++i )
575 Field &f = mMapping[i];
608 const int lastRow {
rowCount( QModelIndex() ) };
609 beginInsertRows( QModelIndex(), lastRow, lastRow );
615 mMapping.push_back( f );
621 if ( index.isValid() && index.model() ==
this && index.row() <
rowCount( QModelIndex() ) )
623 beginRemoveRows( QModelIndex(), index.row(), index.row() );
624 mMapping.removeAt( index.row() );
636 return moveUpOrDown( index );
641 return moveUpOrDown( index,
false );
644QgsFieldMappingModel::ExpressionContextGenerator::ExpressionContextGenerator(
const QgsFields &sourceFields )
645 : mSourceFields( sourceFields )
650 if ( mBaseGenerator )
652 QgsExpressionContext ctx = mBaseGenerator->createExpressionContext();
653 auto fieldMappingScope = std::make_unique<QgsExpressionContextScope>( tr(
"Field Mapping" ) );
654 fieldMappingScope->setFields( mSourceFields );
660 QgsExpressionContext ctx;
663 QgsFeature feature { mSourceFields };
670void QgsFieldMappingModel::ExpressionContextGenerator::setBaseExpressionContextGenerator(
const QgsExpressionContextGenerator *generator )
672 mBaseGenerator = generator;
675void QgsFieldMappingModel::ExpressionContextGenerator::setSourceFields(
const QgsFields &fields )
677 mSourceFields = fields;
@ Invalid
Invalid (not set) property.
@ Field
Field based property.
@ Expression
Expression based property.
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.
@ DestinationAlias
Destination alias.
@ SourceExpression
Expression.
@ 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.
QString typeName() const
Gets the field type.
void setAlias(const QString &alias)
Sets the alias for the field (the friendly displayed name of the field ).
void setPrecision(int precision)
Set the field precision.
void setSubType(QMetaType::Type subType)
If the field is a collection, set its element's type.
void setName(const QString &name)
Set the field name.
void setComment(const QString &comment)
Set the field comment.
void setType(QMetaType::Type type)
Set variant type.
void setLength(int len)
Set the field length.
QMetaType::Type subType() const
If the field is a collection, gets its element's type.
QgsFieldConstraints constraints
void setTypeName(const QString &typeName)
Set the field type.
Container of fields for a vector layer.
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.