Skip to content

Commit d2564bb

Browse files
committedOct 20, 2015
[GRASS] editing fix
1 parent f5a93ef commit d2564bb

File tree

4 files changed

+126
-57
lines changed

4 files changed

+126
-57
lines changed
 

‎src/providers/grass/qgsgrassprovider.cpp

Lines changed: 87 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1297,80 +1297,104 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
12971297
mLayer->map()->newCats()[fid] = newCat;
12981298
QgsDebugMsg( QString( "newCats[%1] = %2" ).arg( fid ).arg( newCat ) );
12991299

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() )
13031304
{
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 )
13121306
{
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++ )
13201316
{
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() )
13241318
{
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;
13341320
}
1335-
addedFeatures[addedFid] = addedFeature;
1321+
if ( feature.attributes().at( i ).isNull() )
1322+
{
1323+
continue;
1324+
}
1325+
addedFeature.setAttribute( i, feature.attributes().at( i ) );
13361326
}
1327+
addedFeatures[addedFid] = addedFeature;
13371328
}
1329+
}
13381330

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 ) )
13431338
{
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() )
13531347
{
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 );
13601349
}
1350+
changedAttributes[changedFid] = attributeMap;
13611351
}
13621352
}
1363-
else
1353+
#endif
1354+
1355+
if ( mLayer->hasTable() )
13641356
{
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() )
13661361
{
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+
}
13681391
if ( !error.isEmpty() )
13691392
{
13701393
QgsGrass::warning( error );
13711394
}
13721395
}
13731396
}
1397+
13741398
// update table
13751399
emit dataChanged();
13761400
}
@@ -1507,7 +1531,16 @@ void QgsGrassProvider::onFeatureDeleted( QgsFeatureId fid )
15071531

15081532
int oldLid = QgsGrassFeatureIterator::lidFromFid( fid );
15091533
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+
15111544
int realLine = oldLid;
15121545
int realCat = cat;
15131546
if ( mLayer->map()->newLids().contains( oldLid ) ) // if it was changed already

‎src/providers/grass/qgsgrassvectormap.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class GRASS_LIB_EXPORT QgsGrassVectorMap : public QObject
8181
QHash<int, int> & newLids() { return mNewLids; }
8282
QHash<int, QgsAbstractGeometryV2*> & oldGeometries() { return mOldGeometries; }
8383
QHash<int, int> & oldTypes() { return mOldTypes; }
84-
QHash<int, int> & newCats() { return mNewCats; }
84+
QHash<QgsFeatureId, int> & newCats() { return mNewCats; }
8585

8686
/** Get geometry of line.
8787
* @return geometry (point,line or polygon(GV_FACE)) or 0 */
@@ -193,7 +193,7 @@ class GRASS_LIB_EXPORT QgsGrassVectorMap : public QObject
193193
QHash<int, int> mOldTypes;
194194
// New categories attached to new features or old features without category
195195
// fid -> cat, the fid may be old fid without category or new (negative) feature id
196-
QHash<int, int> mNewCats;
196+
QHash<QgsFeatureId, int> mNewCats;
197197

198198
// Mutex used to avoid concurrent read/write, used only in editing mode
199199
QMutex mReadWriteMutex;

‎src/providers/grass/qgsgrassvectormaplayer.cpp

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,12 +696,14 @@ void QgsGrassVectorMapLayer::addColumn( const QgsField &field, QString &error )
696696
{
697697
// the column is already in attributes (delete column undo)
698698
QgsDebugMsg( "insert old values" );
699+
printCachedAttributes();
699700
QStringList errors;
700701
Q_FOREACH ( int cat, mAttributes.keys() )
701702
{
702703
QVariant value = mAttributes.value( cat ).value( index );
703704
QString valueString = quotedValue( value );
704-
QString query = QString( "UPDATE %1 SET %2 = %3" ).arg( mFieldInfo->table, field.name(), valueString );
705+
QString query = QString( "UPDATE %1 SET %2 = %3 WHERE %4 = %5" )
706+
.arg( mFieldInfo->table ).arg( field.name() ).arg( valueString ).arg( keyColumnName() ).arg( cat );
705707
QString err;
706708
executeSql( query, err );
707709
if ( !err.isEmpty() )
@@ -856,6 +858,7 @@ void QgsGrassVectorMapLayer::insertAttributes( int cat, const QgsFeature &featur
856858
}
857859
mAttributes[cat] = values;
858860
}
861+
printCachedAttributes();
859862
}
860863

861864
void QgsGrassVectorMapLayer::reinsertAttributes( int cat, QString &error )
@@ -902,10 +905,12 @@ void QgsGrassVectorMapLayer::reinsertAttributes( int cat, QString &error )
902905
{
903906
QgsDebugMsg( "cat not found in mAttributes -> don't restore" );
904907
}
908+
printCachedAttributes();
905909
}
906910

907911
void QgsGrassVectorMapLayer::updateAttributes( int cat, QgsFeature &feature, QString &error, bool nullValues )
908912
{
913+
Q_UNUSED( nullValues )
909914
QgsDebugMsg( QString( "mField = %1 cat = %2" ).arg( mField ).arg( cat ) );
910915

911916
if ( !mHasTable )
@@ -933,6 +938,8 @@ void QgsGrassVectorMapLayer::updateAttributes( int cat, QgsFeature &feature, QSt
933938

934939
int cacheIndex = mAttributeFields.indexFromName( name );
935940

941+
// Merging old and new attributes currently not allowed (entering changing cat)
942+
#if 0
936943
if ( valueVariant.isNull() && !nullValues )
937944
{
938945
// update feature null values by existing values
@@ -942,6 +949,7 @@ void QgsGrassVectorMapLayer::updateAttributes( int cat, QgsFeature &feature, QSt
942949
}
943950
continue;
944951
}
952+
#endif
945953

946954
updates << name + " = " + quotedValue( valueVariant );
947955

@@ -973,6 +981,7 @@ void QgsGrassVectorMapLayer::updateAttributes( int cat, QgsFeature &feature, QSt
973981
mAttributes[cat][index] = cacheUpdates[index];
974982
}
975983
}
984+
printCachedAttributes();
976985
}
977986

978987
void QgsGrassVectorMapLayer::deleteAttribute( int cat, QString &error )
@@ -1123,4 +1132,28 @@ void QgsGrassVectorMapLayer::changeAttributeValue( int cat, QgsField field, QVar
11231132
return;
11241133
}
11251134
mAttributes[cat][index] = value;
1135+
printCachedAttributes();
1136+
}
1137+
1138+
void QgsGrassVectorMapLayer::printCachedAttributes()
1139+
{
1140+
#ifdef QGISDEBUG
1141+
QgsDebugMsgLevel( QString( "mAttributes.size() = %1" ).arg( mAttributes.size() ), 4 );
1142+
QStringList names;
1143+
for ( int i = 0; i < mAttributeFields.size(); i++ )
1144+
{
1145+
names << mAttributeFields[i].name();
1146+
}
1147+
QgsDebugMsgLevel( names.join( "|" ), 4 );
1148+
1149+
Q_FOREACH ( int cat, mAttributes.keys() )
1150+
{
1151+
QStringList values;
1152+
for ( int i = 0; i < mAttributes.value( cat ).size(); i++ )
1153+
{
1154+
values << mAttributes.value( cat ).value( i ).toString();
1155+
}
1156+
QgsDebugMsgLevel( QString( "cat = %1 : %2" ).arg( cat ).arg( values.join( "|" ) ), 4 );
1157+
}
1158+
#endif
11261159
}

‎src/providers/grass/qgsgrassvectormaplayer.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
159159
// update fields to real state
160160
void updateFields();
161161

162+
// for debug only
163+
void printCachedAttributes();
164+
162165
private:
163166
QString quotedValue( QVariant value );
164167
dbDriver * openDriver( QString &error );

0 commit comments

Comments
 (0)
Please sign in to comment.