Skip to content

Commit

Permalink
Fix QTextStream encode with UTF-8
Browse files Browse the repository at this point in the history
  • Loading branch information
zy6p committed May 6, 2021
1 parent 346bffc commit a06e9d2
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions src/analysis/interpolation/qgsgridfilewriter.cpp
Expand Up @@ -48,6 +48,9 @@ int QgsGridFileWriter::writeFile( QgsFeedback *feedback )
}

QTextStream outStream( &outputFile );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
outStream.setCodec( "UTF-8" );
#endif
outStream.setRealNumberPrecision( 8 );
writeHeader( outStream );

Expand Down
10 changes: 8 additions & 2 deletions src/analysis/processing/qgsalgorithmexportmesh.cpp
Expand Up @@ -1276,10 +1276,13 @@ QVariantMap QgsMeshExportCrossSection::processAlgorithm( const QVariantMap &para

QString outputFileName = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );
QFile file( outputFileName );
if ( ! file.open( QIODevice::WriteOnly ) )
if ( ! file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
throw QgsProcessingException( QObject::tr( "Unable to create the outputfile" ) );

QTextStream textStream( &file );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
textStream.setCodec( "UTF-8" );
#endif
QStringList header;
header << QStringLiteral( "fid" ) << QStringLiteral( "x" ) << QStringLiteral( "y" ) << QObject::tr( "offset" );
for ( const DataGroup &datagroup : mDataPerGroup )
Expand Down Expand Up @@ -1588,10 +1591,13 @@ QVariantMap QgsMeshExportTimeSeries::processAlgorithm( const QVariantMap &parame

QString outputFileName = parameterAsFileOutput( parameters, QStringLiteral( "OUTPUT" ), context );
QFile file( outputFileName );
if ( ! file.open( QIODevice::WriteOnly ) )
if ( ! file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
throw QgsProcessingException( QObject::tr( "Unable to create the outputfile" ) );

QTextStream textStream( &file );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
textStream.setCodec( "UTF-8" );
#endif
QStringList header;
header << QStringLiteral( "fid" ) << QStringLiteral( "x" ) << QStringLiteral( "y" ) << QObject::tr( "time" );

Expand Down
Expand Up @@ -134,7 +134,7 @@ QVariantMap QgsNearestNeighbourAnalysisAlgorithm::processAlgorithm( const QVaria
if ( !outputFile.isEmpty() )
{
QFile file( outputFile );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream out( &file );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Expand Down
5 changes: 4 additions & 1 deletion src/analysis/processing/qgsalgorithmpointstopaths.cpp
Expand Up @@ -329,10 +329,13 @@ QVariantMap QgsPointsToPathsAlgorithm::processAlgorithm( const QVariantMap &para
{
const QString filename = QDir( textDir ).filePath( hit.key().toString() + QString( ".txt" ) );
QFile textFile( filename );
if ( !textFile.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( !textFile.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
throw QgsProcessingException( QObject::tr( "Cannot open file for writing " ) + filename );

QTextStream out( &textFile );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
out.setCodec( "UTF-8" );
#endif
out << QString( "angle=Azimuth\n"
"heading=Coordinate_System\n"
"dist_units=Default\n"
Expand Down
Expand Up @@ -17,7 +17,6 @@

#include "qgsalgorithmrasterlayerproperties.h"
#include "qgsstringutils.h"
#include <QTextStream>

///@cond PRIVATE

Expand Down
Expand Up @@ -180,7 +180,7 @@ QVariantMap QgsRasterLayerUniqueValuesReportAlgorithm::processAlgorithm( const Q
if ( !outputFile.isEmpty() )
{
QFile file( outputFile );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
const QString encodedAreaUnit = QgsStringUtils::ampersandEncode( areaUnit );

Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/qgsalgorithmrasterstatistics.cpp
Expand Up @@ -100,7 +100,7 @@ QVariantMap QgsRasterStatisticsAlgorithm::processAlgorithm( const QVariantMap &p
if ( !outputFile.isEmpty() )
{
QFile file( outputFile );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
QTextStream out( &file );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Expand Down
Expand Up @@ -216,7 +216,7 @@ QVariantMap QgsRasterSurfaceVolumeAlgorithm::processAlgorithm( const QVariantMap
if ( !outputFile.isEmpty() )
{
QFile file( outputFile );
if ( file.open( QIODevice::WriteOnly | QIODevice::Text ) )
if ( file.open( QIODevice::WriteOnly | QIODevice::Truncate ) )
{
const QString encodedAreaUnit = QgsStringUtils::ampersandEncode( areaUnit );

Expand Down
3 changes: 3 additions & 0 deletions src/analysis/processing/qgsalgorithmsavelog.cpp
Expand Up @@ -84,6 +84,9 @@ QVariantMap QgsSaveLogToFileAlgorithm::processAlgorithm( const QVariantMap &para
throw QgsProcessingException( QObject::tr( "Could not save log to file %1" ).arg( file ) );
}
QTextStream fout( &exportFile );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
fout.setCodec( "UTF-8" );
#endif
fout << ( useHtml ? feedback->htmlLog() : feedback->textLog() );
}
QVariantMap res;
Expand Down
3 changes: 3 additions & 0 deletions src/analysis/raster/qgsrelief.cpp
Expand Up @@ -543,6 +543,9 @@ bool QgsRelief::exportFrequencyDistributionToCsv( const QString &file )
}

QTextStream outstream( &outFile );
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
outstream.setCodec( "UTF-8" );
#endif
for ( int i = 0; i < 252; ++i )
{
outstream << QString::number( i ) + ',' + QString::number( frequency[i] ) << endl;
Expand Down

0 comments on commit a06e9d2

Please sign in to comment.