| 1 | /***************************************************************************** |
| 2 | ** QNapi |
| 3 | ** Copyright (C) 2008-2017 Piotr KrzemiĆski <[email protected]> |
| 4 | ** |
| 5 | ** This program is free software; you can redistribute it and/or modify |
| 6 | ** it under the terms of the GNU General Public License as published by |
| 7 | ** the Free Software Foundation; either version 2 of the License, or |
| 8 | ** (at your option) any later version. |
| 9 | ** |
| 10 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE |
| 11 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | ** |
| 13 | *****************************************************************************/ |
| 14 | |
| 15 | #include "frmconvert.h" |
| 16 | #include "libqnapi.h" |
| 17 | #include "movieinfo/movieinfoprovider.h" |
| 18 | #include "qnapiopendialog.h" |
| 19 | |
| 20 | #include <QDesktopServices> |
| 21 | #include <QDesktopWidget> |
| 22 | #include <QFileDialog> |
| 23 | #include <QFileInfo> |
| 24 | #include <QMessageBox> |
| 25 | #include <QSharedPointer> |
| 26 | |
| 27 | frmConvert::frmConvert(QWidget *parent, Qt::WindowFlags f) |
| 28 | : QDialog(parent, f), |
| 29 | staticConfig(LibQNapi::staticConfig()), |
| 30 | ppConfig(LibQNapi::loadConfig().postProcessingConfig()), |
| 31 | subtitleFormatsRegistry(LibQNapi::subtitleFormatsRegistry()), |
| 32 | subConverter(subtitleFormatsRegistry, LibQNapi::movieInfoProvider(), |
| 33 | ppConfig.skipConvertAds()), |
| 34 | targetFileNameSelected(false) { |
| 35 | ui.setupUi(this); |
| 36 | |
| 37 | setAttribute(Qt::WA_QuitOnClose, false); |
| 38 | |
| 39 | QRect position = frameGeometry(); |
| 40 | position.moveCenter(QDesktopWidget().availableGeometry().center()); |
| 41 | move(position.topLeft()); |
| 42 | |
| 43 | ui.lbDetectedFormatValue->setText(""); |
| 44 | |
| 45 | ui.cbTargetFormat->clear(); |
| 46 | foreach (QString format, subtitleFormatsRegistry->listFormatNames()) { |
| 47 | ui.cbTargetFormat->addItem(format); |
| 48 | } |
| 49 | |
| 50 | connect(ui.pbSrcSubFileSelect, SIGNAL(clicked()), this, |
| 51 | SLOT(srcSubSelectClicked())); |
| 52 | connect(ui.leSrcSubFile, SIGNAL(textChanged(const QString &)), this, |
| 53 | SLOT(srcSubFileLoaded(const QString &))); |
| 54 | connect(ui.cbTargetFormat, SIGNAL(currentIndexChanged(int)), this, |
| 55 | SLOT(targetFormatChanged(int))); |
| 56 | connect(ui.cbTargetExtension, SIGNAL(currentIndexChanged(int)), this, |
| 57 | SLOT(targetExtensionChanged())); |
| 58 | connect(ui.pbMovieFPSSelect, SIGNAL(clicked()), this, |
| 59 | SLOT(movieFPSSelectClicked())); |
| 60 | connect(ui.pbTargetMovieFPSSelect, SIGNAL(clicked()), this, |
| 61 | SLOT(targetMovieFPSSelectClicked())); |
| 62 | connect(ui.cbDelaySubtitles, SIGNAL(toggled(bool)), this, |
| 63 | SLOT(subDelayToggled())); |
| 64 | connect(ui.pbConvert, SIGNAL(clicked()), this, SLOT(convertClicked())); |
| 65 | |
| 66 | if (ppConfig.subFormat().isEmpty()) { |
| 67 | targetFormat = subtitleFormatsRegistry->listFormatNames().first(); |
| 68 | } else { |
| 69 | targetFormat = ppConfig.subFormat(); |
| 70 | ui.cbTargetFormat->setCurrentText(targetFormat); |
| 71 | } |
| 72 | |
| 73 | if (!ppConfig.subExtension().isEmpty()) { |
| 74 | ui.cbTargetExtension->setCurrentText(ppConfig.subExtension()); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | void frmConvert::srcSubSelectClicked() { |
| 79 | QNapiOpenDialog openSubtitle(this, tr("Choose a subtitles file"), |
| 80 | QFileInfo(ui.leSrcSubFile->text()).path(), |
| 81 | QNapiOpenDialog::Subtitles); |
| 82 | if (openSubtitle.selectFile()) { |
| 83 | ui.leSrcSubFile->setText(openSubtitle.selectedFiles().first()); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | void frmConvert::srcSubFileLoaded(const QString &srcSubFileName) { |
| 88 | bool fileExists = QFileInfo(srcSubFileName).exists(); |
| 89 | |
| 90 | ui.lbDetectedFormat->setEnabled(fileExists); |
| 91 | ui.lbDetectedFormatValue->setVisible(fileExists); |
| 92 | |
| 93 | if (fileExists) { |
| 94 | QString detectedFormat = subConverter.detectFormat(srcSubFileName); |
| 95 | if (detectedFormat.isEmpty()) { |
| 96 | ui.lbDetectedFormatValue->setText(tr("incorrect")); |
| 97 | ui.lbDetectedFormatValue->setStyleSheet("QLabel { color: red }"); |
| 98 | } else { |
| 99 | ui.lbDetectedFormatValue->setText(detectedFormat); |
| 100 | ui.lbDetectedFormatValue->setStyleSheet("QLabel { color: black }"); |
| 101 | } |
| 102 | srcFormat = detectedFormat; |
| 103 | } else { |
| 104 | srcFormat = ""; |
| 105 | } |
| 106 | |
| 107 | ui.lbTargetFormat->setEnabled(!srcFormat.isEmpty()); |
| 108 | ui.cbTargetFormat->setEnabled(!srcFormat.isEmpty()); |
| 109 | ui.lbTargetExtension->setEnabled(!srcFormat.isEmpty()); |
| 110 | ui.cbTargetExtension->setEnabled(!srcFormat.isEmpty()); |
| 111 | ui.cbChangeFPS->setEnabled(!srcFormat.isEmpty()); |
| 112 | ui.cbDelaySubtitles->setEnabled(!srcFormat.isEmpty()); |
| 113 | ui.pbMovieFPSSelect->setEnabled(!srcFormat.isEmpty()); |
| 114 | ui.lbTargetFileName->setEnabled(!srcFormat.isEmpty()); |
| 115 | ui.leTargetFileName->setEnabled(!srcFormat.isEmpty()); |
| 116 | ui.pbConvert->setEnabled(!srcFormat.isEmpty()); |
| 117 | |
| 118 | checkFPSNeeded(); |
| 119 | generateTargetFileName(); |
| 120 | } |
| 121 | |
| 122 | void frmConvert::targetFormatChanged(int targetFormatIdx) { |
| 123 | targetFormat = subtitleFormatsRegistry->listFormatNames().at(targetFormatIdx); |
| 124 | checkFPSNeeded(); |
| 125 | } |
| 126 | |
| 127 | void frmConvert::checkFPSNeeded() { |
| 128 | bool fpsNeeded; |
| 129 | if (srcFormat.isEmpty() || targetFormat.isEmpty()) { |
| 130 | fpsNeeded = false; |
| 131 | } else { |
| 132 | QSharedPointer<const SubtitleFormat> srcSF = |
| 133 | subtitleFormatsRegistry->select(srcFormat); |
| 134 | QSharedPointer<const SubtitleFormat> targetSF = |
| 135 | subtitleFormatsRegistry->select(targetFormat); |
| 136 | fpsNeeded = (srcSF->isTimeBased() != targetSF->isTimeBased()) || |
| 137 | (ui.cbDelaySubtitles->isChecked() && !targetSF->isTimeBased()); |
| 138 | |
| 139 | QString targetDefaultExt = targetSF->defaultExtension(); |
| 140 | ui.cbTargetExtension->setItemText(0, |
| 141 | tr("Default (%1)").arg(targetDefaultExt)); |
| 142 | generateTargetFileName(); |
| 143 | } |
| 144 | |
| 145 | ui.cbMovieFPS->setEnabled(fpsNeeded); |
| 146 | ui.lbMovieFPS->setEnabled(fpsNeeded); |
| 147 | ui.pbMovieFPSSelect->setEnabled(fpsNeeded); |
| 148 | |
| 149 | if (fpsNeeded) { |
| 150 | QFileInfo srcSubFI(ui.leSrcSubFile->text()); |
| 151 | |
| 152 | QString movieFilePathBase = |
| 153 | srcSubFI.absolutePath() + "/" + srcSubFI.completeBaseName(); |
| 154 | |
| 155 | foreach (QString movieExt, staticConfig->movieExtensions()) { |
| 156 | QString movieFilePath = movieFilePathBase + "." + movieExt; |
| 157 | if (QFileInfo(movieFilePath).exists()) { |
| 158 | Maybe<QString> maybeFPS = determineMovieFPS(movieFilePath); |
| 159 | if (maybeFPS) { |
| 160 | ui.cbMovieFPS->setCurrentText(maybeFPS.value()); |
| 161 | ui.cbFPSTo->setCurrentText(maybeFPS.value()); |
| 162 | } |
| 163 | break; |
| 164 | } |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | void frmConvert::movieFPSSelectClicked() { |
| 170 | QNapiOpenDialog openMovie(this, tr("Select a video file"), |
| 171 | QFileInfo(ui.leSrcSubFile->text()).path(), |
| 172 | QNapiOpenDialog::Movies); |
| 173 | if (openMovie.selectFile()) { |
| 174 | QString moviePath = openMovie.selectedFiles().first(); |
| 175 | Maybe<QString> maybeFPS = determineMovieFPS(moviePath); |
| 176 | if (maybeFPS) { |
| 177 | ui.cbMovieFPS->setCurrentText(maybeFPS.value()); |
| 178 | } |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | Maybe<QString> frmConvert::determineMovieFPS(const QString &movieFilePath) { |
| 183 | QSharedPointer<const MovieInfoProvider> mip = LibQNapi::movieInfoProvider(); |
| 184 | Maybe<MovieInfo> mmi = mip->getMovieInfo(movieFilePath); |
| 185 | if (mmi) |
| 186 | return just(QString::number(mmi.value().frameRate(), 'f', 3)); |
| 187 | else |
| 188 | return nothing(); |
| 189 | } |
| 190 | |
| 191 | void frmConvert::targetMovieFPSSelectClicked() { |
| 192 | QNapiOpenDialog openMovie(this, tr("Select a video file"), |
| 193 | QFileInfo(ui.leSrcSubFile->text()).path(), |
| 194 | QNapiOpenDialog::Movies); |
| 195 | if (openMovie.selectFile()) { |
| 196 | QString moviePath = openMovie.selectedFiles().first(); |
| 197 | Maybe<QString> maybeFPS = determineMovieFPS(moviePath); |
| 198 | if (maybeFPS) { |
| 199 | ui.cbFPSTo->setCurrentText(maybeFPS.value()); |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void frmConvert::targetExtensionChanged() { generateTargetFileName(); } |
| 205 | |
| 206 | void frmConvert::generateTargetFileName() { |
| 207 | if (!targetFileNameSelected) { |
| 208 | QFileInfo srcSubFI(ui.leSrcSubFile->text()); |
| 209 | QString extension; |
| 210 | |
| 211 | if (ui.cbTargetExtension->currentIndex() == 0) { |
| 212 | QSharedPointer<const SubtitleFormat> targetSF = |
| 213 | subtitleFormatsRegistry->select(targetFormat); |
| 214 | extension = targetSF->defaultExtension(); |
| 215 | } else { |
| 216 | extension = ui.cbTargetExtension->currentText(); |
| 217 | } |
| 218 | |
| 219 | QString defaultTargetPath = srcSubFI.absolutePath() + "/" + |
| 220 | srcSubFI.completeBaseName() + "." + extension; |
| 221 | ui.leTargetFileName->setText(defaultTargetPath); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | void frmConvert::subDelayToggled() { checkFPSNeeded(); } |
| 226 | |
| 227 | void frmConvert::convertClicked() { |
| 228 | double fpsRatio = 1.0, delayOffset = 0.0; |
| 229 | |
| 230 | if (ui.cbChangeFPS->isChecked()) { |
| 231 | double fpsFrom = ui.cbFPSFrom->currentText().replace(',', '.').toDouble(); |
| 232 | double fpsTo = ui.cbFPSTo->currentText().replace(',', '.').toDouble(); |
| 233 | fpsRatio = fpsTo / fpsFrom; |
| 234 | } |
| 235 | |
| 236 | if (ui.cbDelaySubtitles->isChecked()) { |
| 237 | delayOffset = ui.sbDelayOffset->value(); |
| 238 | } |
| 239 | |
| 240 | if (subConverter.convertSubtitles( |
| 241 | ui.leSrcSubFile->text(), targetFormat, ui.leTargetFileName->text(), |
| 242 | ui.cbMovieFPS->currentText().replace(',', '.').toDouble(), fpsRatio, |
| 243 | delayOffset)) { |
| 244 | QMessageBox::information(this, tr("Subtitles converted"), |
| 245 | tr("Changed subtitles format from '%1' to '%2'") |
| 246 | .arg(srcFormat, targetFormat)); |
| 247 | } else { |
| 248 | QMessageBox::warning(this, tr("Could not change subtitle format!"), |
| 249 | tr("An error occured while converting subtitles!")); |
| 250 | } |
| 251 | } |