Skip to content

Commit eaeea21

Browse files
committedMay 20, 2015
[GRASS][FEATURE] rename maps in browser
1 parent b89b6a5 commit eaeea21

File tree

4 files changed

+171
-27
lines changed

4 files changed

+171
-27
lines changed
 

‎src/providers/grass/qgsgrass.cpp

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,42 @@ QString QgsGrassObject::elementName( Type type )
105105
return "";
106106
}
107107

108+
QString QgsGrassObject::dirName() const
109+
{
110+
return dirName( mType );
111+
}
112+
113+
QString QgsGrassObject::dirName( Type type )
114+
{
115+
if ( type == Raster )
116+
return "cellhd";
117+
else if ( type == Vector )
118+
return "vector";
119+
else if ( type == Region )
120+
return "windows";
121+
else
122+
return "";
123+
}
124+
108125
bool QgsGrassObject::mapsetIdentical( const QgsGrassObject &other )
109126
{
110127
return mGisdbase == other.mGisdbase && mLocation == other.mLocation && mMapset == other.mMapset;
111128
}
112129

130+
QRegExp QgsGrassObject::newNameRegExp( Type type )
131+
{
132+
QRegExp rx;
133+
if ( type == QgsGrassObject::Vector )
134+
{
135+
rx.setPattern( "[A-Za-z_][A-Za-z0-9_]+" );
136+
}
137+
else
138+
{
139+
rx.setPattern( "[A-Za-z0-9_.]+" );
140+
}
141+
return rx;
142+
}
143+
113144
#ifdef Q_OS_WIN
114145
#include <windows.h>
115146
QString GRASS_LIB_EXPORT QgsGrass::shortPath( const QString &path )
@@ -1085,15 +1116,22 @@ QStringList GRASS_LIB_EXPORT QgsGrass::elements( const QString& gisdbase, const
10851116

10861117
QStringList GRASS_LIB_EXPORT QgsGrass::elements( const QString& mapsetPath, const QString& element )
10871118
{
1088-
QgsDebugMsg( QString( "mapsetPath = %1" ).arg( mapsetPath ) );
1119+
QgsDebugMsg( QString( "mapsetPath = %1 element = %2" ).arg( mapsetPath ).arg( element ) );
10891120

10901121
QStringList list;
10911122

10921123
if ( mapsetPath.isEmpty() )
10931124
return list;
10941125

10951126
QDir d = QDir( mapsetPath + "/" + element );
1096-
d.setFilter( QDir::Files );
1127+
if ( element == "vector" )
1128+
{
1129+
d.setFilter( QDir::Dirs | QDir::NoDotAndDotDot );
1130+
}
1131+
else
1132+
{
1133+
d.setFilter( QDir::Files );
1134+
}
10971135

10981136
for ( unsigned int i = 0; i < d.count(); i++ )
10991137
{
@@ -1102,17 +1140,15 @@ QStringList GRASS_LIB_EXPORT QgsGrass::elements( const QString& mapsetPath, con
11021140
return list;
11031141
}
11041142

1105-
bool GRASS_LIB_EXPORT QgsGrass::objectExists( const QgsGrassObject& grassObject )
1143+
QStringList GRASS_LIB_EXPORT QgsGrass::grassObjects( const QString& mapsetPath, QgsGrassObject::Type type )
11061144
{
1107-
QString path = grassObject.mapsetPath();
1108-
if ( grassObject.type() == QgsGrassObject::Raster )
1109-
path += "/cellhd";
1110-
else if ( grassObject.type() == QgsGrassObject::Vector )
1111-
path += "/vector";
1112-
else if ( grassObject.type() == QgsGrassObject::Region )
1113-
path += "/windows";
1145+
return QgsGrass::elements( mapsetPath, QgsGrassObject::dirName( type ) );
1146+
}
11141147

1115-
path += "/" + grassObject.name();
1148+
bool GRASS_LIB_EXPORT QgsGrass::objectExists( const QgsGrassObject& grassObject )
1149+
{
1150+
QString path = grassObject.mapsetPath() + "/" + QgsGrassObject::dirName( grassObject.type() )
1151+
+ "/" + grassObject.name();
11161152
QFileInfo fi( path );
11171153
return fi.exists();
11181154
}
@@ -1748,6 +1784,19 @@ QMap<QString, QString> GRASS_LIB_EXPORT QgsGrass::query( QString gisdbase, QStri
17481784
return result;
17491785
}
17501786

1787+
void QgsGrass::renameObject( const QgsGrassObject & object, const QString& newName )
1788+
{
1789+
QgsDebugMsg( "entered" );
1790+
QString cmd = "g.rename";
1791+
QStringList arguments;
1792+
1793+
arguments << object.elementShort() + "=" + object.name() + "," + newName;
1794+
1795+
int timeout = 10000; // What timeout to use? It can take long time on network or database
1796+
// throws QgsGrass::Exception
1797+
QgsGrass::runModule( object.gisdbase(), object.location(), object.mapset(), cmd, arguments, timeout, false );
1798+
}
1799+
17511800
bool QgsGrass::deleteObject( const QgsGrassObject & object )
17521801
{
17531802
QgsDebugMsg( "entered" );
@@ -1950,6 +1999,15 @@ QString GRASS_LIB_EXPORT QgsGrass::versionString()
19501999
return QString( GRASS_VERSION_STRING );
19512000
}
19522001

2002+
Qt::CaseSensitivity GRASS_LIB_EXPORT QgsGrass::caseSensitivity()
2003+
{
2004+
#ifdef WIN32
2005+
return Qt::CaseInsensitive;
2006+
#else
2007+
return Qt::CaseSensitive;
2008+
#endif
2009+
}
2010+
19532011
bool GRASS_LIB_EXPORT QgsGrass::isMapset( QString path )
19542012
{
19552013
#if 0

‎src/providers/grass/qgsgrass.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ extern "C"
3939
#include <QString>
4040
#include <QMap>
4141
#include <QHash>
42+
#include <QRegExp>
4243
#include <QTemporaryFile>
4344
class QgsCoordinateReferenceSystem;
4445
class QgsRectangle;
@@ -93,8 +94,13 @@ class GRASS_LIB_EXPORT QgsGrassObject
9394
// descriptive full name
9495
QString elementName() const;
9596
static QString elementName( Type type );
97+
// name of directory in GRASS mapset to look for the object (cellhd,vector,window)
98+
QString dirName() const;
99+
static QString dirName( Type type );
96100
// returns true if gisdbase, location and mapset are the same
97101
bool mapsetIdentical( const QgsGrassObject &other );
102+
// get regexp patter for new names, e.g. vectors should not start with number
103+
static QRegExp newNameRegExp( Type type );
98104
private:
99105
QString mGisdbase;
100106
QString mLocation;
@@ -216,10 +222,14 @@ class QgsGrass
216222
const QString& mapset, const QString& mapName );
217223

218224
//! List of elements
225+
// TODO rename elements to objects
219226
static GRASS_LIB_EXPORT QStringList elements( const QString& gisdbase, const QString& locationName,
220227
const QString& mapsetName, const QString& element );
221228
static GRASS_LIB_EXPORT QStringList elements( const QString& mapsetPath, const QString& element );
222229

230+
//! List of existing objects
231+
static GRASS_LIB_EXPORT QStringList grassObjects( const QString& mapsetPath, QgsGrassObject::Type type );
232+
223233
// returns true if object (vector, raster, region) exists
224234
static GRASS_LIB_EXPORT bool objectExists( const QgsGrassObject& grassObject );
225235

@@ -344,6 +354,9 @@ class QgsGrass
344354
static GRASS_LIB_EXPORT QMap<QString, QString> query( QString gisdbase, QString location,
345355
QString mapset, QString map, QgsGrassObject::Type type, double x, double y );
346356

357+
// ! Rename GRASS object, throws QgsGrass::Exception
358+
static GRASS_LIB_EXPORT void renameObject( const QgsGrassObject & object, const QString& newName );
359+
347360
// ! Delete map
348361
static GRASS_LIB_EXPORT bool deleteObject( const QgsGrassObject & object );
349362

@@ -365,6 +378,8 @@ class QgsGrass
365378
static GRASS_LIB_EXPORT int versionRelease();
366379
static GRASS_LIB_EXPORT QString versionString();
367380

381+
// files case sensitivity (insensitive on windows)
382+
static GRASS_LIB_EXPORT Qt::CaseSensitivity caseSensitivity();
368383
// set environment variable
369384
static GRASS_LIB_EXPORT void putEnv( QString name, QString value );
370385

‎src/providers/grass/qgsgrassprovidermodule.cpp

Lines changed: 80 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <QFileInfo>
3030
#include <QDir>
3131
#include <QLabel>
32+
#include <QObject>
3233

3334
//----------------------- QgsGrassLocationItem ------------------------------
3435

@@ -212,11 +213,8 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
212213

213214
QStringList errors;
214215
QgsMimeDataUtils::UriList lst = QgsMimeDataUtils::decodeUriList( data );
215-
#ifdef WIN32
216-
Qt::CaseSensitivity caseSensitivity = Qt::CaseInsensitive;
217-
#else
218-
Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive;
219-
#endif
216+
Qt::CaseSensitivity caseSensitivity = QgsGrass::caseSensitivity();
217+
220218
foreach ( const QgsMimeDataUtils::Uri& u, lst )
221219
{
222220
if ( u.layerType != "raster" && u.layerType != "vector" )
@@ -229,17 +227,20 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
229227
QgsDataProvider* provider = 0;
230228
QStringList extensions;
231229
QStringList existingNames;
230+
QRegExp regExp;
232231
if ( u.layerType == "raster" )
233232
{
234233
rasterProvider = qobject_cast<QgsRasterDataProvider*>( QgsProviderRegistry::instance()->provider( u.providerKey, u.uri ) );
235234
provider = rasterProvider;
236235
existingNames = existingRasters;
236+
regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Raster );
237237
}
238238
else if ( u.layerType == "vector" )
239239
{
240240
vectorProvider = qobject_cast<QgsVectorDataProvider*>( QgsProviderRegistry::instance()->provider( u.providerKey, u.uri ) );
241241
provider = vectorProvider;
242242
existingNames = existingVectors;
243+
regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Vector );
243244
}
244245
QgsDebugMsg( "existingNames = " + existingNames.join( "," ) );
245246

@@ -263,7 +264,7 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
263264
QString newName = u.name;
264265
if ( QgsNewNameDialog::exists( u.name, extensions, existingNames, caseSensitivity ) )
265266
{
266-
QgsNewNameDialog dialog( u.name, u.name, extensions, existingNames, QRegExp(), caseSensitivity );
267+
QgsNewNameDialog dialog( u.name, u.name, extensions, existingNames, regExp, caseSensitivity );
267268
if ( dialog.exec() != QDialog::Accepted )
268269
{
269270
delete provider;
@@ -374,10 +375,9 @@ bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
374375

375376
if ( !errors.isEmpty() )
376377
{
377-
QgsMessageOutput *output = QgsMessageOutput::createMessageOutput();
378-
output->setTitle( tr( "Import to GRASS mapset" ) );
379-
output->setMessage( tr( "Failed to import some layers!\n\n" ) + errors.join( "\n" ), QgsMessageOutput::MessageText );
380-
output->showMessage();
378+
QgsMessageOutput::showMessage( tr( "Import to GRASS mapset" ),
379+
tr( "Failed to import some layers!\n\n" ) + errors.join( "\n" ),
380+
QgsMessageOutput::MessageText );
381381
}
382382

383383
return true;
@@ -407,6 +407,54 @@ QgsGrassObjectItemBase::QgsGrassObjectItemBase( QgsGrassObject grassObject ) :
407407
{
408408
}
409409

410+
void QgsGrassObjectItemBase::renameGrassObject( QgsDataItem* parent )
411+
{
412+
QgsDebugMsg( "Entered" );
413+
414+
QStringList existingNames = QgsGrass::grassObjects( mGrassObject.mapsetPath(), mGrassObject.type() );
415+
// remove current name to avoid warning that exists
416+
existingNames.removeOne( mGrassObject.name() );
417+
QgsDebugMsg( "existingNames = " + existingNames.join( "," ) );
418+
QRegExp regExp = QgsGrassObject::newNameRegExp( mGrassObject.type() );
419+
Qt::CaseSensitivity caseSensitivity = QgsGrass::caseSensitivity();
420+
QgsNewNameDialog dialog( mGrassObject.name(), mGrassObject.name(), QStringList(), existingNames, regExp, caseSensitivity );
421+
422+
if ( dialog.exec() != QDialog::Accepted || dialog.name() == mGrassObject.name() )
423+
{
424+
return;
425+
}
426+
427+
QgsDebugMsg( "rename " + mGrassObject.name() + " -> " + dialog.name() );
428+
429+
QgsGrassObject obj( mGrassObject );
430+
obj.setName( dialog.name() );
431+
QString errorTitle = QObject::tr( "Rename GRASS %1" ).arg( mGrassObject.elementName() );
432+
if ( QgsGrass::objectExists( obj ) )
433+
{
434+
QgsDebugMsg( obj.name() + " exists -> delete" );
435+
if ( !QgsGrass::deleteObject( obj ) )
436+
{
437+
QgsMessageOutput::showMessage( errorTitle, QObject::tr( "Cannot delete %1" ).arg( obj.name() ), QgsMessageOutput::MessageText );
438+
return;
439+
}
440+
}
441+
442+
try
443+
{
444+
QgsGrass::renameObject( mGrassObject, obj.name() );
445+
if ( parent )
446+
{
447+
parent->refresh();
448+
}
449+
}
450+
catch ( QgsGrass::Exception &e )
451+
{
452+
QgsMessageOutput::showMessage( errorTitle,
453+
QObject::tr( "Cannot rename %1 to %2" ).arg( mGrassObject.name() ).arg( obj.name() ) + "\n" + e.what(),
454+
QgsMessageOutput::MessageText );
455+
}
456+
}
457+
410458
void QgsGrassObjectItemBase::deleteGrassObject( QgsDataItem* parent )
411459
{
412460
QgsDebugMsg( "Entered" );
@@ -426,10 +474,10 @@ void QgsGrassObjectItemBase::deleteGrassObject( QgsDataItem* parent )
426474
QgsGrassObjectItem::QgsGrassObjectItem( QgsDataItem* parent, QgsGrassObject grassObject,
427475
QString name, QString path, QString uri,
428476
LayerType layerType, QString providerKey,
429-
bool deleteAction )
477+
bool showObjectActions )
430478
: QgsLayerItem( parent, name, path, uri, layerType, providerKey )
431479
, QgsGrassObjectItemBase( grassObject )
432-
, mDeleteAction( deleteAction )
480+
, mShowObjectActions( showObjectActions )
433481
{
434482
setState( Populated ); // no children, to show non expandable in browser
435483
}
@@ -438,8 +486,12 @@ QList<QAction*> QgsGrassObjectItem::actions()
438486
{
439487
QList<QAction*> lst;
440488

441-
if ( mDeleteAction )
489+
if ( mShowObjectActions )
442490
{
491+
QAction* actionRename = new QAction( tr( "Rename" ), this );
492+
connect( actionRename, SIGNAL( triggered() ), this, SLOT( renameGrassObject() ) );
493+
lst.append( actionRename );
494+
443495
QAction* actionDelete = new QAction( tr( "Delete" ), this );
444496
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteGrassObject() ) );
445497
lst.append( actionDelete );
@@ -448,6 +500,11 @@ QList<QAction*> QgsGrassObjectItem::actions()
448500
return lst;
449501
}
450502

503+
void QgsGrassObjectItem::renameGrassObject()
504+
{
505+
QgsGrassObjectItemBase::renameGrassObject( parent() );
506+
}
507+
451508
void QgsGrassObjectItem::deleteGrassObject()
452509
{
453510
QgsGrassObjectItemBase::deleteGrassObject( parent() );
@@ -467,13 +524,22 @@ QList<QAction*> QgsGrassVectorItem::actions()
467524
{
468525
QList<QAction*> lst;
469526

527+
QAction* actionRename = new QAction( tr( "Rename" ), this );
528+
connect( actionRename, SIGNAL( triggered() ), this, SLOT( renameGrassObject() ) );
529+
lst.append( actionRename );
530+
470531
QAction* actionDelete = new QAction( tr( "Delete" ), this );
471532
connect( actionDelete, SIGNAL( triggered() ), this, SLOT( deleteGrassObject() ) );
472533
lst.append( actionDelete );
473534

474535
return lst;
475536
}
476537

538+
void QgsGrassVectorItem::renameGrassObject()
539+
{
540+
QgsGrassObjectItemBase::renameGrassObject( parent() );
541+
}
542+
477543
void QgsGrassVectorItem::deleteGrassObject()
478544
{
479545
QgsGrassObjectItemBase::deleteGrassObject( parent() );
@@ -484,7 +550,7 @@ void QgsGrassVectorItem::deleteGrassObject()
484550
QgsGrassVectorLayerItem::QgsGrassVectorLayerItem( QgsDataItem* parent, QgsGrassObject grassObject, QString layerName,
485551
QString path, QString uri,
486552
LayerType layerType, bool singleLayer )
487-
: QgsGrassObjectItem( parent, grassObject, layerName, path, uri, layerType, "grass" )
553+
: QgsGrassObjectItem( parent, grassObject, layerName, path, uri, layerType, "grass", mSingleLayer )
488554
, mSingleLayer( singleLayer )
489555
{
490556
}

‎src/providers/grass/qgsgrassprovidermodule.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ class QgsGrassObjectItemBase
6262
QgsGrassObjectItemBase( QgsGrassObject grassObject );
6363

6464
public:
65+
void renameGrassObject( QgsDataItem* parent );
6566
void deleteGrassObject( QgsDataItem* parent );
6667

6768
protected:
@@ -75,16 +76,19 @@ class QgsGrassObjectItem : public QgsLayerItem, public QgsGrassObjectItemBase
7576
QgsGrassObjectItem( QgsDataItem* parent, QgsGrassObject grassObject,
7677
QString name, QString path, QString uri,
7778
LayerType layerType, QString providerKey,
78-
bool deleteAction = true );
79+
bool showObjectActions = true );
7980

8081
virtual QList<QAction*> actions() override;
8182

8283
public slots:
84+
void renameGrassObject();
8385
void deleteGrassObject();
8486

8587
protected:
8688
//QgsGrassObject mGrassObject;
87-
bool mDeleteAction;
89+
// indicates if it is really GRASS object like raster or vector map,
90+
// for example
91+
bool mShowObjectActions;
8892
};
8993

9094
// Vector is collection of layers
@@ -98,6 +102,7 @@ class QgsGrassVectorItem : public QgsDataCollectionItem, public QgsGrassObjectIt
98102
virtual QList<QAction*> actions() override;
99103

100104
public slots:
105+
void renameGrassObject();
101106
void deleteGrassObject();
102107

103108
private:

0 commit comments

Comments
 (0)
Please sign in to comment.