-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathscript.cpp
More file actions
706 lines (623 loc) · 22.8 KB
/
script.cpp
File metadata and controls
706 lines (623 loc) · 22.8 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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
/**
* This file is part of Subtivals.
*
* Subtivals is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Subtivals is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Subtivals. If not, see <http://www.gnu.org/licenses/>
**/
#include <QtCore/QFile>
#include <QtCore/QFileInfo>
#include <QtCore/QSettings>
#include <QtCore/QStringList>
#include <QtCore/QTime>
#include <QtXml/QDomDocument>
#include <QtXml/QDomNodeList>
#include <QRegularExpression>
#include <QFont>
#include "script.h"
#define DEFAULT_FONT_SIZE 27
#define DEFAULT_FONT_NAME "Tiresias Signfont"
enum SectionType {
SECTION_NONE,
SECTION_INFOS,
SECTION_STYLES,
SECTION_EVENTS
};
bool compareSubtitleStartTime(const Subtitle *s1, const Subtitle *s2) {
return s1->msseStart() < s2->msseStart();
}
QString sp2nbsp(const QString s) {
// Replace multiple spaces between words by non breakable spaces
if (QRegularExpression("\\S\\s{2,}\\S").match(s).hasMatch()) {
return QString(s).replace(" ", " ");
}
return s;
}
Script::Script(const QString &p_fileName, int p_charsRate,
int p_subtitleInterval, int p_subtitleMinDuration,
QObject *p_parent)
: QObject(p_parent), m_fileName(p_fileName), m_charsRate(p_charsRate),
m_subtitleInterval(p_subtitleInterval),
m_subtitleMinDuration(p_subtitleMinDuration) {
// Read and process each line of the input file
QFile file(m_fileName);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return;
QStringList content;
while (!file.atEnd()) {
QString line = QString::fromUtf8(file.readLine());
// Mac OS Classic end line support (CR)
QStringList macOsLineList = line.split("\r");
if (macOsLineList.size() > 1) {
content.append(macOsLineList);
} else {
line = line.trimmed().replace("\n", "");
if (!line.startsWith(";")) {
content.append(line);
}
}
}
QFileInfo fileInfo(p_fileName);
QString ext = fileInfo.suffix().toLower();
if (ext == "ass") {
m_format = ASS;
loadFromAss(content);
} else if (ext == "srt") {
m_format = SRT;
loadFromSrt(content);
} else if (ext == "txt") {
m_format = TXT;
loadFromTxt(content);
} else if (ext == "xml") {
m_format = XML;
loadFromXml(content.join(""));
}
std::sort(m_subtitles.begin(), m_subtitles.end(), compareSubtitleStartTime);
}
Script::ScriptFormat Script::format() const { return m_format; }
quint64 Script::totalDuration() const {
if (m_subtitles.isEmpty())
return 0;
return m_subtitles.last()->msseEnd();
}
const QString &Script::fileName() const { return m_fileName; }
const QString Script::title() const {
if (!m_title.isEmpty()) {
return m_title;
}
return QFileInfo(m_fileName).baseName();
}
int Script::charsRate() const { return m_charsRate; };
int Script::subtitleInterval() const { return m_subtitleInterval; };
int Script::subtitleMinDuration() const { return m_subtitleMinDuration; };
SubtitleStyle *Script::style(const QString &p_name) const {
// If style is unknown, return first one.
if (!m_styles.contains(p_name)) {
if (m_styles.isEmpty())
return nullptr;
return m_styles[m_styles.keys().at(0)];
}
return m_styles[p_name];
}
QList<SubtitleStyle *> Script::styles() const { return m_styles.values(); }
int Script::subtitlesCount() const { return m_subtitles.size(); }
QSize Script::resolution() const { return m_resolution; }
bool Script::hasComments() const {
foreach (Subtitle *subtitle, m_subtitles) {
if (!subtitle->comments().isEmpty())
return true;
}
return false;
}
const QList<Subtitle *> Script::subtitles() const {
return QList<Subtitle *>(m_subtitles);
}
const Subtitle *Script::subtitleAt(int i) const {
Q_ASSERT(i >= 0 && i < m_subtitles.count());
return m_subtitles[i];
}
const QList<Subtitle *> Script::currentSubtitles(qlonglong elapsed) const {
QList<Subtitle *> l;
foreach (Subtitle *e, m_subtitles) {
if (e->match(elapsed)) {
l.append(e);
}
}
return l;
}
const QList<Subtitle *> Script::nextSubtitles(qlonglong elapsed) const {
// Look for the first subtitle among next ones
int i = 0;
for (; i < m_subtitles.count(); i++) {
Subtitle *e = m_subtitles.at(i);
if (e->msseStart() >= elapsed) {
break;
}
}
// If not any, return empty
if (i >= m_subtitles.count()) {
const QList<Subtitle *> l;
return l;
}
// Return the list of subtitles starting at this time
qlonglong nextStart = m_subtitles.at(i)->msseStart();
return currentSubtitles(nextStart + 1);
}
const QList<Subtitle *> Script::previousSubtitles(qlonglong elapsed) const {
// Look for the last subtitle among previous ones
int i = m_subtitles.count() - 1;
for (; i >= 0; i--) {
Subtitle *e = m_subtitles.at(i);
if (e->msseEnd() < elapsed) {
break;
}
}
// If not any, return empty
if (i < 0) {
const QList<Subtitle *> l;
return l;
}
// Return the list of subtitles starting at this time
qlonglong previousEnd = m_subtitles.at(i)->msseEnd();
return currentSubtitles(previousEnd - 1);
}
void Script::correctSubtitlesDuration(bool p_state) {
foreach (Subtitle *e, m_subtitles) {
e->correct(p_state);
}
}
void Script::loadFromAss(QStringList content) {
SectionType section = SECTION_NONE;
QList<Subtitle *> comments;
foreach (QString line, content) {
if (line.isEmpty())
continue;
if (line.contains("[Script Info]")) {
section = SECTION_INFOS;
} else if (line.contains("[V4+ Styles]")) {
section = SECTION_STYLES;
} else if (line.contains("[Events]")) {
section = SECTION_EVENTS;
} else {
if (section == SECTION_INFOS) {
int colonPos = line.indexOf(':');
if (colonPos > 0) {
QString key = line.left(colonPos).trimmed().toLower();
QString value = line.mid(colonPos + 1).trimmed();
if (key == "title") {
m_title = value;
}
if (key == "playresx") {
m_resolution.setWidth(value.toInt());
}
if (key == "playresy") {
m_resolution.setHeight(value.toInt());
}
}
} else if (section == SECTION_STYLES) {
int colonPos = line.indexOf(':');
if (colonPos > 0) {
QString key = line.left(colonPos).trimmed().toLower();
QString value = line.mid(colonPos + 1).trimmed();
if (key == "style") {
QList<QString> subparts = value.split(',');
QString name = subparts[0];
QFont font = QFont(subparts[1]);
font.setPixelSize(subparts[2].toInt());
QString c = subparts[3].right(6);
QColor color;
color.setBlue(c.mid(0, 2).toInt(nullptr, 16));
color.setGreen(c.mid(2, 2).toInt(nullptr, 16));
color.setRed(c.mid(4, 2).toInt(nullptr, 16));
int marginL = subparts[19].toInt();
int marginR = subparts[20].toInt();
int marginV = subparts[21].toInt();
// Alignment after the layout of the numpad (1-3 sub, 4-6 mid, 7-9
// top)
Qt::Alignment position = Qt::AlignVCenter;
int alignment = subparts[18].right(1).toInt();
if (alignment <= 3)
position = Qt::AlignBottom;
if (alignment >= 7)
position = Qt::AlignTop;
if (alignment % 3 == 0)
position |= Qt::AlignRight;
if (alignment % 3 == 1)
position |= Qt::AlignLeft;
if (alignment % 3 == 2)
position |= Qt::AlignHCenter;
SubtitleStyle *style = new SubtitleStyle(name, font, color, this);
style->setAlignment(position);
style->setMargins(marginL, marginR, marginV);
m_styles[style->name()] = style;
}
}
} else if (section == SECTION_EVENTS) {
QList<QString> parts = line.split(':');
if (parts.size() <= 1)
continue;
QString key = parts[0].trimmed().toLower();
QString value = line.mid(key.length() + 1).trimmed();
QList<QString> subparts = value.split(',');
if (key != "comment" && key != "dialogue")
continue;
int start =
QTime(0, 0, 0).msecsTo(QTime::fromString(subparts[1], "h:mm:ss.z"));
int end =
QTime(0, 0, 0).msecsTo(QTime::fromString(subparts[2], "h:mm:ss.z"));
SubtitleStyle *style = this->style(subparts[3].trimmed());
int marginL = subparts[5].toInt();
int marginR = subparts[6].toInt();
int marginV = subparts[7].toInt();
int p = value.indexOf(",");
for (int i = 0; i < 8; i++) {
p = value.indexOf(",", p + 1);
}
QString text = value.mid(p + 1);
text = text.trimmed();
// New-lines : ignore at start
text = text.replace(QRegularExpression("^(\\\\[nN])+"), "");
// Transform the hints in the text into HTML:
// Italic HTML-ification
text = text.replace("{\\i1}", "<i>");
text = text.replace("{\\i0}", "</i>");
// Bold HTML-ification
text = text.replace("{\\b1}", "<b>");
text = text.replace("{\\b0}", "</b>");
text = sp2nbsp(text);
QList<SubtitleLine> lines;
foreach (QString line, text.split(QRegularExpression("\\\\[nN]"))) {
// Absolute positioning
int x = -1;
int y = -1;
//{\pos(x,y)} or {\pos(x)} in the beginning of the line
QRegularExpression rx("\\{\\\\pos\\((\\d+)(,(\\d+))?\\)\\}");
QRegularExpressionMatch match = rx.match(line);
if (match.hasMatch()) {
x = match.captured(1).toInt(); // Group 1: The first number (x)
if (!match.captured(3).isEmpty()) {
y = match.captured(3).toInt(); // Group 3: The second number (y)
}
}
// Color HTML-ification
{
QRegularExpression colorTagRegex(
R"(\{\\(?:1c|c)&H([0-9A-Fa-f]{6})&\})");
QRegularExpressionMatch match;
int offset = 0;
while ((match = colorTagRegex.match(line, offset)).hasMatch()) {
int start = match.capturedStart();
int end = match.capturedEnd();
QString assColor = match.captured(1); // BBGGRR
if (assColor.length() == 6) {
QString rr = assColor.mid(4, 2);
QString gg = assColor.mid(2, 2);
QString bb = assColor.mid(0, 2);
QString htmlColor = "<font color=\"#" + rr + gg + bb + "\">";
// Find where to insert </font>, right after the color tag scope
int closeBrace = line.indexOf('}', end - 1);
if (closeBrace != -1) {
line = line.left(start) + htmlColor +
line.mid(closeBrace + 1) + "</font>";
offset = start +
htmlColor.length(); // Move past the inserted color
} else {
// Incomplete tag, break to avoid infinite loop
break;
}
} else {
offset = end;
}
}
}
// Drop others hints that cannot be translated in HTML
lines.append(SubtitleLine(
line.replace(QRegularExpression("\\{.*\\}"), ""), QPoint(x, y)));
}
// Instantiate subtitle !
Subtitle *subtitle = new Subtitle(m_subtitles.size(), QStringList(),
start, end, this, this);
if (key == "dialogue") {
subtitle->setText(lines);
subtitle->setStyle(style);
subtitle->setMargins(marginL, marginR, marginV);
m_subtitles.append(subtitle);
} else if (key == "comment") {
subtitle->setComments(text);
comments.append(subtitle);
}
}
}
}
// Merge comments into subtitles
foreach (Subtitle *comment, comments) {
foreach (Subtitle *subtitle, m_subtitles) {
if (comment->msseStart() == subtitle->msseStart() &&
comment->msseEnd() == subtitle->msseEnd()) {
subtitle->setComments(comment->comments());
break;
}
}
}
}
void Script::loadFromSrt(QStringList content) {
QFont font(DEFAULT_FONT_NAME);
font.setPixelSize(DEFAULT_FONT_SIZE);
SubtitleStyle *style =
new SubtitleStyle(tr("Default"), font, Qt::white, this);
m_styles[style->name()] = style;
if (content.isEmpty())
return;
// Make sure its ends with empty line
if (!content.last().isEmpty())
content.append(QString());
QStringList text;
QString comments;
int start = 0;
int end = 0;
SectionType section = SECTION_NONE;
foreach (QString line, content) {
if (section == SECTION_NONE &&
QRegularExpression("^[0-9]+$").match(line).hasMatch()) {
section = SECTION_INFOS;
} else if (section == SECTION_INFOS) {
QStringList subparts = line.split(QRegularExpression("\\s+\\-\\->\\s+"));
start =
QTime(0, 0, 0).msecsTo(QTime::fromString(subparts[0], "h:mm:ss,z"));
end = QTime(0, 0, 0).msecsTo(QTime::fromString(subparts[1], "h:mm:ss,z"));
section = SECTION_EVENTS;
} else if (section == SECTION_EVENTS) {
if (line.isEmpty()) {
// Instantiate subtitle !
Subtitle *subtitle =
new Subtitle(m_subtitles.size(), text, start, end, this, this);
subtitle->setStyle(style);
subtitle->setComments(comments);
m_subtitles.append(subtitle);
text.clear();
comments.clear();
section = SECTION_NONE;
} else if (line.startsWith("//")) {
comments = line.replace("//", "");
} else {
line = sp2nbsp(line);
text.append(line);
}
}
}
}
void Script::loadFromTxt(QStringList content) {
QFont font(DEFAULT_FONT_NAME);
font.setPixelSize(DEFAULT_FONT_SIZE);
SubtitleStyle *style =
new SubtitleStyle(tr("Default"), font, Qt::white, this);
m_styles[style->name()] = style;
if (content.isEmpty())
return;
// Make sure its ends with empty line
if (!content.last().isEmpty())
content.append(QString());
QStringList text;
QString comments;
int start = 0;
int end = 0;
SectionType section = SECTION_NONE;
foreach (QString line, content) {
if (section == SECTION_NONE) {
section = SECTION_EVENTS;
QRegularExpression times("^([0-9:]+) ([0-9:]+)");
QRegularExpressionMatch match = times.match(line);
if (match.hasMatch()) {
start = QTime(0, 0, 0).msecsTo(
QTime::fromString(match.captured(1), "h:mm:ss:z"));
end = QTime(0, 0, 0).msecsTo(
QTime::fromString(match.captured(2), "h:mm:ss:z"));
continue;
}
}
if (section == SECTION_EVENTS) {
if (line.isEmpty()) {
// Instantiate subtitle !
Subtitle *subtitle =
new Subtitle(m_subtitles.size(), text, start, end, this, this);
subtitle->setStyle(style);
subtitle->setComments(comments);
m_subtitles.append(subtitle);
text.clear();
comments.clear();
section = SECTION_NONE;
} else if (line.startsWith("//")) {
comments = line.replace("//", "");
} else {
// In TXT format : <word> is equivalent to <i>word</i>
line = line.replace(QRegularExpression("<([^i/][^>]+)>"), "<i>\\1</i>");
// and *word* is equivalent to <b>word</b>
line =
line.replace(QRegularExpression("\\*([^\\*]+)\\*"), "<b>\\1</b>");
line = sp2nbsp(line);
text.append(line);
}
}
}
}
void Script::loadFromXml(QString content) {
QColor defaultColor(Qt::white);
QFont defaultFont(DEFAULT_FONT_NAME);
defaultFont.setPixelSize(DEFAULT_FONT_SIZE);
QDomDocument doc;
doc.setContent(content);
QString nameSpace;
// Interop.
QDomNodeList subtitles = doc.elementsByTagName("Subtitle");
if (subtitles.length() == 0) {
// SMTPE.
nameSpace = "dcst:";
subtitles = doc.elementsByTagName(nameSpace + "Subtitle");
}
QString movieName = tr("Default");
QDomNodeList movies = doc.elementsByTagName(
nameSpace.isEmpty() ? "MovieTitle" : nameSpace + "ContentTitleText");
if (movies.length() > 0) {
movieName = movies.at(0).toElement().text();
}
QString defaultSubtitleStyleName(tr("Default"));
// XXX: Take first font tag.
QDomNodeList fonts = doc.elementsByTagName(nameSpace + "Font");
if (fonts.length() > 0) {
// Font is set, show specific name for style.
defaultSubtitleStyleName = movieName;
QDomNode fontNode = fonts.at(0);
defaultFont.setFamily(fontNode.toElement().attribute("Id"));
defaultFont.setItalic(
(fontNode.toElement().attribute("Italic", "no") != "no"));
defaultFont.setBold(
(fontNode.toElement().attribute("Weight", "normal") == "bold"));
defaultFont.setPixelSize(
fontNode.toElement().attribute("Size", "DEFAULT_FONT_SIZE").toInt());
defaultFont.setUnderline(
(fontNode.toElement().attribute("Underlined", "no") != "no"));
defaultColor = QColor(QString("#%1").arg(
fontNode.toElement().attribute("Color", "FFFFFFFF")));
}
SubtitleStyle *defaultSubtitleStyle = new SubtitleStyle(
defaultSubtitleStyleName, defaultFont, defaultColor, this);
m_styles[defaultSubtitleStyle->name()] = defaultSubtitleStyle;
for (int i = 0; i < subtitles.length(); i++) {
QDomNode node = subtitles.at(i);
QString timeIn = node.toElement().attribute("TimeIn");
QString timeOut = node.toElement().attribute("TimeOut");
int start = QTime(0, 0, 0).msecsTo(QTime::fromString(timeIn, "h:mm:ss:z"));
int end = QTime(0, 0, 0).msecsTo(QTime::fromString(timeOut, "h:mm:ss:z"));
// Each <Text> becomes a subtitle with its own style (duplicated start/end).
QDomNodeList textLines = node.childNodes();
for (int j = 0; j < textLines.length(); j++) {
QDomNode line = textLines.at(j);
if (line.nodeName().toLower() != (nameSpace + "text")) {
continue;
}
SubtitleStyle *style = defaultSubtitleStyle;
QFont font(style->font());
QString color;
// Find top level font.
QDomNodeList fonts =
line.toElement().elementsByTagName(nameSpace + "Font");
if (fonts.length() > 0) {
QDomElement fontNode = fonts.at(0).toElement();
if (fontNode.hasAttribute("Color")) {
color = "#" + fontNode.attribute("Color");
}
if (fontNode.hasAttribute("Id")) {
font.setFamily(fontNode.attribute("Id"));
}
if (fontNode.hasAttribute("Weight")) {
font.setBold((fontNode.attribute("Weight") == "bold"));
}
if (fontNode.hasAttribute("Size")) {
font.setPixelSize(fontNode.attribute("Size").toInt());
}
if (fontNode.hasAttribute("Italic")) {
font.setItalic((fontNode.attribute("Italic") != "no"));
}
if (fontNode.hasAttribute("Underlined")) {
font.setUnderline((fontNode.attribute("Underlined") != "no"));
}
}
// Vertical/horizontal alignment.
QString hAlign = line.toElement().attribute("HAlign");
QString vAlign = line.toElement().attribute("VAlign");
// Offsets from baseline/center.
QString vPosition = line.toElement().attribute("VPosition");
QString hPosition = line.toElement().attribute("HPosition");
// Build new style based on alignment+color.
bool isOverriden =
((!color.isEmpty()) || (!hAlign.isEmpty() && hAlign != "center") ||
(!vAlign.isEmpty() && vAlign != "center") ||
(!vPosition.isEmpty()) || (!hPosition.isEmpty()));
if (isOverriden) {
QStringList tokens;
tokens << defaultSubtitleStyle->name() << hAlign << vAlign << vPosition
<< hPosition << color;
QString styleName = tokens.join("-");
if (!m_styles.contains(styleName)) {
// Not yet encountered. Built it!
SubtitleStyle *newSubtitleStyle = new SubtitleStyle(*style, font);
newSubtitleStyle->setName(styleName);
if (!color.isEmpty()) {
newSubtitleStyle->setPrimaryColour(QColor(color));
}
Qt::Alignment alignment = Qt::AlignVCenter;
if (hAlign.toLower() == "left")
alignment = Qt::AlignLeft;
if (hAlign.toLower() == "right")
alignment = Qt::AlignRight;
if (vAlign.toLower() == "bottom")
alignment |= Qt::AlignBottom;
if (vAlign.toLower() == "top")
alignment |= Qt::AlignTop;
// XXX: should inherit.
newSubtitleStyle->setAlignment(alignment);
// XXX: should inherit.
newSubtitleStyle->setOffsets(hPosition.toDouble() / 100.0,
vPosition.toDouble() / 100.0);
m_styles[styleName] = newSubtitleStyle;
}
style = m_styles[styleName];
}
Subtitle *subtitle =
new Subtitle(m_subtitles.size(), QStringList(line.toElement().text()),
start, end, this, this);
subtitle->setStyle(style);
m_subtitles.append(subtitle);
}
}
}
const QString Script::exportList(Script::ScriptFormat p_format) const {
QString output;
if (p_format == Script::CSV) {
QString dateFormat("hh:mm:ss");
// CSV Headers
QStringList headers;
headers << tr("Row") << tr("Start") << tr("End") << tr("Style")
<< tr("Text") << tr("Comments");
output.append(QString("\"%1\"\n").arg(headers.join("\" ; \"")));
// Group subtitles by display order.
int rowIndex = 1;
QList<Subtitle *> next = nextSubtitles(0);
while (next.size() > 0) {
QStringList row;
row << QString("%1").arg(rowIndex);
row << QTime(0, 0, 0)
.addMSecs(next.first()->msseStart())
.toString(dateFormat);
row << QTime(0, 0, 0)
.addMSecs(next.last()->msseEnd())
.toString(dateFormat);
QStringList styles;
QStringList texts;
QStringList comments;
foreach (Subtitle *subtitle, next) {
styles << subtitle->style()->name();
texts << QString(subtitle->prettyText()).replace("\"", "");
if (!subtitle->comments().isEmpty())
comments << QString(subtitle->comments()).replace("\n", "");
}
styles.removeDuplicates();
row << styles.join(" # ");
row << texts.join(" # ");
row << comments.join(" # ");
output.append(QString("\"%1\"\n").arg(row.join("\" ; \"")));
next = nextSubtitles(next.last()->msseStart() + 1);
rowIndex++;
}
}
return output;
}