Skip to content
Permalink
Browse files

grass plugin: avoid garbled japanese/cyrillic chars in the tools' GUI (

…fix #4547, #3164)

Thanks Minoru Akagi for patches!
  • Loading branch information
brushtyler committed Nov 4, 2012
1 parent ef6b3c4 commit c53c85813f4723b75d4e9326d2565fb51eaa8355
Showing with 58 additions and 9 deletions.
  1. +58 −9 src/plugins/grass/qgsgrassmodule.cpp
@@ -353,10 +353,61 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions(
return;
}

QByteArray baDesc = process.readAllStandardOutput();

// GRASS commands usually output text in system default encoding.
// Let's use the System codec whether Qt doesn't recognize the encoding
// of the interface description (see http://hub.qgis.org/issues/4547)
QTextCodec *codec = 0;

QgsDebugMsg( "trying to get encoding name from XML interface description..." );

// XXX: getting the encoding using a regular expression works
// until GRASS will use UTF-16 or UTF-32.
// TODO: We should check the correct encoding by using the BOM (Byte
// Order Mark) from the beginning of the data.
QString xmlDeclaration = QString::fromUtf8( baDesc ).section( '>', 0, 0, QString::SectionIncludeTrailingSep );
QRegExp reg( "<\\?xml\\s+.*encoding\\s*=\\s*(['\"])([A-Za-z][-a-zA-Z0-9_.]*)\\1\\s*\\?>" );
if ( reg.indexIn( xmlDeclaration ) != -1 )
{
QByteArray enc = reg.cap( 2 ).toLocal8Bit();
QgsDebugMsg( QString( "found encoding name '%1'" ).arg( enc ) );

codec = QTextCodec::codecForName( enc );
if ( !codec )
{
QgsDebugMsg( "unrecognized encoding name. Let's use 'System' codec" );
codec = QTextCodec::codecForName( "System" );
Q_ASSERT( codec );
}
}
else
{
QgsDebugMsg( "unable to get encoding name from XML content. Will let Qt detects encoding!" );
}

bool ok = false;
QDomDocument gDoc( "task" );
QString err;
int line, column;
if ( !gDoc.setContent( &process, false, &err, &line, &column ) )

if ( codec )
{
QgsDebugMsg( QString( "parsing XML interface description using '%1' codec..." ).arg( codec->name() ) );
ok = gDoc.setContent( codec->toUnicode( baDesc ), false, &err, &line, &column );
if ( !ok )
{
QgsDebugMsg( "parse FAILED using '%1' codec. Will let Qt detects encoding" );
codec = 0;
}
}

if ( !codec )
{
ok = gDoc.setContent( baDesc, false, &err, &line, &column );
}

if ( !ok )
{
QString errmsg = tr( "Cannot read module description (%1):" ).arg( mXName )
+ tr( "\n%1\nat line %2 column %3" ).arg( err ).arg( line ).arg( column );
@@ -401,7 +452,7 @@ QgsGrassModuleStandardOptions::QgsGrassModuleStandardOptions(
mypInnerFrameLayout->addWidget( &mAdvancedFrame );
mypInnerFrameLayout->addStretch( 1 );

// Hide advanced and set butto next
// Hide advanced and set button next
switchAdvanced();

QVBoxLayout *mypSimpleLayout = new QVBoxLayout( mypSimpleFrame );
@@ -1555,9 +1606,9 @@ void QgsGrassModule::readStdout()
mProcess.setReadChannel( QProcess::StandardOutput );
while ( mProcess.canReadLine() )
{
//line = QString::fromLocal8Bit( mProcess.readLineStdout().ascii() );
QByteArray ba = mProcess.readLine();
line = QString::fromUtf8( ba ).replace( '\n', "" );
//line = QString::fromUtf8( ba ).replace( '\n', "" );
line = QString::fromLocal8Bit( ba ).replace( '\n', "" );
//QgsDebugMsg(QString("line: '%1'").arg(line));

// GRASS_INFO_PERCENT is catched here only because of bugs in GRASS,
@@ -1578,21 +1629,19 @@ void QgsGrassModule::readStderr()
{
QgsDebugMsg( "called." );

mProcess.setReadChannel( QProcess::StandardError );

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() )
{
//line = QString::fromLocal8Bit( mProcess.readLineStderr().ascii() );
QByteArray ba = mProcess.readLine();
line = QString::fromUtf8( ba ).replace( '\n', "" );
//line = QString::fromUtf8( ba ).replace( '\n', "" );
line = QString::fromLocal8Bit( ba ).replace( '\n', "" );
//QgsDebugMsg(QString("line: '%1'").arg(line));

if ( rxpercent.indexIn( line ) != -1 )

0 comments on commit c53c858

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