@@ -1297,80 +1297,104 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
1297
1297
mLayer ->map ()->newCats ()[fid] = newCat;
1298
1298
QgsDebugMsg ( QString ( " newCats[%1] = %2" ).arg ( fid ).arg ( newCat ) );
1299
1299
1300
- QString error;
1301
- // if the cat is user defined, the record may alredy exist
1302
- if ( mLayer ->attributes ().contains ( newCat ) )
1300
+ // Currently neither entering new cat nor changing existing cat is allowed
1301
+ #if 0
1302
+ // There may be other new features with the same cat which we have to update
1303
+ Q_FOREACH ( QgsFeatureId addedFid, addedFeatures.keys() )
1303
1304
{
1304
- QgsDebugMsg ( " record exists" );
1305
- // TODO: open warning dialog?
1306
- // For now we are expecting that user knows what he is doing.
1307
- // We update existing record by non null values and set feature null values to existing values
1308
- mLayer ->updateAttributes ( newCat, feature, error ); // also updates feature by existing non null attributes
1309
-
1310
- // There may be other new features with the same cat which we have to update
1311
- Q_FOREACH ( QgsFeatureId addedFid, addedFeatures.keys () )
1305
+ if ( addedFid == fid )
1312
1306
{
1313
- if ( addedFid == fid )
1314
- {
1315
- continue ;
1316
- }
1317
- int addedCat = mLayer ->map ()->newCats ().value ( addedFid ); // it should always exist
1318
- QgsDebugMsg ( QString ( " addedFid = %1 addedCat = %2" ).arg ( addedFid ).arg ( addedCat ) );
1319
- if ( addedCat == newCat )
1307
+ continue;
1308
+ }
1309
+ int addedCat = mLayer->map()->newCats().value( addedFid ); // it should always exist
1310
+ QgsDebugMsg( QString( "addedFid = %1 addedCat = %2" ).arg( addedFid ).arg( addedCat ) );
1311
+ if ( addedCat == newCat )
1312
+ {
1313
+ QgsFeature addedFeature = addedFeatures[addedFid];
1314
+ // TODO: better to update form mLayer->attributes() ?
1315
+ for ( int i = 0; i < feature.fields()->size(); i++ )
1320
1316
{
1321
- QgsFeature addedFeature = addedFeatures[addedFid];
1322
- // TODO: better to update form mLayer->attributes() ?
1323
- for ( int i = 0 ; i < feature.fields ()->size (); i++ )
1317
+ if ( feature.fields()->field( i ).name() == QgsGrassVectorMap::topoSymbolFieldName() )
1324
1318
{
1325
- if ( feature.fields ()->field ( i ).name () == QgsGrassVectorMap::topoSymbolFieldName () )
1326
- {
1327
- continue ;
1328
- }
1329
- if ( feature.attributes ().at ( i ).isNull () )
1330
- {
1331
- continue ;
1332
- }
1333
- addedFeature.setAttribute ( i, feature.attributes ().at ( i ) );
1319
+ continue;
1334
1320
}
1335
- addedFeatures[addedFid] = addedFeature;
1321
+ if ( feature.attributes().at( i ).isNull() )
1322
+ {
1323
+ continue;
1324
+ }
1325
+ addedFeature.setAttribute( i, feature.attributes().at( i ) );
1336
1326
}
1327
+ addedFeatures[addedFid] = addedFeature;
1337
1328
}
1329
+ }
1338
1330
1339
- // Update all changed attributes
1340
- // TODO: table does not get refreshed immediately
1341
- QgsChangedAttributesMap &changedAttributes = const_cast <QgsChangedAttributesMap &>( mEditBuffer ->changedAttributeValues () );
1342
- Q_FOREACH ( QgsFeatureId changedFid, changedAttributes.keys () )
1331
+ // Update all changed attributes
1332
+ QgsChangedAttributesMap &changedAttributes = const_cast<QgsChangedAttributesMap &>( mEditBuffer->changedAttributeValues() );
1333
+ Q_FOREACH ( QgsFeatureId changedFid, changedAttributes.keys() )
1334
+ {
1335
+ int changedCat = QgsGrassFeatureIterator::catFromFid( changedFid );
1336
+ int realChangedCat = changedCat;
1337
+ if ( mLayer->map()->newCats().contains( changedFid ) )
1343
1338
{
1344
- int changedCat = QgsGrassFeatureIterator::catFromFid ( changedFid );
1345
- int realChangedCat = changedCat;
1346
- if ( mLayer ->map ()->newCats ().contains ( changedFid ) )
1347
- {
1348
- realChangedCat = mLayer ->map ()->newCats ().value ( changedFid );
1349
- }
1350
- QgsDebugMsg ( QString ( " changedFid = %1 changedCat = %2 realChangedCat = %3" )
1351
- .arg ( changedFid ).arg ( changedCat ).arg ( realChangedCat ) );
1352
- if ( realChangedCat == newCat )
1339
+ realChangedCat = mLayer->map()->newCats().value( changedFid );
1340
+ }
1341
+ QgsDebugMsg( QString( "changedFid = %1 changedCat = %2 realChangedCat = %3" )
1342
+ .arg( changedFid ).arg( changedCat ).arg( realChangedCat ) );
1343
+ if ( realChangedCat == newCat )
1344
+ {
1345
+ QgsAttributeMap attributeMap = changedAttributes[changedFid];
1346
+ Q_FOREACH ( int index, attributeMap.keys() )
1353
1347
{
1354
- QgsAttributeMap attributeMap = changedAttributes[changedFid];
1355
- Q_FOREACH ( int index , attributeMap.keys () )
1356
- {
1357
- attributeMap[index ] = feature.attributes ().value ( index );
1358
- }
1359
- changedAttributes[changedFid] = attributeMap;
1348
+ attributeMap[index] = feature.attributes().value( index );
1360
1349
}
1350
+ changedAttributes[changedFid] = attributeMap;
1361
1351
}
1362
1352
}
1363
- else
1353
+ #endif
1354
+
1355
+ if ( mLayer ->hasTable () )
1364
1356
{
1365
- if ( mLayer ->hasTable () )
1357
+ QString error;
1358
+ // The record may exist if cat is manually defined by user (currently editing of cat column is disabled )
1359
+ bool recordExists = mLayer ->recordExists ( newCat, error );
1360
+ if ( !error.isEmpty () )
1366
1361
{
1367
- mLayer ->insertAttributes ( newCat, feature, error );
1362
+ QgsGrass::warning ( error );
1363
+ }
1364
+ else
1365
+ {
1366
+ error.clear ();
1367
+ if ( !recordExists )
1368
+ {
1369
+ QgsDebugMsg ( " record does not exist" );
1370
+ if ( mLayer ->attributes ().contains ( newCat ) )
1371
+ {
1372
+ QgsDebugMsg ( " attributes exist -> reinsert" );
1373
+ mLayer ->reinsertAttributes ( newCat, error );
1374
+ }
1375
+ else
1376
+ {
1377
+ mLayer ->insertAttributes ( newCat, feature, error );
1378
+ }
1379
+ }
1380
+ else
1381
+ {
1382
+ // Currently disabled
1383
+ #if 0
1384
+ // Manual entry of cat is not currently allowed
1385
+ // TODO: open warning dialog?
1386
+ // For now we are expecting that user knows what he is doing.
1387
+ // We update existing record by non null values and set feature null values to existing values
1388
+ mLayer->updateAttributes( newCat, feature, error ); // also updates feature by existing non null attributes
1389
+ #endif
1390
+ }
1368
1391
if ( !error.isEmpty () )
1369
1392
{
1370
1393
QgsGrass::warning ( error );
1371
1394
}
1372
1395
}
1373
1396
}
1397
+
1374
1398
// update table
1375
1399
emit dataChanged ();
1376
1400
}
@@ -1507,7 +1531,16 @@ void QgsGrassProvider::onFeatureDeleted( QgsFeatureId fid )
1507
1531
1508
1532
int oldLid = QgsGrassFeatureIterator::lidFromFid ( fid );
1509
1533
int cat = QgsGrassFeatureIterator::catFromFid ( fid );
1510
- int layerField = QgsGrassFeatureIterator::layerFromFid ( fid );
1534
+ int layerField = 0 ;
1535
+ if ( FID_IS_NEW ( fid ) )
1536
+ {
1537
+ layerField = mLayerField ;
1538
+ }
1539
+ else
1540
+ {
1541
+ layerField = QgsGrassFeatureIterator::layerFromFid ( fid );
1542
+ }
1543
+
1511
1544
int realLine = oldLid;
1512
1545
int realCat = cat;
1513
1546
if ( mLayer ->map ()->newLids ().contains ( oldLid ) ) // if it was changed already
0 commit comments