summaryrefslogtreecommitdiff
path: root/utfscriptmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'utfscriptmodel.cpp')
-rw-r--r--utfscriptmodel.cpp105
1 files changed, 105 insertions, 0 deletions
diff --git a/utfscriptmodel.cpp b/utfscriptmodel.cpp
new file mode 100644
index 0000000..d7ef114
--- /dev/null
+++ b/utfscriptmodel.cpp
@@ -0,0 +1,105 @@
+#include <QRegExp>
+#include <QDebug>
+#include "utfscriptmodel.h"
+
+utfScriptModel::utfScriptModel()
+{
+ fl = new QFile(this);
+ rows = 0;
+ positions = NULL;
+}
+
+utfScriptModel::utfScriptModel(const QString &file)
+{
+ utfScriptModel();
+ openScript(file);
+}
+
+utfScriptModel::~utfScriptModel()
+{
+ fl->close();
+ delete fl;
+ if(positions != NULL)
+ delete [] positions;
+}
+
+void utfScriptModel::openScript(const QString &file)
+{
+ QTextStream str;
+ fl->setFileName(file);
+ fl->open(QIODevice::ReadWrite | QIODevice::Text);
+ str.setDevice(fl);
+ str.setCodec("UTF-8");
+ str.seek(0);
+ // let's hog some memory, baby!
+ while(!str.atEnd())
+ lines.append(str.readLine());
+ calcPositions();
+}
+
+// updates int rows and int *positions
+void utfScriptModel::calcPositions()
+{
+ if(positions != NULL)
+ delete [] positions;
+
+ // lines.size() is actually way too much, but at least we can be
+ // sure it's enough without having to calculate the actual size
+ positions = new qint64[lines.size()+1];
+
+ rows = 0;
+ int i;
+ for(i=0; i<lines.size(); i++)
+ if(lines.at(i).contains(QRegExp("^<\\d{4}>")))
+ positions[rows++] = i;
+ positions[rows] = i;
+}
+
+int utfScriptModel::rowCount(const QModelIndex &parent) const
+{
+ if(!fl->isOpen())
+ return 0;
+ return rows;
+}
+
+int utfScriptModel::columnCount(const QModelIndex &parent) const
+{
+ return 4; // #line, original, translation, comments
+}
+
+QVariant utfScriptModel::data(const QModelIndex &index, int role) const
+{
+ if(!index.isValid() || index.row() >= rows || index.column() > 3 || role != Qt::DisplayRole)
+ return QVariant();
+
+ QRegExp tra("<(\\d{4})>\\s*(.+)");
+ QRegExp ori("//\\s*<(\\d{4})>\\s*(.+)");
+ QRegExp com("//(.+)");
+
+ QString ln, num, orig, trans, comm;
+ for(int i = positions[index.row()]; i < positions[index.row()+1]; i++) {
+ ln = lines.at(i);
+ if(tra.exactMatch(ln)) {
+ num = tra.cap(1);
+ trans = tra.cap(2);
+ } else if(ori.exactMatch(ln)) {
+ orig = ori.cap(2);
+ if(orig == trans)
+ trans = "";
+ } else if(com.exactMatch(ln)) {
+ if(comm != "")
+ comm += "\n";
+ comm += com.cap(1);
+ } else
+ break;
+ }
+
+ return index.column() == 0 ? num : index.column() == 1 ? orig : index.column() == 2 ? trans : comm;
+}
+
+QVariant utfScriptModel::headerData(int section, Qt::Orientation orientation, int role) const
+{
+ if(orientation == Qt::Vertical || role != Qt::DisplayRole)
+ return QVariant();
+ return QString(section == 0 ? "Line" : section == 1 ? "Original" : section == 2 ? "Translation" : "Comments");
+}