changeset 56:e20aafd4bd18

[Fix] Bug ID 1651071: There were still problems with different encodings. All save functions use not Utf-8 for file saving and the indenter loads the formatted file as Utf-8. The encoding change now really reloads the file from disc with the set encoding. git-svn-id: svn://svn.code.sf.net/p/universalindent/code/trunk@213 59b1889a-e5ac-428c-b0c7-476e01d41282
author thomas_-_s <thomas_-_s@59b1889a-e5ac-428c-b0c7-476e01d41282>
date Tue, 06 Feb 2007 09:51:21 +0000
parents 22221d05024d
children 6ea2eeb5ab5d
files src/indenthandler.cpp src/indenthandler.h src/mainwindow.cpp
diffstat 3 files changed, 26 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/src/indenthandler.cpp	Mon Feb 05 19:41:50 2007 +0000
+++ b/src/indenthandler.cpp	Tue Feb 06 09:51:21 2007 +0000
@@ -132,7 +132,7 @@
 
     // write the source code to the input file for the indenter
     outSrcFile.open( QFile::ReadWrite | QFile::Text );
-    outSrcFile.write( sourceCode.toAscii() );
+    outSrcFile.write( sourceCode.toUtf8() );
     outSrcFile.close();
     
     // errors and standard outputs from the process call are merged together
@@ -197,7 +197,9 @@
     // read the output file generated by the indenter call
     outSrcFile.setFileName(dataDirctoryStr + outputFileName + inputFileExtension);
     outSrcFile.open(QFile::ReadOnly | QFile::Text);
-    formattedSourceCode = outSrcFile.readAll();
+    QTextStream outSrcStrm(&outSrcFile);
+    outSrcStrm.setCodec( QTextCodec::codecForName("UTF-8") );
+    formattedSourceCode = outSrcStrm.readAll();
     outSrcFile.close();
 
     // delete the temporary input and output files
--- a/src/indenthandler.h	Mon Feb 05 19:41:50 2007 +0000
+++ b/src/indenthandler.h	Tue Feb 06 09:51:21 2007 +0000
@@ -30,6 +30,9 @@
 #include <QDir>
 #include <QMessageBox>
 #include <QMainWindow>
+#include <QTextStream>
+#include <QTextCodec>
+
 
 class IndentHandler : public QWidget
 {
--- a/src/mainwindow.cpp	Mon Feb 05 19:41:50 2007 +0000
+++ b/src/mainwindow.cpp	Tue Feb 06 09:51:21 2007 +0000
@@ -172,7 +172,7 @@
     else {
         QTextStream inSrcStrm(&inSrcFile);
         QApplication::setOverrideCursor(Qt::WaitCursor);
-		//inSrcStrm.setCodec( QTextCodec::codecForName("UTF-8") );
+		inSrcStrm.setCodec( QTextCodec::codecForName("UTF-8") );
         fileContent = inSrcStrm.readAll();
         QApplication::restoreOverrideCursor();
         inSrcFile.close();
@@ -243,7 +243,7 @@
     QFile::remove(fileName);
     QFile outSrcFile(fileName);
     outSrcFile.open( QFile::ReadWrite | QFile::Text );
-    outSrcFile.write( savedSourceContent.toAscii() );
+    outSrcFile.write( savedSourceContent.toUtf8() );
     outSrcFile.close();
 
     QFileInfo fileInfo(fileName);
@@ -271,7 +271,7 @@
         QFile outSrcFile(currentSourceFile);
         savedSourceContent = txtedSourceCode->toPlainText();
         outSrcFile.open( QFile::ReadWrite | QFile::Text );
-        outSrcFile.write( savedSourceContent.toAscii() );
+        outSrcFile.write( savedSourceContent.toUtf8() );
         outSrcFile.close();
 
         txtedSourceCode->document()->setModified( false );
@@ -936,12 +936,22 @@
 */
 void MainWindow::encodingChanged(QAction* encodingAction) {
 	if ( maybeSave() ) {
-		QByteArray qb = savedSourceContent.toLocal8Bit();
-		QTextStream inSrcStrm(&qb);
-		QString encodingName = encodingAction->text();
-		inSrcStrm.setCodec( QTextCodec::codecForName(encodingName.toAscii()) );
-		QString newSrcCode = inSrcStrm.readAll();
-		txtedSourceCode->setPlainText( newSrcCode );
+        QFile inSrcFile(currentSourceFile);
+        QString fileContent = "";
+
+        if ( !inSrcFile.open(QFile::ReadOnly | QFile::Text) ) {
+            QMessageBox::warning(NULL, tr("Error opening file"), tr("Cannot read the file ")+"\""+currentSourceFile+"\"." );
+        }
+        else {
+            QTextStream inSrcStrm(&inSrcFile);
+            QApplication::setOverrideCursor(Qt::WaitCursor);
+            QString encodingName = encodingAction->text();
+            inSrcStrm.setCodec( QTextCodec::codecForName(encodingName.toAscii()) );
+            fileContent = inSrcStrm.readAll();
+            QApplication::restoreOverrideCursor();
+            inSrcFile.close();
+            txtedSourceCode->setPlainText( fileContent );
+        }
 	}
 }