mirror of
https://github.com/qgis/QGIS.git
synced 2025-03-01 00:46:20 -05:00
[GRASS][FEATURE] copy layers within location mapsets using browser drag and drop
This commit is contained in:
parent
80a8c49bfa
commit
27c2b5c034
@ -76,6 +76,52 @@ QgsGrassObject::QgsGrassObject( const QString& gisdbase, const QString& location
|
||||
{
|
||||
}
|
||||
|
||||
bool QgsGrassObject::setFromUri( const QString& uri )
|
||||
{
|
||||
QgsDebugMsg( "uri = " + uri );
|
||||
QFileInfo fi( uri );
|
||||
|
||||
if ( fi.isFile() )
|
||||
{
|
||||
QString path = fi.canonicalFilePath();
|
||||
QgsDebugMsg( "path = " + path );
|
||||
// /gisdbase_path/location/mapset/cellhd/raster_map
|
||||
QRegExp rx( "(.*)/([^/]*)/([^/]*)/cellhd/([^/]*)", Qt::CaseInsensitive );
|
||||
if ( rx.indexIn( path ) > -1 )
|
||||
{
|
||||
mGisdbase = rx.cap( 1 );
|
||||
mLocation = rx.cap( 2 );
|
||||
mMapset = rx.cap( 3 );
|
||||
mName = rx.cap( 4 );
|
||||
mType = Raster;
|
||||
return QgsGrass::isLocation( mGisdbase + "/" + mLocation );
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// /gisdbase_path/location/mapset/vector_map/layer
|
||||
// QFileInfo.canonicalPath() on non existing file does not work (returns empty string)
|
||||
// QFileInfo.absolutePath() does not necessarily remove symbolic links or redundant "." or ".."
|
||||
QDir dir = fi.dir(); // .../mapset/vector_map - does not exist
|
||||
if ( dir.cdUp() ) // .../mapset/
|
||||
{
|
||||
QString path = dir.canonicalPath();
|
||||
QRegExp rx( "(.*)/([^/]*)/([^/]*)" );
|
||||
if ( rx.indexIn( path ) > -1 )
|
||||
{
|
||||
mGisdbase = rx.cap( 1 );
|
||||
mLocation = rx.cap( 2 );
|
||||
mMapset = rx.cap( 3 );
|
||||
mName = fi.dir().dirName();
|
||||
mType = Vector;
|
||||
QgsDebugMsg( "parsed : " + toString() );
|
||||
return QgsGrass::isLocation( mGisdbase + "/" + mLocation );
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
QString QgsGrassObject::elementShort() const
|
||||
{
|
||||
if ( mType == Raster )
|
||||
@ -122,9 +168,23 @@ QString QgsGrassObject::dirName( Type type )
|
||||
return "";
|
||||
}
|
||||
|
||||
bool QgsGrassObject::mapsetIdentical( const QgsGrassObject &other )
|
||||
QString QgsGrassObject::toString() const
|
||||
{
|
||||
return mGisdbase == other.mGisdbase && mLocation == other.mLocation && mMapset == other.mMapset;
|
||||
return elementName() + " : " + mapsetPath() + " : " + mName;
|
||||
}
|
||||
|
||||
bool QgsGrassObject::locationIdentical( const QgsGrassObject &other ) const
|
||||
{
|
||||
QFileInfo fi( locationPath() );
|
||||
QFileInfo otherFi( other.locationPath() );
|
||||
return fi == otherFi;
|
||||
}
|
||||
|
||||
bool QgsGrassObject::mapsetIdentical( const QgsGrassObject &other ) const
|
||||
{
|
||||
QFileInfo fi( mapsetPath() );
|
||||
QFileInfo otherFi( other.mapsetPath() );
|
||||
return fi == otherFi;
|
||||
}
|
||||
|
||||
QRegExp QgsGrassObject::newNameRegExp( Type type )
|
||||
@ -1792,11 +1852,32 @@ void QgsGrass::renameObject( const QgsGrassObject & object, const QString& newNa
|
||||
|
||||
arguments << object.elementShort() + "=" + object.name() + "," + newName;
|
||||
|
||||
int timeout = 10000; // What timeout to use? It can take long time on network or database
|
||||
int timeout = -1; // What timeout to use? It can take long time on network or database
|
||||
// throws QgsGrass::Exception
|
||||
QgsGrass::runModule( object.gisdbase(), object.location(), object.mapset(), cmd, arguments, timeout, false );
|
||||
}
|
||||
|
||||
void QgsGrass::copyObject( const QgsGrassObject & srcObject, const QgsGrassObject & destObject )
|
||||
{
|
||||
QgsDebugMsg( "srcObject = " + srcObject.toString() );
|
||||
QgsDebugMsg( "destObject = " + destObject.toString() );
|
||||
|
||||
if ( !srcObject.locationIdentical( destObject ) ) // should not happen
|
||||
{
|
||||
throw QgsGrass::Exception( QObject::tr( "Attempt to copy from different location." ) );
|
||||
}
|
||||
|
||||
QString cmd = "g.copy";
|
||||
QStringList arguments;
|
||||
|
||||
arguments << srcObject.elementShort() + "=" + srcObject.name() + "@" + srcObject.mapset() + "," + destObject.name();
|
||||
|
||||
int timeout = -1; // What timeout to use? It can take long time on network or database
|
||||
// throws QgsGrass::Exception
|
||||
// TODO: g.copy does not seem to return error code if fails (6.4.3RC1)
|
||||
QgsGrass::runModule( destObject.gisdbase(), destObject.location(), destObject.mapset(), cmd, arguments, timeout, false );
|
||||
}
|
||||
|
||||
bool QgsGrass::deleteObject( const QgsGrassObject & object )
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
@ -2008,28 +2089,14 @@ Qt::CaseSensitivity GRASS_LIB_EXPORT QgsGrass::caseSensitivity()
|
||||
#endif
|
||||
}
|
||||
|
||||
bool GRASS_LIB_EXPORT QgsGrass::isMapset( QString path )
|
||||
bool GRASS_LIB_EXPORT QgsGrass::isLocation( const QString& path )
|
||||
{
|
||||
#if 0
|
||||
/* TODO: G_is_mapset() was added to GRASS 6.1 06-05-24,
|
||||
enable its use after some period (others do update) */
|
||||
return G_is_location( path.toUtf8().constData() ) == 1;
|
||||
}
|
||||
|
||||
if ( QgsGrass::versionMajor() > 6 || QgsGrass::versionMinor() > 0 )
|
||||
{
|
||||
if ( G_is_mapset( path.toUtf8().constData() ) )
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
#endif
|
||||
QString windf = path + "/WIND";
|
||||
if ( QFile::exists( windf ) )
|
||||
return true;
|
||||
#if 0
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
bool GRASS_LIB_EXPORT QgsGrass::isMapset( const QString& path )
|
||||
{
|
||||
return G_is_mapset( path.toUtf8().constData() ) == 1;
|
||||
}
|
||||
|
||||
QString GRASS_LIB_EXPORT QgsGrass::lockFilePath()
|
||||
|
@ -81,6 +81,7 @@ class GRASS_LIB_EXPORT QgsGrassObject
|
||||
void setGisdbase( const QString& gisdbase ) { mGisdbase = gisdbase; }
|
||||
QString location() const { return mLocation; }
|
||||
void setLocation( const QString& location ) { mLocation = location; }
|
||||
QString locationPath() const { return mGisdbase + "/" + mLocation; }
|
||||
QString mapset() const { return mMapset; }
|
||||
void setMapset( const QString& mapset ) { mMapset = mapset; }
|
||||
QString mapsetPath() const { return mGisdbase + "/" + mLocation + "/" + mMapset; }
|
||||
@ -89,6 +90,8 @@ class GRASS_LIB_EXPORT QgsGrassObject
|
||||
QString fullName() const { return mName + "@" + mMapset; }
|
||||
Type type() const { return mType; }
|
||||
void setType( Type type ) { mType = type; }
|
||||
// set from QGIS layer uri, returns true if set correctly, verifies also if location is a GRASS location
|
||||
bool setFromUri( const QString& uri );
|
||||
// element name used as modules param, e.g. g.remove element=name
|
||||
QString elementShort() const;
|
||||
// descriptive full name
|
||||
@ -97,8 +100,11 @@ class GRASS_LIB_EXPORT QgsGrassObject
|
||||
// name of directory in GRASS mapset to look for the object (cellhd,vector,window)
|
||||
QString dirName() const;
|
||||
static QString dirName( Type type );
|
||||
QString toString() const;
|
||||
// returns true if gisdbase and location are the same
|
||||
bool locationIdentical( const QgsGrassObject &other ) const;
|
||||
// returns true if gisdbase, location and mapset are the same
|
||||
bool mapsetIdentical( const QgsGrassObject &other );
|
||||
bool mapsetIdentical( const QgsGrassObject &other ) const;
|
||||
// get regexp patter for new names, e.g. vectors should not start with number
|
||||
static QRegExp newNameRegExp( Type type );
|
||||
private:
|
||||
@ -277,8 +283,11 @@ class QgsGrass
|
||||
|
||||
static GRASS_LIB_EXPORT void init( void );
|
||||
|
||||
//! test if the directory is location
|
||||
static GRASS_LIB_EXPORT bool isLocation( const QString& path );;
|
||||
|
||||
// ! test if the directory is mapset
|
||||
static GRASS_LIB_EXPORT bool isMapset( QString path );
|
||||
static GRASS_LIB_EXPORT bool isMapset( const QString& path );
|
||||
|
||||
// ! Get the lock file
|
||||
static GRASS_LIB_EXPORT QString lockFilePath();
|
||||
@ -357,6 +366,9 @@ class QgsGrass
|
||||
// ! Rename GRASS object, throws QgsGrass::Exception
|
||||
static GRASS_LIB_EXPORT void renameObject( const QgsGrassObject & object, const QString& newName );
|
||||
|
||||
// ! Copy GRASS object, throws QgsGrass::Exception
|
||||
static GRASS_LIB_EXPORT void copyObject( const QgsGrassObject & srcObject, const QgsGrassObject & destObject );
|
||||
|
||||
// ! Delete map
|
||||
static GRASS_LIB_EXPORT bool deleteObject( const QgsGrassObject & object );
|
||||
|
||||
|
@ -37,9 +37,19 @@ extern "C"
|
||||
QgsGrassImport::QgsGrassImport( QgsGrassObject grassObject )
|
||||
: QObject()
|
||||
, mGrassObject( grassObject )
|
||||
, mFutureWatcher( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
QgsGrassImport::~QgsGrassImport()
|
||||
{
|
||||
if ( mFutureWatcher && !mFutureWatcher->isFinished() )
|
||||
{
|
||||
QgsDebugMsg( "mFutureWatcher not finished -> waitForFinished()" );
|
||||
mFutureWatcher->waitForFinished();
|
||||
}
|
||||
}
|
||||
|
||||
void QgsGrassImport::setError( QString error )
|
||||
{
|
||||
QgsDebugMsg( "error: " + error );
|
||||
@ -51,6 +61,34 @@ QString QgsGrassImport::error()
|
||||
return mError;
|
||||
}
|
||||
|
||||
void QgsGrassImport::importInThread()
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
mFutureWatcher = new QFutureWatcher<bool>( this );
|
||||
connect( mFutureWatcher, SIGNAL( finished() ), SLOT( onFinished() ) );
|
||||
mFutureWatcher->setFuture( QtConcurrent::run( run, this ) );
|
||||
}
|
||||
|
||||
bool QgsGrassImport::run( QgsGrassImport *imp )
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
imp->import();
|
||||
return true;
|
||||
}
|
||||
|
||||
void QgsGrassImport::onFinished()
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
emit finished( this );
|
||||
}
|
||||
|
||||
QStringList QgsGrassImport::names() const
|
||||
{
|
||||
QStringList list;
|
||||
list << mGrassObject.name();
|
||||
return list;
|
||||
}
|
||||
|
||||
//------------------------------ QgsGrassRasterImport ------------------------------------
|
||||
QgsGrassRasterImport::QgsGrassRasterImport( QgsRasterPipe* pipe, const QgsGrassObject& grassObject,
|
||||
const QgsRectangle &extent, int xSize, int ySize )
|
||||
@ -59,7 +97,6 @@ QgsGrassRasterImport::QgsGrassRasterImport( QgsRasterPipe* pipe, const QgsGrassO
|
||||
, mExtent( extent )
|
||||
, mXSize( xSize )
|
||||
, mYSize( ySize )
|
||||
, mFutureWatcher( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
@ -73,21 +110,6 @@ QgsGrassRasterImport::~QgsGrassRasterImport()
|
||||
delete mPipe;
|
||||
}
|
||||
|
||||
void QgsGrassRasterImport::importInThread()
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
mFutureWatcher = new QFutureWatcher<bool>( this );
|
||||
connect( mFutureWatcher, SIGNAL( finished() ), SLOT( onFinished() ) );
|
||||
mFutureWatcher->setFuture( QtConcurrent::run( run, this ) );
|
||||
}
|
||||
|
||||
bool QgsGrassRasterImport::run( QgsGrassRasterImport *imp )
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
imp->import();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QgsGrassRasterImport::import()
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
@ -152,7 +174,7 @@ bool QgsGrassRasterImport::import()
|
||||
{
|
||||
name += QString( "_%1" ).arg( band );
|
||||
}
|
||||
arguments.append( "output=" + name );
|
||||
arguments.append( "output=" + name ); // get list of all output names
|
||||
QTemporaryFile gisrcFile;
|
||||
QProcess* process = 0;
|
||||
try
|
||||
@ -236,13 +258,7 @@ bool QgsGrassRasterImport::import()
|
||||
return true;
|
||||
}
|
||||
|
||||
void QgsGrassRasterImport::onFinished()
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
emit finished( this );
|
||||
}
|
||||
|
||||
QString QgsGrassRasterImport::uri() const
|
||||
QString QgsGrassRasterImport::srcDescription() const
|
||||
{
|
||||
if ( !mPipe || !mPipe->provider() )
|
||||
{
|
||||
@ -285,7 +301,6 @@ QStringList QgsGrassRasterImport::names() const
|
||||
QgsGrassVectorImport::QgsGrassVectorImport( QgsVectorDataProvider* provider, const QgsGrassObject& grassObject )
|
||||
: QgsGrassImport( grassObject )
|
||||
, mProvider( provider )
|
||||
, mFutureWatcher( 0 )
|
||||
{
|
||||
}
|
||||
|
||||
@ -299,24 +314,8 @@ QgsGrassVectorImport::~QgsGrassVectorImport()
|
||||
delete mProvider;
|
||||
}
|
||||
|
||||
void QgsGrassVectorImport::importInThread()
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
mFutureWatcher = new QFutureWatcher<bool>( this );
|
||||
connect( mFutureWatcher, SIGNAL( finished() ), SLOT( onFinished() ) );
|
||||
mFutureWatcher->setFuture( QtConcurrent::run( run, this ) );
|
||||
}
|
||||
|
||||
bool QgsGrassVectorImport::run( QgsGrassVectorImport *imp )
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
imp->import();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool QgsGrassVectorImport::import()
|
||||
{
|
||||
|
||||
QgsDebugMsg( "entered" );
|
||||
|
||||
if ( !mProvider )
|
||||
@ -432,13 +431,7 @@ bool QgsGrassVectorImport::import()
|
||||
return true;
|
||||
}
|
||||
|
||||
void QgsGrassVectorImport::onFinished()
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
emit finished( this );
|
||||
}
|
||||
|
||||
QString QgsGrassVectorImport::uri() const
|
||||
QString QgsGrassVectorImport::srcDescription() const
|
||||
{
|
||||
if ( !mProvider )
|
||||
{
|
||||
@ -447,9 +440,36 @@ QString QgsGrassVectorImport::uri() const
|
||||
return mProvider->dataSourceUri();
|
||||
}
|
||||
|
||||
QStringList QgsGrassVectorImport::names() const
|
||||
//------------------------------ QgsGrassCopy ------------------------------------
|
||||
QgsGrassCopy::QgsGrassCopy( const QgsGrassObject& srcObject, const QgsGrassObject& destObject )
|
||||
: QgsGrassImport( destObject )
|
||||
, mSrcObject( srcObject )
|
||||
{
|
||||
QStringList list;
|
||||
list << mGrassObject.name();
|
||||
return list;
|
||||
}
|
||||
|
||||
QgsGrassCopy::~QgsGrassCopy()
|
||||
{
|
||||
}
|
||||
|
||||
bool QgsGrassCopy::import()
|
||||
{
|
||||
QgsDebugMsg( "entered" );
|
||||
|
||||
try
|
||||
{
|
||||
QgsGrass::copyObject( mSrcObject, mGrassObject );
|
||||
}
|
||||
catch ( QgsGrass::Exception &e )
|
||||
{
|
||||
setError( e.what() );
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
QString QgsGrassCopy::srcDescription() const
|
||||
{
|
||||
return mSrcObject.toString();
|
||||
}
|
||||
|
@ -30,22 +30,29 @@ class GRASS_LIB_EXPORT QgsGrassImport : public QObject
|
||||
Q_OBJECT
|
||||
public:
|
||||
QgsGrassImport( QgsGrassObject grassObject );
|
||||
virtual ~QgsGrassImport() {}
|
||||
virtual ~QgsGrassImport();
|
||||
QgsGrassObject grassObject() const { return mGrassObject; }
|
||||
virtual void importInThread() = 0;
|
||||
virtual QString uri() const = 0;
|
||||
virtual void importInThread();
|
||||
virtual bool import() = 0;
|
||||
// source description for error message purposes (maybe uri or something similar)
|
||||
virtual QString srcDescription() const = 0;
|
||||
// get error if import failed
|
||||
QString error();
|
||||
virtual QStringList names() const = 0;
|
||||
virtual QStringList names() const;
|
||||
|
||||
public slots:
|
||||
void onFinished();
|
||||
|
||||
signals:
|
||||
// sent when process finished
|
||||
void finished( QgsGrassImport *import );
|
||||
|
||||
protected:
|
||||
static bool run( QgsGrassImport *imp );
|
||||
void setError( QString error );
|
||||
QgsGrassObject mGrassObject;
|
||||
QString mError;
|
||||
QFutureWatcher<bool>* mFutureWatcher;
|
||||
};
|
||||
|
||||
class GRASS_LIB_EXPORT QgsGrassRasterImport : public QgsGrassImport
|
||||
@ -56,22 +63,18 @@ class GRASS_LIB_EXPORT QgsGrassRasterImport : public QgsGrassImport
|
||||
QgsGrassRasterImport( QgsRasterPipe* pipe, const QgsGrassObject& grassObject,
|
||||
const QgsRectangle &extent, int xSize, int ySize );
|
||||
~QgsGrassRasterImport();
|
||||
bool import();
|
||||
void importInThread() override;
|
||||
QString uri() const override;
|
||||
bool import() override;
|
||||
QString srcDescription() const override;
|
||||
// get list of extensions (for bands)
|
||||
static QStringList extensions( QgsRasterDataProvider* provider );
|
||||
// get list of all output names (basename + extension for each band)
|
||||
QStringList names() const override;
|
||||
public slots:
|
||||
void onFinished();
|
||||
|
||||
private:
|
||||
static bool run( QgsGrassRasterImport *imp );
|
||||
QgsRasterPipe* mPipe;
|
||||
QgsRectangle mExtent;
|
||||
int mXSize;
|
||||
int mYSize;
|
||||
QFutureWatcher<bool>* mFutureWatcher;
|
||||
};
|
||||
|
||||
class GRASS_LIB_EXPORT QgsGrassVectorImport : public QgsGrassImport
|
||||
@ -81,17 +84,26 @@ class GRASS_LIB_EXPORT QgsGrassVectorImport : public QgsGrassImport
|
||||
// takes provider ownership
|
||||
QgsGrassVectorImport( QgsVectorDataProvider* provider, const QgsGrassObject& grassObject );
|
||||
~QgsGrassVectorImport();
|
||||
bool import();
|
||||
void importInThread() override;
|
||||
QString uri() const override;
|
||||
// get list of all output names
|
||||
QStringList names() const override;
|
||||
public slots:
|
||||
void onFinished();
|
||||
bool import() override;
|
||||
QString srcDescription() const override;
|
||||
|
||||
private:
|
||||
static bool run( QgsGrassVectorImport *imp );
|
||||
QgsVectorDataProvider* mProvider;
|
||||
QFutureWatcher<bool>* mFutureWatcher;
|
||||
};
|
||||
|
||||
class GRASS_LIB_EXPORT QgsGrassCopy : public QgsGrassImport
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
// takes provider ownership
|
||||
QgsGrassCopy( const QgsGrassObject& srcObject, const QgsGrassObject& destObject );
|
||||
~QgsGrassCopy();
|
||||
bool import() override;
|
||||
QString srcDescription() const override;
|
||||
|
||||
private:
|
||||
QgsGrassObject mSrcObject;
|
||||
|
||||
};
|
||||
|
||||
#endif // QGSGRASSIMPORT_H
|
||||
|
@ -46,12 +46,6 @@ QgsGrassLocationItem::QgsGrassLocationItem( QgsDataItem* parent, QString dirPath
|
||||
mType = QgsDataItem::Directory;
|
||||
}
|
||||
|
||||
bool QgsGrassLocationItem::isLocation( QString path )
|
||||
{
|
||||
//QgsDebugMsg( "path = " + path );
|
||||
return QFile::exists( path + "/" + "PERMANENT" + "/" + "DEFAULT_WIND" );
|
||||
}
|
||||
|
||||
QVector<QgsDataItem*>QgsGrassLocationItem::createChildren()
|
||||
{
|
||||
QVector<QgsDataItem*> mapsets;
|
||||
@ -63,7 +57,7 @@ QVector<QgsDataItem*>QgsGrassLocationItem::createChildren()
|
||||
{
|
||||
QString path = dir.absoluteFilePath( name );
|
||||
|
||||
if ( QgsGrassMapsetItem::isMapset( path ) )
|
||||
if ( QgsGrass::isMapset( path ) )
|
||||
{
|
||||
QgsGrassMapsetItem * mapset = new QgsGrassMapsetItem( this, path, mPath + "/" + name );
|
||||
mapsets.append( mapset );
|
||||
@ -89,11 +83,6 @@ QgsGrassMapsetItem::QgsGrassMapsetItem( QgsDataItem* parent, QString dirPath, QS
|
||||
mIconName = "grass_mapset.png";
|
||||
}
|
||||
|
||||
bool QgsGrassMapsetItem::isMapset( QString path )
|
||||
{
|
||||
return QFile::exists( path + "/WIND" );
|
||||
}
|
||||
|
||||
QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
|
||||
{
|
||||
QgsDebugMsg( "Entered" );
|
||||
@ -122,8 +111,8 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
|
||||
// somewhere not properly escaped (there was bug in QgsMimeDataUtils for example)
|
||||
QString uri = mDirPath + "/" + name + "/" + layerName;
|
||||
QgsLayerItem::LayerType layerType = QgsLayerItem::Vector;
|
||||
QString typeName = layerName.split( "_" )[1];
|
||||
QString baseLayerName = layerName.split( "_" )[0];
|
||||
QString typeName = layerName.split( "_" ).value( 1 );
|
||||
QString baseLayerName = layerName.split( "_" ).value( 0 );
|
||||
|
||||
if ( typeName == "point" )
|
||||
layerType = QgsLayerItem::Point;
|
||||
@ -135,8 +124,8 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
|
||||
QString layerPath = mapPath + "/" + layerName;
|
||||
if ( !map )
|
||||
{
|
||||
/* This may happen (one layer only) in GRASS 7 with points (no topo layers) */
|
||||
QgsLayerItem *layer = new QgsGrassVectorLayerItem( this, vectorObject, name + " " + baseLayerName, layerPath, uri, layerType, true );
|
||||
/* This may happen (one layer only) in GRASS 7 with points (no topo layers) or if topo layers are disabled */
|
||||
QgsLayerItem *layer = new QgsGrassVectorLayerItem( this, vectorObject, name, layerPath, uri, layerType, true );
|
||||
//layer->setState( Populated );
|
||||
items.append( layer );
|
||||
}
|
||||
@ -228,53 +217,93 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
|
||||
QStringList extensions;
|
||||
QStringList existingNames;
|
||||
QRegExp regExp;
|
||||
QgsGrassObject srcObject;
|
||||
QString srcName;
|
||||
|
||||
// use g.copy for GRASS maps in the same location
|
||||
bool useCopy = false;
|
||||
|
||||
if ( u.layerType == "raster" )
|
||||
{
|
||||
rasterProvider = qobject_cast<QgsRasterDataProvider*>( QgsProviderRegistry::instance()->provider( u.providerKey, u.uri ) );
|
||||
provider = rasterProvider;
|
||||
if ( u.providerKey == "grassraster" && srcObject.setFromUri( u.uri )
|
||||
&& srcObject.locationIdentical( mapsetObject ) )
|
||||
{
|
||||
useCopy = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
rasterProvider = qobject_cast<QgsRasterDataProvider*>( QgsProviderRegistry::instance()->provider( u.providerKey, u.uri ) );
|
||||
provider = rasterProvider;
|
||||
}
|
||||
existingNames = existingRasters;
|
||||
regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Raster );
|
||||
}
|
||||
else if ( u.layerType == "vector" )
|
||||
{
|
||||
vectorProvider = qobject_cast<QgsVectorDataProvider*>( QgsProviderRegistry::instance()->provider( u.providerKey, u.uri ) );
|
||||
provider = vectorProvider;
|
||||
if ( u.providerKey == "grass" && srcObject.setFromUri( u.uri )
|
||||
&& srcObject.locationIdentical( mapsetObject ) )
|
||||
{
|
||||
useCopy = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
vectorProvider = qobject_cast<QgsVectorDataProvider*>( QgsProviderRegistry::instance()->provider( u.providerKey, u.uri ) );
|
||||
provider = vectorProvider;
|
||||
}
|
||||
existingNames = existingVectors;
|
||||
regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Vector );
|
||||
}
|
||||
QgsDebugMsg( "existingNames = " + existingNames.join( "," ) );
|
||||
|
||||
if ( !provider )
|
||||
if ( useCopy )
|
||||
{
|
||||
errors.append( tr( "Cannot create provider %1 : %2" ).arg( u.providerKey ).arg( u.uri ) );
|
||||
continue;
|
||||
srcName = srcObject.name();
|
||||
}
|
||||
if ( !provider->isValid() )
|
||||
else
|
||||
{
|
||||
errors.append( tr( "Provider is not valid %1 : %2" ).arg( u.providerKey ).arg( u.uri ) );
|
||||
delete provider;
|
||||
continue;
|
||||
if ( !provider )
|
||||
{
|
||||
errors.append( tr( "Cannot create provider %1 : %2" ).arg( u.providerKey ).arg( u.uri ) );
|
||||
continue;
|
||||
}
|
||||
if ( !provider->isValid() )
|
||||
{
|
||||
errors.append( tr( "Provider is not valid %1 : %2" ).arg( u.providerKey ).arg( u.uri ) );
|
||||
delete provider;
|
||||
continue;
|
||||
}
|
||||
if ( u.layerType == "raster" )
|
||||
{
|
||||
extensions = QgsGrassRasterImport::extensions( rasterProvider );
|
||||
}
|
||||
srcName = u.name;
|
||||
}
|
||||
|
||||
if ( u.layerType == "raster" )
|
||||
// TODO: add a method in QgsGrass to convert a name to GRASS valid name
|
||||
QString destName = srcName.replace( " ", "_" );
|
||||
if ( QgsNewNameDialog::exists( destName, extensions, existingNames, caseSensitivity ) )
|
||||
{
|
||||
extensions = QgsGrassRasterImport::extensions( rasterProvider );
|
||||
}
|
||||
|
||||
QString newName = u.name;
|
||||
if ( QgsNewNameDialog::exists( u.name, extensions, existingNames, caseSensitivity ) )
|
||||
{
|
||||
QgsNewNameDialog dialog( u.name, u.name, extensions, existingNames, regExp, caseSensitivity );
|
||||
QgsNewNameDialog dialog( srcName, destName, extensions, existingNames, regExp, caseSensitivity );
|
||||
if ( dialog.exec() != QDialog::Accepted )
|
||||
{
|
||||
delete provider;
|
||||
continue;
|
||||
}
|
||||
newName = dialog.name();
|
||||
destName = dialog.name();
|
||||
}
|
||||
|
||||
QgsGrassImport *import = 0;
|
||||
if ( u.layerType == "raster" )
|
||||
QStringList newNames;
|
||||
if ( useCopy )
|
||||
{
|
||||
QgsDebugMsg( "location is the same -> g.copy" );
|
||||
QgsGrassObject destObject( mapsetObject );
|
||||
destObject.setName( destName );
|
||||
destObject.setType( srcObject.type() );
|
||||
import = new QgsGrassCopy( srcObject, destObject );
|
||||
}
|
||||
|
||||
else if ( u.layerType == "raster" )
|
||||
{
|
||||
QgsRectangle newExtent = rasterProvider->extent();
|
||||
int newXSize;
|
||||
@ -298,6 +327,7 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
|
||||
continue;
|
||||
}
|
||||
newXSize = window.cols;
|
||||
|
||||
newYSize = window.rows;
|
||||
|
||||
newExtent = QgsGrass::extent( &window );
|
||||
@ -326,13 +356,13 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
|
||||
QgsDebugMsg( QString( "newXSize = %1 newYSize = %2" ).arg( newXSize ).arg( newYSize ) );
|
||||
|
||||
QString path = mPath + "/" + "raster" + "/" + u.name;
|
||||
QgsGrassObject rasterObject( mGisdbase, mLocation, mName, newName, QgsGrassObject::Raster );
|
||||
QgsGrassObject rasterObject( mGisdbase, mLocation, mName, destName, QgsGrassObject::Raster );
|
||||
import = new QgsGrassRasterImport( pipe, rasterObject, newExtent, newXSize, newYSize ); // takes pipe ownership
|
||||
}
|
||||
else if ( u.layerType == "vector" )
|
||||
{
|
||||
QString path = mPath + "/" + "raster" + "/" + u.name;
|
||||
QgsGrassObject vectorObject( mGisdbase, mLocation, mName, newName, QgsGrassObject::Vector );
|
||||
QgsGrassObject vectorObject( mGisdbase, mLocation, mName, destName, QgsGrassObject::Vector );
|
||||
import = new QgsGrassVectorImport( vectorProvider, vectorObject ); // takes provider ownership
|
||||
}
|
||||
|
||||
@ -390,7 +420,7 @@ void QgsGrassMapsetItem::onImportFinished( QgsGrassImport* import )
|
||||
{
|
||||
QgsMessageOutput *output = QgsMessageOutput::createMessageOutput();
|
||||
output->setTitle( tr( "Import to GRASS mapset failed" ) );
|
||||
output->setMessage( tr( "Failed to import %1 to %2: %3" ).arg( import->uri() ).arg( import->grassObject()
|
||||
output->setMessage( tr( "Failed to import %1 to %2: %3" ).arg( import->srcDescription() ).arg( import->grassObject()
|
||||
.mapsetPath() ).arg( import->error() ), QgsMessageOutput::MessageText );
|
||||
output->showMessage();
|
||||
}
|
||||
@ -553,12 +583,20 @@ QgsGrassVectorLayerItem::QgsGrassVectorLayerItem( QgsDataItem* parent, QgsGrassO
|
||||
: QgsGrassObjectItem( parent, grassObject, layerName, path, uri, layerType, "grass", mSingleLayer )
|
||||
, mSingleLayer( singleLayer )
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
QString QgsGrassVectorLayerItem::layerName() const
|
||||
{
|
||||
// to get map + layer when added from browser
|
||||
return mGrassObject.name() + " " + name();
|
||||
if ( mSingleLayer )
|
||||
{
|
||||
return name();
|
||||
}
|
||||
else
|
||||
{
|
||||
// to get map + layer when added from browser
|
||||
return mGrassObject.name() + " " + name();
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------- QgsGrassRasterItem ------------------------------
|
||||
@ -588,7 +626,7 @@ QGISEXTERN int dataCapabilities()
|
||||
|
||||
QGISEXTERN QgsDataItem * dataItem( QString theDirPath, QgsDataItem* parentItem )
|
||||
{
|
||||
if ( QgsGrassLocationItem::isLocation( theDirPath ) )
|
||||
if ( QgsGrass::isLocation( theDirPath ) )
|
||||
{
|
||||
QString path;
|
||||
QDir dir( theDirPath );
|
||||
|
@ -29,7 +29,6 @@ class QgsGrassLocationItem : public QgsDirectoryItem
|
||||
|
||||
QIcon icon() override { return QgsDataItem::icon(); }
|
||||
|
||||
static bool isLocation( QString path );
|
||||
QVector<QgsDataItem*> createChildren() override;
|
||||
};
|
||||
|
||||
@ -41,7 +40,6 @@ class QgsGrassMapsetItem : public QgsDirectoryItem
|
||||
|
||||
QIcon icon() override { return QgsDataItem::icon(); }
|
||||
|
||||
static bool isMapset( QString path );
|
||||
QVector<QgsDataItem*> createChildren() override;
|
||||
virtual bool acceptDrop() override { return true; }
|
||||
virtual bool handleDrop( const QMimeData * data, Qt::DropAction action ) override;
|
||||
|
Loading…
x
Reference in New Issue
Block a user