Add subtitle import function, fix crash on add subtitle on new project

parent eb909089
......@@ -66,14 +66,8 @@ std::shared_ptr<SubtitleModel> SubtitleModel::getModel()
return pCore->projectManager()->getSubtitleModel();
}
void SubtitleModel::parseSubtitle(const QString subPath)
{
qDebug()<<"Parsing started";
if (!subPath.isEmpty()) {
m_subtitleFilter->set("av.filename", subPath.toUtf8().constData());
}
QString filePath = m_subtitleFilter->get("av.filename");
m_subFilePath = filePath;
void SubtitleModel::importSubtitle(const QString filePath)
{
QString start,end,comment;
QString timeLine;
GenTime startPos, endPos;
......@@ -230,6 +224,18 @@ void SubtitleModel::parseSubtitle(const QString subPath)
}
assFile.close();
}
jsontoSubtitle(toJson());
}
void SubtitleModel::parseSubtitle(const QString subPath)
{
qDebug()<<"Parsing started";
if (!subPath.isEmpty()) {
m_subtitleFilter->set("av.filename", subPath.toUtf8().constData());
}
QString filePath = m_subtitleFilter->get("av.filename");
m_subFilePath = filePath;
importSubtitle(filePath);
//jsontoSubtitle(toJson());
}
......
......@@ -95,6 +95,9 @@ public:
@param newPos is new start position of subtitle
*/
void moveSubtitle(GenTime oldPos, GenTime newPos);
/** @brief Function that imports a subtitle file */
void importSubtitle(const QString filePath);
/** @brief Exports the subtitle model to json */
QString toJson();
......
......@@ -81,8 +81,9 @@ KdenliveDoc::KdenliveDoc(const QUrl &url, QString projectFolder, QUndoGroup *und
, m_modified(false)
, m_documentOpenStatus(CleanProject)
, m_projectFolder(std::move(projectFolder))
, m_guideModel(new MarkerListModel(m_commandStack, this))
, m_subtitleModel(nullptr)
{
m_guideModel.reset(new MarkerListModel(m_commandStack, this));
connect(m_guideModel.get(), &MarkerListModel::modelChanged, this, &KdenliveDoc::guidesChanged);
connect(this, SIGNAL(updateCompositionMode(int)), parent, SLOT(slotUpdateCompositeAction(int)));
bool success = false;
......
<!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
<kpartgui name="kdenlive" version="195" translationDomain="kdenlive">
<kpartgui name="kdenlive" version="196" translationDomain="kdenlive">
<MenuBar>
<Menu name="file" >
<Action name="file_save"/>
......@@ -30,6 +30,7 @@
<Menu name="subtitles" ><text>Subtitles</text>
<Action name="subtitle_tool" />
<Action name="add_subtitle" />
<Action name="import_subtitle" />
</Menu>
<Separator />
<Action name="bin_view_mode" />
......
......@@ -1703,6 +1703,8 @@ void MainWindow::setupActions()
addAction(QStringLiteral("delete_all_guides"), i18n("Delete All Guides"), this, SLOT(slotDeleteAllGuides()),
QIcon::fromTheme(QStringLiteral("edit-delete")));
addAction(QStringLiteral("add_subtitle"), i18n("Add Subtitle"), this, SLOT(slotAddSubtitle()), QIcon::fromTheme(QStringLiteral("list-add")), Qt::SHIFT +Qt::Key_S);
addAction(QStringLiteral("import_subtitle"), i18n("Import Subtitle File"), this, SLOT(slotImportSubtitle()), QIcon::fromTheme(QStringLiteral("list-add")));
m_saveAction = KStandardAction::save(pCore->projectManager(), SLOT(saveFile()), actionCollection());
m_saveAction->setIcon(QIcon::fromTheme(QStringLiteral("document-save")));
......@@ -4170,6 +4172,13 @@ void MainWindow::slotActivateTarget()
}
}
void MainWindow::resetSubtitles()
{
// Hide subtitle track
m_buttonSubtitleEditTool->setChecked(false);
getMainTimeline()->showSubtitles = false;
}
void MainWindow::slotEditSubtitle(const QString subPath)
{
std::shared_ptr<SubtitleModel> subtitleModel = pCore->currentDoc()->getSubtitleModel();
......@@ -4182,13 +4191,22 @@ void MainWindow::slotEditSubtitle(const QString subPath)
void MainWindow::slotAddSubtitle()
{
if (!getMainTimeline()->showSubtitles) {
if (pCore->currentDoc()->getSubtitleModel() == nullptr || !getMainTimeline()->showSubtitles) {
slotEditSubtitle();
m_buttonSubtitleEditTool->setChecked(true);
}
getCurrentTimeline()->controller()->addSubtitle();
}
void MainWindow::slotImportSubtitle()
{
if (pCore->currentDoc()->getSubtitleModel() == nullptr || !getMainTimeline()->showSubtitles) {
slotEditSubtitle();
m_buttonSubtitleEditTool->setChecked(true);
}
getCurrentTimeline()->controller()->importSubtitle();
}
#ifdef DEBUG_MAINW
#undef DEBUG_MAINW
#endif
......@@ -141,6 +141,9 @@ public:
/** @brief Raise (show) the project bin*/
void raiseBin();
/** @brief Hide subtitle track */
void resetSubtitles();
protected:
/** @brief Closes the window.
* @return false if the user presses "Cancel" on a confirmation dialog or
......@@ -509,7 +512,10 @@ private slots:
void slotActivateVideoTrackSequence();
/** @brief Select target for current track */
void slotActivateTarget();
/** @brief Add a subtitle at current position */
void slotAddSubtitle();
/** @brief Import a subtitle file */
void slotImportSubtitle();
signals:
Q_SCRIPTABLE void abortRenderJob(const QString &url);
......
......@@ -271,6 +271,7 @@ bool ProjectManager::closeCurrentDocument(bool saveChanges, bool quit)
disconnect(pCore->window()->getMainTimeline()->controller(), &TimelineController::durationChanged, this, &ProjectManager::adjustProjectDuration);
pCore->window()->getMainTimeline()->controller()->clipActions.clear();
pCore->window()->getMainTimeline()->controller()->prepareClose();
pCore->window()->resetSubtitles();
if (m_mainTimelineModel) {
m_mainTimelineModel->prepareClose();
}
......
......@@ -52,6 +52,7 @@
#include "audiomixer/mixermanager.hpp"
#include <KColorScheme>
#include <KRecentDirs>
#include <QApplication>
#include <QClipboard>
#include <QQuickItem>
......@@ -3851,6 +3852,22 @@ void TimelineController::addSubtitle(int startframe)
pCore->pushUndo(local_undo, local_redo, i18n("Add subtitle"));
}
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());
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
subtitleModel->importSubtitle(d->selectedUrls().first().toLocalFile());
}
}
void TimelineController::deleteSubtitle(int startframe, int endframe, QString text)
{
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
......
......@@ -582,6 +582,8 @@ public:
Q_INVOKABLE void addSubtitle(int startframe = -1);
/** @brief Delete subtitle clip with frame as start position*/
Q_INVOKABLE void deleteSubtitle(int frameframe, int endframe, QString Ctext);
/** @brief Import a subtitle file*/
void importSubtitle();
public slots:
void resetView();
......
......@@ -490,7 +490,7 @@ void TimelineWidget::connectSubtitleModel()
{
showSubtitles = !showSubtitles;
//qDebug()<<"null ptr NOT here at root context";
rootObject()->setProperty("showSubtitles",showSubtitles);
rootObject()->setProperty("showSubtitles", showSubtitles);
rootContext()->setContextProperty("subtitleModel", pCore->projectManager()->current()->getSubtitleModel().get());
}
}
\ No newline at end of file
}
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