Skip to content
Permalink
Browse files

[GRASS] create new mapset from browser

  • Loading branch information
blazek committed Sep 25, 2015
1 parent 11f28fc commit e47a97bdf8ddf93f88d61cce52f67051bbbf6529
@@ -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() )
@@ -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;
}
@@ -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 ) );
@@ -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 );

@@ -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 );
@@ -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" );
@@ -36,6 +36,7 @@ class QgsGrassItemActions : public QObject
QList<QAction*> actions();

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

0 comments on commit e47a97b

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