1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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");
}
|