Skip to content
Permalink
Browse files

[GRASS] initial editing test

  • Loading branch information
blazek committed Oct 7, 2015
1 parent 3dcabfb commit e223bccde5f40452a0806638ad9cee70f192f797
@@ -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
@@ -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 );

@@ -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();
}
}
}

@@ -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 )
@@ -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;
}

@@ -702,6 +702,8 @@ void QgsGrassVectorMap::closeAllIterators()
}

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

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

@@ -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 */
@@ -222,6 +226,8 @@ class QgsGrassVectorMapStore

// Lock open/close map
QMutex mMutex;

static QgsGrassVectorMapStore * mStore;
};

#endif // QGSGRASSVECTORMAP_H
@@ -40,7 +40,6 @@ QgsGrassVectorMapLayer::QgsGrassVectorMapLayer( QgsGrassVectorMap *map, int fiel
: mField( field )
, mValid( false )
, mMap( map )
, mCidxFieldIndex( -1 )
, mFieldInfo( 0 )
, mDriver( 0 )
, mHasTable( false )
@@ -52,7 +51,6 @@ QgsGrassVectorMapLayer::QgsGrassVectorMapLayer( QgsGrassVectorMap *map, int fiel
void QgsGrassVectorMapLayer::clear()
{
QgsDebugMsg( "entered" );
mCidxFieldIndex = -1;
mTableFields.clear();
mFields.clear();
mAttributeFields.clear();
@@ -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()
@@ -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

@@ -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;
}
}
@@ -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();

@@ -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; }
@@ -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;

@@ -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)

0 comments on commit e223bcc

Please sign in to comment.
You can’t perform that action at this time.