Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix test on QT<5.13 and use switches
  • Loading branch information
elpaso authored and nyalldawson committed Apr 3, 2020
1 parent a21800c commit a5ce755
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 66 deletions.
101 changes: 56 additions & 45 deletions src/gui/qgsfieldmappingmodel.cpp
Expand Up @@ -94,64 +94,75 @@ QVariant QgsFieldMappingModel::data( const QModelIndex &index, int role ) const
{
if ( index.isValid() )
{
const int col { index.column() };
const ColumnDataIndex col { static_cast<ColumnDataIndex>( index.column() ) };
const Field &f { mMapping.at( index.row() ) };

const QgsFieldConstraints::Constraints constraints { fieldConstraints( f.field ) };

if ( role == Qt::DisplayRole || role == Qt::EditRole )
switch ( role )
{
switch ( static_cast<ColumnDataIndex>( col ) )
case Qt::DisplayRole:
case Qt::EditRole:
{
case ColumnDataIndex::SourceExpression:
{
return f.expression;
}
case ColumnDataIndex::DestinationName:
{
return f.field.displayName();
}
case ColumnDataIndex::DestinationType:
{
return static_cast<int>( f.field.type() );
}
case ColumnDataIndex::DestinationLength:
{
return f.field.length();
}
case ColumnDataIndex::DestinationPrecision:
switch ( col )
{
return f.field.precision();
case ColumnDataIndex::SourceExpression:
{
return f.expression;
}
case ColumnDataIndex::DestinationName:
{
return f.field.displayName();
}
case ColumnDataIndex::DestinationType:
{
return static_cast<int>( f.field.type() );
}
case ColumnDataIndex::DestinationLength:
{
return f.field.length();
}
case ColumnDataIndex::DestinationPrecision:
{
return f.field.precision();
}
case ColumnDataIndex::DestinationConstraints:
{
return constraints != 0 ? tr( "Constraints active" ) : QString();
}
}
case ColumnDataIndex::DestinationConstraints:
{
return constraints != 0 ? tr( "Constraints active" ) : QString();
}
}
}
else if ( role == Qt::ToolTipRole &&
col == static_cast<int>( ColumnDataIndex::DestinationConstraints ) &&
constraints != 0 )
{
QStringList constraintDescription;
if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintUnique ) )
{
constraintDescription.push_back( tr( "Unique" ) );
break;
}
if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
case Qt::ToolTipRole:
{
constraintDescription.push_back( tr( "Not null" ) );
if ( col == ColumnDataIndex::DestinationConstraints &&
constraints != 0 )
{
QStringList constraintDescription;
if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintUnique ) )
{
constraintDescription.push_back( tr( "Unique" ) );
}
if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
{
constraintDescription.push_back( tr( "Not null" ) );
}
if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
{
constraintDescription.push_back( tr( "Expression" ) );
}
return constraintDescription.join( QStringLiteral( "<br>" ) );
}
break;
}
if ( constraints.testFlag( QgsFieldConstraints::Constraint::ConstraintNotNull ) )
case Qt::BackgroundRole:
{
constraintDescription.push_back( tr( "Expression" ) );
if ( constraints != 0 )
{
return QBrush( QColor( 255, 224, 178 ) );
}
break;
}
return constraintDescription.join( QStringLiteral( "<br>" ) );
}
else if ( role == Qt::BackgroundRole &&
constraints != 0 )
{
return QBrush( QColor( 255, 224, 178 ) );
}
}
return QVariant();
Expand Down
49 changes: 28 additions & 21 deletions src/gui/qgsfieldmappingwidget.cpp
Expand Up @@ -94,13 +94,10 @@ void QgsFieldMappingWidget::appendField( const QgsField &field, const QString &e

bool QgsFieldMappingWidget::removeSelectedFields()
{
std::list<int> rowsToRemove;
const auto constSelection { mTableView->selectionModel()->selectedRows() };
for ( const auto &index : constSelection )
{
rowsToRemove.push_back( index.row() );
}
rowsToRemove.sort();
if ( ! mTableView->selectionModel()->hasSelection() )
return false;

std::list<int> rowsToRemove { selectedRows() };
rowsToRemove.reverse();
for ( const auto &row : rowsToRemove )
{
Expand All @@ -114,13 +111,10 @@ bool QgsFieldMappingWidget::removeSelectedFields()

bool QgsFieldMappingWidget::moveSelectedFieldsUp()
{
std::list<int> rowsToMoveUp;
const auto constSelection { mTableView->selectionModel()->selectedRows() };
for ( const auto &index : constSelection )
{
rowsToMoveUp.push_back( index.row() );
}
rowsToMoveUp.sort();
if ( ! mTableView->selectionModel()->hasSelection() )
return false;

const std::list<int> rowsToMoveUp { selectedRows() };
for ( const auto &row : rowsToMoveUp )
{
if ( ! model()->moveUp( model()->index( row, 0, QModelIndex() ) ) )
Expand All @@ -133,13 +127,10 @@ bool QgsFieldMappingWidget::moveSelectedFieldsUp()

bool QgsFieldMappingWidget::moveSelectedFieldsDown()
{
std::list<int> rowsToMoveDown;
const auto constSelection { mTableView->selectionModel()->selectedRows() };
for ( const auto &index : constSelection )
{
rowsToMoveDown.push_back( index.row() );
}
rowsToMoveDown.sort();
if ( ! mTableView->selectionModel()->hasSelection() )
return false;

std::list<int> rowsToMoveDown { selectedRows() };
rowsToMoveDown.reverse();
for ( const auto &row : rowsToMoveDown )
{
Expand All @@ -165,6 +156,22 @@ void QgsFieldMappingWidget::updateColumns()
}
}

std::list<int> QgsFieldMappingWidget::selectedRows()
{
std::list<int> rows;
if ( mTableView->selectionModel()->hasSelection() )
{
const auto constSelection { mTableView->selectionModel()->selectedIndexes() };
for ( const auto &index : constSelection )
{
rows.push_back( index.row() );
}
rows.sort();
rows.unique();
}
return rows;
}

void QgsFieldMappingWidget::ExpressionDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const
{
QgsFieldExpressionWidget *editorWidget { qobject_cast<QgsFieldExpressionWidget *>( editor ) };
Expand Down
2 changes: 2 additions & 0 deletions src/gui/qgsfieldmappingwidget.h
Expand Up @@ -99,6 +99,8 @@ class GUI_EXPORT QgsFieldMappingWidget : public QWidget, private Ui::QgsFieldMap

QAbstractTableModel *mModel;
void updateColumns();
//! Returns selected row indexes in ascending order
std::list<int> selectedRows( );

class ExpressionDelegate: public QStyledItemDelegate
{
Expand Down

0 comments on commit a5ce755

Please sign in to comment.