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

0001 /*
0002     SPDX-FileCopyrightText: 2010-2022 Mladen Milinkovic <max@smoothware.net>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "textdemux.h"
0008 
0009 #include "core/richtext/richdocument.h"
0010 #include "core/subtitle.h"
0011 #include "streamprocessor/streamprocessor.h"
0012 
0013 #include <KLocalizedString>
0014 
0015 #include <QLabel>
0016 #include <QBoxLayout>
0017 #include <QProgressBar>
0018 
0019 using namespace SubtitleComposer;
0020 
0021 TextDemux::TextDemux(QWidget *parent)
0022     : QObject(parent),
0023       m_subtitle(nullptr),
0024       m_subtitleTemp(nullptr),
0025       m_streamProcessor(new StreamProcessor(this)),
0026       m_progressWidget(new QWidget(parent))
0027 {
0028     // progress Bar
0029     m_progressWidget->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
0030     m_progressWidget->hide();
0031 
0032     QLabel *label = new QLabel(i18n("Importing Subtitle Stream"), m_progressWidget);
0033 
0034     m_progressBar = new QProgressBar(m_progressWidget);
0035     m_progressBar->setFormat(i18nc("%p is the percent value, % is the percent sign", "%p%"));
0036     m_progressBar->setMinimumWidth(300);
0037     m_progressBar->setTextVisible(true);
0038 
0039     QBoxLayout *layout = new QBoxLayout(QBoxLayout::LeftToRight, m_progressWidget);
0040     layout->setContentsMargins(1, 0, 1, 0);
0041     layout->setSpacing(1);
0042     layout->addWidget(label);
0043     layout->addWidget(m_progressBar);
0044 
0045     // stream processor
0046     connect(m_streamProcessor, &StreamProcessor::streamProgress, this, &TextDemux::onStreamProgress);
0047     connect(m_streamProcessor, &StreamProcessor::streamError, this, &TextDemux::onStreamError);
0048     connect(m_streamProcessor, &StreamProcessor::streamFinished, this, &TextDemux::onStreamFinished);
0049     connect(m_streamProcessor, &StreamProcessor::textDataAvailable, this, &TextDemux::onStreamData);
0050 }
0051 
0052 void
0053 TextDemux::demuxFile(Subtitle *subtitle, const QString filename, int textStreamIndex)
0054 {
0055     if(!subtitle)
0056         return;
0057 
0058     m_streamProcessor->close();
0059 
0060     m_subtitle = subtitle;
0061     m_subtitleTemp = new Subtitle();
0062 
0063     if(m_streamProcessor->open(filename) && m_streamProcessor->initText(textStreamIndex))
0064         m_streamProcessor->start();
0065 }
0066 
0067 void
0068 TextDemux::onStreamData(const QString &text, quint64 msecStart, quint64 msecDuration)
0069 {
0070     RichString stxt;
0071     stxt.setRichString(text);
0072 
0073     SubtitleLine *line = new SubtitleLine(Time(double(msecStart)), Time(double(msecStart) + double(msecDuration)));
0074     line->primaryDoc()->setPlainText(stxt);
0075     m_subtitleTemp->insertLine(line);
0076 }
0077 
0078 void
0079 TextDemux::onStreamProgress(quint64 msecPos, quint64 msecLength)
0080 {
0081     m_progressBar->setRange(0, msecLength / 1000);
0082     m_progressBar->setValue(msecPos / 1000);
0083     m_progressWidget->show();
0084 }
0085 
0086 void
0087 TextDemux::onStreamError(int code, const QString &message, const QString &debug)
0088 {
0089     m_subtitleTemp.reset();
0090 
0091     emit onError(i18n("Subtitle demux failed %1: %2\n%3", code, message, debug));
0092     m_streamProcessor->close();
0093     m_progressWidget->hide();
0094 }
0095 
0096 void
0097 TextDemux::onStreamFinished()
0098 {
0099     m_subtitle->setPrimaryData(*m_subtitleTemp, true);
0100     m_subtitleTemp.reset();
0101 
0102     m_progressWidget->hide();
0103 }
0104 
0105 QWidget *
0106 TextDemux::progressWidget()
0107 {
0108     return m_progressWidget;
0109 }