Skip to content

Commit e223bcc

Browse files
committed
[GRASS] initial editing test
1 parent 3dcabfb commit e223bcc

File tree

10 files changed

+414
-37
lines changed

10 files changed

+414
-37
lines changed

src/providers/grass/qgsgrass.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2124,6 +2124,35 @@ bool QgsGrass::deleteObjectDialog( const QgsGrassObject & object )
21242124
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes;
21252125
}
21262126

2127+
void QgsGrass::createVectorMap( const QgsGrassObject & object, QString &error )
2128+
{
2129+
QgsDebugMsg( "entered" );
2130+
2131+
QgsGrass::setMapset( object );
2132+
2133+
struct Map_info *Map = 0;
2134+
QgsGrass::lock();
2135+
G_TRY
2136+
{
2137+
Map = vectNewMapStruct();
2138+
Vect_open_new( Map, object.name().toUtf8().data(), 0 );
2139+
2140+
#if ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR >= 4 ) || GRASS_VERSION_MAJOR > 6
2141+
Vect_build( Map );
2142+
#else
2143+
Vect_build( Map, stderr );
2144+
#endif
2145+
Vect_set_release_support( Map );
2146+
Vect_close( Map );
2147+
}
2148+
G_CATCH( QgsGrass::Exception &e )
2149+
{
2150+
error = tr( "Cannot create new vector: %1" ).arg( e.what() );
2151+
}
2152+
QgsGrass::vectDestroyMapStruct( Map );
2153+
QgsGrass::unlock();
2154+
}
2155+
21272156
void QgsGrass::createTable( dbDriver *driver, const QString tableName, const QgsFields &fields )
21282157
{
21292158
if ( !driver ) // should not happen

src/providers/grass/qgsgrass.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
459459
*/
460460
static bool deleteObjectDialog( const QgsGrassObject & object );
461461

462+
/** Create new vector map
463+
* @param object GRASS object specifying location/mapset/map
464+
* @param error */
465+
static void createVectorMap( const QgsGrassObject & object, QString &error );
466+
462467
/** Create new table. Throws QgsGrass::Exception */
463468
static void createTable( dbDriver *driver, const QString tableName, const QgsFields &fields );
464469

src/providers/grass/qgsgrassprovider.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,14 @@ const QgsFields & QgsGrassProvider::fields() const
418418
else
419419
{
420420
// Original fields must be returned during editing because edit buffer updates fields by indices
421-
return mLayer->fields();
421+
if ( mEditBuffer )
422+
{
423+
return mLayer->fields();
424+
}
425+
else
426+
{
427+
return mLayer->tableFields();
428+
}
422429
}
423430
}
424431

@@ -1454,6 +1461,8 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
14541461

14551462
setAddedFeaturesSymbol();
14561463
}
1464+
QgsDebugMsg( QString( "mCidxFieldIndex = %1 cidxFieldNumCats() = %2" )
1465+
.arg( mCidxFieldIndex ).arg( mLayer->cidxFieldNumCats() ) );
14571466
}
14581467

14591468
void QgsGrassProvider::onFeatureDeleted( QgsFeatureId fid )

src/providers/grass/qgsgrassprovidermodule.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -211,31 +211,18 @@ QString QgsGrassItemActions::newVectorMap()
211211
QString name = dialog.name();
212212
QgsDebugMsg( "name = " + name );
213213

214-
QgsGrass::setMapset( mGrassObject );
214+
QgsGrassObject mapObject = mGrassObject;
215+
mapObject.setName( name );
216+
mapObject.setType( QgsGrassObject::Vector );
215217

216-
struct Map_info *Map = 0;
217-
QgsGrass::lock();
218-
G_TRY
219-
{
220-
Map = QgsGrass::vectNewMapStruct();
221-
Vect_open_new( Map, dialog.name().toUtf8().data(), 0 );
222-
223-
#if ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR >= 4 ) || GRASS_VERSION_MAJOR > 6
224-
Vect_build( Map );
225-
#else
226-
Vect_build( Map, stderr );
227-
#endif
228-
Vect_set_release_support( Map );
229-
Vect_close( Map );
230-
QgsGrass::vectDestroyMapStruct( Map );
231-
}
232-
G_CATCH( QgsGrass::Exception &e )
218+
QString error;
219+
220+
QgsGrass::createVectorMap( mapObject, error );
221+
if ( !error.isEmpty() )
233222
{
234-
QgsGrass::warning( tr( "Cannot create new vector: %1" ).arg( e.what() ) );
223+
QgsGrass::warning( error );
235224
name = "";
236-
QgsGrass::vectDestroyMapStruct( Map );
237225
}
238-
QgsGrass::unlock();
239226
return name;
240227
}
241228

src/providers/grass/qgsgrassvectormap.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -702,6 +702,8 @@ void QgsGrassVectorMap::closeAllIterators()
702702
}
703703

704704
//------------------------------------ QgsGrassVectorMapStore ------------------------------------
705+
QgsGrassVectorMapStore * QgsGrassVectorMapStore::mStore = 0;
706+
705707
QgsGrassVectorMapStore::QgsGrassVectorMapStore()
706708
{
707709
}
@@ -714,6 +716,10 @@ QgsGrassVectorMapStore::~QgsGrassVectorMapStore()
714716
QgsGrassVectorMapStore *QgsGrassVectorMapStore::instance()
715717
{
716718
static QgsGrassVectorMapStore instance;
719+
if ( mStore )
720+
{
721+
return mStore;
722+
}
717723
return &instance;
718724
}
719725

src/providers/grass/qgsgrassvectormap.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ class QgsGrassVectorMapStore
211211

212212
static QgsGrassVectorMapStore *instance();
213213

214+
// Default instance may be overriden explicitely to avoid (temporarily) to share maps by providers
215+
// This is only used for editing test to have an independent map
216+
static void setStore( QgsGrassVectorMapStore * store ) { mStore = store; }
217+
214218
/** Open map.
215219
* @param grassObject
216220
* @return map, the map may be invalide */
@@ -222,6 +226,8 @@ class QgsGrassVectorMapStore
222226

223227
// Lock open/close map
224228
QMutex mMutex;
229+
230+
static QgsGrassVectorMapStore * mStore;
225231
};
226232

227233
#endif // QGSGRASSVECTORMAP_H

src/providers/grass/qgsgrassvectormaplayer.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ QgsGrassVectorMapLayer::QgsGrassVectorMapLayer( QgsGrassVectorMap *map, int fiel
4040
: mField( field )
4141
, mValid( false )
4242
, mMap( map )
43-
, mCidxFieldIndex( -1 )
4443
, mFieldInfo( 0 )
4544
, mDriver( 0 )
4645
, mHasTable( false )
@@ -52,7 +51,6 @@ QgsGrassVectorMapLayer::QgsGrassVectorMapLayer( QgsGrassVectorMap *map, int fiel
5251
void QgsGrassVectorMapLayer::clear()
5352
{
5453
QgsDebugMsg( "entered" );
55-
mCidxFieldIndex = -1;
5654
mTableFields.clear();
5755
mFields.clear();
5856
mAttributeFields.clear();
@@ -64,13 +62,22 @@ void QgsGrassVectorMapLayer::clear()
6462
mFieldInfo = 0;
6563
}
6664

65+
int QgsGrassVectorMapLayer::cidxFieldIndex()
66+
{
67+
if ( !mMap->map() )
68+
{
69+
return -1;
70+
}
71+
return Vect_cidx_get_field_index( mMap->map(), mField );
72+
}
73+
6774
int QgsGrassVectorMapLayer::cidxFieldNumCats()
6875
{
69-
if ( !mMap->map() || mCidxFieldIndex < 0 )
76+
if ( !mMap->map() || cidxFieldIndex() < 0 )
7077
{
7178
return 0;
7279
}
73-
return Vect_cidx_get_num_cats_by_index( mMap->map(), mCidxFieldIndex );
80+
return Vect_cidx_get_num_cats_by_index( mMap->map(), cidxFieldIndex() );
7481
}
7582

7683
void QgsGrassVectorMapLayer::load()
@@ -89,8 +96,7 @@ void QgsGrassVectorMapLayer::load()
8996
return;
9097
}
9198

92-
mCidxFieldIndex = Vect_cidx_get_field_index( mMap->map(), mField );
93-
QgsDebugMsg( QString( "mCidxFieldIndex = %1 cidxFieldNumCats() = %2" ).arg( cidxFieldNumCats() ) );
99+
QgsDebugMsg( QString( "cidxFieldIndex() = %1 cidxFieldNumCats() = %2" ).arg( cidxFieldIndex() ).arg( cidxFieldNumCats() ) );
94100

95101
mFieldInfo = Vect_get_field( mMap->map(), mField ); // should work also with field = 0
96102

@@ -289,18 +295,18 @@ void QgsGrassVectorMapLayer::load()
289295
mTableFields.append( QgsField( "cat", QVariant::Int, "integer" ) );
290296
QPair<double, double> minMax( 0, 0 );
291297

292-
if ( mCidxFieldIndex >= 0 )
298+
if ( cidxFieldIndex() >= 0 )
293299
{
294300
int ncats, cat, type, id;
295301

296-
ncats = Vect_cidx_get_num_cats_by_index( mMap->map(), mCidxFieldIndex );
302+
ncats = Vect_cidx_get_num_cats_by_index( mMap->map(), cidxFieldIndex() );
297303

298304
if ( ncats > 0 )
299305
{
300-
Vect_cidx_get_cat_by_index( mMap->map(), mCidxFieldIndex, 0, &cat, &type, &id );
306+
Vect_cidx_get_cat_by_index( mMap->map(), cidxFieldIndex(), 0, &cat, &type, &id );
301307
minMax.first = cat;
302308

303-
Vect_cidx_get_cat_by_index( mMap->map(), mCidxFieldIndex, ncats - 1, &cat, &type, &id );
309+
Vect_cidx_get_cat_by_index( mMap->map(), cidxFieldIndex(), ncats - 1, &cat, &type, &id );
304310
minMax.second = cat;
305311
}
306312
}

src/providers/grass/qgsgrassvectormaplayer.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
5151
bool isValid() const { return mValid; }
5252
QgsGrassVectorMap *map() { return mMap; }
5353

54+
/** Category index index */
55+
int cidxFieldIndex();
56+
5457
/** Current number of cats in cat index, changing during editing */
5558
int cidxFieldNumCats();
5659

@@ -59,6 +62,10 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
5962
* Original fields must be returned by provider fields() */
6063
QgsFields & fields() { return mFields; }
6164

65+
/** Current fields, as modified during editing, it contains cat field, without topo field.
66+
* This fields are used by layers which are not editied to reflect current state of editing. */
67+
QgsFields & tableFields() { return mTableFields; }
68+
6269
static QStringList fieldNames( QgsFields & fields );
6370

6471
QMap<int, QList<QVariant> > & attributes() { return mAttributes; }
@@ -159,7 +166,6 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
159166
int mField;
160167
bool mValid;
161168
QgsGrassVectorMap *mMap;
162-
int mCidxFieldIndex;
163169
struct field_info *mFieldInfo;
164170
dbDriver *mDriver;
165171

tests/src/providers/grass/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
ADD_DEFINITIONS("-DGRASS_EXPORT=${DLLIMPORT} -DGRASS_LIB_EXPORT=${DLLIMPORT}")
22

3-
INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/providers/grass)
3+
INCLUDE_DIRECTORIES(
4+
${CMAKE_SOURCE_DIR}/src/providers/grass
5+
${GDAL_INCLUDE_DIR}
6+
${PROJ_INCLUDE_DIR}
7+
${GEOS_INCLUDE_DIR}
8+
${POSTGRES_INCLUDE_DIR}
9+
)
410

511

612
MACRO (ADD_QGIS_GRASS_TEST grass_build_version testname testsrc)

0 commit comments

Comments
 (0)