Skip to content

Commit

Permalink
[GRASS] create new cat on attribute change if it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Oct 15, 2015
1 parent afdf4e1 commit c358aea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 12 deletions.
52 changes: 40 additions & 12 deletions src/providers/grass/qgsgrassprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1254,16 +1254,7 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
}
if ( newCat == 0 )
{
QgsDebugMsg( QString( "get new cat for cidxFieldIndex() = %1" ).arg( mLayer->cidxFieldIndex() ) );
if ( mLayer->cidxFieldIndex() == -1 )
{
// No features with this field yet in map
newCat = 1;
}
else
{
newCat = cidxGetMaxCat( mLayer->cidxFieldIndex() ) + 1;
}
newCat = getNewCat();
}
QgsDebugMsg( QString( "newCat = %1" ).arg( newCat ) );
Vect_cat_set( mCats, mLayerField, newCat );
Expand Down Expand Up @@ -1714,8 +1705,31 @@ void QgsGrassProvider::onAttributeValueChanged( QgsFeatureId fid, int idx, const
}
else
{
QgsDebugMsg( "no cat -> add new cat to line" );
// TODO
int newCat = getNewCat();
QgsDebugMsg( QString( "no cat -> add new cat %1 to line" ).arg( newCat ) );
int type = readLine( mPoints, mCats, realLine );
if ( type <= 0 )
{
QgsDebugMsg( "cannot read line" );
}
else
{
Vect_cat_set( mCats, mLayerField, newCat );
mLayer->map()->lockReadWrite();
int newLid = rewriteLine( realLine, type, mPoints, mCats );
Q_UNUSED( newLid )

// TODO: - store the new cat somewhere for cats mapping
QString error;
// it does insert new record if it doesn't exist
mLayer->changeAttributeValue( newCat, field, value, error );
if ( !error.isEmpty() )
{
QgsGrass::warning( error );
}

mLayer->map()->unlockReadWrite();
}
}
}
}
Expand Down Expand Up @@ -1877,6 +1891,20 @@ int QgsGrassProvider::cidxGetMaxCat( int idx )
return cat;
}

int QgsGrassProvider::getNewCat()
{
QgsDebugMsg( QString( "get new cat for cidxFieldIndex() = %1" ).arg( mLayer->cidxFieldIndex() ) );
if ( mLayer->cidxFieldIndex() == -1 )
{
// No features with this field yet in map
return 1;
}
else
{
return cidxGetMaxCat( mLayer->cidxFieldIndex() ) + 1;
}
}

QgsGrassVectorMapLayer * QgsGrassProvider::openLayer() const
{
return mLayer->map()->openLayer( mLayerField );
Expand Down
2 changes: 2 additions & 0 deletions src/providers/grass/qgsgrassprovider.h
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,8 @@ class GRASS_LIB_EXPORT QgsGrassProvider : public QgsVectorDataProvider
bool openLayer();
// update topo symbol of new features
void setAddedFeaturesSymbol();
// get new, not yet used cat
int getNewCat();

QgsGrassObject mGrassObject;
// field part of layer or -1 if no field specified
Expand Down

0 comments on commit c358aea

Please sign in to comment.