Allow importing subtitle file at cursor position, save subtitles on project archiving

parent febe7173
......@@ -66,7 +66,7 @@ std::shared_ptr<SubtitleModel> SubtitleModel::getModel()
return pCore->projectManager()->getSubtitleModel();
}
void SubtitleModel::importSubtitle(const QString filePath)
void SubtitleModel::importSubtitle(const QString filePath, int offset)
{
QString start,end,comment;
QString timeLine;
......@@ -79,7 +79,7 @@ void SubtitleModel::importSubtitle(const QString filePath)
*/
if (filePath.isEmpty())
return;
GenTime subtitleOffset(offset, pCore->getCurrentFps());
if (filePath.contains(".srt")) {
QFile srtFile(filePath);
if (!srtFile.exists() || !srtFile.open(QIODevice::ReadOnly)) {
......@@ -120,7 +120,7 @@ void SubtitleModel::importSubtitle(const QString filePath)
}
turn++;
} else {
addSubtitle(startPos,endPos,comment);
addSubtitle(startPos + subtitleOffset, endPos + subtitleOffset, comment);
//reinitialize
comment = timeLine = "";
turn = 0; r = 0;
......@@ -213,7 +213,7 @@ void SubtitleModel::importSubtitle(const QString filePath)
// Text
comment = dialogue[9]+ remainingStr;
//qDebug()<<"Start: "<< start << "End: "<<end << comment;
addSubtitle(startPos,endPos,comment);
addSubtitle(startPos + subtitleOffset, endPos + subtitleOffset, comment);
}
}
turn++;
......
......@@ -97,7 +97,7 @@ public:
void moveSubtitle(GenTime oldPos, GenTime newPos);
/** @brief Function that imports a subtitle file */
void importSubtitle(const QString filePath);
void importSubtitle(const QString filePath, int offset = 0);
/** @brief Exports the subtitle model to json */
QString toJson();
......
......@@ -896,7 +896,32 @@ bool ArchiveWidget::processProjectFile()
// Make a copy of original project file for extra safety
QString backupPath = archive_url->url().toLocalFile() + QDir::separator() + m_name + QStringLiteral("-backup.kdenlive");
QFile source(pCore->currentDoc()->url().toLocalFile());
source.copy(backupPath);
if (!source.copy(backupPath)) {
// Error
KMessageBox::error(this, i18n("Cannot write to file %1", backupPath));
return false;
}
// Copy subtitle files if any
QString sub = pCore->currentDoc()->url().toLocalFile();
if (QFileInfo::exists(sub + QStringLiteral(".srt"))) {
QFile subFile(sub + QStringLiteral(".srt"));
backupPath = archive_url->url().toLocalFile() + QDir::separator() + QFileInfo(subFile).fileName();
if (!subFile.copy(backupPath)) {
// Error
KMessageBox::error(this, i18n("Cannot write to file %1", backupPath));
return false;
}
}
if (QFileInfo::exists(sub + QStringLiteral(".ass"))) {
QFile subFile(sub + QStringLiteral(".ass"));
backupPath = archive_url->url().toLocalFile() + QDir::separator() + QFileInfo(subFile).fileName();
if (!subFile.copy(backupPath)) {
// Error
KMessageBox::error(this, i18n("Cannot write to file %1", backupPath));
return false;
}
}
QString path = archive_url->url().toLocalFile() + QDir::separator() + m_name + QStringLiteral(".kdenlive");
QFile file(path);
......
......@@ -50,6 +50,7 @@
#include "timeline2/view/dialogs/trackdialog.h"
#include "transitions/transitionsrepository.hpp"
#include "audiomixer/mixermanager.hpp"
#include "ui_import_subtitle_ui.h"
#include <KColorScheme>
#include <KRecentDirs>
......@@ -3854,17 +3855,17 @@ void TimelineController::addSubtitle(int startframe)
void TimelineController::importSubtitle()
{
const QString dialogFilter = i18n("Subtitle Files") + QStringLiteral(" (*.srt, *.ass)");
QString clipFolder = KRecentDirs::dir(QStringLiteral(":KdenliveSubtitlesFolder"));
if (clipFolder.isEmpty()) {
clipFolder = QDir::homePath();
}
QPointer<QFileDialog> d = new QFileDialog(QApplication::activeWindow(), i18n("Open Subtitle File"), clipFolder, dialogFilter);
d->setFileMode(QFileDialog::ExistingFile);
if (d->exec() == QDialog::Accepted && !d->selectedUrls().isEmpty()) {
KRecentDirs::add(QStringLiteral(":KdenliveSubtitlesFolder"), d->selectedUrls().first().adjusted(QUrl::RemoveFilename).toLocalFile());
QPointer<QDialog> d = new QDialog;
Ui::ImportSub_UI view;
view.setupUi(d);
d->setWindowTitle(i18n("Import Subtitle"));
if (d->exec() == QDialog::Accepted && !view.subtitle_url->url().isEmpty()) {
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
subtitleModel->importSubtitle(d->selectedUrls().first().toLocalFile());
int offset = 0;
if (view.cursor_pos->isChecked()) {
offset = pCore->getTimelinePosition();
}
subtitleModel->importSubtitle(view.subtitle_url->url().toLocalFile(), offset);
}
}
......@@ -3884,5 +3885,4 @@ void TimelineController::deleteSubtitle(int startframe, int endframe, QString te
};
local_redo();
pCore->pushUndo(local_undo, local_redo, i18n("Delete subtitle"));
return;
}
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>ImportSub_UI</class>
<widget class="QDialog" name="ImportSub_UI">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>383</width>
<height>204</height>
</rect>
</property>
<property name="windowTitle">
<string>Dialog</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="4" column="0">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Subtitle file</string>
</property>
</widget>
</item>
<item row="1" column="0">
<widget class="KUrlRequester" name="subtitle_url">
<property name="filter">
<string>*.srt *.ass</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="cursor_pos">
<property name="text">
<string>Import at timeline cursor position</string>
</property>
</widget>
</item>
<item row="3" column="0">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>KUrlRequester</class>
<extends>QWidget</extends>
<header>kurlrequester.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>ImportSub_UI</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>ImportSub_UI</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>
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