Skip to content

Commit

Permalink
[GRASS] initial editing test
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Oct 7, 2015
1 parent 3dcabfb commit e223bcc
Show file tree
Hide file tree
Showing 10 changed files with 414 additions and 37 deletions.
29 changes: 29 additions & 0 deletions src/providers/grass/qgsgrass.cpp
Expand Up @@ -2124,6 +2124,35 @@ bool QgsGrass::deleteObjectDialog( const QgsGrassObject & object )
QMessageBox::Yes | QMessageBox::No ) == QMessageBox::Yes;
}

void QgsGrass::createVectorMap( const QgsGrassObject & object, QString &error )
{
QgsDebugMsg( "entered" );

QgsGrass::setMapset( object );

struct Map_info *Map = 0;
QgsGrass::lock();
G_TRY
{
Map = vectNewMapStruct();
Vect_open_new( Map, object.name().toUtf8().data(), 0 );

#if ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR >= 4 ) || GRASS_VERSION_MAJOR > 6
Vect_build( Map );
#else
Vect_build( Map, stderr );
#endif
Vect_set_release_support( Map );
Vect_close( Map );
}
G_CATCH( QgsGrass::Exception &e )
{
error = tr( "Cannot create new vector: %1" ).arg( e.what() );
}
QgsGrass::vectDestroyMapStruct( Map );
QgsGrass::unlock();
}

void QgsGrass::createTable( dbDriver *driver, const QString tableName, const QgsFields &fields )
{
if ( !driver ) // should not happen
Expand Down
5 changes: 5 additions & 0 deletions src/providers/grass/qgsgrass.h
Expand Up @@ -459,6 +459,11 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
*/
static bool deleteObjectDialog( const QgsGrassObject & object );

/** Create new vector map
* @param object GRASS object specifying location/mapset/map
* @param error */
static void createVectorMap( const QgsGrassObject & object, QString &error );

/** Create new table. Throws QgsGrass::Exception */
static void createTable( dbDriver *driver, const QString tableName, const QgsFields &fields );

Expand Down
11 changes: 10 additions & 1 deletion src/providers/grass/qgsgrassprovider.cpp
Expand Up @@ -418,7 +418,14 @@ const QgsFields & QgsGrassProvider::fields() const
else
{
// Original fields must be returned during editing because edit buffer updates fields by indices
return mLayer->fields();
if ( mEditBuffer )
{
return mLayer->fields();
}
else
{
return mLayer->tableFields();
}
}
}

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

setAddedFeaturesSymbol();
}
QgsDebugMsg( QString( "mCidxFieldIndex = %1 cidxFieldNumCats() = %2" )
.arg( mCidxFieldIndex ).arg( mLayer->cidxFieldNumCats() ) );
}

void QgsGrassProvider::onFeatureDeleted( QgsFeatureId fid )
Expand Down
29 changes: 8 additions & 21 deletions src/providers/grass/qgsgrassprovidermodule.cpp
Expand Up @@ -211,31 +211,18 @@ QString QgsGrassItemActions::newVectorMap()
QString name = dialog.name();
QgsDebugMsg( "name = " + name );

QgsGrass::setMapset( mGrassObject );
QgsGrassObject mapObject = mGrassObject;
mapObject.setName( name );
mapObject.setType( QgsGrassObject::Vector );

struct Map_info *Map = 0;
QgsGrass::lock();
G_TRY
{
Map = QgsGrass::vectNewMapStruct();
Vect_open_new( Map, dialog.name().toUtf8().data(), 0 );

#if ( GRASS_VERSION_MAJOR == 6 && GRASS_VERSION_MINOR >= 4 ) || GRASS_VERSION_MAJOR > 6
Vect_build( Map );
#else
Vect_build( Map, stderr );
#endif
Vect_set_release_support( Map );
Vect_close( Map );
QgsGrass::vectDestroyMapStruct( Map );
}
G_CATCH( QgsGrass::Exception &e )
QString error;

QgsGrass::createVectorMap( mapObject, error );
if ( !error.isEmpty() )
{
QgsGrass::warning( tr( "Cannot create new vector: %1" ).arg( e.what() ) );
QgsGrass::warning( error );
name = "";
QgsGrass::vectDestroyMapStruct( Map );
}
QgsGrass::unlock();
return name;
}

Expand Down
6 changes: 6 additions & 0 deletions src/providers/grass/qgsgrassvectormap.cpp
Expand Up @@ -702,6 +702,8 @@ void QgsGrassVectorMap::closeAllIterators()
}

//------------------------------------ QgsGrassVectorMapStore ------------------------------------
QgsGrassVectorMapStore * QgsGrassVectorMapStore::mStore = 0;

QgsGrassVectorMapStore::QgsGrassVectorMapStore()
{
}
Expand All @@ -714,6 +716,10 @@ QgsGrassVectorMapStore::~QgsGrassVectorMapStore()
QgsGrassVectorMapStore *QgsGrassVectorMapStore::instance()
{
static QgsGrassVectorMapStore instance;
if ( mStore )
{
return mStore;
}
return &instance;
}

Expand Down
6 changes: 6 additions & 0 deletions src/providers/grass/qgsgrassvectormap.h
Expand Up @@ -211,6 +211,10 @@ class QgsGrassVectorMapStore

static QgsGrassVectorMapStore *instance();

// Default instance may be overriden explicitely to avoid (temporarily) to share maps by providers
// This is only used for editing test to have an independent map
static void setStore( QgsGrassVectorMapStore * store ) { mStore = store; }

/** Open map.
* @param grassObject
* @return map, the map may be invalide */
Expand All @@ -222,6 +226,8 @@ class QgsGrassVectorMapStore

// Lock open/close map
QMutex mMutex;

static QgsGrassVectorMapStore * mStore;
};

#endif // QGSGRASSVECTORMAP_H
26 changes: 16 additions & 10 deletions src/providers/grass/qgsgrassvectormaplayer.cpp
Expand Up @@ -40,7 +40,6 @@ QgsGrassVectorMapLayer::QgsGrassVectorMapLayer( QgsGrassVectorMap *map, int fiel
: mField( field )
, mValid( false )
, mMap( map )
, mCidxFieldIndex( -1 )
, mFieldInfo( 0 )
, mDriver( 0 )
, mHasTable( false )
Expand All @@ -52,7 +51,6 @@ QgsGrassVectorMapLayer::QgsGrassVectorMapLayer( QgsGrassVectorMap *map, int fiel
void QgsGrassVectorMapLayer::clear()
{
QgsDebugMsg( "entered" );
mCidxFieldIndex = -1;
mTableFields.clear();
mFields.clear();
mAttributeFields.clear();
Expand All @@ -64,13 +62,22 @@ void QgsGrassVectorMapLayer::clear()
mFieldInfo = 0;
}

int QgsGrassVectorMapLayer::cidxFieldIndex()
{
if ( !mMap->map() )
{
return -1;
}
return Vect_cidx_get_field_index( mMap->map(), mField );
}

int QgsGrassVectorMapLayer::cidxFieldNumCats()
{
if ( !mMap->map() || mCidxFieldIndex < 0 )
if ( !mMap->map() || cidxFieldIndex() < 0 )
{
return 0;
}
return Vect_cidx_get_num_cats_by_index( mMap->map(), mCidxFieldIndex );
return Vect_cidx_get_num_cats_by_index( mMap->map(), cidxFieldIndex() );
}

void QgsGrassVectorMapLayer::load()
Expand All @@ -89,8 +96,7 @@ void QgsGrassVectorMapLayer::load()
return;
}

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

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

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

if ( mCidxFieldIndex >= 0 )
if ( cidxFieldIndex() >= 0 )
{
int ncats, cat, type, id;

ncats = Vect_cidx_get_num_cats_by_index( mMap->map(), mCidxFieldIndex );
ncats = Vect_cidx_get_num_cats_by_index( mMap->map(), cidxFieldIndex() );

if ( ncats > 0 )
{
Vect_cidx_get_cat_by_index( mMap->map(), mCidxFieldIndex, 0, &cat, &type, &id );
Vect_cidx_get_cat_by_index( mMap->map(), cidxFieldIndex(), 0, &cat, &type, &id );
minMax.first = cat;

Vect_cidx_get_cat_by_index( mMap->map(), mCidxFieldIndex, ncats - 1, &cat, &type, &id );
Vect_cidx_get_cat_by_index( mMap->map(), cidxFieldIndex(), ncats - 1, &cat, &type, &id );
minMax.second = cat;
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/providers/grass/qgsgrassvectormaplayer.h
Expand Up @@ -51,6 +51,9 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
bool isValid() const { return mValid; }
QgsGrassVectorMap *map() { return mMap; }

/** Category index index */
int cidxFieldIndex();

/** Current number of cats in cat index, changing during editing */
int cidxFieldNumCats();

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

/** Current fields, as modified during editing, it contains cat field, without topo field.
* This fields are used by layers which are not editied to reflect current state of editing. */
QgsFields & tableFields() { return mTableFields; }

static QStringList fieldNames( QgsFields & fields );

QMap<int, QList<QVariant> > & attributes() { return mAttributes; }
Expand Down Expand Up @@ -159,7 +166,6 @@ class GRASS_LIB_EXPORT QgsGrassVectorMapLayer : public QObject
int mField;
bool mValid;
QgsGrassVectorMap *mMap;
int mCidxFieldIndex;
struct field_info *mFieldInfo;
dbDriver *mDriver;

Expand Down
8 changes: 7 additions & 1 deletion tests/src/providers/grass/CMakeLists.txt
@@ -1,6 +1,12 @@
ADD_DEFINITIONS("-DGRASS_EXPORT=${DLLIMPORT} -DGRASS_LIB_EXPORT=${DLLIMPORT}")

INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/providers/grass)
INCLUDE_DIRECTORIES(
${CMAKE_SOURCE_DIR}/src/providers/grass
${GDAL_INCLUDE_DIR}
${PROJ_INCLUDE_DIR}
${GEOS_INCLUDE_DIR}
${POSTGRES_INCLUDE_DIR}
)


MACRO (ADD_QGIS_GRASS_TEST grass_build_version testname testsrc)
Expand Down

0 comments on commit e223bcc

Please sign in to comment.