Skip to content

Commit c358aea

Browse files
committed
[GRASS] create new cat on attribute change if it does not exist
1 parent afdf4e1 commit c358aea

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

src/providers/grass/qgsgrassprovider.cpp

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,16 +1254,7 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
12541254
}
12551255
if ( newCat == 0 )
12561256
{
1257-
QgsDebugMsg( QString( "get new cat for cidxFieldIndex() = %1" ).arg( mLayer->cidxFieldIndex() ) );
1258-
if ( mLayer->cidxFieldIndex() == -1 )
1259-
{
1260-
// No features with this field yet in map
1261-
newCat = 1;
1262-
}
1263-
else
1264-
{
1265-
newCat = cidxGetMaxCat( mLayer->cidxFieldIndex() ) + 1;
1266-
}
1257+
newCat = getNewCat();
12671258
}
12681259
QgsDebugMsg( QString( "newCat = %1" ).arg( newCat ) );
12691260
Vect_cat_set( mCats, mLayerField, newCat );
@@ -1714,8 +1705,31 @@ void QgsGrassProvider::onAttributeValueChanged( QgsFeatureId fid, int idx, const
17141705
}
17151706
else
17161707
{
1717-
QgsDebugMsg( "no cat -> add new cat to line" );
1718-
// TODO
1708+
int newCat = getNewCat();
1709+
QgsDebugMsg( QString( "no cat -> add new cat %1 to line" ).arg( newCat ) );
1710+
int type = readLine( mPoints, mCats, realLine );
1711+
if ( type <= 0 )
1712+
{
1713+
QgsDebugMsg( "cannot read line" );
1714+
}
1715+
else
1716+
{
1717+
Vect_cat_set( mCats, mLayerField, newCat );
1718+
mLayer->map()->lockReadWrite();
1719+
int newLid = rewriteLine( realLine, type, mPoints, mCats );
1720+
Q_UNUSED( newLid )
1721+
1722+
// TODO: - store the new cat somewhere for cats mapping
1723+
QString error;
1724+
// it does insert new record if it doesn't exist
1725+
mLayer->changeAttributeValue( newCat, field, value, error );
1726+
if ( !error.isEmpty() )
1727+
{
1728+
QgsGrass::warning( error );
1729+
}
1730+
1731+
mLayer->map()->unlockReadWrite();
1732+
}
17191733
}
17201734
}
17211735
}
@@ -1877,6 +1891,20 @@ int QgsGrassProvider::cidxGetMaxCat( int idx )
18771891
return cat;
18781892
}
18791893

1894+
int QgsGrassProvider::getNewCat()
1895+
{
1896+
QgsDebugMsg( QString( "get new cat for cidxFieldIndex() = %1" ).arg( mLayer->cidxFieldIndex() ) );
1897+
if ( mLayer->cidxFieldIndex() == -1 )
1898+
{
1899+
// No features with this field yet in map
1900+
return 1;
1901+
}
1902+
else
1903+
{
1904+
return cidxGetMaxCat( mLayer->cidxFieldIndex() ) + 1;
1905+
}
1906+
}
1907+
18801908
QgsGrassVectorMapLayer * QgsGrassProvider::openLayer() const
18811909
{
18821910
return mLayer->map()->openLayer( mLayerField );

src/providers/grass/qgsgrassprovider.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ class GRASS_LIB_EXPORT QgsGrassProvider : public QgsVectorDataProvider
410410
bool openLayer();
411411
// update topo symbol of new features
412412
void setAddedFeaturesSymbol();
413+
// get new, not yet used cat
414+
int getNewCat();
413415

414416
QgsGrassObject mGrassObject;
415417
// field part of layer or -1 if no field specified

0 commit comments

Comments
 (0)