Skip to content

Commit

Permalink
[GRASS] add new layers from browser
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Sep 24, 2015
1 parent 216e461 commit d25a224
Show file tree
Hide file tree
Showing 10 changed files with 402 additions and 242 deletions.
11 changes: 9 additions & 2 deletions src/plugins/grass/qgsgrassplugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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* ) ) );
Expand All @@ -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 )
Expand Down Expand Up @@ -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 );
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/grass/qgsgrassplugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
12 changes: 11 additions & 1 deletion src/providers/grass/qgsgrass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions src/providers/grass/qgsgrassfeatureiterator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
Expand Down
45 changes: 39 additions & 6 deletions src/providers/grass/qgsgrassprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
Expand All @@ -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 );

Expand Down Expand Up @@ -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
Expand Down
Loading

0 comments on commit d25a224

Please sign in to comment.