Skip to content
Permalink
Browse files

[GRASS] browser file watcher

  • Loading branch information
blazek committed Sep 16, 2015
1 parent c54ea43 commit 0daf6dd10f0fcc753031ba485f6d2e91b21de4c7
Showing with 125 additions and 15 deletions.
  1. +117 −13 src/providers/grass/qgsgrassprovidermodule.cpp
  2. +8 −2 src/providers/grass/qgsgrassprovidermodule.h
@@ -101,6 +101,7 @@ QList<QgsGrassImport*> QgsGrassMapsetItem::mImports;
QgsGrassMapsetItem::QgsGrassMapsetItem( QgsDataItem* parent, QString dirPath, QString path )
: QgsDirectoryItem( parent, "", dirPath, path )
, mMapsetFileSystemWatcher( 0 )
, mRefreshLater( false )
{
QDir dir( mDirPath );
mName = dir.dirName();
@@ -115,17 +116,16 @@ QgsGrassMapsetItem::QgsGrassMapsetItem( QgsDataItem* parent, QString dirPath, QS
void QgsGrassMapsetItem::setState( State state )
{
QgsDebugMsg( "Entered" );
QgsDirectoryItem::setState( state );
// TODO: verify and re-enable, it seems to be causing strange icon switching during import, sometimes
#if 0

// TODO: it seems to be causing strange icon switching during import, sometimes
if ( state == Populated )
{
if ( !mMapsetFileSystemWatcher )
{
mMapsetFileSystemWatcher = new QFileSystemWatcher( this );
mMapsetFileSystemWatcher->addPath( mDirPath + "/vector" );
mMapsetFileSystemWatcher->addPath( mDirPath + "/cellhd" );
connect( mMapsetFileSystemWatcher, SIGNAL( directoryChanged( const QString & ) ), SLOT( directoryChanged() ) );
connect( mMapsetFileSystemWatcher, SIGNAL( directoryChanged( const QString & ) ), SLOT( onDirectoryChanged() ) );
}
}
else if ( state == NotPopulated )
@@ -136,7 +136,8 @@ void QgsGrassMapsetItem::setState( State state )
mMapsetFileSystemWatcher = 0;
}
}
#endif

QgsDirectoryItem::setState( state );
}

bool QgsGrassMapsetItem::objectInImports( QgsGrassObject grassObject )
@@ -168,8 +169,14 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
QVector<QgsDataItem*> items;

QStringList vectorNames = QgsGrass::vectors( mDirPath );
foreach ( QString name, vectorNames )
Q_FOREACH ( QString name, vectorNames )
{
if ( mRefreshLater )
{
deleteLater( items );
return items;
}

QgsGrassObject vectorObject( mGisdbase, mLocation, mName, name, QgsGrassObject::Vector );

// Skip temporary import maps. If Vect_open_old during refresh fails due to missing topo, hist file remains open
@@ -293,6 +300,11 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()

foreach ( QString name, rasterNames )
{
if ( mRefreshLater )
{
deleteLater( items );
return items;
}
QString path = mPath + "/" + "raster" + "/" + name;
QString uri = mDirPath + "/" + "cellhd" + "/" + name;
QgsDebugMsg( "uri = " + uri );
@@ -311,6 +323,11 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
QStringList groupNames = QgsGrass::groups( mDirPath );
foreach ( QString name, groupNames )
{
if ( mRefreshLater )
{
deleteLater( items );
return items;
}
QString path = mPath + "/" + "group" + "/" + name;
QString uri = mDirPath + "/" + "group" + "/" + name;
QgsDebugMsg( "uri = " + uri );
@@ -322,6 +339,11 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()

foreach ( QgsGrassImport* import, mImports )
{
if ( mRefreshLater )
{
deleteLater( items );
return items;
}
if ( !import )
{
continue;
@@ -631,6 +653,37 @@ void QgsGrassMapsetItem::openMapset()
QgsGrass::saveMapset();
}

void QgsGrassMapsetItem::onDirectoryChanged()
{
QgsDebugMsg( "entered" );
if ( state() == Populating )
{
// schedule to refresh later, because refres() simply returns if Populating
mRefreshLater = true;
}
else
{
refresh();
}
}

void QgsGrassMapsetItem::childrenCreated()
{
QgsDebugMsg( QString( "mRefreshLater = %1" ).arg( mRefreshLater ) );

if ( mRefreshLater )
{
QgsDebugMsg( "directory changed during createChidren() -> refresh() again" );
mRefreshLater = false;
setState( Populated );
refresh();
}
else
{
QgsDirectoryItem::childrenCreated();
}
}

//----------------------- QgsGrassObjectItemBase ------------------------------

QgsGrassObjectItemBase::QgsGrassObjectItemBase( QgsGrassObject grassObject ) :
@@ -702,6 +755,9 @@ void QgsGrassObjectItemBase::deleteGrassObject( QgsDataItem* parent )
}
}


//------------------------ QgsGrassObjectItem ----------------------------------

QgsGrassObjectItem::QgsGrassObjectItem( QgsDataItem* parent, QgsGrassObject grassObject,
QString name, QString path, QString uri,
LayerType layerType, QString providerKey,
@@ -732,13 +788,6 @@ QList<QAction*> QgsGrassObjectItem::actions()
return lst;
}

bool QgsGrassObjectItem::equal( const QgsDataItem *other )
{
const QgsGrassObjectItem * item = qobject_cast<const QgsGrassObjectItem *>( other );
return QgsLayerItem::equal( other ) && item && mGrassObject == item->mGrassObject
&& mShowObjectActions == item->mShowObjectActions;
}

void QgsGrassObjectItem::renameGrassObject()
{
QgsGrassObjectItemBase::renameGrassObject( parent() );
@@ -749,20 +798,49 @@ void QgsGrassObjectItem::deleteGrassObject()
QgsGrassObjectItemBase::deleteGrassObject( parent() );
}

bool QgsGrassObjectItem::equal( const QgsDataItem *other )
{
const QgsGrassObjectItem * item = qobject_cast<const QgsGrassObjectItem *>( other );
return QgsLayerItem::equal( other ) && item && mGrassObject == item->mGrassObject
&& mShowObjectActions == item->mShowObjectActions;
}

//----------------------- QgsGrassVectorItem ------------------------------

QgsGrassVectorItem::QgsGrassVectorItem( QgsDataItem* parent, QgsGrassObject grassObject, QString path, QString labelName, bool valid ) :
QgsDataCollectionItem( parent, labelName.isEmpty() ? grassObject.name() : labelName, path )
, QgsGrassObjectItemBase( grassObject )
, mValid( valid )
, mWatcher( 0 )
{
QgsDebugMsg( "name = " + grassObject.name() + " path = " + path );
mCapabilities = NoCapabilities; // disable Fertile from QgsDataCollectionItem
setCapabilities( QgsDataItem::NoCapabilities ); // disable fertility
if ( !mValid )
{
setState( Populated );
setIconName( "/mIconDelete.png" );
}
QString watchPath = mGrassObject.mapsetPath() + "/vector/" + mGrassObject.name();
QgsDebugMsg( "add watcher on " + watchPath );
// The watcher does not seem to work without parent
mWatcher = new QFileSystemWatcher( this );
mWatcher->addPath( watchPath );
connect( mWatcher, SIGNAL( directoryChanged( const QString & ) ), SLOT( onDirectoryChanged() ) );
}

QgsGrassVectorItem::~QgsGrassVectorItem()
{
delete mWatcher;
}

void QgsGrassVectorItem::onDirectoryChanged()
{
QgsDebugMsg( "entered" );
if ( parent() )
{
parent()->refresh();
}
}

QList<QAction*> QgsGrassVectorItem::actions()
@@ -790,6 +868,32 @@ void QgsGrassVectorItem::deleteGrassObject()
QgsGrassObjectItemBase::deleteGrassObject( parent() );
}

bool QgsGrassVectorItem::equal( const QgsDataItem *other )
{
if ( QgsDataCollectionItem::equal( other ) )
{
const QgsGrassVectorItem * item = qobject_cast<const QgsGrassVectorItem *>( other );
if ( item && mGrassObject == item->mGrassObject && mValid == item->mValid )
{
if ( mChildren.size() == item->mChildren.size() )
{
// check children
for ( int i = 0; i < mChildren.size(); i++ )
{
QgsDataItem *child = mChildren.value( i );
QgsDataItem *otherChild = item->mChildren.value( i );
if ( !child || !otherChild || !child->equal( otherChild ) )
{
return false;
}
}
return true;
}
}
}
return false;
}

//----------------------- QgsGrassVectorLayerItem ------------------------------

QgsGrassVectorLayerItem::QgsGrassVectorLayerItem( QgsDataItem* parent, QgsGrassObject grassObject, QString layerName,
@@ -66,13 +66,16 @@ class QgsGrassMapsetItem : public QgsDirectoryItem
public slots:
void onImportFinished( QgsGrassImport* import );
void openMapset();
void onDirectoryChanged();
virtual void childrenCreated() override;

private:
bool objectInImports( QgsGrassObject grassObject );
//void showImportError(const QString& error);
QString mLocation;
QString mGisdbase;
QFileSystemWatcher *mMapsetFileSystemWatcher;
bool mRefreshLater;
// running imports
static QList<QgsGrassImport*> mImports;
};
@@ -100,7 +103,7 @@ class QgsGrassObjectItem : public QgsLayerItem, public QgsGrassObjectItemBase
bool showObjectActions = true );

virtual QList<QAction*> actions() override;
virtual bool equal( const QgsDataItem *other ) override;
virtual bool equal( const QgsDataItem *other );

public slots:
void renameGrassObject();
@@ -120,17 +123,20 @@ class QgsGrassVectorItem : public QgsDataCollectionItem, public QgsGrassObjectIt
public:
// labelName - name to be displayed in tree if it should be different from grassObject.name() (e.g. invalid vector)
QgsGrassVectorItem( QgsDataItem* parent, QgsGrassObject grassObject, QString path, QString labelName = QString::null, bool valid = true );
~QgsGrassVectorItem() {}
~QgsGrassVectorItem();

virtual QList<QAction*> actions() override;
virtual bool equal( const QgsDataItem *other ) override;

public slots:
void renameGrassObject();
void deleteGrassObject();
void onDirectoryChanged();

private:
QgsGrassObject mVector;
bool mValid;
QFileSystemWatcher *mWatcher;
};

class QgsGrassVectorLayerItem : public QgsGrassObjectItem

0 comments on commit 0daf6dd

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