Skip to content
Permalink
Browse files

[GRASS] better format module's output

  • Loading branch information
blazek committed Sep 27, 2015
1 parent 667fb04 commit f2a1efe7be4ba8726d6f4a10559950f812844d59
@@ -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() )
@@ -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 )
@@ -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;
}
}
@@ -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
@@ -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 */
@@ -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 );
}

0 comments on commit f2a1efe

Please sign in to comment.
You can’t perform that action at this time.