Skip to content

Commit f2a1efe

Browse files
committed
[GRASS] better format module's output
1 parent 667fb04 commit f2a1efe

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

src/plugins/grass/qgsgrassmodule.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -883,11 +883,6 @@ void QgsGrassModule::readStderr()
883883
QgsDebugMsg( "called." );
884884

885885
QString line;
886-
QRegExp rxpercent( "GRASS_INFO_PERCENT: (\\d+)" );
887-
QRegExp rxmessage( "GRASS_INFO_MESSAGE\\(\\d+,\\d+\\): (.*)" );
888-
QRegExp rxwarning( "GRASS_INFO_WARNING\\(\\d+,\\d+\\): (.*)" );
889-
QRegExp rxerror( "GRASS_INFO_ERROR\\(\\d+,\\d+\\): (.*)" );
890-
QRegExp rxend( "GRASS_INFO_END\\(\\d+,\\d+\\)" );
891886

892887
mProcess.setReadChannel( QProcess::StandardError );
893888
while ( mProcess.canReadLine() )

src/providers/grass/qgsgrass.cpp

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2648,28 +2648,44 @@ void QgsGrass::sleep( int ms )
26482648
#endif
26492649
}
26502650

2651-
QgsGrass::ModuleOutput QgsGrass::parseModuleOutput( const QString & input, QString &text, QString &html, int &percent )
2651+
QgsGrass::ModuleOutput QgsGrass::parseModuleOutput( const QString & input, QString &text, QString &html, int &value )
26522652
{
2653+
QgsDebugMsg( "input = " + input );
2654+
#ifdef QGISDEBUG
2655+
QString ascii;
2656+
for ( int i = 0; i < input.size(); i++ )
2657+
{
2658+
int c = input.at( i ).toAscii();
2659+
ascii += QString().sprintf( "%2x ", c );
2660+
}
2661+
QgsDebugMsg( "ascii = " + ascii );
2662+
#endif
2663+
26532664
QRegExp rxpercent( "GRASS_INFO_PERCENT: (\\d+)" );
26542665
QRegExp rxmessage( "GRASS_INFO_MESSAGE\\(\\d+,\\d+\\): (.*)" );
26552666
QRegExp rxwarning( "GRASS_INFO_WARNING\\(\\d+,\\d+\\): (.*)" );
26562667
QRegExp rxerror( "GRASS_INFO_ERROR\\(\\d+,\\d+\\): (.*)" );
26572668
QRegExp rxend( "GRASS_INFO_END\\(\\d+,\\d+\\)" );
2669+
// GRASS added G_progress() which does not suport GRASS_MESSAGE_FORMAT=gui
2670+
// and it is printing fprintf(stderr, "%10ld\b\b\b\b\b\b\b\b\b\b", n);
2671+
// Ticket created https://trac.osgeo.org/grass/ticket/2751
2672+
QRegExp rxprogress( " +(\\d+)\\b\\b\\b\\b\\b\\b\\b\\b\\b\\b" );
26582673

2659-
2674+
// We return simple messages in html non formated, monospace text should be set on widget
2675+
// where it is used because output may be formated assuming fixed width font
26602676
if ( input.trimmed().isEmpty() )
26612677
{
26622678
return OutputNone;
26632679
}
26642680
else if ( rxpercent.indexIn( input ) != -1 )
26652681
{
2666-
percent = rxpercent.cap( 1 ).toInt();
2682+
value = rxpercent.cap( 1 ).toInt();
26672683
return OutputPercent;
26682684
}
26692685
else if ( rxmessage.indexIn( input ) != -1 )
26702686
{
26712687
text = rxmessage.cap( 1 );
2672-
html = "<pre>" + text + "</pre>" ;
2688+
html = text;
26732689
return OutputMessage;
26742690
}
26752691
else if ( rxwarning.indexIn( input ) != -1 )
@@ -2690,10 +2706,15 @@ QgsGrass::ModuleOutput QgsGrass::parseModuleOutput( const QString & input, QStri
26902706
{
26912707
return OutputNone;
26922708
}
2709+
else if ( rxprogress.indexIn( input ) != -1 )
2710+
{
2711+
value = rxprogress.cap( 1 ).toInt();
2712+
return OutputProgress;
2713+
}
26932714
else // some plain text which cannot be parsed
26942715
{
26952716
text = input;
2696-
html = "<pre>" + text + "</pre>";
2717+
html = text;
26972718
return OutputMessage;
26982719
}
26992720
}

src/providers/grass/qgsgrass.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
145145
{
146146
OutputNone,
147147
OutputPercent,
148+
OutputProgress, // number of items processed if total number is unknown
148149
OutputMessage,
149150
OutputWarning,
150151
OutputError
@@ -577,8 +578,8 @@ class GRASS_LIB_EXPORT QgsGrass : public QObject
577578
* @param input input string read from module stderr
578579
* @param text parsed text
579580
* @param html html formated parsed text, e.g. + icons
580-
* @param percent progress 0-100 */
581-
static ModuleOutput parseModuleOutput( const QString & input, QString &text, QString &html, int &percent );
581+
* @param value percent 0-100 or progress as absolut number if total is unknown*/
582+
static ModuleOutput parseModuleOutput( const QString & input, QString &text, QString &html, int &value );
582583

583584
public slots:
584585
/** Close mapset and show warning if closing failed */

src/providers/grass/qgsgrassimport.cpp

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,27 +76,35 @@ void QgsGrassImportProgress::onReadyReadStandardError()
7676
{
7777
QgsDebugMsg( "line = '" + line + "'" );
7878
QString text, html;
79-
int percent;
80-
QgsGrass::ModuleOutput type = QgsGrass::parseModuleOutput( line, text, html, percent );
79+
int value;
80+
QgsGrass::ModuleOutput type = QgsGrass::parseModuleOutput( line, text, html, value );
8181
if ( type == QgsGrass::OutputPercent )
8282
{
8383
mProgressMin = 0;
8484
mProgressMax = 100;
85-
mProgressValue = percent;
85+
mProgressValue = value;
8686
emit progressChanged( html, mProgressHtml, mProgressMin, mProgressMax, mProgressValue );
8787
}
88+
else if ( type == QgsGrass::OutputProgress )
89+
{
90+
html = tr( "Progress: %1" ).arg( value );
91+
append( html );
92+
}
8893
else if ( type == QgsGrass::OutputMessage || type == QgsGrass::OutputWarning || type == QgsGrass::OutputError )
8994
{
90-
mProgressHtml += html;
91-
QgsDebugMsg( "text = " + text );
92-
emit progressChanged( html, mProgressHtml, mProgressMin, mProgressMax, mProgressValue );
95+
append( html );
9396
}
9497
}
9598
}
9699
}
97100

98101
void QgsGrassImportProgress::append( const QString & html )
99102
{
103+
QgsDebugMsg( "html = " + html );
104+
if ( !mProgressHtml.isEmpty() )
105+
{
106+
mProgressHtml += "<br>";
107+
}
100108
mProgressHtml += html;
101109
emit progressChanged( html, mProgressHtml, mProgressMin, mProgressMax, mProgressValue );
102110
}

0 commit comments

Comments
 (0)