Skip to content
Permalink
Browse files

Fix remove GRASS vector map from mapset on Win OS (should be fix #3646

…together with grass-dev package),

disable refresh button in GRASS browser if a module is running
  • Loading branch information
brushtyler committed Sep 10, 2011
1 parent aebb6ed commit f1d7062c3ed3474785ca2ef03675397cbfa37d32
@@ -19,6 +19,10 @@
#include "qgsgrass.h"

#include "qgisinterface.h"
#include "qgslegendinterface.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
//#include "qgsgrassprovider.h"
#include "qgsapplication.h"
#include "qgslogger.h"

@@ -36,7 +40,7 @@

QgsGrassBrowser::QgsGrassBrowser( QgisInterface *iface,
QWidget * parent, Qt::WFlags f )
: QMainWindow( parent, Qt::Dialog ), mIface( iface )
: QMainWindow( parent, Qt::Dialog ), mIface( iface ), mRunningMods( 0 )
{
Q_UNUSED( f );
QgsDebugMsg( "QgsGrassBrowser()" );
@@ -166,7 +170,7 @@ void QgsGrassBrowser::addMap()
QStringList list = QgsGrass::vectorLayers(
QgsGrass::getDefaultGisdbase(),
QgsGrass::getDefaultLocation(),
mModel->itemMapset( *it ), map );
mapset, map );

// TODO: common method for vector names
QStringList split = uri.split( '/', QString::SkipEmptyParts );
@@ -376,11 +380,54 @@ void QgsGrassBrowser::deleteMap()
{
QgsDebugMsg( "entered." );

QString gisbase = QgsGrass::getDefaultGisdbase();
QString location = QgsGrass::getDefaultLocation();

// Filter VectorLayer type from selection
QModelIndexList indexes;
foreach( QModelIndex index, mTree->selectionModel()->selectedIndexes() )
{
int type = mModel->itemType( index );
QString mapset = mModel->itemMapset( index );
QString map = mModel->itemMap( index );

// check whether the layer is loaded in QGis canvas
if ( type == QgsGrassModel::Vector )
{
QStringList layers = QgsGrass::vectorLayers( gisbase, location, mapset, map );
for ( int i = 0; i < layers.count(); i++ )
{
QString uri = gisbase + "/" + location + "/"
+ mapset + "/" + map + "/" + layers[i];

foreach( QgsMapLayer *layer, mIface->legendInterface()->layers() )
{
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( layer );
if ( vl && vl->dataProvider()->name() == "grass" && vl->source() == uri )
{
#if 0
/* The following lines allow to delete a grass vector layer
even if it's loaded in canvas, but it needs to compile
the grass plugin against the grass provider.
This causes a known problem on OSX
(see at http://hub.qgis.org/issues/3999) */

// the layer is loaded in canvas,
// freeze it! (this allow to delete it from the mapset)
QgsGrassProvider * grassProvider =
qobject_cast<QgsGrassProvider *>( vl->dataProvider() );
if ( grassProvider )
grassProvider->freeze();
#else
QMessageBox::information( this, tr( "Information" ),
tr( "Remove the selected layer(s) from QGis canvas before continue." ) );
return;
#endif
}
}
}
}

if ( type != QgsGrassModel::VectorLayer )
{
indexes << index;
@@ -557,3 +604,13 @@ void QgsGrassBrowser::setLocation( const QString &gisbase, const QString &locati
{
mModel->setLocation( gisbase, location );
}

void QgsGrassBrowser::moduleStarted()
{
mActionRefresh->setDisabled( ++mRunningMods > 0 );
}

void QgsGrassBrowser::moduleFinished()
{
mActionRefresh->setDisabled( --mRunningMods > 0 );
}
@@ -77,6 +77,12 @@ class QgsGrassBrowser: public QMainWindow
// Double click
void doubleClicked( const QModelIndex & index );

// Called when a module started
void moduleStarted();

// Called when a module finished
void moduleFinished();

private slots:
// Context menu
void showContextMenu( const QPoint &position );
@@ -113,6 +119,8 @@ class QgsGrassBrowser: public QMainWindow

//! Escape HTML tags and convert \n to <br>
QString formatMessage( QString msg );

int mRunningMods;
};

#endif // QGSGRASSBROWSER_H
@@ -1489,6 +1489,7 @@ void QgsGrassModule::run()

mProcess.setEnvironment( environment );
mProcess.start( cmd, execArguments );
emit moduleStarted();

mProcess.waitForStarted();
if ( mProcess.state() != QProcess::Running )
@@ -1527,6 +1528,8 @@ void QgsGrassModule::finished( int exitCode, QProcess::ExitStatus exitStatus )
{
mOutputTextBrowser->append( tr( "<B>Module crashed or killed</B>" ) );
}

emit moduleFinished();
mRunButton->setText( tr( "Run" ) );
}

@@ -93,6 +93,13 @@ class QgsGrassModule: public QDialog, private Ui::QgsGrassModuleBase
// Returns empty list if not found.
static QStringList execArguments( QString module );

signals:
// ! emitted when the module started
void moduleStarted();

// ! emitted when the module finished
void moduleFinished();

public slots:
//! Run the module with current options
void on_mRunButton_clicked() { run(); }
@@ -163,8 +163,11 @@ void QgsGrassTools::runModule( QString name )
}
else
{
m = qobject_cast<QWidget *>( new QgsGrassModule( this, name,
mIface, path, mTabWidget ) );
QgsGrassModule *gmod = new QgsGrassModule( this, name, mIface, path, mTabWidget );
connect( gmod, SIGNAL( moduleStarted() ), mBrowser, SLOT( moduleStarted() ) );
connect( gmod, SIGNAL( moduleFinished() ), mBrowser, SLOT( moduleFinished() ) );

m = qobject_cast<QWidget *>( gmod );
}

int height = mTabWidget->iconSize().height();
@@ -1184,40 +1184,46 @@ void QgsGrassProvider::updateMap( int mapId )

void QgsGrassProvider::closeMap( int mapId )
{
QgsDebugMsg( QString( "Close map %1 nUsers = %2" ).arg( mapId ).arg( mMaps[mapId].nUsers ) );
GMAP *map = &mMaps[mapId];
QgsDebugMsg( QString( "Close map %1 nUsers = %2" ).arg( mapId ).arg( map->nUsers ) );

// TODO: not tested because delete is never used for providers
mMaps[mapId].nUsers--;
map->nUsers--;

if ( mMaps[mapId].nUsers == 0 ) // No more users, free sources
if ( map->nUsers == 0 ) // No more users, free sources
{
QgsDebugMsg( "No more users -> delete map" );

// TODO: do this better, probably maintain QgsGrassEdit as one user
if ( mMaps[mapId].update )
if ( map->update )
{
QMessageBox::warning( 0, "Warning", "The vector was currently edited, "
"you can expect crash soon." );
}

if ( mMaps[mapId].valid )
if ( map->valid )
{
bool mapsetunset = !G__getenv( "MAPSET" ) || !*G__getenv( "MAPSET" );
if ( mapsetunset )
G__setenv(( char * )"MAPSET", mMaps[mapId].mapset.toUtf8().data() );
try
{
Vect_close( mMaps[mapId].map );
}
catch ( QgsGrass::Exception &e )
G__setenv(( char * )"MAPSET", map->mapset.toUtf8().data() );

if ( !map->frozen )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "Vect_close failed: %1" ).arg( e.what() ) );
try
{
Vect_close( map->map );
}
catch ( QgsGrass::Exception &e )
{
Q_UNUSED( e );
QgsDebugMsg( QString( "Vect_close failed: %1" ).arg( e.what() ) );
}
}

if ( mapsetunset )
G__setenv(( char * )"MAPSET", "" );
}
mMaps[mapId].valid = false;
map->valid = false;
}
}

0 comments on commit f1d7062

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