Skip to content

Commit a9c8a01

Browse files
committed
[GRASS] insert record for new feature
1 parent f2a1efe commit a9c8a01

2 files changed

Lines changed: 31 additions & 8 deletions

File tree

‎src/providers/grass/qgsgrassprovider.cpp‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1116,6 +1116,7 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
11161116
{
11171117
struct line_pnts *points = Vect_new_line_struct();
11181118
struct line_cats *cats = Vect_new_cats_struct();
1119+
int newCat = 0;
11191120

11201121
setPoints( points, geometry );
11211122
// TODO: get also old type if it is feature previously deleted
@@ -1130,7 +1131,6 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
11301131
// resetting fid probably is not possible because it is stored in undo commands and used in buffer maps
11311132

11321133
QgsDebugMsg( QString( "get new cat for mCidxFieldIndex = %1" ).arg( mCidxFieldIndex ) );
1133-
int newCat = 0;
11341134
if ( mCidxFieldIndex == -1 )
11351135
{
11361136
// No features with this field yet in map
@@ -1142,6 +1142,12 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
11421142
}
11431143
QgsDebugMsg( QString( "newCat = %1" ).arg( newCat ) );
11441144
Vect_cat_set( cats, mLayerField, newCat );
1145+
QString error;
1146+
mLayer->insertAttributes( newCat, error );
1147+
if ( !error.isEmpty() )
1148+
{
1149+
QgsGrass::warning( error );
1150+
}
11451151
}
11461152
}
11471153
else
@@ -1204,6 +1210,18 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
12041210
}
12051211
// TODO: create also centroid and add it to undo
12061212
}
1213+
1214+
if ( newCat > 0 )
1215+
{
1216+
if ( mLayer->hasTable() )
1217+
{
1218+
addedFeatures[fid].setAttribute( mLayer->keyColumn(), newCat );
1219+
}
1220+
else
1221+
{
1222+
addedFeatures[fid].setAttribute( 0, newCat );
1223+
}
1224+
}
12071225
}
12081226
}
12091227

‎src/providers/grass/qgsgrassvectormaplayer.cpp‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,8 @@ void QgsGrassVectorMapLayer::createTable( const QgsFields &fields, QString &erro
626626
{
627627
QgsDebugMsg( error );
628628
}
629+
mHasTable = true;
630+
mKeyColumn = 0;
629631
}
630632
QgsDebugMsg( "Table successfully created" );
631633
}
@@ -784,16 +786,19 @@ void QgsGrassVectorMapLayer::insertAttributes( int cat, QString &error )
784786
{
785787
QgsDebugMsg( QString( "mField = %1 cat = %2" ).arg( mField ).arg( cat ) );
786788

787-
QString query = QString( "INSERT INTO %1 ( %2 ) VALUES ( %3 )" ).arg( mFieldInfo->table ).arg( mFieldInfo->key ).arg( cat );
788-
executeSql( query, error );
789-
if ( error.isEmpty() )
789+
if ( mHasTable )
790790
{
791-
QList<QVariant> values;
792-
for ( int i = 0; i < mAttributeFields.size(); i++ )
791+
QString query = QString( "INSERT INTO %1 ( %2 ) VALUES ( %3 )" ).arg( mFieldInfo->table ).arg( mFieldInfo->key ).arg( cat );
792+
executeSql( query, error );
793+
if ( error.isEmpty() )
793794
{
794-
values << QVariant();
795+
QList<QVariant> values;
796+
for ( int i = 0; i < mAttributeFields.size(); i++ )
797+
{
798+
values << QVariant();
799+
}
800+
mAttributes[cat] = values;
795801
}
796-
mAttributes[cat] = values;
797802
}
798803
}
799804

0 commit comments

Comments
 (0)