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 @@ ...@@ -34,9 +34,11 @@
#include <mlt++/Mlt.h> #include <mlt++/Mlt.h>
#include <KLocalizedString> #include <KLocalizedString>
#include <KMessageBox>
#include <QJsonArray> #include <QJsonArray>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QApplication>
SubtitleModel::SubtitleModel(Mlt::Tractor *tractor, std::shared_ptr<TimelineItemModel> timeline, QObject *parent) SubtitleModel::SubtitleModel(Mlt::Tractor *tractor, std::shared_ptr<TimelineItemModel> timeline, QObject *parent)
: QAbstractListModel(parent) : QAbstractListModel(parent)
...@@ -837,11 +839,30 @@ QString SubtitleModel::toJson() ...@@ -837,11 +839,30 @@ QString SubtitleModel::toJson()
return QString(jsonDoc.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; QFile srcFile(pCore->currentDoc()->subTitlePath(false));
if (outFile.isEmpty()) { if (srcFile.exists()) {
outFile = pCore->currentDoc()->subTitlePath(); // use srt format as default unless file is imported (m_subFilePath) 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"); bool assFormat = outFile.endsWith(".ass");
if (!assFormat) { if (!assFormat) {
...@@ -927,8 +948,9 @@ void SubtitleModel::jsontoSubtitle(const QString &data, QString updatedFileName) ...@@ -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(); //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) { if (line > 0) {
m_subtitleFilter->set("av.filename", outFile.toUtf8().constData()); m_subtitleFilter->set("av.filename", outFile.toUtf8().constData());
m_tractor->attach(*m_subtitleFilter.get()); m_tractor->attach(*m_subtitleFilter.get());
......
...@@ -128,13 +128,15 @@ public: ...@@ -128,13 +128,15 @@ public:
GenTime getStartPosForId(int id) const; GenTime getStartPosForId(int id) const;
int getPreviousSub(int id) const; int getPreviousSub(int id) const;
int getNextSub(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: public slots:
/** @brief Function that parses through a subtitle file */ /** @brief Function that parses through a subtitle file */
void parseSubtitle(const QString subPath = QString()); void parseSubtitle(const QString subPath = QString());
/** @brief Import model to a temporary subtitle file to which the Subtitle effect is applied*/ /** @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*/ /** @brief Update a subtitle text*/
bool setText(int id, const QString text); bool setText(int id, const QString text);
......
...@@ -762,19 +762,11 @@ void KdenliveDoc::setUrl(const QUrl &url) ...@@ -762,19 +762,11 @@ void KdenliveDoc::setUrl(const QUrl &url)
void KdenliveDoc::updateSubtitle(QString newUrl) void KdenliveDoc::updateSubtitle(QString newUrl)
{ {
if (auto ptr = m_subtitleModel.lock()) { if (auto ptr = m_subtitleModel.lock()) {
if (newUrl.isEmpty() && m_url.isValid()) {
newUrl = m_url.toLocalFile();
}
QString subPath; QString subPath;
if (newUrl.isEmpty()) { bool checkOverwrite = QUrl::fromLocalFile(newUrl) != m_url;
subPath = subTitlePath(); QFileInfo info(newUrl);
} else { subPath = info.dir().absoluteFilePath(QString("%1.srt").arg(info.fileName()));
// Update path of subtitle file ptr->copySubtitle(subPath, checkOverwrite);
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);
} }
} }
...@@ -1793,11 +1785,11 @@ QString& KdenliveDoc::modifiedDecimalPoint() { ...@@ -1793,11 +1785,11 @@ QString& KdenliveDoc::modifiedDecimalPoint() {
return m_modifiedDecimalPoint; return m_modifiedDecimalPoint;
} }
QString KdenliveDoc::subTitlePath() const QString KdenliveDoc::subTitlePath(bool final)
{ {
QString path; QString path;
QString documentId = QDir::cleanPath(getDocumentProperty(QStringLiteral("documentid"))); 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())); return QFileInfo(m_url.toLocalFile()).dir().absoluteFilePath(QString("%1.srt").arg(m_url.fileName()));
} else { } else {
path = QDir::temp().absoluteFilePath(QString("%1.srt").arg(documentId)); path = QDir::temp().absoluteFilePath(QString("%1.srt").arg(documentId));
......
...@@ -174,8 +174,8 @@ public: ...@@ -174,8 +174,8 @@ public:
QString &modifiedDecimalPoint(); QString &modifiedDecimalPoint();
/** @brief Initialize subtitle model */ /** @brief Initialize subtitle model */
void initializeSubtitles(const std::shared_ptr<SubtitleModel> m_subtitle); void initializeSubtitles(const std::shared_ptr<SubtitleModel> m_subtitle);
/** @brief Returns a path for current document's subtitle file */ /** @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 */
QString subTitlePath(); const QString subTitlePath(bool final);
private: private:
QUrl m_url; QUrl m_url;
......
...@@ -921,8 +921,7 @@ void EffectStackModel::importEffects(const std::weak_ptr<Mlt::Service> &service, ...@@ -921,8 +921,7 @@ void EffectStackModel::importEffects(const std::weak_ptr<Mlt::Service> &service,
// Required to load master audio effects // Required to load master audio effects
if (m_ownerId.first == ObjectType::Master && filter->get("mlt_service") == QLatin1String("avfilter.subtitles")) { if (m_ownerId.first == ObjectType::Master && filter->get("mlt_service") == QLatin1String("avfilter.subtitles")) {
// A subtitle filter, update project // A subtitle filter, update project
QString subFile(filter->get("av.filename")); pCore->window()->slotEditSubtitle(true);
pCore->window()->slotEditSubtitle(subFile);
} else if (auto ms = m_masterService.lock()) { } else if (auto ms = m_masterService.lock()) {
ms->attach(*filter.get()); ms->attach(*filter.get());
} }
......
...@@ -4194,9 +4194,16 @@ void MainWindow::resetSubtitles() ...@@ -4194,9 +4194,16 @@ void MainWindow::resetSubtitles()
m_buttonSubtitleEditTool->setChecked(false); m_buttonSubtitleEditTool->setChecked(false);
getMainTimeline()->showSubtitles = false; getMainTimeline()->showSubtitles = false;
pCore->subtitleWidget()->setModel(nullptr); 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(); std::shared_ptr<SubtitleModel> subtitleModel = pCore->getSubtitleModel();
if (subtitleModel == nullptr) { if (subtitleModel == nullptr) {
...@@ -4205,10 +4212,13 @@ void MainWindow::slotEditSubtitle(QString subPath) ...@@ -4205,10 +4212,13 @@ void MainWindow::slotEditSubtitle(QString subPath)
getMainTimeline()->controller()->getModel()->setSubModel(subtitleModel); getMainTimeline()->controller()->getModel()->setSubModel(subtitleModel);
pCore->currentDoc()->initializeSubtitles(subtitleModel); pCore->currentDoc()->initializeSubtitles(subtitleModel);
pCore->subtitleWidget()->setModel(subtitleModel); pCore->subtitleWidget()->setModel(subtitleModel);
if (!subPath.isEmpty() && QFileInfo(subPath).isRelative()) { const QString subPath = pCore->currentDoc()->subTitlePath(true);
subPath.prepend(pCore->currentDoc()->documentRoot()); 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; getMainTimeline()->showSubtitles = true;
m_buttonSubtitleEditTool->setChecked(true); m_buttonSubtitleEditTool->setChecked(true);
getMainTimeline()->connectSubtitleModel(true); getMainTimeline()->connectSubtitleModel(true);
......
...@@ -298,7 +298,7 @@ public slots: ...@@ -298,7 +298,7 @@ public slots:
void slotSwitchTimelineZone(bool toggled); void slotSwitchTimelineZone(bool toggled);
/** @brief Open the online services search dialog. */ /** @brief Open the online services search dialog. */
void slotDownloadResources(); void slotDownloadResources();
void slotEditSubtitle(QString subPath = QString()); void slotEditSubtitle(bool loadExisting = false);
private slots: private slots:
/** @brief Shows the shortcut dialog. */ /** @brief Shows the shortcut dialog. */
......
...@@ -295,9 +295,7 @@ bool ProjectManager::saveFileAs(const QString &outputFileName, bool saveACopy) ...@@ -295,9 +295,7 @@ bool ProjectManager::saveFileAs(const QString &outputFileName, bool saveACopy)
// Sync document properties // Sync document properties
prepareSave(); prepareSave();
QString saveFolder = QFileInfo(outputFileName).absolutePath(); QString saveFolder = QFileInfo(outputFileName).absolutePath();
if (!saveACopy) { m_project->updateSubtitle(outputFileName);
m_project->updateSubtitle(outputFileName);
}
QString scene = projectSceneList(saveFolder); QString scene = projectSceneList(saveFolder);
if (!m_replacementPattern.isEmpty()) { if (!m_replacementPattern.isEmpty()) {
QMapIterator<QString, QString> i(m_replacementPattern); QMapIterator<QString, QString> i(m_replacementPattern);
...@@ -307,7 +305,6 @@ bool ProjectManager::saveFileAs(const QString &outputFileName, bool saveACopy) ...@@ -307,7 +305,6 @@ bool ProjectManager::saveFileAs(const QString &outputFileName, bool saveACopy)
} }
} }
if (!m_project->saveSceneList(outputFileName, scene)) { if (!m_project->saveSceneList(outputFileName, scene)) {
m_project->updateSubtitle();
return false; return false;
} }
QUrl url = QUrl::fromLocalFile(outputFileName); 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