@@ -413,7 +413,9 @@ bool QgsGrass::init( void )
413413 }
414414
415415 bool userGisbase = false ;
416- bool valid = false ;
416+ bool valid = isValidGrassBaseDir ( gisBase );
417+ // TODO add GISBASE selection to options dialog
418+ #if 0
417419 while ( !( valid = isValidGrassBaseDir( gisBase ) ) )
418420 {
419421
@@ -446,103 +448,111 @@ bool QgsGrass::init( void )
446448 gisBase = shortPath( gisBase );
447449#endif
448450 }
451+ #endif
449452
450453 if ( !valid )
451454 {
452- // warn user
453- QMessageBox::information ( 0 , QObject::tr ( " GRASS plugin" ),
454- QObject::tr ( " GRASS data won't be available if GISBASE is not specified." ) );
455+ nonInitializable = true ;
456+ error_message = tr ( " GRASS was not found in '%1'(GISBASE), provider and plugin will not work." ).arg ( gisBase );
457+ QgsDebugMsg ( error_message );
458+ #if 0
459+ // TODO: how to emit message from provider (which does not know about QgisApp)
460+ QgisApp::instance()->messageBar()->pushMessage( tr( "GRASS error" ),
461+ error_message, QgsMessageBar: WARNING );
462+ #endif
455463 }
456-
457- if ( userGisbase )
464+ else
458465 {
459- settings.setValue ( " /GRASS/gisbase" , gisBase );
460- }
461-
462- QgsDebugMsg ( QString ( " Valid GRASS gisBase is: %1" ).arg ( gisBase ) );
463- // GISBASE environment variable must be set because is required by directly called GRASS functions
464- putEnv ( " GISBASE" , gisBase );
465- mGisbase = gisBase;
466-
467- // Create list of paths to GRASS modules
468- // PATH environment variable is not used to search for modules (since 2.12) because it could
469- // create a lot of confusion especially if both GRASS 6 and 7 are installed and path to one version
470- // $GISBASE/bin somehow gets to PATH and another version plugin is loaded to QGIS, because if a module
471- // is missing in one version, it could be found in another $GISBASE/bin and misleadin error could be reported
472- mGrassModulesPaths .clear ();
473- mGrassModulesPaths << gisbase () + " /bin" ;
474- mGrassModulesPaths << gisbase () + " /scripts" ;
475- mGrassModulesPaths << QgsApplication::pkgDataPath () + " /grass/scripts" ;
466+ if ( userGisbase )
467+ {
468+ settings.setValue ( " /GRASS/gisbase" , gisBase );
469+ }
476470
477- // On windows the GRASS libraries are in
478- // QgsApplication::prefixPath(), we have to add them
479- // to PATH to enable running of GRASS modules
480- // and database drivers
471+ QgsDebugMsg ( QString ( " Valid GRASS gisBase is: %1" ).arg ( gisBase ) );
472+ // GISBASE environment variable must be set because is required by directly called GRASS functions
473+ putEnv ( " GISBASE" , gisBase );
474+ mGisbase = gisBase;
475+
476+ // Create list of paths to GRASS modules
477+ // PATH environment variable is not used to search for modules (since 2.12) because it could
478+ // create a lot of confusion especially if both GRASS 6 and 7 are installed and path to one version
479+ // $GISBASE/bin somehow gets to PATH and another version plugin is loaded to QGIS, because if a module
480+ // is missing in one version, it could be found in another $GISBASE/bin and misleadin error could be reported
481+ mGrassModulesPaths .clear ();
482+ mGrassModulesPaths << gisbase () + " /bin" ;
483+ mGrassModulesPaths << gisbase () + " /scripts" ;
484+ mGrassModulesPaths << QgsApplication::pkgDataPath () + " /grass/scripts" ;
485+
486+ // On windows the GRASS libraries are in
487+ // QgsApplication::prefixPath(), we have to add them
488+ // to PATH to enable running of GRASS modules
489+ // and database drivers
481490#ifdef Q_OS_WIN
482- // It seems that QgsApplication::prefixPath() is not initialized at this point
483- // TODO: verify if this is required and why (PATH to required libs should be set in qgis-grass.bat)
484- // mGrassModulesPaths << shortPath( QCoreApplication::applicationDirPath() ) );
485-
486- // Add path to MSYS bin
487- // Warning: MSYS sh.exe will translate this path to '/bin'
488- QString msysBin = QCoreApplication::applicationDirPath () + " /msys/bin/" ;
489- if ( QFileInfo ( msysBin ).isDir () )
490- {
491- mGrassModulesPaths << shortPath ( QCoreApplication::applicationDirPath () + " /msys/bin/" );
492- }
491+ // It seems that QgsApplication::prefixPath() is not initialized at this point
492+ // TODO: verify if this is required and why (PATH to required libs should be set in qgis-grass.bat)
493+ // mGrassModulesPaths << shortPath( QCoreApplication::applicationDirPath() ) );
494+
495+ // Add path to MSYS bin
496+ // Warning: MSYS sh.exe will translate this path to '/bin'
497+ QString msysBin = QCoreApplication::applicationDirPath () + " /msys/bin/" ;
498+ if ( QFileInfo ( msysBin ).isDir () )
499+ {
500+ mGrassModulesPaths << shortPath ( QCoreApplication::applicationDirPath () + " /msys/bin/" );
501+ }
493502#endif
494503
495- // QString p = getenv( "PATH" );
496- // path.append( sep + p );
497-
498- QgsDebugMsg ( " mGrassModulesPaths = " + mGrassModulesPaths .join ( " ," ) );
499- // putEnv( "PATH", path );
500-
501- // TODO: move where it is required for QProcess
502- // Set GRASS_PAGER if not set, it is necessary for some
503- // modules printing to terminal, e.g. g.list
504- // We use 'cat' because 'more' is not present in MSYS (Win)
505- // and it doesn't work well in built in shell (Unix/Mac)
506- // and 'less' is not user friendly (for example user must press
507- // 'q' to quit which is definitely difficult for normal user)
508- // Also scroling can be don in scrollable window in both
509- // MSYS terminal and built in shell.
510- if ( !getenv ( " GRASS_PAGER" ) )
511- {
512- QString pager;
513- QStringList pagers;
514- // pagers << "more" << "less" << "cat"; // se notes above
515- pagers << " cat" ;
516-
517- for ( int i = 0 ; i < pagers.size (); i++ )
504+ // QString p = getenv( "PATH" );
505+ // path.append( sep + p );
506+
507+ QgsDebugMsg ( " mGrassModulesPaths = " + mGrassModulesPaths .join ( " ," ) );
508+ // putEnv( "PATH", path );
509+
510+ // TODO: move where it is required for QProcess
511+ // Set GRASS_PAGER if not set, it is necessary for some
512+ // modules printing to terminal, e.g. g.list
513+ // We use 'cat' because 'more' is not present in MSYS (Win)
514+ // and it doesn't work well in built in shell (Unix/Mac)
515+ // and 'less' is not user friendly (for example user must press
516+ // 'q' to quit which is definitely difficult for normal user)
517+ // Also scroling can be don in scrollable window in both
518+ // MSYS terminal and built in shell.
519+ if ( !getenv ( " GRASS_PAGER" ) )
518520 {
519- int state;
520-
521- QProcess p;
522- p.start ( pagers.at ( i ) );
523- p.waitForStarted ();
524- state = p.state ();
525- p.write ( " \004 " ); // Ctrl-D
526- p.closeWriteChannel ();
527- p.waitForFinished ( 1000 );
528- p.kill ();
529-
530- if ( state == QProcess::Running )
521+ QString pager;
522+ QStringList pagers;
523+ // pagers << "more" << "less" << "cat"; // se notes above
524+ pagers << " cat" ;
525+
526+ for ( int i = 0 ; i < pagers.size (); i++ )
531527 {
532- pager = pagers.at ( i );
533- break ;
528+ int state;
529+
530+ QProcess p;
531+ p.start ( pagers.at ( i ) );
532+ p.waitForStarted ();
533+ state = p.state ();
534+ p.write ( " \004 " ); // Ctrl-D
535+ p.closeWriteChannel ();
536+ p.waitForFinished ( 1000 );
537+ p.kill ();
538+
539+ if ( state == QProcess::Running )
540+ {
541+ pager = pagers.at ( i );
542+ break ;
543+ }
534544 }
535- }
536545
537- if ( pager.length () > 0 )
538- {
539- putEnv ( " GRASS_PAGER" , pager );
546+ if ( pager.length () > 0 )
547+ {
548+ putEnv ( " GRASS_PAGER" , pager );
549+ }
540550 }
541551 }
542552
543553 initialized = 1 ;
544554 unlock ();
545- return true ;
555+ return valid ;
546556}
547557
548558/*
0 commit comments