void testUpdate() { const QgsKeyValueWidgetFactory factory( QStringLiteral( "testKeyValue" ) ); QgsEditorWidgetWrapper* wrapper = factory.create( nullptr, 0, nullptr, nullptr ); QVERIFY( wrapper ); QSignalSpy spy( wrapper, SIGNAL( valueChanged( const QVariant& ) ) ); QgsKeyValueWidget* widget = qobject_cast< QgsKeyValueWidget* >( wrapper->widget() ); QVERIFY( widget ); QVariantMap initial; initial[QStringLiteral( "1" )] = "one"; initial[QStringLiteral( "2" )] = "two"; wrapper->setValue( initial ); const QVariant value = wrapper->value(); QCOMPARE( int( value.type() ), int( QVariant::Map ) ); QCOMPARE( value.toMap(), initial ); QCOMPARE( spy.count(), 0 ); QAbstractItemModel* model = widget->tableView->model(); model->setData( model->index( 0, 1 ), "hello" ); QCOMPARE( spy.count(), 1 ); QVariantMap expected = initial; expected[QStringLiteral( "1" )] = "hello"; QVariant eventValue = spy.at( 0 ).at( 0 ).value<QVariant>(); QCOMPARE( int( eventValue.type() ), int( QVariant::Map ) ); QCOMPARE( eventValue.toMap(), expected ); QCOMPARE( wrapper->value().toMap(), expected ); QCOMPARE( spy.count(), 1 ); }
void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const { QgsVectorLayer *vl = layer( model ); if ( !vl ) return; int fieldIdx = model->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt(); QgsFeatureId fid = model->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong(); QVariant oldValue = model->data( index, Qt::EditRole ); QVariant newValue; QgsEditorWidgetWrapper *eww = QgsEditorWidgetWrapper::fromWidget( editor ); if ( !eww ) return; newValue = eww->value(); if ( ( oldValue != newValue && newValue.isValid() ) || oldValue.isNull() != newValue.isNull() ) { // This fixes https://issues.qgis.org/issues/16492 QgsFeatureRequest request( fid ); request.setFlags( QgsFeatureRequest::NoGeometry ); request.setSubsetOfAttributes( QgsAttributeList() ); QgsFeature feature; vl->getFeatures( request ).nextFeature( feature ); if ( feature.isValid() ) { vl->beginEditCommand( tr( "Attribute changed" ) ); vl->changeAttributeValue( fid, fieldIdx, newValue, oldValue ); vl->endEditCommand(); } } }
void QgsAttributeTableDelegate::setModelData( QWidget *editor, QAbstractItemModel *model, const QModelIndex &index ) const { QgsVectorLayer *vl = layer( model ); if ( vl == NULL ) return; int fieldIdx = model->data( index, QgsAttributeTableModel::FieldIndexRole ).toInt(); QgsFeatureId fid = model->data( index, QgsAttributeTableModel::FeatureIdRole ).toLongLong(); QVariant oldValue = model->data( index, Qt::EditRole ); QVariant newValue; QgsEditorWidgetWrapper* eww = QgsEditorWidgetWrapper::fromWidget( editor ); if ( !eww ) return; newValue = eww->value(); if (( oldValue != newValue && newValue.isValid() ) || oldValue.isNull() != newValue.isNull() ) { vl->beginEditCommand( tr( "Attribute changed" ) ); vl->changeAttributeValue( fid, fieldIdx, newValue, oldValue ); vl->endEditCommand(); } }