Skip to content

Commit

Permalink
[GRASS] better format module's output
Browse files Browse the repository at this point in the history
  • Loading branch information
blazek committed Sep 27, 2015
1 parent 667fb04 commit f2a1efe
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 18 deletions.
5 changes: 0 additions & 5 deletions src/plugins/grass/qgsgrassmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -883,11 +883,6 @@ void QgsGrassModule::readStderr()
QgsDebugMsg( "called." );

QString line;
QRegExp rxpercent( "GRASS_INFO_PERCENT: (\\d+)" );
QRegExp rxmessage( "GRASS_INFO_MESSAGE\\(\\d+,\\d+\\): (.*)" );
QRegExp rxwarning( "GRASS_INFO_WARNING\\(\\d+,\\d+\\): (.*)" );
QRegExp rxerror( "GRASS_INFO_ERROR\\(\\d+,\\d+\\): (.*)" );
QRegExp rxend( "GRASS_INFO_END\\(\\d+,\\d+\\)" );

mProcess.setReadChannel( QProcess::StandardError );
while ( mProcess.canReadLine() )
Expand Down
31 changes: 26 additions & 5 deletions src/providers/grass/qgsgrass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2648,28 +2648,44 @@ void QgsGrass::sleep( int ms )
#endif
}

QgsGrass::ModuleOutput QgsGrass::parseModuleOutput( const QString & input, QString &text, QString &html, int &percent )
QgsGrass::ModuleOutput QgsGrass::parseModuleOutput( const QString & input, QString &text, QString &html, int &value )
{
QgsDebugMsg( "input = " + input );
#ifdef QGISDEBUG
QString ascii;
for ( int i = 0; i < input.size(); i++ )
{
int c = input.at( i ).toAscii();
ascii += QString().sprintf( "%2x ", c );
}
QgsDebugMsg( "ascii = " + ascii );
#endif

QRegExp rxpercent( "GRASS_INFO_PERCENT: (\\d+)" );
QRegExp rxmessage( "GRASS_INFO_MESSAGE\\(\\d+,\\d+\\): (.*)" );
QRegExp rxwarning( "GRASS_INFO_WARNING\\(\\d+,\\d+\\): (.*)" );
QRegExp rxerror( "GRASS_INFO_ERROR\\(\\d+,\\d+\\): (.*)" );
QRegExp rxend( "GRASS_INFO_END\\(\\d+,\\d+\\)" );
// GRASS added G_progress() which does not suport GRASS_MESSAGE_FORMAT=gui
// and it is printing fprintf(stderr, "%10ld\b\b\b\b\b\b\b\b\b\b", n);
// Ticket created https://trac.osgeo.org/grass/ticket/2751
QRegExp rxprogress( " +(\\d+)\\b\\b\\b\\b\\b\\b\\b\\b\\b\\b" );


// We return simple messages in html non formated, monospace text should be set on widget
// where it is used because output may be formated assuming fixed width font
if ( input.trimmed().isEmpty() )
{
return OutputNone;
}
else if ( rxpercent.indexIn( input ) != -1 )
{
percent = rxpercent.cap( 1 ).toInt();
value = rxpercent.cap( 1 ).toInt();
return OutputPercent;
}
else if ( rxmessage.indexIn( input ) != -1 )
{
text = rxmessage.cap( 1 );
html = "<pre>" + text + "</pre>" ;
html = text;
return OutputMessage;
}
else if ( rxwarning.indexIn( input ) != -1 )
Expand All @@ -2690,10 +2706,15 @@ QgsGrass::ModuleOutput QgsGrass::parseModuleOutput( const QString & input, QStri
{
return OutputNone;
}
else if ( rxprogress.indexIn( input ) != -1 )
{
value = rxprogress.cap( 1 ).toInt();
return OutputProgress;
}
else // some plain text which cannot be parsed
{
text = input;
html = "<pre>" + text + "</pre>";
html = text;
return OutputMessage;
}
}
5 changes: 3 additions & 2 deletions src/providers/grass/qgsgrass.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
{
OutputNone,
OutputPercent,
OutputProgress, // number of items processed if total number is unknown
OutputMessage,
OutputWarning,
OutputError
Expand Down Expand Up @@ -577,8 +578,8 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
* @param input input string read from module stderr
* @param text parsed text
* @param html html formated parsed text, e.g. + icons
* @param percent progress 0-100 */
static ModuleOutput parseModuleOutput( const QString & input, QString &text, QString &html, int &percent );
* @param value percent 0-100 or progress as absolut number if total is unknown*/
static ModuleOutput parseModuleOutput( const QString & input, QString &text, QString &html, int &value );

public slots:
/** Close mapset and show warning if closing failed */
Expand Down
20 changes: 14 additions & 6 deletions src/providers/grass/qgsgrassimport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,35 @@ void QgsGrassImportProgress::onReadyReadStandardError()
{
QgsDebugMsg( "line = '" + line + "'" );
QString text, html;
int percent;
QgsGrass::ModuleOutput type = QgsGrass::parseModuleOutput( line, text, html, percent );
int value;
QgsGrass::ModuleOutput type = QgsGrass::parseModuleOutput( line, text, html, value );
if ( type == QgsGrass::OutputPercent )
{
mProgressMin = 0;
mProgressMax = 100;
mProgressValue = percent;
mProgressValue = value;
emit progressChanged( html, mProgressHtml, mProgressMin, mProgressMax, mProgressValue );
}
else if ( type == QgsGrass::OutputProgress )
{
html = tr( "Progress: %1" ).arg( value );
append( html );
}
else if ( type == QgsGrass::OutputMessage || type == QgsGrass::OutputWarning || type == QgsGrass::OutputError )
{
mProgressHtml += html;
QgsDebugMsg( "text = " + text );
emit progressChanged( html, mProgressHtml, mProgressMin, mProgressMax, mProgressValue );
append( html );
}
}
}
}

void QgsGrassImportProgress::append( const QString & html )
{
QgsDebugMsg( "html = " + html );
if ( !mProgressHtml.isEmpty() )
{
mProgressHtml += "<br>";
}
mProgressHtml += html;
emit progressChanged( html, mProgressHtml, mProgressMin, mProgressMax, mProgressValue );
}
Expand Down

0 comments on commit f2a1efe

Please sign in to comment.