Skip to content

Commit

Permalink
[GRASS] create new mapset from browser
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Sep 25, 2015
1 parent 11f28fc commit e47a97b
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 37 deletions.
39 changes: 5 additions & 34 deletions src/plugins/grass/qgsgrassnewmapset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1298,42 +1298,13 @@ void QgsGrassNewMapset::createMapset()

if ( mapset != "PERMANENT" )
{
QString locationPath = gisdbase() + "/" + location;
QDir locationDir( locationPath );

if ( !locationDir.mkdir( mapset ) )
{
QgsGrass::warning( tr( "Cannot create new mapset directory" ) );
return;
}

// Copy WIND Better way to copy file in Qt?
QStringList lines;
QFile in( locationPath + "/PERMANENT/DEFAULT_WIND" );
if ( !in.open( QIODevice::ReadOnly ) )
{
QgsGrass::warning( tr( "Cannot open DEFAULT_WIND" ) );
return;
}

QFile out( locationPath + "/" + mapset + "/WIND" );
if ( !out.open( QIODevice::WriteOnly ) )
{
QgsGrass::warning( tr( "Cannot open WIND" ) );
return;
}
QTextStream stream( &out );

//QTextStream stream( &file );
QString line;
char buf[100];
while ( in.readLine( buf, 100 ) != -1 )
QString error;
QgsGrass::createMapset( gisdbase(), location, mapset, error );
if ( !error.isEmpty() )
{
stream << buf;
QgsGrass::warning( tr( "Cannot create new mapset: %1" ).arg( error ) );
}

in.close();
out.close();
return;
}

if ( mOpenNewMapsetCheckBox->isChecked() )
Expand Down
25 changes: 23 additions & 2 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,9 +249,9 @@ QRegExp QgsGrassObject::newNameRegExp( Type type )
{
rx.setPattern( "[A-Za-z_][A-Za-z0-9_]+" );
}
else
else // location, raster, see G_legal_filename
{
rx.setPattern( "[A-Za-z0-9_\\-][A-Za-z0-9_\\-.]+" );
rx.setPattern( "[\\w_\\-][\\w_\\-.]+" );
}
return rx;
}
Expand Down Expand Up @@ -969,6 +969,27 @@ void QgsGrass::saveMapset()
getDefaultMapset() );
}

void QgsGrass::createMapset( const QString& gisdbase, const QString& location,
const QString& mapset, QString& error )
{
QgsDebugMsg( "entered." );
QString locationPath = gisdbase + "/" + location;
QDir locationDir( locationPath );

if ( !locationDir.mkdir( mapset ) )
{
error = tr( "Cannot create new mapset directory" );
return;
}

QString src = locationPath + "/PERMANENT/DEFAULT_WIND";
QString dest = locationPath + "/" + mapset + "/WIND";
if ( !QFile::copy( src, dest ) )
{
error = tr( "Cannot copy %1 to %2" ).arg( src ).arg( dest );
}
}

QStringList QgsGrass::locations( const QString& gisdbase )
{
QgsDebugMsg( QString( "gisdbase = %1" ).arg( gisdbase ) );
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 @@ -244,6 +244,10 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
/** \brief Save current mapset to project file. */
static void saveMapset();

/** Create new mapset in existing location */
static void createMapset( const QString& gisdbase, const QString& location,
const QString& mapset, QString& error );

//! Check if given directory contains a GRASS installation
static bool isValidGrassBaseDir( const QString& gisbase );

Expand Down
33 changes: 32 additions & 1 deletion src/providers/grass/qgsgrassprovidermodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ QList<QAction*> QgsGrassItemActions::actions()
list << optionsAction;

// TODO: add icons to provider
// TODO: check mapset ownership
// TODO: check ownership
if ( mGrassObject.type() == QgsGrassObject::Location )
{
QAction* newMapsetAction = new QAction( QgsApplication::getThemeIcon( "grass_new_mapset.png" ), tr( "New mapset" ), this );
connect( newMapsetAction, SIGNAL( triggered() ), SLOT( newMapset() ) );
list << newMapsetAction;
}

if ( mGrassObject.type() == QgsGrassObject::Mapset )
{
QAction* openMapsetAction = new QAction( QgsApplication::getThemeIcon( "grass_open_mapset.png" ), tr( "Open mapset" ), this );
Expand Down Expand Up @@ -88,6 +95,30 @@ QList<QAction*> QgsGrassItemActions::actions()
return list;
}

void QgsGrassItemActions::newMapset()
{
QgsDebugMsg( "entered" );

QStringList existingNames = QgsGrass::mapsets( mGrassObject.gisdbase(), mGrassObject.mapsetPath() );
QgsDebugMsg( "existingNames = " + existingNames.join( "," ) );
QRegExp regExp = QgsGrassObject::newNameRegExp( QgsGrassObject::Mapset );
Qt::CaseSensitivity caseSensitivity = QgsGrass::caseSensitivity();
QgsNewNameDialog dialog( "", "", QStringList(), existingNames, regExp, caseSensitivity );

if ( dialog.exec() != QDialog::Accepted )
{
return;
}
QString name = dialog.name();
QgsDebugMsg( "name = " + name );
QString error;
QgsGrass::createMapset( mGrassObject.gisdbase(), mGrassObject.location(), name, error );
if ( !error.isEmpty() )
{
QgsGrass::warning( tr( "Cannot create new mapset: %1" ).arg( error ) );
}
}

void QgsGrassItemActions::openMapset()
{
QgsDebugMsg( "entered" );
Expand Down
1 change: 1 addition & 0 deletions src/providers/grass/qgsgrassprovidermodule.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class QgsGrassItemActions : public QObject
QList<QAction*> actions();

public slots:
void newMapset();
void openMapset();
void renameGrassObject();
void deleteGrassObject();
Expand Down

0 comments on commit e47a97b

Please sign in to comment.