Commit a5ceebf6 authored by Sashmita Raghav's avatar Sashmita Raghav
Browse files

Add timeline tractor to Subtitle Model to attach subtitle filter

parent 2cf1a425
......@@ -3,17 +3,27 @@
#include "core.h"
#include "project/projectmanager.h"
#include "timeline2/model/snapmodel.hpp"
#include "profiles/profilemodel.hpp"
#include <mlt++/MltProperties.h>
#include <mlt++/Mlt.h>
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
SubtitleModel::SubtitleModel(std::weak_ptr<DocUndoStack> undo_stack, QObject *parent)
SubtitleModel::SubtitleModel(Mlt::Tractor *tractor, QObject *parent)
: QAbstractListModel(parent)
, m_undoStack(std::move(undo_stack))
, m_tractor(tractor)
, m_lock(QReadWriteLock::Recursive)
{
//qDebug()<< "subtitle constructor";
qDebug()<< "subtitle constructor";
m_subtitleFilter = nullptr;
m_subtitleFilter.reset(new Mlt::Filter(pCore->getCurrentProfile()->profile(), "av.filename"));
qDebug()<<"Filter!";
if(tractor != nullptr){
qDebug()<<"Tractor!";
m_tractor->attach(*m_subtitleFilter.get());
}
setup();
}
......@@ -36,11 +46,10 @@ std::shared_ptr<SubtitleModel> SubtitleModel::getModel()
}
void SubtitleModel::parseSubtitle()
{ qDebug()<<"Parsing started";
//QModelIndex index;
//int paramName = m_model->data(index, AssetParameterModel::NameRole).toInt();
//QString filename(m_asset->get(paramName.toUtf8().constData()));
{
qDebug()<<"Parsing started";
QString filePath= "path_to_subtitle_file.srt";
m_subFilePath = filePath;
QString start,end,comment;
QString timeLine;
GenTime startPos, endPos;
......@@ -226,11 +235,11 @@ void SubtitleModel::addSubtitle(GenTime start, GenTime end, QString &str)
qDebug()<<"already present in model"<<"string :"<<m_subtitleList[start].first<<" start time "<<start.frames(pCore->getCurrentFps())<<"end time : "<< m_subtitleList[start].second.frames(pCore->getCurrentFps());
return;
}
if(model->m_subtitleList.count(start) > 0 ){
/*if(model->m_subtitleList.count(start) > 0 ){
qDebug()<<"Start time already in model";
editSubtitle(start, str, end);
return;
}
}*/
auto it= model->m_subtitleList.lower_bound(start); // returns the key and its value *just* greater than start.
//Q_ASSERT(it->first < model->m_subtitleList.end()->second.second); // returns warning if added subtitle start time is less than last subtitle's end time
int insertRow= static_cast<int>(model->m_subtitleList.size());//converts the returned unsigned size() to signed int
......@@ -327,13 +336,18 @@ void SubtitleModel::addSnapPoint(GenTime startpos)
std::swap(m_regSnaps, validSnapModels);
}
void SubtitleModel::editEndPos(GenTime startPos, GenTime oldEndPos, GenTime newEndPos)
void SubtitleModel::editEndPos(GenTime startPos, GenTime newEndPos)
{
qDebug()<<"Changing the sub end timings in model";
auto model = getModel();
if(oldEndPos == newEndPos) return;
if(model->m_subtitleList.count(startPos) <= 0){
//is not present in model only
return;
}
int row = static_cast<int>(std::distance(model->m_subtitleList.begin(), model->m_subtitleList.find(startPos)));
model->m_subtitleList[startPos].second = newEndPos;
emit model->dataChanged(model->index(row), model->index(row), QVector<int>() << EndPosRole);
qDebug()<<startPos.frames(pCore->getCurrentFps())<<m_subtitleList[startPos].second.frames(pCore->getCurrentFps());
return;
}
......@@ -388,16 +402,16 @@ QString SubtitleModel::toJson()
currentSubtitle.insert(QLatin1String("dialogue"), QJsonValue(subtitle.second.first));
currentSubtitle.insert(QLatin1String("endPos"), QJsonValue(subtitle.second.second.seconds()));
list.push_back(currentSubtitle);
qDebug()<<subtitle.first.seconds();
//qDebug()<<subtitle.first.seconds();
}
QJsonDocument jsonDoc(list);
qDebug()<<QString(jsonDoc.toJson());
//qDebug()<<QString(jsonDoc.toJson());
return QString(jsonDoc.toJson());
}
void SubtitleModel::jsontoSubtitle(const QString &data)
{
QString outFile = "path_to_temp_Subtitle.ass";
QString filePath= "path_to_subtitle_file.srt";
if (!outFile.contains(".ass"))
return; // this function currrently writes in ass file format only
QFile outF(outFile);
......@@ -457,7 +471,10 @@ void SubtitleModel::jsontoSubtitle(const QString &data)
.arg(milli_2,2,10,QChar('0'));
//Format: Layer, Start, End, Style, Actor, MarginL, MarginR, MarginV, Effect, Text
out <<"Dialogue: 0,"<<startTimeString<<","<<endTimeString<<","<<styleName<<",,0000,0000,0000,,"<<dialogue<<endl;
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();
}
}
}
qDebug()<<"Setting subtitle filter";
m_subtitleFilter->set("av.filename", outFile.toUtf8().constData());
m_tractor->attach(*m_subtitleFilter.get());
}
\ No newline at end of file
......@@ -13,6 +13,7 @@
#include <map>
#include <memory>
#include <mlt++/MltProperties.h>
#include <mlt++/Mlt.h>
class DocUndoStack;
class SnapInterface;
......@@ -27,7 +28,7 @@ class SubtitleModel:public QAbstractListModel
public:
/* @brief Construct a subtitle list bound to the timeline */
explicit SubtitleModel(std::weak_ptr<DocUndoStack> undo_stack, QObject *parent = nullptr);
explicit SubtitleModel(Mlt::Tractor *tractor = nullptr, QObject *parent = nullptr);
enum { SubtitleRole = Qt::UserRole + 1, StartPosRole, EndPosRole, StartFrameRole, EndFrameRole };
/** @brief Function that parses through a subtitle file */
......@@ -50,7 +51,7 @@ public:
@param oldPos is the old position of the end time
@param pos defines the new position of the end time
*/
void editEndPos(GenTime startPos, GenTime oldEndPos, GenTime newEndPos);
void editEndPos(GenTime startPos, GenTime newEndPos);
/** @brief Edit subtitle , i.e. text and/or end time
@param startPos is start timing position of subtitles
......@@ -84,6 +85,7 @@ private:
QString scriptInfoSection="", styleSection = "",eventSection="";
QString styleName="";
QString m_subFilePath;
//To get subtitle file from effects parameter:
//std::unique_ptr<Mlt::Properties> m_asset;
......@@ -91,6 +93,8 @@ private:
std::vector<std::weak_ptr<SnapInterface>> m_regSnaps;
mutable QReadWriteLock m_lock;
std::unique_ptr<Mlt::Filter> m_subtitleFilter;
Mlt::Tractor *m_tractor;
signals:
void modelChanged();
......
......@@ -84,15 +84,12 @@ KdenliveDoc::KdenliveDoc(const QUrl &url, QString projectFolder, QUndoGroup *und
{
m_guideModel.reset(new MarkerListModel(m_commandStack, this));
connect(m_guideModel.get(), &MarkerListModel::modelChanged, this, &KdenliveDoc::guidesChanged);
m_subtitleModel.reset(new SubtitleModel (m_commandStack, this));
connect(m_subtitleModel.get(), &SubtitleModel::modelChanged, this, &KdenliveDoc::subtitlesChanged);
connect(this, SIGNAL(updateCompositionMode(int)), parent, SLOT(slotUpdateCompositeAction(int)));
bool success = false;
connect(m_commandStack.get(), &QUndoStack::indexChanged, this, &KdenliveDoc::slotModified);
connect(m_commandStack.get(), &DocUndoStack::invalidate, this, &KdenliveDoc::checkPreviewStack, Qt::DirectConnection);
// connect(m_commandStack, SIGNAL(cleanChanged(bool)), this, SLOT(setModified(bool)));
QMetaObject::invokeMethod(m_subtitleModel.get(), "parseSubtitle", Qt::QueuedConnection);
// init default document properties
m_documentProperties[QStringLiteral("zoom")] = QLatin1Char('8');
m_documentProperties[QStringLiteral("verticalzoom")] = QLatin1Char('1');
......@@ -1300,7 +1297,7 @@ void KdenliveDoc::loadDocumentProperties()
if (pl.isNull()) {
return;
}
QMetaObject::invokeMethod(m_subtitleModel.get(), "parseSubtitle", Qt::QueuedConnection);
//QMetaObject::invokeMethod(m_subtitleModel.get(), "parseSubtitle", Qt::QueuedConnection);
QDomNodeList props = pl.elementsByTagName(QStringLiteral("property"));
QString name;
QDomElement e;
......@@ -1785,7 +1782,15 @@ std::shared_ptr<SubtitleModel> KdenliveDoc::getSubtitleModel() const
void KdenliveDoc::subtitlesChanged()
{
m_subtitleModel->jsontoSubtitle(m_subtitleModel->toJson()); //Update subtitle file everytime the subtitle model is changed
//m_subtitleModel->parseSubtitle();
m_subtitleModel->jsontoSubtitle(m_subtitleModel->toJson()); //Update subtitle file everytime the subtitle model is changed
return;
}
void KdenliveDoc::initializeSubtitles(const std::shared_ptr<SubtitleModel> m_subtitle)
{
m_subtitleModel = m_subtitle;
connect(m_subtitleModel.get(), &SubtitleModel::modelChanged, this, &KdenliveDoc::subtitlesChanged);
m_subtitleModel->parseSubtitle();
//QMetaObject::invokeMethod(m_subtitle.get(), "parseSubtitle", Qt::QueuedConnection);
}
\ No newline at end of file
......@@ -172,6 +172,8 @@ public:
QString &modifiedDecimalPoint();
/** @brief Returns a pointer to the subtitle model */
std::shared_ptr<SubtitleModel> getSubtitleModel() const;
/** @brief Initialize and connect subtitle model */
void initializeSubtitles(const std::shared_ptr<SubtitleModel> m_subtitle);
private:
QUrl m_url;
......@@ -238,6 +240,8 @@ public slots:
void slotAutoSave(const QString &scene);
/** @brief Groups were changed, save to MLT. */
void groupsChanged(const QString &groups);
/** @brief Subtitles were changed, update subtitle file */
void subtitlesChanged();
private slots:
void slotModified();
......@@ -247,7 +251,6 @@ private slots:
void checkPreviewStack(int ix);
/** @brief Guides were changed, save to MLT. */
void guidesChanged();
void subtitlesChanged();
signals:
void resetProjectList();
......
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