23     const QMap<QString, QString> &expressions,
 
   25   : QAbstractTableModel( parent )
 
   26   , mSourceFields( sourceFields )
 
   27   , mExpressionContextGenerator( new ExpressionContextGenerator( mSourceFields ) )
 
   34   if ( role == Qt::DisplayRole )
 
   36     switch ( orientation )
 
   44             return tr( 
"Source Expression" );
 
   56             return tr( 
"Length" );
 
   60             return tr( 
"Precision" );
 
   64             return tr( 
"Constraints" );
 
   85   if ( parent.isValid() )
 
   87   return mMapping.count();
 
   92   if ( parent.isValid() )
 
   99   if ( index.isValid() )
 
  102     const Field &f { mMapping.at( index.row() ) };
 
  104     const QgsFieldConstraints::Constraints constraints { fieldConstraints( f.field ) };
 
  108       case Qt::DisplayRole:
 
  119             return f.field.displayName();
 
  123             return static_cast<int>( f.field.type() );
 
  127             return f.field.length();
 
  131             return f.field.precision();
 
  135             return constraints != 0 ? tr( 
"Constraints active" ) : QString();
 
  140       case Qt::ToolTipRole:
 
  145           QStringList constraintDescription;
 
  146           if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintUnique ) )
 
  148             constraintDescription.push_back( tr( 
"Unique" ) );
 
  150           if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
 
  152             constraintDescription.push_back( tr( 
"Not null" ) );
 
  154           if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintExpression ) )
 
  156             constraintDescription.push_back( tr( 
"Expression" ) );
 
  158           return constraintDescription.join( QLatin1String( 
"<br>" ) );
 
  162       case Qt::BackgroundRole:
 
  164         if ( constraints != 0 )
 
  166           return QBrush( QColor( 255, 224, 178 ) );
 
  177   if ( index.isValid() &&
 
  181     return Qt::ItemFlags( Qt::ItemIsSelectable |
 
  185   return Qt::ItemFlags();
 
  190   if ( index.isValid() )
 
  192     if ( role == Qt::EditRole )
 
  194       Field &f = mMapping[index.row()];
 
  210           f.
field.
setType( 
static_cast<QVariant::Type
>( value.toInt( ) ) );
 
  216           const int length { value.toInt( &ok ) };
 
  224           const int precision { value.toInt( &ok ) };
 
  234       emit dataChanged( index, index );
 
  244 QgsFieldConstraints::Constraints QgsFieldMappingModel::fieldConstraints( 
const QgsField &
field )
 const 
  246   QgsFieldConstraints::Constraints constraints;
 
  265 bool QgsFieldMappingModel::moveUpOrDown( 
const QModelIndex &index, 
bool up )
 
  267   if ( ! index.isValid() && index.model() == 
this )
 
  271   const int row { up ? index.row() - 1 : index.row() };
 
  273   if ( row < 0 || row + 1 >= 
rowCount( QModelIndex() ) )
 
  277   beginMoveRows( QModelIndex( ), row, row, QModelIndex(), row + 2 );
 
  278 #if QT_VERSION < QT_VERSION_CHECK(5, 13, 0) 
  279   mMapping.swap( row, row + 1 );
 
  281   mMapping.swapItemsAt( row, row + 1 );
 
  287 QString QgsFieldMappingModel::findExpressionForDestinationField( 
const QgsFieldMappingModel::Field &f, QStringList &excludedFieldNames )
 
  291   for ( 
const QgsField &sf : qgis::as_const( mSourceFields ) )
 
  295       excludedFieldNames.push_back( sf.name() );
 
  300   for ( 
const QgsField &sf : qgis::as_const( mSourceFields ) )
 
  302     if ( excludedFieldNames.contains( sf.name() ) || sf.type() != f.
field.
type() )
 
  304     excludedFieldNames.push_back( sf.name() );
 
  313   if ( mExpressionContextGenerator )
 
  314     mExpressionContextGenerator->setSourceFields( mSourceFields );
 
  315   QStringList usedFields;
 
  317   for ( 
const Field &f : qgis::as_const( mMapping ) )
 
  324   for ( 
auto it = mMapping.begin(); it != mMapping.end(); ++it )
 
  326     if ( it->expression.isEmpty() )
 
  328       const QString expression { findExpressionForDestinationField( *it, usedFields ) };
 
  329       if ( ! expression.isEmpty() )
 
  330         it->expression = expression;
 
  338   return mExpressionContextGenerator.get();
 
  343   mExpressionContextGenerator->setBaseExpressionContextGenerator( generator );
 
  347     const QMap<QString, QString> &expressions )
 
  352   QStringList usedFields;
 
  353   for ( 
const QgsField &df : destinationFields )
 
  358     if ( expressions.contains( f.
field.
name() ) )
 
  363       if ( exp.isField() &&
 
  364            mSourceFields.
names().contains( qgis::setToList( exp.referencedColumns() ).first() ) )
 
  366         usedFields.push_back( qgis::setToList( exp.referencedColumns() ).first() );
 
  371       const QString expression { findExpressionForDestinationField( f, usedFields ) };
 
  372       if ( ! expression.isEmpty() )
 
  375     mMapping.push_back( f );
 
  382   return mDestinationEditable;
 
  392   static const QMap<QVariant::Type, QString> sDataTypes
 
  394     { QVariant::Type::Int, tr( 
"Whole number (integer - 32bit)" ) },
 
  395     { QVariant::Type::LongLong, tr( 
"Whole number (integer - 64bit)" ) },
 
  396     { QVariant::Type::Double, tr( 
"Decimal number (double)" ) },
 
  397     { QVariant::Type::String, tr( 
"Text (string)" ) },
 
  398     { QVariant::Type::Date, tr( 
"Date" ) },
 
  399     { QVariant::Type::Time, tr( 
"Time" ) },
 
  400     { QVariant::Type::DateTime, tr( 
"Date & Time" ) },
 
  401     { QVariant::Type::Bool, tr( 
"Boolean" ) },
 
  402     { QVariant::Type::ByteArray, tr( 
"Binary object (BLOB)" ) },
 
  414   QMap< QString, QgsProperty > fieldMap;
 
  418     const bool isField = exp.
isField();
 
  419     fieldMap.insert( 
field.originalName, isField
 
  429   for ( 
int i = 0; i < mMapping.count(); ++i )
 
  431     Field &f = mMapping[i];
 
  464   const int lastRow { 
rowCount( QModelIndex( ) ) };
 
  465   beginInsertRows( QModelIndex(), lastRow, lastRow );
 
  470   mMapping.push_back( f );
 
  476   if ( index.isValid() && index.model() == 
this && index.row() < 
rowCount( QModelIndex() ) )
 
  478     beginRemoveRows( QModelIndex(), index.row(), index.row() );
 
  479     mMapping.removeAt( index.row() );
 
  491   return moveUpOrDown( index );
 
  496   return moveUpOrDown( index, 
false );
 
  499 QgsFieldMappingModel::ExpressionContextGenerator::ExpressionContextGenerator( 
const QgsFields &sourceFields )
 
  500   : mSourceFields( sourceFields )
 
  504 QgsExpressionContext QgsFieldMappingModel::ExpressionContextGenerator::createExpressionContext()
 const 
  506   if ( mBaseGenerator )
 
  509     std::unique_ptr< QgsExpressionContextScope > fieldMappingScope = qgis::make_unique< QgsExpressionContextScope >( tr( 
"Field Mapping" ) );
 
  510     fieldMappingScope->setFields( mSourceFields );
 
  528   mBaseGenerator = generator;
 
  531 void QgsFieldMappingModel::ExpressionContextGenerator::setSourceFields( 
const QgsFields &fields )
 
  533   mSourceFields = fields;
 
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 id, geometry and a list of field/values...
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().
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.
@ SourceExpression
Expression.
@ DestinationPrecision
Destination field precision.
@ DestinationConstraints
Destination field constraints.
@ DestinationName
Destination field name.
@ DestinationType
Destination field QVariant::Type casted to (int)
@ DestinationLength
Destination field length.
Qt::ItemFlags flags(const QModelIndex &index) const override
static const QMap< QVariant::Type, QString > dataTypes()
Returns a static map of supported data types.
QgsFields sourceFields() const
Returns a list of source fields.
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
void setBaseExpressionContextGenerator(const QgsExpressionContextGenerator *generator)
Sets the base expression context generator, which will generate the expression contexts for expressio...
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.
void setPrecision(int precision)
Set the field precision.
void setName(const QString &name)
Set the field name.
void setLength(int len)
Set the field length.
void setType(QVariant::Type type)
Set variant type.
QgsFieldConstraints constraints
Container of fields for a vector layer.
QStringList names() const
Returns a list with field names.
A store for object properties.
@ ExpressionBasedProperty
Expression based property (QgsExpressionBasedProperty)
@ StaticProperty
Static property (QgsStaticProperty)
@ FieldBasedProperty
Field based property (QgsFieldBasedProperty)
@ InvalidProperty
Invalid (not set) property.
QString expressionString() const
Returns the expression used for the property value.
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.
Type propertyType() const
Returns the property type.
static QgsProperty fromField(const QString &fieldName, bool isActive=true)
Returns a new FieldBasedProperty created from the specified field name.
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.