Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
use field widget in merge feature dialog
  • Loading branch information
3nids committed May 22, 2023
1 parent eb97cf0 commit 62387fd
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 47 deletions.
157 changes: 111 additions & 46 deletions src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -28,6 +28,7 @@
#include "qgsgui.h"
#include "qgsfieldformatter.h"
#include "qgsfieldformatterregistry.h"
#include "qgseditorwidgetwrapper.h"

#include <limits>
#include <QComboBox>
Expand Down Expand Up @@ -106,7 +107,7 @@ QgsMergeAttributesDialog::QgsMergeAttributesDialog( const QgsFeatureList &featur
}

connect( mSkipAllButton, &QAbstractButton::clicked, this, &QgsMergeAttributesDialog::setAllToSkip );
connect( mTableWidget, &QTableWidget::cellChanged, this, &QgsMergeAttributesDialog::tableWidgetCellChanged );
connect( mTableWidget, &QTableWidget::cellClicked, this, &QgsMergeAttributesDialog::tableWidgetCellClicked );

setAttributeTableConfig( mVectorLayer->attributeTableConfig() );
}
Expand Down Expand Up @@ -263,7 +264,6 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
{
refreshMergedValue( i );
}

}

QComboBox *QgsMergeAttributesDialog::createMergeComboBox( QVariant::Type columnType, int column )
Expand Down Expand Up @@ -302,6 +302,8 @@ QComboBox *QgsMergeAttributesDialog::createMergeComboBox( QVariant::Type columnT

connect( newComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int index )
{
bool isManual = newComboBox->currentData() == QStringLiteral( "manual" );
updateManualWidget( column, isManual );
refreshMergedValue( column );
} );

Expand Down Expand Up @@ -368,54 +370,59 @@ void QgsMergeAttributesDialog::refreshMergedValue( int col )
const int fieldIdx = mTableWidget->horizontalHeaderItem( col )->data( FieldIndex ).toInt();

//evaluate behavior (feature value or min / max / mean )
QTableWidgetItem *item = mTableWidget->item( mTableWidget->rowCount() - 1, col );
const QString mergeBehaviorString = comboBox->currentData().toString();
QVariant mergeResult; // result to show in the merge result field
if ( mergeBehaviorString == QLatin1String( "concat" ) )
{
mergeResult = concatenationAttribute( col );
}
else if ( mergeBehaviorString == QLatin1String( "skip" ) )
{
mergeResult = tr( "Skipped" );
}
else if ( mergeBehaviorString == QLatin1String( "manual" ) )
{
mergeResult = item->data( Qt::DisplayRole );
}
else if ( mergeBehaviorString.startsWith( 'f' ) )

if ( mergeBehaviorString == QLatin1String( "manual" ) )
{
//an existing feature value
QgsFeatureId featureId = STRING_TO_FID( mergeBehaviorString.mid( 1 ) );
mergeResult = featureAttribute( featureId, fieldIdx );
// nothing to do
}
else
{
//numerical statistic
QgsStatisticalSummary::Statistic stat = static_cast< QgsStatisticalSummary::Statistic >( comboBox->currentData().toInt() );
mergeResult = calcStatistic( fieldIdx, stat );
}
QVariant mergeResult; // result to show in the merge result field
QTableWidgetItem *item = mTableWidget->item( mTableWidget->rowCount() - 1, col );

//insert string into table widget
mUpdating = true; // prevent combobox changing to "manual" value
if ( mergeBehaviorString == QLatin1String( "concat" ) )
{
mergeResult = concatenationAttribute( col );
}
else if ( mergeBehaviorString == QLatin1String( "skip" ) )
{
mergeResult = tr( "Skipped" );
}
else if ( mergeBehaviorString.startsWith( 'f' ) )
{
//an existing feature value
QgsFeatureId featureId = STRING_TO_FID( mergeBehaviorString.mid( 1 ) );
mergeResult = featureAttribute( featureId, fieldIdx );
}
else
{
//numerical statistic
QgsStatisticalSummary::Statistic stat = static_cast< QgsStatisticalSummary::Statistic >( comboBox->currentData().toInt() );
mergeResult = calcStatistic( fieldIdx, stat );
}

// Result formatting
QString stringVal;
if ( mergeBehaviorString != QLatin1String( "skip" ) && mergeBehaviorString != QLatin1String( "manual" ) )
{
const QgsEditorWidgetSetup setup = mFields.at( fieldIdx ).editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
stringVal = formatter->representValue( mVectorLayer, fieldIdx, setup.config(), QVariant(), mergeResult );
}
else
{
stringVal = mergeResult.toString();
}
//insert string into table widget
mUpdating = true; // prevent combobox changing to "manual" value

// Result formatting
QString stringVal;
if ( mergeBehaviorString != QLatin1String( "skip" ) && mergeBehaviorString != QLatin1String( "manual" ) )
{
const QgsEditorWidgetSetup setup = mFields.at( fieldIdx ).editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
stringVal = formatter->representValue( mVectorLayer, fieldIdx, setup.config(), QVariant(), mergeResult );
}
else
{
stringVal = mergeResult.toString();
}

item->setData( Qt::DisplayRole, stringVal );
item->setData( Qt::UserRole, mergeResult );
item->setData( Qt::DisplayRole, stringVal );
item->setData( Qt::UserRole, mergeResult );

mUpdating = false;
mUpdating = false;
}
}

QVariant QgsMergeAttributesDialog::featureAttribute( QgsFeatureId featureId, int fieldIdx )
Expand Down Expand Up @@ -668,7 +675,51 @@ void QgsMergeAttributesDialog::mRemoveFeatureFromSelectionButton_clicked()
mTargetFeatureId = mFeatureList.first().id();
}

void QgsMergeAttributesDialog::tableWidgetCellChanged( int row, int column )
void QgsMergeAttributesDialog::updateManualWidget( int column, bool isManual )
{
int row = mTableWidget->rowCount() - 1;
int idx = mTableWidget->horizontalHeaderItem( column )->data( FieldIndex ).toInt();


if ( isManual )
{
QTableWidgetItem *item = mTableWidget->takeItem( row, column );
if ( item )
{
QVariant currentValue = item->data( Qt::UserRole );
delete item;

const QgsAttributeEditorContext context;
QgsEditorWidgetWrapper *eww = QgsGui::editorWidgetRegistry()->create( mVectorLayer, idx, nullptr, this, context );
QWidget *w = eww->widget();
mTableWidget->setCellWidget( row, column, w );
eww->setValue( currentValue );
}
}
else
{
QWidget *w = mTableWidget->cellWidget( row, column );
QgsEditorWidgetWrapper *eww = QgsEditorWidgetWrapper::fromWidget( w );

if ( eww )
{
QVariant currentValue = eww->value();
const QgsEditorWidgetSetup setup = mFields.at( idx ).editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
QString stringVal = formatter->representValue( mVectorLayer, idx, setup.config(), QVariant(), currentValue );

mTableWidget->removeCellWidget( row, column );

QTableWidgetItem *mergedItem = new QTableWidgetItem();
mergedItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mergedItem->setData( Qt::DisplayRole, stringVal );
mergedItem->setData( Qt::UserRole, currentValue );
mTableWidget->setItem( row, column, mergedItem );
}
}
}

void QgsMergeAttributesDialog::tableWidgetCellClicked( int row, int column )
{
if ( mUpdating )
return;
Expand All @@ -679,6 +730,8 @@ void QgsMergeAttributesDialog::tableWidgetCellChanged( int row, int column )
return;
}

updateManualWidget( column, true );

QComboBox *currentComboBox = qobject_cast<QComboBox *>( mTableWidget->cellWidget( 0, column ) );
if ( currentComboBox )
{
Expand Down Expand Up @@ -722,16 +775,28 @@ QgsAttributes QgsMergeAttributesDialog::mergedAttributes() const
if ( !comboBox )
continue;

QTableWidgetItem *currentItem = mTableWidget->item( mFeatureList.size() + 1, widgetIndex );
if ( !currentItem )
continue;

QVariant value;
QWidget *w = mTableWidget->cellWidget( mFeatureList.size() + 1, widgetIndex );
QgsEditorWidgetWrapper *eww = QgsEditorWidgetWrapper::fromWidget( w );
if ( eww )
{
value = eww->value();
}
else
{
QTableWidgetItem *currentItem = mTableWidget->item( mFeatureList.size() + 1, widgetIndex );
if ( !currentItem )
continue;
value = currentItem->data( Qt::UserRole );
}

if ( fieldIdx >= results.count() )
results.resize( fieldIdx + 1 ); // make sure the results vector is long enough (maybe not necessary)

if ( comboBox->currentData().toString() != QLatin1String( "skip" ) )
{
results[fieldIdx] = currentItem->data( Qt::UserRole );
results[fieldIdx] = value;
}
widgetIndex++;
}
Expand Down
3 changes: 2 additions & 1 deletion src/app/qgsmergeattributesdialog.h
Expand Up @@ -79,7 +79,8 @@ class APP_EXPORT QgsMergeAttributesDialog: public QDialog, private Ui::QgsMergeA
void mFromSelectedPushButton_clicked();
void mFromLargestPushButton_clicked();
void mRemoveFeatureFromSelectionButton_clicked();
void tableWidgetCellChanged( int row, int column );
void tableWidgetCellClicked( int row, int column );
void updateManualWidget( int column, bool isManual );

private:
QgsMergeAttributesDialog(); //default constructor forbidden
Expand Down

0 comments on commit 62387fd

Please sign in to comment.