Skip to content

Commit

Permalink
[GRASS] re-enabled module output layer freeze in canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Oct 14, 2015
1 parent 70bc957 commit 68ebd4b
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 202 deletions.
2 changes: 1 addition & 1 deletion src/plugins/grass/qgsgrassmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ void QgsGrassModule::finished( int exitCode, QProcess::ExitStatus exitStatus )
mProgressBar->setValue( 100 );
mSuccess = true;
mViewButton->setEnabled( true );
mOptions->thawOutput();
mOptions->freezeOutput( false );
mCanvas->refresh();
}
else
Expand Down
211 changes: 89 additions & 122 deletions src/plugins/grass/qgsgrassmoduleoptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "qgslogger.h"
#include "qgsmapcanvas.h"
#include "qgsmaplayer.h"
#include "qgsmaplayerregistry.h"
#include "qgsrasterlayer.h"
#include "qgsvectorlayer.h"
#include "qgsvectordataprovider.h"
Expand Down Expand Up @@ -471,154 +472,120 @@ QStringList QgsGrassModuleStandardOptions::checkOutput()
return list;
}

void QgsGrassModuleStandardOptions::freezeOutput()
QList<QgsGrassProvider *> QgsGrassModuleStandardOptions::grassProviders()
{
QgsDebugMsg( "called." );

#if 0 // defined(Q_OS_WIN)
for ( int i = 0; i < mItems.size(); i++ )
QList<QgsGrassProvider *> providers;
Q_FOREACH ( QgsMapLayer *layer, QgsMapLayerRegistry::instance()->mapLayers().values() )
{
QgsGrassModuleOption *opt = dynamic_cast<QgsGrassModuleOption *>( mItems[i] );
if ( !opt )
continue;

QgsDebugMsg( "opt->key() = " + opt->key() );

if ( opt->isOutput()
&& opt->outputType() == QgsGrassModuleOption::Vector )
if ( layer->type() == QgsMapLayer::VectorLayer )
{
QgsDebugMsg( "freeze vector layers" );

QChar sep = '/';

int nlayers = mCanvas->layerCount();
for ( int i = 0; i < nlayers; i++ )
QgsVectorLayer *vector = qobject_cast<QgsVectorLayer*>( layer );
if ( vector && vector->providerType() == "grass" )
{
QgsMapLayer *layer = mCanvas->layer( i );

if ( layer->type() != QgsMapLayer::VectorLayer )
continue;

QgsVectorLayer *vector = ( QgsVectorLayer* )layer;
if ( vector->providerType() != "grass" )
continue;

//TODO dynamic_cast ?
QgsGrassProvider *provider = ( QgsGrassProvider * ) vector->dataProvider();

// TODO add map() mapset() location() gisbase() to grass provider
QString source = QDir::cleanPath( provider->dataSourceUri() );

QgsDebugMsg( "source = " + source );

// Check GISDBASE and LOCATION
QStringList split = source.split( sep );

if ( split.size() < 4 )
continue;
split.pop_back(); // layer

QString map = split.last();
split.pop_back(); // map

QString mapset = split.last();
split.pop_back(); // mapset

QString loc = source.remove( QRegExp( "/[^/]+/[^/]+/[^/]+$" ) );
loc = QDir( loc ).canonicalPath();

QDir curlocDir( QgsGrass::getDefaultGisdbase() + sep + QgsGrass::getDefaultLocation() );
QString curloc = curlocDir.canonicalPath();

if ( loc != curloc )
continue;

if ( mapset != QgsGrass::getDefaultMapset() )
continue;

if ( provider->isFrozen() )
continue;
QgsGrassProvider *provider = qobject_cast<QgsGrassProvider *>( vector->dataProvider() );
if ( provider )
{
providers << provider;
}
}
}
}
return providers;
}

provider->freeze();
QList<QgsGrassRasterProvider *> QgsGrassModuleStandardOptions::grassRasterProviders()
{
QList<QgsGrassRasterProvider *> providers;
Q_FOREACH ( QgsMapLayer *layer, QgsMapLayerRegistry::instance()->mapLayers().values() )
{
if ( layer->type() == QgsMapLayer::RasterLayer )
{
QgsRasterLayer *raster = qobject_cast<QgsRasterLayer*>( layer );
if ( raster && raster->providerType() == "grassraster" )
{
QgsGrassRasterProvider *provider = qobject_cast<QgsGrassRasterProvider *>( raster->dataProvider() );
if ( provider )
{
providers << provider;
}
}
}
}
#endif
return providers;
}

void QgsGrassModuleStandardOptions::thawOutput()
// freezeOutput/thawOutput is only necessary in Windows, where files cannot be overwritten
// when open by another app. It is enabled on all platforms, so that it gets tested.
void QgsGrassModuleStandardOptions::freezeOutput( bool freeze )
{
QgsDebugMsg( "called." );

#if 0 // defined(Q_OS_WIN)
for ( int i = 0; i < mItems.size(); i++ )
for ( int i = 0; i < mParams.size(); i++ )
{
QgsGrassModuleOption *opt = dynamic_cast<QgsGrassModuleOption *>( mItems[i] );
if ( !opt )
QgsGrassModuleOption *opt = dynamic_cast<QgsGrassModuleOption *>( mParams[i] );
if ( !opt || !opt->isOutput() )
{
continue;

}
QgsDebugMsg( "opt->key() = " + opt->key() );

if ( opt->isOutput()
&& opt->outputType() == QgsGrassModuleOption::Vector )
if ( opt->outputType() == QgsGrassModuleOption::Vector )
{
QgsDebugMsg( "thaw vector layers" );
QgsDebugMsg( "freeze vector layers" );

QChar sep = '/';
QgsGrassObject outputObject = QgsGrass::getDefaultMapsetObject();
outputObject.setName( opt->value() );
outputObject.setType( QgsGrassObject::Vector );
QgsDebugMsg( "outputObject = " + outputObject.toString() );

int nlayers = mCanvas->layerCount();
for ( int i = 0; i < nlayers; i++ )
Q_FOREACH ( QgsGrassProvider *provider, grassProviders() )
{
QgsMapLayer *layer = mCanvas->layer( i );

if ( layer->type() != QgsMapLayer::VectorLayer )
continue;

QgsVectorLayer *vector = ( QgsVectorLayer* )layer;
if ( vector->providerType() != "grass" )
continue;

//TODO dynamic_cast ?
QgsGrassProvider *provider = ( QgsGrassProvider * ) vector->dataProvider();

// TODO add map() mapset() location() gisbase() to grass provider
QString source = QDir::cleanPath( provider->dataSourceUri() );

QgsDebugMsg( "source = " + source );

// Check GISDBASE and LOCATION
QStringList split = source.split( sep );

if ( split.size() < 4 )
continue;
split.pop_back(); // layer

QString map = split.last();
split.pop_back(); // map

QString mapset = split.last();
split.pop_back(); // mapset

QString loc = source.remove( QRegExp( "/[^/]+/[^/]+/[^/]+$" ) );
loc = QDir( loc ).canonicalPath();

QDir curlocDir( QgsGrass::getDefaultGisdbase() + sep + QgsGrass::getDefaultLocation() );
QString curloc = curlocDir.canonicalPath();

if ( loc != curloc )
continue;

if ( mapset != QgsGrass::getDefaultMapset() )
continue;
QgsGrassObject layerObject;
layerObject.setFromUri( provider->dataSourceUri() );
if ( layerObject == outputObject )
{
if ( freeze )
{
QgsDebugMsg( "freeze map " + provider->dataSourceUri() );
provider->freeze();
}
else
{
QgsDebugMsg( "thaw map " + provider->dataSourceUri() );
provider->thaw();
}
}
}
}
else if ( opt->outputType() == QgsGrassModuleOption::Raster )
{
QgsDebugMsg( "freeze raster layers" );

if ( !provider->isFrozen() )
continue;
QgsGrassObject outputObject = QgsGrass::getDefaultMapsetObject();
outputObject.setName( opt->value() );
outputObject.setType( QgsGrassObject::Raster );
QgsDebugMsg( "outputObject = " + outputObject.toString() );

provider->thaw();
Q_FOREACH ( QgsGrassRasterProvider *provider, grassRasterProviders() )
{
QgsGrassObject layerObject;
layerObject.setFromUri( provider->dataSourceUri() );
if ( layerObject == outputObject )
{
if ( freeze )
{
QgsDebugMsg( "freeze map " + provider->dataSourceUri() );
provider->freeze();
}
else
{
QgsDebugMsg( "thaw map " + provider->dataSourceUri() );
provider->thaw();
}
}
}
}
}
#endif
}

QStringList QgsGrassModuleStandardOptions::output( int type )
Expand Down
18 changes: 11 additions & 7 deletions src/plugins/grass/qgsgrassmoduleoptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include "qgscoordinatereferencesystem.h"

#include "qgsgrassmoduleparam.h"
#include "qgsgrassprovider.h"
#include "qgsgrassrasterprovider.h"

class QgsGrassTools;
class QgsGrassModule;
Expand Down Expand Up @@ -64,11 +66,9 @@ class QgsGrassModuleOptions
// return list of existing output maps
virtual QStringList checkOutput() { return QStringList() ; }

//! Freeze output vector maps used in QGIS on Windows
virtual void freezeOutput() {}

//! Thaw output vector maps used in QGIS on Windows
virtual void thawOutput() { }
//! Freeze output maps used in QGIS
// freeze / thaw output layers
virtual void freezeOutput( bool freeze = true ) { Q_UNUSED( freeze ) }

//! Check if option is ready
// Returns empty string or error message
Expand Down Expand Up @@ -163,8 +163,8 @@ class QgsGrassModuleStandardOptions: public QWidget, public QgsGrassModuleOption

// Reimplemented methods from QgsGrassModuleOptions
QStringList checkOutput() override;
void freezeOutput() override;
void thawOutput() override;
// freeze / thaw output layers
void freezeOutput( bool freeze = true ) override;
QStringList ready() override;
QStringList output( int type ) override;
bool hasOutput( int type ) override;
Expand All @@ -189,6 +189,10 @@ class QgsGrassModuleStandardOptions: public QWidget, public QgsGrassModuleOption
*/
bool getCurrentMapRegion( QgsGrassModuleInput * param, struct Cell_head *window );

// List of providers used by layers in QgsMapLayerRegistry
QList<QgsGrassProvider *> grassProviders();
QList<QgsGrassRasterProvider *> grassRasterProviders();

//! Name of module executable
QString mXName;

Expand Down
13 changes: 10 additions & 3 deletions src/providers/grass/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)
../qgsgrassimport.h
../qgsgrassoptions.h
../qgsgrassprovider.h
../qgsgrassrasterprovider.h
../qgsgrassvector.h
../qgsgrassvectormap.h
../qgsgrassvectormaplayer.h
Expand All @@ -40,6 +41,7 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)
../qgsgrassimport.cpp
../qgsgrassoptions.cpp
../qgsgrassprovider.cpp
../qgsgrassrasterprovider.cpp
../qgsgrassvector.cpp
../qgsgrassvectormap.cpp
../qgsgrassvectormaplayer.cpp
Expand Down Expand Up @@ -131,12 +133,17 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)
#
# grass raster provider
#
QT4_WRAP_CPP(GRASS_RASTERPROVIDER_MOC_SRCS ../qgsgrassrasterprovider.h)
ADD_LIBRARY(grassrasterprovider${GRASS_BUILD_VERSION} MODULE ../qgsgrassrasterprovider.cpp ${GRASS_RASTERPROVIDER_MOC_SRCS})
SET (GRASS_RASTER_PROVIDER_SRCS
../qgsgrassrasterprovidermodule.cpp
)
ADD_LIBRARY(grassrasterprovider${GRASS_BUILD_VERSION} MODULE ${GRASS_RASTER_PROVIDER_SRCS})
SET_TARGET_PROPERTIES(grassrasterprovider${GRASS_BUILD_VERSION} PROPERTIES
COMPILE_FLAGS "-DGRASS_BASE=\\\"${GRASS_PREFIX}\\\" \"-DGRASS_EXPORT=${DLLEXPORT}\" \"-DGRASS_LIB_EXPORT=${DLLIMPORT}\""
)
TARGET_LINK_LIBRARIES(grassrasterprovider${GRASS_BUILD_VERSION} qgisgrass${GRASS_BUILD_VERSION} qgis_core)
TARGET_LINK_LIBRARIES(grassrasterprovider${GRASS_BUILD_VERSION}
qgisgrass${GRASS_BUILD_VERSION}
qgis_core
)

# override default path where built files are put to allow running qgis without installing
# the modules go under libexec subdir
Expand Down
10 changes: 10 additions & 0 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,11 @@ QString QgsGrass::getDefaultLocation()
return defaultLocation;
}

QgsGrassObject QgsGrass::getDefaultLocationObject()
{
return QgsGrassObject( defaultGisdbase, defaultLocation, "", "", QgsGrassObject::Location );
}

QString QgsGrass::getDefaultLocationPath()
{
if ( !active )
Expand All @@ -558,6 +563,11 @@ QString QgsGrass::getDefaultMapset()
return defaultMapset;
}

QgsGrassObject QgsGrass::getDefaultMapsetObject()
{
return QgsGrassObject( defaultGisdbase, defaultLocation, defaultMapset, "", QgsGrassObject::Mapset );
}

QString QgsGrass::getDefaultMapsetPath()
{
return getDefaultLocationPath() + "/" + defaultMapset;
Expand Down
4 changes: 4 additions & 0 deletions src/providers/grass/qgsgrass.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,16 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
//! Get default LOCATION_NAME, returns LOCATION_NAME name or empty string if not in active mode
static QString getDefaultLocation();

static QgsGrassObject getDefaultLocationObject();

//! Get default path to location (gisdbase/location) or empty string if not in active mode
static QString getDefaultLocationPath();

//! Get default MAPSET, returns MAPSET name or empty string if not in active mode
static QString getDefaultMapset();

static QgsGrassObject getDefaultMapsetObject();

//! Get default path to MAPSET (gisdbase/location/mapset) or empty string if not in active mode
static QString getDefaultMapsetPath();

Expand Down
Loading

0 comments on commit 68ebd4b

Please sign in to comment.