-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
453 lines (406 loc) · 9.36 KB
/
main.cpp
File metadata and controls
453 lines (406 loc) · 9.36 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <QDialog>
#include "base.h"
#include "mainwin.h"
#include "replace.h"
QList<TPage> book;
QList<TIcon> icons;
QList<TBookmark> bookmarks;
QList<TPage> getBook() {
return book;
}
void prjInit() {
book.clear();
icons.clear();
bookmarks.clear();
}
TPage* putPage(TPage page) {
book.append(page);
return &book.last();
}
TPage* addPage(TPage page) {
page.id = QUuid::createUuid();
book.append(page);
return &book.last();
}
TPage* createPage() {
TPage page;
page.curRow = -1;
page.id = QUuid::createUuid();
page.text.clear();
book.append(page);
return &book.last();
}
void removePage(QUuid id) {
int i;
for (i = 0; i < book.size(); i++) {
if (book[i].id == id) {
book.removeAt(i);
break;
}
}
}
void putText(TLine& line,QString data,int type) {
switch (type) {
case TL_SRC:
line.src.text = data;
line.trn.text.clear();
break;
case TL_TRN:
line.src.text.clear();
line.trn.text = data;
break;
}
}
QStringList splitLine(QString text, QString div) {
QStringList lst;
lst << "" << "";
int pos = text.indexOf(div);
if (pos < 0) {
lst.last() = text;
} else {
lst.first() = text.left(pos);
lst.last() = text.mid(pos + 1);
}
return lst;
}
void normLine(TLine& line) {
QStringList pair;
int pos;
line.src.text.remove("\n");
line.src.name.remove(" ");
line.src.text.remove(" ");
pair.clear();
pair.append(line.src.name);
pair.prepend(line.src.text);
if (line.src.name.isEmpty()) {
if (line.src.text.endsWith("」")) {
pair = splitLine(line.src.text, "「");
line.src.name = pair.first();
line.src.text = pair.last();
line.src.text.remove("「");
line.src.text.remove("」");
} else if (line.src.text.endsWith(")")) {
pos = line.src.text.indexOf("(");
if ((pos > 0) && (pos < 10)) {
pair = splitLine(line.src.text, "(");
line.src.name = pair.first();
line.src.text = pair.last().prepend("(");
}
}
if (line.src.text.indexOf("\t") != -1) {
pair = splitLine(line.src.text, "\t");
line.src.name = pair.first();
line.src.text = pair.last();
}
if (line.src.text.startsWith("【")) {
int idx = line.src.text.indexOf("】");
if (idx != -1) {
line.src.name = line.src.text.mid(1,idx-1);
line.src.text = line.src.text.mid(idx+1);
}
}
line.src.name.remove("【");
line.src.name.remove("】");
}
if (line.trn.name.isEmpty()) {
pair = splitLine(line.trn.text, "\t");
line.trn.name = pair.first();
line.trn.text = pair.last();
}
}
// icons
TIcon* findIcon(QUuid id) {
TIcon* ico = NULL;
if (id.isNull()) return ico;
for (int i = 0; i < icons.size(); i++) {
if (icons.at(i).id == id) {
ico = &icons[i];
}
}
return ico;
}
QIcon getIcon(QUuid id) {
if (id.isNull()) return QIcon(":/folder.png");
QIcon ico;
for (int i = 0; i < icons.size(); i++) {
if (icons.at(i).id == id) {
ico = icons.at(i).icon;
}
}
return ico;
}
QIcon getPageIcon(TPage* page) {
QPixmap pix(32,32);
QPainter pnt;
int prc = getProgress(page);
pnt.begin(&pix);
int high = 0.30 * prc;
pnt.fillRect(0,0,32,32,Qt::lightGray);
pnt.fillRect(0,0,32,8,Qt::red);
pnt.fillRect(1,1,high,6,Qt::green);
pnt.setFont(QFont("FreeSans",14,QFont::Bold));
pnt.setPen(Qt::black);
pnt.drawText(QRect(1,8,30,24),Qt::AlignCenter,QString::number(prc));
pnt.end();
return QIcon(pix);
}
int addIcon(TIcon ico) {
int res = 1;
if (ico.id.isNull()) {
ico.id = QUuid::createUuid();
}
if (getIcon(ico.id).isNull()) {
icons.append(ico);
} else {
res = 0;
}
return res;
}
void rmIcon(QUuid id) {
for (int i = icons.size() - 1; i >= 0; i--) {
if (icons.at(i).id == id)
icons.removeAt(i);
}
}
// bookmarks
QList<TBookmark>* get_bmlist_ptr() {
return &bookmarks;
}
TBookmark* findBookmark(QUuid id) {
TBookmark* res = NULL;
for (int i = 0; i < bookmarks.size(); i++) {
if (bookmarks.at(i).id == id) {
res = &bookmarks[i];
}
}
return res;
}
QUuid addBookmark(TBookmark bm) {
TBookmark* tb = NULL;
if (bm.id.isNull()) { // new bookmark
bm.id = QUuid::createUuid();
bookmarks.append(bm);
// qDebug() << "new bm " << bm.id;
} else { // find if exists
tb = findBookmark(bm.id);
if (!tb) {
// qDebug() << "new bm(2) " << bm.id;
bookmarks.append(bm); // not found
} else {
tb->name = bm.name; // found: update existing bookmark
tb->descr = bm.descr;
if (tb->pgid.isNull()) {
tb->pgid = bm.pgid;
tb->row = bm.row;
}
}
}
return bm.id;
}
void rmBookmark(QUuid id) {
for (int i = bookmarks.size() - 1; i >= 0; i--) {
if (bookmarks.at(i).id == id)
bookmarks.removeAt(i);
}
}
// abelsoft scripts
TPage loadAbelsoft(QString fnam, int) {
TPage page;
QFile file(fnam);
QString line;
QString name;
TLine tlin;
tlin.type = TL_TEXT;
int idx,i;
QStringList taglist;
QStringList arglist;
QStringList argvals;
QTextCodec* codec = QTextCodec::codecForName("Shift-JIS");
if (file.open(QFile::ReadOnly)) {
while (!file.atEnd()) {
line = codec->toUnicode(file.readLine());
line.remove("\r");
line.remove("\n");
idx = line.indexOf(";");
if (idx > -1) line = line.left(idx);
if (!line.isEmpty()) {
if (line.startsWith("<")) {
taglist = line.split(QRegExp("[<>]"),Qt::SkipEmptyParts);
for (idx = 0; idx < taglist.size(); idx++) {
arglist = taglist.at(idx).split(" ",Qt::SkipEmptyParts);
if (arglist.first() == "WINDOW") {
for (i = 1; i < arglist.size(); i++) {
argvals = arglist.at(i).split("=",Qt::SkipEmptyParts);
if (argvals.first() == "NAME") name = argvals.last().remove("\"");
}
} else if (arglist.first() == "IMG") {
for (i = 1; i < arglist.size(); i++) {
argvals = arglist.at(i).split("=",Qt::SkipEmptyParts);
if (argvals.first() == "SRC") {
tlin.src.name.clear();
tlin.src.text.clear();
page.text.append(tlin);
tlin.src.text = QString("[img = ").append(argvals.last().remove("\"")).append("]");
page.text.append(tlin);
}
}
} else if (arglist.first() == "SOUND") {
// do nothing
} else {
tlin.src.name.clear();
tlin.src.text = QString("[%0]").arg(taglist.at(idx));
page.text.append(tlin);
}
}
} else {
if (!name.isEmpty()) {
if (line.startsWith("「")) line.remove(0,1);
if (line.endsWith("」")) line.resize(line.length() - 1);
}
tlin.src.name = name;
tlin.src.text = line;
page.text.append(tlin);
name.clear();
}
}
}
}
return page;
}
TPage loadPage(QString fnam, int type) {
TPage page;
QFile file(fnam);
QString line;
QStringList com;
QString cond = "";
QStringList condStack;
TLine tlin;
QString bg = "";
QString pl = "";
QString pm = "";
QString pr = "";
page.id = 0;
page.text.clear();
condStack.clear();
if (file.open(QFile::ReadOnly)) {
while (!file.atEnd()) {
tlin.type = 0;
tlin.src.text.clear();
tlin.src.name.clear();
tlin.trn.text.clear();
tlin.trn.name.clear();
line = QString::fromUtf8(file.readLine()).remove("\r").remove("\n");
// if (line != "") {
if (line.startsWith("@")) {
com = line.split(":",Qt::KeepEmptyParts);
while (com.size() < 5) com.append("");
// @S:id:name selection
if (com[0] == "@S") {
tlin.type = TL_SELECT;
putText(tlin,com[2],type);
page.text.append(tlin);
}
// @B:id ... @E block
if (com[0] == "@B") {
if (cond != "") condStack.append(cond);
cond = com[1];
}
if (com[0] == "@E") {
if (condStack.size() > 0) {
cond = condStack.last();
condStack.removeLast();
} else {
cond.clear();
}
}
// @L:id:text condition line
if (com[0] == "@L") {
tlin.type = TL_TEXT;
putText(tlin,com[2],type);
page.text.append(tlin);
}
// @BG:name
if (com[0] == "@BG") {
bg = com[1];
}
// @PL[PM,PR]:name
if (com[0] == "@PL") {
pl = com[1];
}
if (com[0] == "@PM") {
pm = com[1];
}
if (com[0] == "@PR") {
pr = com[1];
}
} else {
tlin.type = TL_TEXT;
tlin.flag = 0;
putText(tlin,line,type);
normLine(tlin);
page.text.append(tlin);
}
// }
}
}
return page;
}
int getLineStatus(TLine line) {
int res = LS_NONE;
if (line.type == TL_TEXT) {
if (!(line.src.text.startsWith("[") ||
(line.src.text.startsWith("==")) ||
(line.src.text.isEmpty() && line.src.name.isEmpty()))) {
res = LS_UNTRN;
if (line.src.text.isEmpty() || !line.trn.text.isEmpty())
res = LS_TRN;
}
}
return res;
}
void getCounts(TPage* page,int& trans, int& total) {
trans = 0;
total = 0;
foreach(TLine line, page->text) {
switch(getLineStatus(line)) {
case LS_TRN:
trans++;
total++;
break;
case LS_UNTRN:
total++;
break;
}
}
}
int getProgress(TPage* page) {
int total;
int trans;
getCounts(page,trans,total);
if (total == 0) return 0;
return trans * 100.0 / (double)total;
}
TPage* findPage(QUuid id) {
int i;
TPage* page = NULL;
for (i=0; i<book.size(); i++) {
if (book[i].id == id) {
page = &book[i];
break;
}
}
return page;
}
int main(int ac,char** av) {
QApplication app(ac,av);
app.setOrganizationName("samstyle");
app.setApplicationName("transtext");
MWindow win;
Replacer rpl(&win);
app.connect(&win, &MWindow::rqReplace, &rpl, &Replacer::show);
app.connect(&rpl, &Replacer::confirm, &win, &MWindow::replace);
win.show();
if (ac > 1)
win.openPrj(QString(av[1]));
return app.exec();
}