Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[GRASS] open mapset from browser
  • Loading branch information
blazek committed Jul 23, 2015
1 parent 9dcb2a1 commit 266aa7b
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 55 deletions.
35 changes: 7 additions & 28 deletions src/plugins/grass/qgsgrassplugin.cpp
Expand Up @@ -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() );
Expand All @@ -240,6 +242,7 @@ void QgsGrassPlugin::initGui()

void QgsGrassPlugin::mapsetChanged()
{
QgsDebugMsg( "entered" );
if ( !QgsGrass::activeMode() )
{
mRegionAction->setEnabled( false );
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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 );

Expand All @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions src/plugins/grass/qgsgrassplugin.h
Expand Up @@ -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
Expand Down
24 changes: 14 additions & 10 deletions src/plugins/grass/qgsgrasstools.cpp
Expand Up @@ -137,18 +137,21 @@ void QgsGrassTools::showTabs()
repaint();
#endif

QString conf = QgsApplication::pkgDataPath() + "/grass/config/default.qgc";
if ( QgsGrass::activeMode() )
// 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 ( 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() ) );
}

if ( QgsGrass::activeMode() )
{
mMessageLabel->hide();
mTabWidget->setEnabled( true );
}
else
Expand All @@ -175,6 +178,7 @@ void QgsGrassTools::showTabs()
QApplication::restoreOverrideCursor();
}
#else
mMessageLabel->show();
mTabWidget->setEnabled( false );
#endif
}
Expand Down
15 changes: 12 additions & 3 deletions src/plugins/grass/qgsgrasstoolsbase.ui
Expand Up @@ -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">
Expand Down Expand Up @@ -154,6 +161,8 @@
</widget>
</item>
</layout>
<zorder>mTabWidget</zorder>
<zorder>mMessageLabel</zorder>
</widget>
</widget>
<resources/>
Expand Down
1 change: 1 addition & 0 deletions src/providers/grass/CMakeLists.txt
Expand Up @@ -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
Expand Down
43 changes: 37 additions & 6 deletions src/providers/grass/qgsgrass.cpp
Expand Up @@ -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>
Expand Down Expand Up @@ -518,6 +519,12 @@ bool QgsGrass::isValidGrassBaseDir( const QString& gisBase )
return false;
}

QgsGrass *QgsGrass::instance()
{
static QgsGrass sInstance;
return &sInstance;
}

bool QgsGrass::activeMode()
{
init();
Expand Down Expand Up @@ -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." );

Expand Down Expand Up @@ -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 )
Expand Down Expand Up @@ -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() );
Expand Down
26 changes: 21 additions & 5 deletions src/providers/grass/qgsgrass.h
Expand Up @@ -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

Expand All @@ -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,
Expand Down Expand Up @@ -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 );

Expand Down Expand Up @@ -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 );

Expand All @@ -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
Expand Down
17 changes: 16 additions & 1 deletion src/providers/grass/qgsgrassprovidermodule.cpp
Expand Up @@ -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 )
Expand Down Expand Up @@ -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 ) :
Expand Down
1 change: 1 addition & 0 deletions src/providers/grass/qgsgrassprovidermodule.h
Expand Up @@ -65,6 +65,7 @@ class QgsGrassMapsetItem : public QgsDirectoryItem

public slots:
void onImportFinished( QgsGrassImport* import );
void openMapset();

private:
bool objectInImports( QgsGrassObject grassObject );
Expand Down

0 comments on commit 266aa7b

Please sign in to comment.