Skip to content
Permalink
Browse files

[GRASS] add new layers from browser

  • Loading branch information
blazek committed Sep 24, 2015
1 parent 216e461 commit d25a2249d8c31706c5fbfc88062ce539e4ae0781
@@ -281,6 +281,7 @@ void QgsGrassPlugin::initGui()
connect( QgsGrass::instance(), SIGNAL( mapsetChanged() ), SLOT( mapsetChanged() ) );
connect( QgsGrass::instance(), SIGNAL( regionChanged() ), SLOT( displayRegion() ) );
connect( QgsGrass::instance(), SIGNAL( regionPenChanged() ), SLOT( displayRegion() ) );
connect( QgsGrass::instance(), SIGNAL( newLayer( QString, QString ) ), SLOT( onNewLayer( QString, QString ) ) );

// Connect start/stop editing
connect( QgsMapLayerRegistry::instance(), SIGNAL( layerWasAdded( QgsMapLayer* ) ), this, SLOT( onLayerWasAdded( QgsMapLayer* ) ) );
@@ -293,8 +294,6 @@ void QgsGrassPlugin::initGui()
// open tools when plugin is loaded so that main app restores tools dock widget state
mTools = new QgsGrassTools( qGisInterface, qGisInterface->mainWindow() );
qGisInterface->addDockWidget( Qt::RightDockWidgetArea, mTools );


}

void QgsGrassPlugin::onLayerWasAdded( QgsMapLayer* theMapLayer )
@@ -573,6 +572,14 @@ void QgsGrassPlugin::newVector()
// TODO: start editing?
}

void QgsGrassPlugin::onNewLayer( QString uri, QString name )
{
QgsDebugMsg( "uri = " + uri + " name = " + name );
QgsVectorLayer* vectorLayer = qGisInterface->addVectorLayer( uri, name, "grass" );
vectorLayer->startEditing();
qGisInterface->setActiveLayer( vectorLayer );
}

void QgsGrassPlugin::postRender( QPainter *painter )
{
Q_UNUSED( painter );
@@ -122,6 +122,9 @@ class QgsGrassPlugin : public QObject, public QgisPlugin
// Start editing tools
void addFeature();

// Called when new layer was created in browser
void onNewLayer( QString uri, QString name );

private:
void resetEditActions();

@@ -689,6 +689,11 @@ void QgsGrass::setMapset( QString gisdbase, QString location, QString mapset )
#endif
}

void QgsGrass::setMapset( QgsGrassObject grassObject )
{
setMapset( grassObject.gisdbase(), grassObject.location(), grassObject.mapset() );
}

jmp_buf QgsGrass::jumper;

int QgsGrass::initialized = 0;
@@ -2603,6 +2608,7 @@ void QgsGrass::vectDestroyMapStruct( struct Map_info *map )
// TODO: replace by Vect_destroy_map_struct once it appears in GRASS
// TODO: until switch to hypothetical Vect_destroy_map_struct verify that Vect_destroy_map_struct cannot
// call G_fatal_error, otherwise check and remove use of vectDestroyMapStruct from G_CATCH blocks
QgsDebugMsg( QString( "free map = %1" ).arg(( long )map ) );
qgsFree( map );
map = 0;
}
@@ -80,7 +80,7 @@ class GRASS_LIB_EXPORT QgsGrassObject
{
public:
//! Element type
enum Type { None, Raster, Group, Vector, Region };
enum Type { None, Location, Mapset, Raster, Group, Vector, Region };

QgsGrassObject() : mType( None ) {}
QgsGrassObject( const QString& gisdbase, const QString& location = QString::null,
@@ -205,6 +205,10 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
*/
static void setMapset( QString gisdbase, QString location, QString mapset );

/** Set mapset according to object gisdbase, location and mapset
* @param grassObject */
static void setMapset( QgsGrassObject grassObject );

//! Error codes returned by error()
enum GERROR
{
@@ -547,6 +551,8 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
// Sleep miliseconds (for debugging)
static void sleep( int ms );

void emitNewLayer( QString uri, QString name ) { emit newLayer( uri, name ); }

public slots:
/** Close mapset and show warning if closing failed */
bool closeMapsetWarn();
@@ -570,6 +576,10 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
/** Emitted when region pen changed */
void regionPenChanged();

/** Request from browser to open a new layer for editing, the plugin should connect
* to this signal and add the layer to canvas and start editing. */
void newLayer( QString uri, QString name );

private:
static int initialized; // Set to 1 after initialization
static bool active; // is active mode
@@ -419,6 +419,13 @@ bool QgsGrassFeatureIterator::fetchFeature( QgsFeature& feature )
break;
}
int tmpLid, tmpType, tmpCat;

int numFields = Vect_cidx_get_num_fields( mSource->map() );
if ( mSource->mCidxFieldIndex < 0 || mSource->mCidxFieldIndex >= numFields )
{
QgsDebugMsg( QString( "mCidxFieldIndex %1 out of range (0,%2)" ).arg( mSource->mCidxFieldIndex ).arg( numFields - 1 ) );
break;
}
Vect_cidx_get_cat_by_index( mSource->map(), mSource->mCidxFieldIndex, mNextCidx++, &tmpCat, &tmpType, &tmpLid );
// Warning: selection array is only of type line/area of current layer -> check type first
if ( !( tmpType & mSource->mGrassType ) )
@@ -1119,16 +1119,31 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )
{
// TODO: redo of deleted new features - save new cats somewhere,
// resetting fid probably is not possible because it is stored in undo commands and used in buffer maps
int newCat = cidxGetMaxCat( mCidxFieldIndex ) + 1;

QgsDebugMsg( QString( "get new cat for mCidxFieldIndex = %1" ).arg( mCidxFieldIndex ) );
int newCat = 0;
if ( mCidxFieldIndex == -1 )
{
// No features with this field yet in map
newCat = 1;
}
else
{
newCat = cidxGetMaxCat( mCidxFieldIndex ) + 1;
}
QgsDebugMsg( QString( "newCat = %1" ).arg( newCat ) );
Vect_cat_set( cats, mLayerField, newCat );
}
}
if ( cat > 0 )
else
{
// TODO: orig field, maybe different
int field = mLayerField;
Vect_cat_set( cats, field, cat );
// Old feature delete undo
if ( cat > 0 )
{
// TODO: orig field, maybe different
int field = mLayerField;
Vect_cat_set( cats, field, cat );
}
}

if ( type > 0 && points->n_points > 0 )
@@ -1139,6 +1154,13 @@ void QgsGrassProvider::onFeatureAdded( QgsFeatureId fid )

QgsDebugMsg( QString( "newLine = %1" ).arg( newLid ) );

if ( mCidxFieldIndex == -1 && type != GV_BOUNDARY )
{
// first feature in this layer
mCidxFieldIndex = Vect_cidx_get_field_index( map(), mLayerField );
QgsDebugMsg( QString( "new mCidxFieldIndex = %1" ).arg( mCidxFieldIndex ) );
}

// fid may be new (negative) or old, if this is delete undo
int oldLid = QgsGrassFeatureIterator::lidFromFid( fid );

@@ -1428,17 +1450,28 @@ int QgsGrassProvider::cidxGetNumFields()

int QgsGrassProvider::cidxGetFieldNumber( int idx )
{
if ( idx < 0 || idx >= cidxGetNumFields() )
{
QgsDebugMsg( QString( "idx %1 out of range (0,%2)" ).arg( idx ).arg( cidxGetNumFields() - 1 ) );
return 0;
}
return ( Vect_cidx_get_field_number( map(), idx ) );
}

int QgsGrassProvider::cidxGetMaxCat( int idx )
{
if ( idx < 0 || idx >= cidxGetNumFields() )
{
QgsDebugMsg( QString( "idx %1 out of range (0,%2)" ).arg( idx ).arg( cidxGetNumFields() - 1 ) );
return 0;
}

int ncats = Vect_cidx_get_num_cats_by_index( map(), idx );

int cat, type, id;
Vect_cidx_get_cat_by_index( map(), idx, ncats - 1, &cat, &type, &id );

return ( cat );
return cat;
}

QgsGrassVectorMapLayer * QgsGrassProvider::openLayer() const

0 comments on commit d25a224

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