Skip to content

Commit

Permalink
[GRASS] insert record for new feature
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Sep 27, 2015
1 parent f2a1efe commit a9c8a01
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
20 changes: 19 additions & 1 deletion src/providers/grass/qgsgrassprovider.cpp
Expand Up @@ -1116,6 +1116,7 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
{
struct line_pnts *points = Vect_new_line_struct();
struct line_cats *cats = Vect_new_cats_struct();
int newCat = 0;

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

QgsDebugMsg( QString( "get new cat for mCidxFieldIndex = %1" ).arg( mCidxFieldIndex ) );
int newCat = 0;
if ( mCidxFieldIndex == -1 )
{
// No features with this field yet in map
Expand All @@ -1142,6 +1142,12 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
}
QgsDebugMsg( QString( "newCat = %1" ).arg( newCat ) );
Vect_cat_set( cats, mLayerField, newCat );
QString error;
mLayer->insertAttributes( newCat, error );
if ( !error.isEmpty() )
{
QgsGrass::warning( error );
}
}
}
else
Expand Down Expand Up @@ -1204,6 +1210,18 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
}
// TODO: create also centroid and add it to undo
}

if ( newCat > 0 )
{
if ( mLayer->hasTable() )
{
addedFeatures[fid].setAttribute( mLayer->keyColumn(), newCat );
}
else
{
addedFeatures[fid].setAttribute( 0, newCat );
}
}
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/providers/grass/qgsgrassvectormaplayer.cpp
Expand Up @@ -626,6 +626,8 @@ void QgsGrassVectorMapLayer::createTable( const QgsFields &fields, QString &erro
{
QgsDebugMsg( error );
}
mHasTable = true;
mKeyColumn = 0;
}
QgsDebugMsg( "Table successfully created" );
}
Expand Down Expand Up @@ -784,16 +786,19 @@ void QgsGrassVectorMapLayer::insertAttributes( int cat, QString &error )
{
QgsDebugMsg( QString( "mField = %1 cat = %2" ).arg( mField ).arg( cat ) );

QString query = QString( "INSERT INTO %1 ( %2 ) VALUES ( %3 )" ).arg( mFieldInfo->table ).arg( mFieldInfo->key ).arg( cat );
executeSql( query, error );
if ( error.isEmpty() )
if ( mHasTable )
{
QList<QVariant> values;
for ( int i = 0; i < mAttributeFields.size(); i++ )
QString query = QString( "INSERT INTO %1 ( %2 ) VALUES ( %3 )" ).arg( mFieldInfo->table ).arg( mFieldInfo->key ).arg( cat );
executeSql( query, error );
if ( error.isEmpty() )
{
values << QVariant();
QList<QVariant> values;
for ( int i = 0; i < mAttributeFields.size(); i++ )
{
values << QVariant();
}
mAttributes[cat] = values;
}
mAttributes[cat] = values;
}
}

Expand Down

0 comments on commit a9c8a01

Please sign in to comment.