Skip to content

Commit

Permalink
[GRASS] refresh module input when location changed
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Sep 29, 2015
1 parent ac38d89 commit 0692f25
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
56 changes: 36 additions & 20 deletions src/plugins/grass/qgsgrassmoduleinput.cpp
Expand Up @@ -59,29 +59,15 @@ QgsGrassModuleInputModel::QgsGrassModuleInputModel( QObject *parent )
: QStandardItemModel( parent )
, mWatcher( 0 )
{
QgsDebugMsg( "entered" );
setColumnCount( 1 );
reload();

QString locationPath = QgsGrass::getDefaultLocationPath();
mWatcher = new QFileSystemWatcher( this );
mWatcher->addPath( locationPath );
connect( mWatcher, SIGNAL( directoryChanged( const QString & ) ), SLOT( onDirectoryChanged( const QString & ) ) );

// Watching all dirs in loacation because a dir may become a mapset later, when WIND is created
connect( QgsGrass::instance(), SIGNAL( mapsetChanged() ), SLOT( onMapsetChanged() ) );

//QStringList mapsets = QgsGrass::mapsets( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation() );
QStringList dirNames = locationDirNames();
foreach ( QString dirName, dirNames )
{
QString dirPath = locationPath + "/" + dirName;
// Watch the dir in any case, WIND mabe created later
mWatcher->addPath( dirPath );

foreach ( QString watchedDir, watchedDirs() )
{
watch( dirPath + "/" + watchedDir );
}
}
connect( mWatcher, SIGNAL( directoryChanged( const QString & ) ), SLOT( onDirectoryChanged( const QString & ) ) );
reload();
}

void QgsGrassModuleInputModel::onDirectoryChanged( const QString & path )
Expand Down Expand Up @@ -242,12 +228,44 @@ void QgsGrassModuleInputModel::refreshMapset( QStandardItem *mapsetItem, const Q

void QgsGrassModuleInputModel::reload()
{
QgsDebugMsg( "entered" );
mWatcher->removePaths( mWatcher->files() );
mWatcher->removePaths( mWatcher->directories() );

clear();

mLocationPath = QgsGrass::getDefaultLocationPath();

QStringList mapsets = QgsGrass::mapsets( QgsGrass::getDefaultGisdbase(), QgsGrass::getDefaultLocation() );
foreach ( QString mapset, mapsets )
{
addMapset( mapset );
}

mWatcher->addPath( mLocationPath );

// Watching all dirs in location because a dir may become a mapset later, when WIND is created
QStringList dirNames = locationDirNames();
foreach ( QString dirName, dirNames )
{
QString dirPath = mLocationPath + "/" + dirName;
// Watch the dir in any case, WIND mabe created later
mWatcher->addPath( dirPath );

foreach ( QString watchedDir, watchedDirs() )
{
watch( dirPath + "/" + watchedDir );
}
}
}

void QgsGrassModuleInputModel::onMapsetChanged()
{
QgsDebugMsg( "entered" );
if ( mLocationPath != QgsGrass::getDefaultLocationPath() )
{
reload();
}
}

QgsGrassModuleInputModel::~QgsGrassModuleInputModel()
Expand All @@ -263,7 +281,6 @@ QgsGrassModuleInputModel *QgsGrassModuleInputModel::instance()

QVariant QgsGrassModuleInputModel::data( const QModelIndex & index, int role ) const
{
QgsDebugMsg( "entered" );
QVariant data = QStandardItemModel::data( index, role );
if ( role == Qt::DisplayRole || role == Qt::EditRole ) // EditRole for combo
{
Expand Down Expand Up @@ -306,7 +323,6 @@ bool QgsGrassModuleInputProxy::filterAcceptsRow( int sourceRow, const QModelInde

bool QgsGrassModuleInputProxy::lessThan( const QModelIndex & left, const QModelIndex & right ) const
{
Q_UNUSED( right )
if ( mSourceModel )
{
// keep current mapset on top
Expand Down
4 changes: 4 additions & 0 deletions src/plugins/grass/qgsgrassmoduleinput.h
Expand Up @@ -74,18 +74,22 @@ class QgsGrassModuleInputModel : public QStandardItemModel
/** Reload current mapset */
void reload();

void onMapsetChanged();

void onDirectoryChanged( const QString & path );

private:
void addMapset( const QString & mapset );
void refreshMapset( QStandardItem *mapsetItem, const QString & mapset );
// Add to watched paths if exists and if not yet watched
void watch( const QString & path );
QString mLocationPath;
// mapset watched dirs
QStringList watchedDirs() { QStringList l; l << "cellhd" << "vector"; return l; }
// names of
QStringList locationDirNames();
QFileSystemWatcher *mWatcher;

};

// Filter maps by type
Expand Down

0 comments on commit 0692f25

Please sign in to comment.