Skip to content

Commit adf5d79

Browse files
committedApr 3, 2018
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.
1 parent 1ff91ce commit adf5d79

File tree

2 files changed

+17
-21
lines changed

2 files changed

+17
-21
lines changed
 

‎src/app/qgsmergeattributesdialog.cpp

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#include "qgseditorwidgetregistry.h"
3030
#include "qgssettings.h"
3131
#include "qgsgui.h"
32+
#include "qgsfieldformatter.h"
33+
#include "qgsfieldformatterregistry.h"
3234

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

144-
QgsAttributeEditorContext context;
145-
146146
for ( int i = 0; i < mFeatureList.size(); ++i )
147147
{
148148
verticalHeaderLabels << FID_TO_STRING( mFeatureList[i].id() );
@@ -153,16 +153,13 @@ void QgsMergeAttributesDialog::createTableWidgetContents()
153153
{
154154
int idx = mTableWidget->horizontalHeaderItem( j )->data( FieldIndex ).toInt();
155155

156-
QTableWidgetItem *attributeValItem = new QTableWidgetItem( attrs.at( idx ).toString() );
156+
const QgsEditorWidgetSetup setup = mFields.at( idx ).editorWidgetSetup();
157+
const QgsFieldFormatter *formatter = QgsApplication::fieldFormatterRegistry()->fieldFormatter( setup.type() );
158+
QString stringVal = formatter->representValue( mVectorLayer, idx, setup.config(), QVariant(), attrs.at( idx ) );
159+
160+
QTableWidgetItem *attributeValItem = new QTableWidgetItem( stringVal );
157161
attributeValItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable );
158162
mTableWidget->setItem( i + 1, j, attributeValItem );
159-
QgsEditorWidgetWrapper *eww = QgsGui::editorWidgetRegistry()->create( mVectorLayer, idx, nullptr, mTableWidget, context );
160-
if ( eww )
161-
{
162-
eww->setValue( attrs.at( idx ) );
163-
mTableWidget->setCellWidget( i + 1, j, eww->widget() );
164-
mTableWidget->setCellWidget( i + 1, j, eww->widget() );
165-
}
166163
}
167164
}
168165

@@ -317,14 +314,10 @@ void QgsMergeAttributesDialog::refreshMergedValue( int col )
317314
}
318315

319316
//insert string into table widget
320-
QTableWidgetItem *newTotalItem = new QTableWidgetItem();
321-
newTotalItem->setData( Qt::DisplayRole, mergeResult );
322-
newTotalItem->setFlags( Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsEditable );
323-
324-
//block signals to prevent table widget switching combo box to "manual" entry
325-
mTableWidget->blockSignals( true );
326-
mTableWidget->setItem( mTableWidget->rowCount() - 1, col, newTotalItem );
327-
mTableWidget->blockSignals( false );
317+
mUpdating = true; // prevent combobox changing to "manual" value
318+
QTableWidgetItem *item = mTableWidget->item( mTableWidget->rowCount() - 1, col );
319+
item->setData( Qt::DisplayRole, mergeResult );
320+
mUpdating = false;
328321
}
329322

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

338331
if ( i < mFeatureList.size() )
339332
{
340-
QgsEditorWidgetWrapper *wrapper = QgsEditorWidgetWrapper::fromWidget( mTableWidget->cellWidget( i + 1, col ) );
341-
if ( wrapper )
342-
return wrapper->value();
333+
const QgsFeature f = mFeatureList.at( i );
334+
return f.attributes().at( fieldIdx );
343335
}
344336

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

499491
void QgsMergeAttributesDialog::tableWidgetCellChanged( int row, int column )
500492
{
493+
if ( mUpdating )
494+
return;
495+
501496
if ( row < mTableWidget->rowCount() - 1 )
502497
{
503498
//only looking for edits in the final row

‎src/app/qgsmergeattributesdialog.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ class APP_EXPORT QgsMergeAttributesDialog: public QDialog, private Ui::QgsMergeA
102102

103103
QgsFields mFields;
104104
QSet<int> mHiddenAttributes;
105+
bool mUpdating = false;
105106

106107
static const QList< QgsStatisticalSummary::Statistic > DISPLAY_STATS;
107108

0 commit comments

Comments
 (0)
Please sign in to comment.