File indexing completed on 2024-12-22 04:40:08

0001 /*
0002     SPDX-FileCopyrightText: 2007-2009 Sergio Pistone <sergio_pistone@yahoo.com.ar>
0003     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #ifndef SUBVIEWER1INPUTFORMAT_H
0009 #define SUBVIEWER1INPUTFORMAT_H
0010 
0011 #include "core/richtext/richdocument.h"
0012 #include "helpers/common.h"
0013 #include "formats/inputformat.h"
0014 
0015 #include <QRegularExpression>
0016 
0017 namespace SubtitleComposer {
0018 class SubViewer1InputFormat : public InputFormat
0019 {
0020     friend class FormatManager;
0021 
0022 protected:
0023     SubViewer1InputFormat()
0024         : InputFormat($("SubViewer 1.0"), QStringList($("sub")))
0025     {}
0026 
0027     bool parseSubtitles(Subtitle &subtitle, const QString &data) const override
0028     {
0029         staticRE$(reTime, "\\[([0-2][0-9]):([0-5][0-9]):([0-5][0-9])\\]\\n([^\n]*)\\n", REu);
0030         QRegularExpressionMatchIterator itTime = reTime.globalMatch(data);
0031         if(!itTime.hasNext())
0032             return false;
0033 
0034         for(;;) {
0035             QRegularExpressionMatch mTime = itTime.next();
0036 
0037             const Time showTime(mTime.captured(1).toInt(), mTime.captured(2).toInt(), mTime.captured(3).toInt(), 0);
0038             const QString text(mTime.captured(4).replace('|', '\n').trimmed());
0039 
0040             if(!itTime.hasNext())
0041                 break;
0042             mTime = itTime.peekNext();
0043 
0044             const Time hideTime(mTime.captured(1).toInt(), mTime.captured(2).toInt(), mTime.captured(3).toInt(), 0);
0045 
0046             SubtitleLine *l = new SubtitleLine(showTime, hideTime);
0047             l->primaryDoc()->setPlainText(text);
0048             subtitle.insertLine(l);
0049 
0050         }
0051 
0052         return subtitle.count() > 0;
0053     }
0054 };
0055 }
0056 
0057 #endif