@@ -414,6 +414,7 @@ bool QgsGrass::init( void )
414
414
mGrassModulesPaths << gisbase () + " /bin" ;
415
415
mGrassModulesPaths << gisbase () + " /scripts" ;
416
416
mGrassModulesPaths << QgsApplication::pkgDataPath () + " /grass/scripts" ;
417
+ mGrassModulesPaths << qgisGrassModulePath ();
417
418
418
419
// On windows the GRASS libraries are in
419
420
// QgsApplication::prefixPath(), we have to add them
@@ -1543,9 +1544,7 @@ QStringList QgsGrass::grassObjects( const QgsGrassObject& mapsetObject, QgsGrass
1543
1544
{
1544
1545
#if GRASS_VERSION_MAJOR >= 7
1545
1546
QString cmd = gisbase () + " /scripts/t.list" ;
1546
- #ifdef Q_OS_WIN
1547
- cmd += " .py" ;
1548
- #endif
1547
+
1549
1548
QStringList arguments;
1550
1549
1551
1550
// Running t.list module is quite slow (about 500ms) -> check first if temporal db exists.
@@ -1560,19 +1559,27 @@ QStringList QgsGrass::grassObjects( const QgsGrassObject& mapsetObject, QgsGrass
1560
1559
arguments << " type=" + QgsGrassObject::elementShort ( type );
1561
1560
1562
1561
int timeout = -1 ; // What timeout to use? It can take long time on network or database
1563
- QByteArray data = runModule ( mapsetObject.gisdbase (), mapsetObject.location (), mapsetObject.mapset (), cmd, arguments, timeout, false );
1564
- Q_FOREACH ( QString fullName, QString::fromLocal8Bit ( data ).split ( ' \n ' ) )
1562
+ try
1565
1563
{
1566
- fullName = fullName. trimmed ( );
1567
- if ( ! fullName. isEmpty ( ) )
1564
+ QByteArray data = runModule ( mapsetObject. gisdbase (), mapsetObject. location (), mapsetObject. mapset (), cmd, arguments, timeout, false );
1565
+ Q_FOREACH ( QString fullName, QString::fromLocal8Bit ( data ). split ( ' \n ' ) )
1568
1566
{
1569
- QStringList nameMapset = fullName.split ( " @ " );
1570
- if ( nameMapset. value ( 1 ) == mapsetObject. mapset () || nameMapset. value ( 1 ) .isEmpty () )
1567
+ fullName = fullName.trimmed ( );
1568
+ if ( !fullName .isEmpty () )
1571
1569
{
1572
- list << nameMapset.value ( 0 );
1570
+ QStringList nameMapset = fullName.split ( " @" );
1571
+ if ( nameMapset.value ( 1 ) == mapsetObject.mapset () || nameMapset.value ( 1 ).isEmpty () )
1572
+ {
1573
+ list << nameMapset.value ( 0 );
1574
+ }
1573
1575
}
1574
1576
}
1575
1577
}
1578
+ catch ( QgsGrass::Exception &e )
1579
+ {
1580
+ // TODO: notify somehow user
1581
+ QgsDebugMsg ( QString ( " Cannot run %1: %2" ).arg ( cmd ).arg ( e.what () ) );
1582
+ }
1576
1583
}
1577
1584
#endif
1578
1585
}
@@ -1958,6 +1965,47 @@ bool QgsGrass::mapRegion( QgsGrassObject::Type type, QString gisdbase,
1958
1965
return true ;
1959
1966
}
1960
1967
1968
+ QString QgsGrass::findModule ( QString module )
1969
+ {
1970
+ QgsDebugMsg ( " called." );
1971
+ if ( QFile::exists ( module ) )
1972
+ {
1973
+ return module ; // full path
1974
+ }
1975
+
1976
+ QStringList extensions;
1977
+ #ifdef Q_OS_WIN
1978
+ // On windows try .bat first
1979
+ extensions << " .bat" << " .py" << " .exe" ;
1980
+ #endif
1981
+ // and then try if it's a module without extension (standard on UNIX)
1982
+ extensions << " " ;
1983
+
1984
+ QStringList paths;
1985
+ // Try first full path
1986
+ paths << " " ;
1987
+ paths << QgsGrass::grassModulesPaths ();
1988
+
1989
+ // Extensions first to prefer .bat over .exe on Windows
1990
+ Q_FOREACH ( const QString& ext, extensions )
1991
+ {
1992
+ Q_FOREACH ( const QString& path, paths )
1993
+ {
1994
+ QString full = path + " /" + module + ext;
1995
+ if ( QFile::exists ( full ) )
1996
+ {
1997
+ QgsDebugMsg ( " found " + full );
1998
+ return full;
1999
+ }
2000
+ else
2001
+ {
2002
+ QgsDebugMsg ( " not found " + full );
2003
+ }
2004
+ }
2005
+ }
2006
+ return QString ();
2007
+ }
2008
+
1961
2009
QProcess *QgsGrass::startModule ( const QString& gisdbase, const QString& location,
1962
2010
const QString& mapset, const QString& moduleName, const QStringList& arguments,
1963
2011
QTemporaryFile &gisrcFile, bool qgisModule )
@@ -1970,12 +2018,12 @@ QProcess *QgsGrass::startModule( const QString& gisdbase, const QString& locati
1970
2018
{
1971
2019
module += QString::number ( QgsGrass::versionMajor () );
1972
2020
}
1973
- #ifdef Q_OS_WIN
1974
- if ( !module .endsWith ( " .exe" , Qt::CaseInsensitive ) && !module .endsWith ( " .py" , Qt::CaseInsensitive ) )
2021
+
2022
+ QString modulePath = findModule ( module );
2023
+ if ( modulePath.isEmpty () )
1975
2024
{
1976
- module += " .exe " ;
2025
+ throw QgsGrass::Exception ( QObject::tr ( " Cannot find module %1 " ). arg ( module ) ) ;
1977
2026
}
1978
- #endif
1979
2027
1980
2028
// We have to set GISRC file, uff
1981
2029
if ( !gisrcFile.open () )
@@ -2008,9 +2056,8 @@ QProcess *QgsGrass::startModule( const QString& gisdbase, const QString& locati
2008
2056
2009
2057
process->setEnvironment ( environment );
2010
2058
2011
- QgsDebugMsg ( module + " " + arguments.join ( " " ) );
2012
- // process->start( module, arguments, QProcess::Unbuffered );
2013
- process->start ( module , arguments );
2059
+ QgsDebugMsg ( modulePath + " " + arguments.join ( " " ) );
2060
+ process->start ( modulePath, arguments );
2014
2061
if ( !process->waitForStarted () )
2015
2062
{
2016
2063
throw QgsGrass::Exception ( error );
0 commit comments