File indexing completed on 2024-04-28 04:51:35
0001 /* 0002 SPDX-FileCopyrightText: 2023 Jean-Baptiste Mardelle <jb@kdenlive.org> 0003 0004 SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL 0005 */ 0006 0007 #include "importsubtitle.h" 0008 #include "bin/model/subtitlemodel.hpp" 0009 #include "core.h" 0010 0011 #include "kdenlive_debug.h" 0012 #include <QFontDatabase> 0013 0014 #include "klocalizedstring.h" 0015 #include <KCharsets> 0016 #include <kio_version.h> 0017 0018 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0019 #include <QTextCodec> 0020 #endif 0021 0022 ImportSubtitle::ImportSubtitle(const QString &path, QWidget *parent) 0023 : QDialog(parent) 0024 { 0025 setFont(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont)); 0026 setupUi(this); 0027 QStringList listCodecs = KCharsets::charsets()->descriptiveEncodingNames(); 0028 const QString filter = QStringLiteral("*.srt *.ass *.vtt *.sbv"); 0029 #if KIO_VERSION >= QT_VERSION_CHECK(5, 108, 0) 0030 subtitle_url->setNameFilter(filter); 0031 #else 0032 subtitle_url->setFilter(filter); 0033 #endif 0034 codecs_list->addItems(listCodecs); 0035 info_message->setVisible(false); 0036 // Set UTF-8 as default codec 0037 QString utf8Desc = KCharsets::charsets()->descriptionForEncoding(QStringLiteral("UTF-8")); 0038 int matchIndex = codecs_list->findText(utf8Desc); 0039 if (matchIndex > -1) { 0040 codecs_list->setCurrentIndex(matchIndex); 0041 } 0042 codecs_list->setToolTip(i18n("Character encoding used to save the subtitle file.")); 0043 codecs_list->setWhatsThis(xi18nc("@info:whatsthis", "If unsure,try :<br/><b>Unicode (UTF-8)</b>.")); 0044 caption_original_framerate->setValue(pCore->getCurrentFps()); 0045 caption_target_framerate->setValue(pCore->getCurrentFps()); 0046 0047 Fun updateSub = [this]() { 0048 QFile srtFile(subtitle_url->url().toLocalFile()); 0049 if (!srtFile.exists() || !srtFile.open(QIODevice::ReadOnly)) { 0050 info_message->setMessageType(KMessageWidget::Warning); 0051 info_message->setText(i18n("Cannot read file %1", srtFile.fileName())); 0052 info_message->animatedShow(); 0053 return true; 0054 } 0055 QByteArray codec = KCharsets::charsets()->encodingForName(codecs_list->currentText()).toUtf8(); 0056 QTextStream stream(&srtFile); 0057 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) 0058 QTextCodec *inputEncoding = QTextCodec::codecForName(codec); 0059 if (inputEncoding) { 0060 stream.setCodec(inputEncoding); 0061 } else { 0062 qWarning() << "No QTextCodec named" << codec; 0063 stream.setCodec("UTF-8"); 0064 } 0065 #else 0066 std::optional<QStringConverter::Encoding> inputEncoding = QStringConverter::encodingForName(codec.data()); 0067 if (inputEncoding) { 0068 stream.setEncoding(inputEncoding.value()); 0069 } 0070 // else: UTF8 is the default 0071 #endif 0072 text_preview->clear(); 0073 text_preview->setPlainText(stream.readAll()); 0074 return true; 0075 }; 0076 0077 Fun checkEncoding = [this, updateSub]() { 0078 bool ok; 0079 QString guessedEncoding(SubtitleModel::guessFileEncoding(subtitle_url->url().toLocalFile(), &ok)); 0080 qDebug() << "Guessed subtitle encoding is" << guessedEncoding; 0081 if (ok == false) { 0082 info_message->setMessageType(KMessageWidget::Warning); 0083 info_message->setText(i18n("Encoding could not be guessed, using UTF-8")); 0084 guessedEncoding = QStringLiteral("UTF-8"); 0085 } else { 0086 info_message->setMessageType(KMessageWidget::Information); 0087 info_message->setText(i18n("Encoding detected as %1", guessedEncoding)); 0088 } 0089 info_message->animatedShow(); 0090 int matchIndex = codecs_list->findText(KCharsets::charsets()->descriptionForEncoding(guessedEncoding)); 0091 if (matchIndex > -1) { 0092 codecs_list->setCurrentIndex(matchIndex); 0093 updateSub(); 0094 } 0095 return true; 0096 }; 0097 0098 if (!path.isEmpty()) { 0099 subtitle_url->setText(path); 0100 checkEncoding(); 0101 } 0102 connect(codecs_list, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [updateSub]() { updateSub(); }); 0103 connect(subtitle_url, &KUrlRequester::urlSelected, [checkEncoding]() { checkEncoding(); }); 0104 setWindowTitle(i18n("Import Subtitle")); 0105 } 0106 0107 ImportSubtitle::~ImportSubtitle() {}