[GRASS] open mapset from browser

This commit is contained in:
Radim Blazek 2015-07-23 17:19:05 +02:00
parent 9dcb2a1260
commit 266aa7bc95
9 changed files with 109 additions and 55 deletions

View File

@ -225,6 +225,8 @@ void QgsGrassPlugin::initGui()
connect( qGisInterface, SIGNAL( currentLayerChanged( QgsMapLayer * ) ),
this, SLOT( setEditAction() ) );
connect( QgsGrass::instance(), SIGNAL( mapsetChanged() ), SLOT( mapsetChanged() ) );
// Init Region symbology
mRegionPen.setColor( QColor( settings.value( "/GRASS/region/color", "#ff0000" ).toString() ) );
mRegionPen.setWidth( settings.value( "/GRASS/region/width", 0 ).toInt() );
@ -240,6 +242,7 @@ void QgsGrassPlugin::initGui()
void QgsGrassPlugin::mapsetChanged()
{
QgsDebugMsg( "entered" );
if ( !QgsGrass::activeMode() )
{
mRegionAction->setEnabled( false );
@ -260,11 +263,6 @@ void QgsGrassPlugin::mapsetChanged()
mRegionAction->setChecked( on );
switchRegion( on );
if ( mTools )
{
mTools->mapsetChanged();
}
QString gisdbase = QgsGrass::getDefaultGisdbase();
QString location = QgsGrass::getDefaultLocation();
try
@ -281,21 +279,11 @@ void QgsGrassPlugin::mapsetChanged()
setTransform();
redrawRegion();
}
}
void QgsGrassPlugin::saveMapset()
{
// QgsDebugMsg("entered.");
// Save working mapset in project file
QgsProject::instance()->writeEntry( "GRASS", "/WorkingGisdbase",
QgsProject::instance()->writePath( QgsGrass::getDefaultGisdbase() ) );
QgsProject::instance()->writeEntry( "GRASS", "/WorkingLocation",
QgsGrass::getDefaultLocation() );
QgsProject::instance()->writeEntry( "GRASS", "/WorkingMapset",
QgsGrass::getDefaultMapset() );
if ( mTools )
{
mTools->mapsetChanged();
}
}
// Open tools
@ -584,9 +572,6 @@ void QgsGrassPlugin::openMapset()
QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open the mapset. %1" ).arg( err ) );
return;
}
saveMapset();
mapsetChanged();
}
void QgsGrassPlugin::closeMapset()
@ -600,9 +585,6 @@ void QgsGrassPlugin::closeMapset()
QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot close mapset. %1" ).arg( err ) );
return;
}
saveMapset();
mapsetChanged();
}
void QgsGrassPlugin::newMapset()
@ -656,7 +638,6 @@ void QgsGrassPlugin::projectRead()
QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot close current mapset. %1" ).arg( err ) );
return;
}
mapsetChanged();
err = QgsGrass::openMapset( gisdbase, location, mapset );
@ -665,8 +646,6 @@ void QgsGrassPlugin::projectRead()
QMessageBox::warning( 0, tr( "Warning" ), tr( "Cannot open GRASS mapset. %1" ).arg( err ) );
return;
}
mapsetChanged();
}
void QgsGrassPlugin::newProject()

View File

@ -119,8 +119,6 @@ class QgsGrassPlugin : public QObject, public QgisPlugin
void projectRead();
//! New project
void newProject();
//! Save mapset to project
void saveMapset();
//! Set edit action
void setEditAction();
//! Close the edit if layer is removed

View File

@ -137,18 +137,21 @@ void QgsGrassTools::showTabs()
repaint();
#endif
QString conf = QgsApplication::pkgDataPath() + "/grass/config/default.qgc";
// Build modules tree if empty
QgsDebugMsg( QString( "topLevelItemCount = %1" ).arg( mModulesTree->topLevelItemCount() ) );
if ( mModulesTree->topLevelItemCount() == 0 )
{
// Load the modules lists
QApplication::setOverrideCursor( Qt::WaitCursor );
QString conf = QgsApplication::pkgDataPath() + "/grass/config/default.qgc";
loadConfig( conf, mModulesTree, mModulesListModel, false );
QApplication::restoreOverrideCursor();
QgsDebugMsg( QString( "topLevelItemCount = %1" ).arg( mModulesTree->topLevelItemCount() ) );
}
if ( QgsGrass::activeMode() )
{
QgsDebugMsg( QString( "topLevelItemCount = %1" ).arg( mModulesTree->topLevelItemCount() ) );
if ( mModulesTree->topLevelItemCount() == 0 )
{
// Load the modules lists
QApplication::setOverrideCursor( Qt::WaitCursor );
loadConfig( conf, mModulesTree, mModulesListModel, false );
QApplication::restoreOverrideCursor();
}
QgsDebugMsg( QString( "topLevelItemCount = %1" ).arg( mModulesTree->topLevelItemCount() ) );
mMessageLabel->hide();
mTabWidget->setEnabled( true );
}
else
@ -175,6 +178,7 @@ void QgsGrassTools::showTabs()
QApplication::restoreOverrideCursor();
}
#else
mMessageLabel->show();
mTabWidget->setEnabled( false );
#endif
}

View File

@ -15,9 +15,16 @@
</property>
<widget class="QWidget" name="mContents">
<layout class="QVBoxLayout" name="verticalLayout">
<property name="margin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="mMessageLabel">
<property name="text">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;No mapset is open. You can open a GRASS mapset from the browser using the mapset item's context menu action &lt;span style=&quot; font-style:italic;&quot;&gt;Open mapset&lt;/span&gt;.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QTabWidget" name="mTabWidget">
<property name="currentIndex">
@ -154,6 +161,8 @@
</widget>
</item>
</layout>
<zorder>mTabWidget</zorder>
<zorder>mMessageLabel</zorder>
</widget>
</widget>
<resources/>

View File

@ -23,6 +23,7 @@ MACRO(ADD_GRASSLIB GRASS_BUILD_VERSION)
SET(GRASS_MINOR_VERSION ${GRASS_MINOR_VERSION${GRASS_BUILD_VERSION}})
QT4_WRAP_CPP(GRASS_LIBRARY_MOC_SRCS
../qgsgrass.h
../qgsgrassprovider.h
../qgsgrassimport.h
../qgsgrassoptions.h

View File

@ -30,13 +30,14 @@
#include "qgsgrass.h"
#include "qgslogger.h"
#include "qgsapplication.h"
#include "qgsconfig.h"
#include "qgscoordinatereferencesystem.h"
#include "qgsfield.h"
#include "qgsrectangle.h"
#include "qgsconfig.h"
#include "qgslocalec.h"
#include "qgslogger.h"
#include "qgsproject.h"
#include "qgsrectangle.h"
#include <QFileDialog>
#include <QMessageBox>
@ -518,6 +519,12 @@ bool QgsGrass::isValidGrassBaseDir( const QString& gisBase )
return false;
}
QgsGrass *QgsGrass::instance()
{
static QgsGrass sInstance;
return &sInstance;
}
bool QgsGrass::activeMode()
{
init();
@ -833,10 +840,12 @@ QString GRASS_LIB_EXPORT QgsGrass::openMapset( const QString& gisdbase,
mMapsetLock = lock;
return NULL;
saveMapset();
emit QgsGrass::instance()->mapsetChanged();
return QString::null;
}
QString QgsGrass::closeMapset()
QString GRASS_LIB_EXPORT QgsGrass::closeMapset()
{
QgsDebugMsg( "entered." );
@ -894,7 +903,24 @@ QString QgsGrass::closeMapset()
}
}
return NULL;
saveMapset();
emit QgsGrass::instance()->mapsetChanged();
return QString::null;
}
void GRASS_LIB_EXPORT QgsGrass::saveMapset()
{
QgsDebugMsg( "entered." );
// Save working mapset in project file
QgsProject::instance()->writeEntry( "GRASS", "/WorkingGisdbase",
QgsProject::instance()->writePath( getDefaultGisdbase() ) );
QgsProject::instance()->writeEntry( "GRASS", "/WorkingLocation",
getDefaultLocation() );
QgsProject::instance()->writeEntry( "GRASS", "/WorkingMapset",
getDefaultMapset() );
}
QStringList GRASS_LIB_EXPORT QgsGrass::locations( const QString& gisdbase )
@ -2268,6 +2294,11 @@ void GRASS_LIB_EXPORT QgsGrass::putEnv( QString name, QString value )
putenv( envChar );
}
void GRASS_LIB_EXPORT QgsGrass::warning( const QString &message )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ), message );
}
void GRASS_LIB_EXPORT QgsGrass::warning( QgsGrass::Exception &e )
{
QMessageBox::warning( 0, QObject::tr( "Warning" ), e.what() );

View File

@ -128,8 +128,9 @@ class GRASS_LIB_EXPORT QgsGrassObject
/*!
Methods for C library initialization and error handling.
*/
class QgsGrass
class GRASS_LIB_EXPORT QgsGrass : public QObject
{
Q_OBJECT
public:
static GRASS_LIB_EXPORT jmp_buf jumper; // used to get back from fatal error
@ -154,6 +155,9 @@ class QgsGrass
int red1, red2, green1, green2, blue1, blue2;
};
/** Get singleton instance of this class. Used as signals proxy between provider and plugin. */
static QgsGrass* instance();
//! Get info about the mode
/** QgsGrass may be running in active or passive mode.
* Active mode means that GISRC is set up and GISRC file is available,
@ -202,18 +206,23 @@ class QgsGrass
//! Get last error message
static GRASS_LIB_EXPORT QString errorMessage( void );
/** \brief Open existing GRASS mapset
* \return NULL string or error message
/** Open existing GRASS mapset.
* Emits signal mapsetChanged().
* \return Empty string or error message
*/
static GRASS_LIB_EXPORT QString openMapset( const QString& gisdbase,
const QString& location, const QString& mapset );
/** \brief Close mapset if it was opened from QGIS.
* Delete GISRC, lock and temporary directory
* \return NULL string or error message
* Delete GISRC, lock and temporary directory.
* Emits signal mapsetChanged().
* \return Empty string or error message
*/
static GRASS_LIB_EXPORT QString closeMapset();
/** \brief Save current mapset to project file. */
static GRASS_LIB_EXPORT void saveMapset();
//! Check if given directory contains a GRASS installation
static GRASS_LIB_EXPORT bool isValidGrassBaseDir( const QString& gisBase );
@ -444,6 +453,9 @@ class QgsGrass
return QgsApplication::libexecPath() + "grass/modules";
}
/** Show warning dialog with message */
static GRASS_LIB_EXPORT void warning( const QString &message );
/** Show warning dialog with exception message */
static GRASS_LIB_EXPORT void warning( QgsGrass::Exception &e );
@ -455,6 +467,10 @@ class QgsGrass
// Sleep miliseconds (for debugging)
static GRASS_LIB_EXPORT void sleep( int ms );
signals:
/** Signal emited after mapset was opened */
void mapsetChanged();
private:
static int initialized; // Set to 1 after initialization
static bool active; // is active mode

View File

@ -342,7 +342,12 @@ QVector<QgsDataItem*> QgsGrassMapsetItem::createChildren()
QList<QAction*> QgsGrassMapsetItem::actions()
{
return QgsGrassItemActions::instance()->actions();
QList<QAction*> list = QgsGrassItemActions::instance()->actions();
// TODO: check mapset ownership
QAction* actionOpen = new QAction( QgsApplication::getThemeIcon( "grass_open_mapset.png" ), tr( "Open mapset" ), this );
connect( actionOpen, SIGNAL( triggered() ), SLOT( openMapset() ) );
list.append( actionOpen );
return list;
}
bool QgsGrassMapsetItem::handleDrop( const QMimeData * data, Qt::DropAction )
@ -614,6 +619,16 @@ void QgsGrassMapsetItem::onImportFinished( QgsGrassImport* import )
refresh();
}
void QgsGrassMapsetItem::openMapset()
{
QgsDebugMsg( "entered" );
QString error = QgsGrass::openMapset( mGisdbase, mLocation, name() );
if ( !error.isEmpty() )
{
QgsGrass::warning( error );
}
}
//----------------------- QgsGrassObjectItemBase ------------------------------
QgsGrassObjectItemBase::QgsGrassObjectItemBase( QgsGrassObject grassObject ) :

View File

@ -65,6 +65,7 @@ class QgsGrassMapsetItem : public QgsDirectoryItem
public slots:
void onImportFinished( QgsGrassImport* import );
void openMapset();
private:
bool objectInImports( QgsGrassObject grassObject );