Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
In merge attributes dialog, don't show editor widgets in table
This was originally done to allow the table to show mapped values
such as value maps/relations. But it creates the false impression
that these values are editable, since the editor widgets are
not read only.

Instead, use the field formatter to create a value representation
string and make the body of the table read-only instead.

(cherry-picked from adf5d7)
  • Loading branch information
nyalldawson committed Apr 6, 2018
1 parent 1e29996 commit 8d71ed3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
37 changes: 16 additions & 21 deletions src/app/qgsmergeattributesdialog.cpp
Expand Up @@ -29,6 +29,8 @@
#include "qgseditorwidgetregistry.h"
#include "qgssettings.h"
#include "qgsgui.h"
#include "qgsfieldformatter.h"
#include "qgsfieldformatterregistry.h"

#include <limits>
#include <QComboBox>
Expand Down Expand Up @@ -141,8 +143,6 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
QStringList verticalHeaderLabels; //the id column is in the
verticalHeaderLabels << tr( "Id" );

QgsAttributeEditorContext context;

for ( int i = 0; i < mFeatureList.size(); ++i )
{
verticalHeaderLabels << FID_TO_STRING( mFeatureList[i].id() );
Expand All @@ -153,16 +153,13 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
{
int idx = mTableWidget->horizontalHeaderItem( j )->data( FieldIndex ).toInt();

QTableWidgetItem *attributeValItem = new QTableWidgetItem( attrs.at( idx ).toString() );
const QgsEditorWidgetSetup setup = mFields.at( idx ).editorWidgetSetup();
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
QString stringVal = formatter->representValue( mVectorLayer, idx, setup.config(), QVariant(), attrs.at( idx ) );

QTableWidgetItem *attributeValItem = new QTableWidgetItem( stringVal );
attributeValItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
mTableWidget->setItem( i + 1, j, attributeValItem );
QgsEditorWidgetWrapper *eww = QgsGui::editorWidgetRegistry()->create( mVectorLayer, idx, nullptr, mTableWidget, context );
if ( eww )
{
eww->setValue( attrs.at( idx ) );
mTableWidget->setCellWidget( i + 1, j, eww->widget() );
mTableWidget->setCellWidget( i + 1, j, eww->widget() );
}
}
}

Expand Down Expand Up @@ -317,14 +314,10 @@ void QgsMergeAttributesDialog::refreshMergedValue( int col )
}

//insert string into table widget
QTableWidgetItem *newTotalItem = new QTableWidgetItem();
newTotalItem->setData( Qt::DisplayRole, mergeResult );
newTotalItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );

//block signals to prevent table widget switching combo box to "manual" entry
mTableWidget->blockSignals( true );
mTableWidget->setItem( mTableWidget->rowCount() - 1, col, newTotalItem );
mTableWidget->blockSignals( false );
mUpdating = true; // prevent combobox changing to "manual" value
QTableWidgetItem *item = mTableWidget->item( mTableWidget->rowCount() - 1, col );
item->setData( Qt::DisplayRole, mergeResult );
mUpdating = false;
}

QVariant QgsMergeAttributesDialog::featureAttribute( QgsFeatureId featureId, int col )
Expand All @@ -337,9 +330,8 @@ QVariant QgsMergeAttributesDialog::featureAttribute( QgsFeatureId featureId, int

if ( i < mFeatureList.size() )
{
QgsEditorWidgetWrapper *wrapper = QgsEditorWidgetWrapper::fromWidget( mTableWidget->cellWidget( i + 1, col ) );
if ( wrapper )
return wrapper->value();
const QgsFeature f = mFeatureList.at( i );
return f.attributes().at( fieldIdx );
}

return QVariant( mVectorLayer->fields().at( fieldIdx ).type() );
Expand Down Expand Up @@ -498,6 +490,9 @@ void QgsMergeAttributesDialog::mRemoveFeatureFromSelectionButton_clicked()

void QgsMergeAttributesDialog::tableWidgetCellChanged( int row, int column )
{
if ( mUpdating )
return;

if ( row < mTableWidget->rowCount() - 1 )
{
//only looking for edits in the final row
Expand Down
1 change: 1 addition & 0 deletions src/app/qgsmergeattributesdialog.h
Expand Up @@ -102,6 +102,7 @@ class APP_EXPORT QgsMergeAttributesDialog: public QDialog, private Ui::QgsMergeA

QgsFields mFields;
QSet<int> mHiddenAttributes;
bool mUpdating = false;

static const QList< QgsStatisticalSummary::Statistic > DISPLAY_STATS;

Expand Down

0 comments on commit 8d71ed3

Please sign in to comment.