Commit d2409500 authored by Jean-Baptiste Mardelle's avatar Jean-Baptiste Mardelle
Browse files

Subtitles: work on temp files until we save the project so that each change to...

Subtitles: work on temp files until we save the project so that each change to the subtitles is not instantly saved
parent 2cc7b347
......@@ -34,9 +34,11 @@
#include <mlt++/Mlt.h>
#include <KLocalizedString>
#include <KMessageBox>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
#include <QApplication>
SubtitleModel::SubtitleModel(Mlt::Tractor *tractor, std::shared_ptr<TimelineItemModel> timeline, QObject *parent)
: QAbstractListModel(parent)
......@@ -837,11 +839,30 @@ QString SubtitleModel::toJson()
return QString(jsonDoc.toJson());
}
void SubtitleModel::jsontoSubtitle(const QString &data, QString updatedFileName)
void SubtitleModel::copySubtitle(const QString &path, bool checkOverwrite)
{
QString outFile = updatedFileName.isEmpty() ? m_subtitleFilter->get("av.filename") : updatedFileName;
if (outFile.isEmpty()) {
outFile = pCore->currentDoc()->subTitlePath(); // use srt format as default unless file is imported (m_subFilePath)
QFile srcFile(pCore->currentDoc()->subTitlePath(false));
if (srcFile.exists()) {
QFile prev(path);
if (prev.exists()) {
if (checkOverwrite || !path.endsWith(QStringLiteral(".srt"))) {
if (KMessageBox::questionYesNo(QApplication::activeWindow(), i18n("File %1 already exists.\nDo you want to overwrite it?", path)) == KMessageBox::No) {
return;
}
}
prev.remove();
}
srcFile.copy(path);
}
}
void SubtitleModel::jsontoSubtitle(const QString &data)
{
QString outFile = pCore->currentDoc()->subTitlePath(false);
QString masterFile = m_subtitleFilter->get("av.filename");
if (masterFile.isEmpty()) {
m_subtitleFilter->set("av.filename", outFile.toUtf8().constData());
}
bool assFormat = outFile.endsWith(".ass");
if (!assFormat) {
......@@ -927,8 +948,9 @@ void SubtitleModel::jsontoSubtitle(const QString &data, QString updatedFileName)
//qDebug() << "ADDING SUBTITLE to FILE AT START POS: " << startPos <<" END POS: "<<endPos;//<< ", FPS: " << pCore->getCurrentFps();
}
outF.close();
}
qDebug()<<"Setting subtitle filter: "<<outFile;
qDebug()<<"Saving subtitle filter: "<<outFile;
if (line > 0) {
m_subtitleFilter->set("av.filename", outFile.toUtf8().constData());
m_tractor->attach(*m_subtitleFilter.get());
......
......@@ -128,13 +128,15 @@ public:
GenTime getStartPosForId(int id) const;
int getPreviousSub(int id) const;
int getNextSub(int id) const;
/** @brief Copy subtitle file to a new path */
void copySubtitle(const QString &path, bool checkOverwrite);
public slots:
/** @brief Function that parses through a subtitle file */
void parseSubtitle(const QString subPath = QString());
/** @brief Import model to a temporary subtitle file to which the Subtitle effect is applied*/
void jsontoSubtitle(const QString &data, QString updatedFileName = QString());
void jsontoSubtitle(const QString &data);
/** @brief Update a subtitle text*/
bool setText(int id, const QString text);
......
......@@ -762,19 +762,11 @@ void KdenliveDoc::setUrl(const QUrl &url)
void KdenliveDoc::updateSubtitle(QString newUrl)
{
if (auto ptr = m_subtitleModel.lock()) {
if (newUrl.isEmpty() && m_url.isValid()) {
newUrl = m_url.toLocalFile();
}
QString subPath;
if (newUrl.isEmpty()) {
subPath = subTitlePath();
} else {
// Update path of subtitle file
QString documentId = QDir::cleanPath(getDocumentProperty(QStringLiteral("documentid")));
QFileInfo info(newUrl);
subPath = info.dir().absoluteFilePath(QString("%1.srt").arg(info.fileName()));
}
ptr->jsontoSubtitle(ptr->toJson(), subPath);
bool checkOverwrite = QUrl::fromLocalFile(newUrl) != m_url;
QFileInfo info(newUrl);
subPath = info.dir().absoluteFilePath(QString("%1.srt").arg(info.fileName()));
ptr->copySubtitle(subPath, checkOverwrite);
}
}
......@@ -1793,11 +1785,11 @@ QString& KdenliveDoc::modifiedDecimalPoint() {
return m_modifiedDecimalPoint;
}
QString KdenliveDoc::subTitlePath()
const QString KdenliveDoc::subTitlePath(bool final)
{
QString path;
QString documentId = QDir::cleanPath(getDocumentProperty(QStringLiteral("documentid")));
if (m_url.isValid()) {
if (m_url.isValid() && final) {
return QFileInfo(m_url.toLocalFile()).dir().absoluteFilePath(QString("%1.srt").arg(m_url.fileName()));
} else {
path = QDir::temp().absoluteFilePath(QString("%1.srt").arg(documentId));
......
......@@ -174,8 +174,8 @@ public:
QString &modifiedDecimalPoint();
/** @brief Initialize subtitle model */
void initializeSubtitles(const std::shared_ptr<SubtitleModel> m_subtitle);
/** @brief Returns a path for current document's subtitle file */
QString subTitlePath();
/** @brief Returns a path for current document's subtitle file. If final is true, this will be the project filename with ".srt" appended. Otherwise a file in /tmp */
const QString subTitlePath(bool final);
private:
QUrl m_url;
......
......@@ -921,8 +921,7 @@ void EffectStackModel::importEffects(const std::weak_ptr<Mlt::Service> &service,
// Required to load master audio effects
if (m_ownerId.first == ObjectType::Master && filter->get("mlt_service") == QLatin1String("avfilter.subtitles")) {
// A subtitle filter, update project
QString subFile(filter->get("av.filename"));
pCore->window()->slotEditSubtitle(subFile);
pCore->window()->slotEditSubtitle(true);
} else if (auto ms = m_masterService.lock()) {
ms->attach(*filter.get());
}
......
......@@ -4194,9 +4194,16 @@ void MainWindow::resetSubtitles()
m_buttonSubtitleEditTool->setChecked(false);
getMainTimeline()->showSubtitles = false;
pCore->subtitleWidget()->setModel(nullptr);
if (pCore->currentDoc()) {
const QString workPath = pCore->currentDoc()->subTitlePath(false);
QFile workFile(workPath);
if (workFile.exists()) {
workFile.remove();
}
}
}
void MainWindow::slotEditSubtitle(QString subPath)
void MainWindow::slotEditSubtitle(bool loadExisting)
{
std::shared_ptr<SubtitleModel> subtitleModel = pCore->getSubtitleModel();
if (subtitleModel == nullptr) {
......@@ -4205,10 +4212,13 @@ void MainWindow::slotEditSubtitle(QString subPath)
getMainTimeline()->controller()->getModel()->setSubModel(subtitleModel);
pCore->currentDoc()->initializeSubtitles(subtitleModel);
pCore->subtitleWidget()->setModel(subtitleModel);
if (!subPath.isEmpty() && QFileInfo(subPath).isRelative()) {
subPath.prepend(pCore->currentDoc()->documentRoot());
const QString subPath = pCore->currentDoc()->subTitlePath(true);
const QString workPath = pCore->currentDoc()->subTitlePath(false);
QFile subFile(subPath);
if (subFile.exists()) {
subFile.copy(workPath);
subtitleModel->parseSubtitle(workPath);
}
subtitleModel->parseSubtitle(subPath);
getMainTimeline()->showSubtitles = true;
m_buttonSubtitleEditTool->setChecked(true);
getMainTimeline()->connectSubtitleModel(true);
......
......@@ -298,7 +298,7 @@ public slots:
void slotSwitchTimelineZone(bool toggled);
/** @brief Open the online services search dialog. */
void slotDownloadResources();
void slotEditSubtitle(QString subPath = QString());
void slotEditSubtitle(bool loadExisting = false);
private slots:
/** @brief Shows the shortcut dialog. */
......
......@@ -295,9 +295,7 @@ bool ProjectManager::saveFileAs(const QString &outputFileName, bool saveACopy)
// Sync document properties
prepareSave();
QString saveFolder = QFileInfo(outputFileName).absolutePath();
if (!saveACopy) {
m_project->updateSubtitle(outputFileName);
}
m_project->updateSubtitle(outputFileName);
QString scene = projectSceneList(saveFolder);
if (!m_replacementPattern.isEmpty()) {
QMapIterator<QString, QString> i(m_replacementPattern);
......@@ -307,7 +305,6 @@ bool ProjectManager::saveFileAs(const QString &outputFileName, bool saveACopy)
}
}
if (!m_project->saveSceneList(outputFileName, scene)) {
m_project->updateSubtitle();
return false;
}
QUrl url = QUrl::fromLocalFile(outputFileName);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment