|
From: Tapio V. <aa...@us...> - 2011-01-28 07:34:36
|
Module: editor
Branch: master
Commit: 042b08f5d2dc08300dedf9cf0c6fa084e24a849b
Author: Tapio Vierros <tap...@gm...>
Date: Fri Jan 28 09:33:31 2011 +0200
Handle encoding in plain text lyrics import too.
---
editorapp.cc | 4 ++--
songparser.cc | 13 +------------
textcodecselector.hh | 16 ++++++++++++++++
3 files changed, 19 insertions(+), 14 deletions(-)
diff --git a/editorapp.cc b/editorapp.cc
index 99d2b5d..b9b1b8b 100644
--- a/editorapp.cc
+++ b/editorapp.cc
@@ -15,6 +15,7 @@
#include "notelabel.hh"
#include "notegraphwidget.hh"
#include "songwriter.hh"
+#include "textcodecselector.hh"
namespace {
static const QString PROJECT_SAVE_FILE_EXTENSION = "songproject"; // FIXME: Nice extension here
@@ -427,8 +428,7 @@ void EditorApp::on_actionLyricsFromFile_triggered()
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
- QTextStream in(&file);
- QString text = in.readAll();
+ QString text = TextCodecSelector::readAllAndHandleEncoding(file, this);
if (text != "") noteGraph->setLyrics(text);
}
}
diff --git a/songparser.cc b/songparser.cc
index c917154..2a48c8e 100644
--- a/songparser.cc
+++ b/songparser.cc
@@ -38,18 +38,7 @@ SongParser::SongParser(Song& s):
if (finfo.size() < 10 || finfo.size() > 100000) throw SongParserException("Does not look like a song file (wrong size)", 1, true);
// Determine encoding
- QString data;
- {
- QByteArray ba = file.readAll();
- if (!ba.isEmpty()) {
- data = QString::fromLocal8Bit(ba, ba.size());
- if (data.toLocal8Bit().size() != ba.size()) {
- // Not UTF8 :(
- QTextCodec* codec = TextCodecSelector::codecForContent(ba);
- if (codec) data = codec->toUnicode(ba);
- }
- }
- }
+ QString data = TextCodecSelector::readAllAndHandleEncoding(file);
file.close();
if (smCheck(data)) type = SM;
diff --git a/textcodecselector.hh b/textcodecselector.hh
index 225a332..79cc4bc 100644
--- a/textcodecselector.hh
+++ b/textcodecselector.hh
@@ -25,6 +25,7 @@
#include <QLabel>
#include <QVBoxLayout>
#include <QByteArray>
+#include <QFile>
#include <iostream>
@@ -89,6 +90,21 @@ public:
return 0;
}
+ static QString readAllAndHandleEncoding(QFile &file, QWidget *parent = 0)
+ {
+ QString data = "";
+ QByteArray ba = file.readAll();
+ if (!ba.isEmpty()) {
+ data = QString::fromLocal8Bit(ba, ba.size());
+ if (data.toLocal8Bit().size() != ba.size()) {
+ // Not UTF8 :(
+ QTextCodec* codec = codecForContent(ba, parent);
+ if (codec) data = codec->toUnicode(ba);
+ }
+ }
+ return data;
+ }
+
private:
QListWidget *list;
QList<QByteArray> codecs;
|