Fix crash on exit caused by subtitlemodel

parent f74787ed
......@@ -60,6 +60,9 @@ SubtitleModel::SubtitleModel(Mlt::Tractor *tractor, std::shared_ptr<TimelineItem
styleSection = QString("[V4 Styles]\nFormat: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding\nStyle: Default,Consolas,%1,16777215,65535,255,0,-1,0,1,2,2,6,40,40,%2,0,1\n").arg(fontSize).arg(fontMargin);
eventSection = QStringLiteral("[Events]\n");
styleName = QStringLiteral("Default");
connect(this, &SubtitleModel::modelChanged, [this]() {
jsontoSubtitle(toJson());
});
}
......@@ -75,11 +78,6 @@ void SubtitleModel::setup()
connect(this, &SubtitleModel::modelReset, this, &SubtitleModel::modelChanged);
}
std::shared_ptr<SubtitleModel> SubtitleModel::getModel()
{
return pCore->projectManager()->getSubtitleModel();
}
void SubtitleModel::importSubtitle(const QString filePath, int offset, bool externalImport)
{
QString start,end,comment;
......@@ -533,15 +531,14 @@ void SubtitleModel::removeSnapPoint(GenTime startpos)
void SubtitleModel::editEndPos(GenTime startPos, GenTime newEndPos, bool refreshModel)
{
qDebug()<<"Changing the sub end timings in model";
auto model = getModel();
if (model->m_subtitleList.count(startPos) <= 0) {
if (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;
int row = static_cast<int>(std::distance(m_subtitleList.begin(), m_subtitleList.find(startPos)));
m_subtitleList[startPos].second = newEndPos;
// Trigger update of the qml view
emit model->dataChanged(model->index(row), model->index(row), {EndFrameRole});
emit dataChanged(index(row), index(row), {EndFrameRole});
if (refreshModel) {
emit modelChanged();
}
......@@ -629,12 +626,11 @@ void SubtitleModel::editSubtitle(GenTime startPos, QString newSubtitleText, GenT
return;
}
qDebug()<<"Editing existing subtitle in model";
auto model = getModel();
int row = static_cast<int>(std::distance(model->m_subtitleList.begin(), model->m_subtitleList.find(startPos)));
model->m_subtitleList[startPos].first = newSubtitleText ;
model->m_subtitleList[startPos].second = endPos;
int row = static_cast<int>(std::distance(m_subtitleList.begin(), m_subtitleList.find(startPos)));
m_subtitleList[startPos].first = newSubtitleText ;
m_subtitleList[startPos].second = endPos;
qDebug()<<startPos.frames(pCore->getCurrentFps())<<m_subtitleList[startPos].first<<m_subtitleList[startPos].second.frames(pCore->getCurrentFps());
emit model->dataChanged(model->index(row), model->index(row), QVector<int>() << SubtitleRole);
emit dataChanged(index(row), index(row), QVector<int>() << SubtitleRole);
emit modelChanged();
return;
}
......
......@@ -159,8 +159,6 @@ signals:
void modelChanged();
protected:
/** @brief Helper function that retrieves a pointer to the subtitle model*/
static std::shared_ptr<SubtitleModel> getModel();
/** @brief Add time as snap in the registered snap model */
void addSnapPoint(GenTime startpos);
/** @brief Remove time as snap in the registered snap model */
......
......@@ -53,7 +53,7 @@ Core::Core()
void Core::prepareShutdown()
{
m_guiConstructed = false;
m_mainWindow->getCurrentTimeline()->controller()->prepareClose();
//m_mainWindow->getCurrentTimeline()->controller()->prepareClose();
projectItemModel()->blockSignals(true);
QThreadPool::globalInstance()->clear();
}
......@@ -277,6 +277,14 @@ void Core::selectTimelineItem(int id)
}
}
std::shared_ptr<SubtitleModel> Core::getSubtitleModel()
{
if (m_guiConstructed && m_mainWindow->getCurrentTimeline()->controller()->getModel()) {
return m_mainWindow->getCurrentTimeline()->controller()->getModel()->getSubtitleModel();
}
return nullptr;
}
std::shared_ptr<JobManager> Core::jobManager()
{
return m_jobManager;
......
......@@ -40,6 +40,7 @@ class ProfileModel;
class ProjectItemModel;
class ProjectManager;
class SubtitleEdit;
class SubtitleModel;
namespace Mlt {
class Repository;
......@@ -236,6 +237,8 @@ public:
void addGuides(QList <int> guides);
/** @brief Temporarily un/plug a list of clips in timeline. */
void temporaryUnplug(QList<int> clipIds, bool hide);
/** @brief Returns the current doc's subtitle model. */
std::shared_ptr<SubtitleModel> getSubtitleModel();
KSharedDataCache audioThumbCache;
......
......@@ -82,7 +82,6 @@ KdenliveDoc::KdenliveDoc(const QUrl &url, QString projectFolder, QUndoGroup *und
, m_documentOpenStatus(CleanProject)
, m_projectFolder(std::move(projectFolder))
, m_guideModel(new MarkerListModel(m_commandStack, this))
, m_subtitleModel(nullptr)
{
connect(m_guideModel.get(), &MarkerListModel::modelChanged, this, &KdenliveDoc::guidesChanged);
connect(this, SIGNAL(updateCompositionMode(int)), parent, SLOT(slotUpdateCompositeAction(int)));
......@@ -762,7 +761,7 @@ void KdenliveDoc::setUrl(const QUrl &url)
void KdenliveDoc::updateSubtitle(QString newUrl)
{
if (m_subtitleModel) {
if (auto ptr = m_subtitleModel.lock()) {
if (newUrl.isEmpty() && m_url.isValid()) {
newUrl = m_url.toLocalFile();
}
......@@ -775,8 +774,7 @@ void KdenliveDoc::updateSubtitle(QString newUrl)
QFileInfo info(newUrl);
subPath = info.dir().absoluteFilePath(QString("%1.srt").arg(info.fileName()));
}
qDebug()<<"===== SAVING SUBTITLE TO NEW PATH: "<<subPath;
m_subtitleModel->jsontoSubtitle(m_subtitleModel->toJson(), subPath);
ptr->jsontoSubtitle(ptr->toJson(), subPath);
}
}
......@@ -1795,11 +1793,6 @@ QString& KdenliveDoc::modifiedDecimalPoint() {
return m_modifiedDecimalPoint;
}
std::shared_ptr<SubtitleModel> KdenliveDoc::getSubtitleModel() const
{
return m_subtitleModel;
}
QString KdenliveDoc::subTitlePath()
{
QString path;
......@@ -1812,25 +1805,8 @@ QString KdenliveDoc::subTitlePath()
return path;
}
void KdenliveDoc::subtitlesChanged()
{
//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, QString subPath)
void KdenliveDoc::initializeSubtitles(std::shared_ptr<SubtitleModel> m_subtitle)
{
m_subtitleModel = m_subtitle;
if (QFileInfo(subPath).isRelative()) {
subPath.prepend(m_documentRoot);
}
connect(m_subtitleModel.get(), &SubtitleModel::modelChanged, this, &KdenliveDoc::subtitlesChanged);
m_subtitleModel->parseSubtitle(subPath);
//QMetaObject::invokeMethod(m_subtitle.get(), "parseSubtitle", Qt::QueuedConnection);
}
void KdenliveDoc::removeSubtitles()
{
m_subtitleModel->removeAllSubtitles();
}
......@@ -172,12 +172,8 @@ public:
* @return Original decimal point, or an empty string if it was “.” already
*/
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, QString subPath = QString());
/** @brief Delete all subtitles from subtitle model */
void removeSubtitles();
/** @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();
......@@ -210,7 +206,7 @@ private:
QMap<QString, QString> m_documentProperties;
QMap<QString, QString> m_documentMetadata;
std::shared_ptr<MarkerListModel> m_guideModel;
std::shared_ptr<SubtitleModel> m_subtitleModel;
std::weak_ptr<SubtitleModel> m_subtitleModel;
QString m_modifiedDecimalPoint;
......@@ -246,8 +242,6 @@ 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();
......
......@@ -4196,15 +4196,19 @@ void MainWindow::resetSubtitles()
pCore->subtitleWidget()->setModel(nullptr);
}
void MainWindow::slotEditSubtitle(const QString subPath)
void MainWindow::slotEditSubtitle(QString subPath)
{
std::shared_ptr<SubtitleModel> subtitleModel = pCore->currentDoc()->getSubtitleModel();
std::shared_ptr<SubtitleModel> subtitleModel = pCore->getSubtitleModel();
if (subtitleModel == nullptr) {
// Starting a new subtitle for this project
subtitleModel.reset(new SubtitleModel(getMainTimeline()->controller()->tractor(), getMainTimeline()->controller()->getModel(), this));
getMainTimeline()->controller()->getModel()->setSubModel(subtitleModel);
pCore->currentDoc()->initializeSubtitles(subtitleModel, subPath);
pCore->currentDoc()->initializeSubtitles(subtitleModel);
pCore->subtitleWidget()->setModel(subtitleModel);
if (!subPath.isEmpty() && QFileInfo(subPath).isRelative()) {
subPath.prepend(pCore->currentDoc()->documentRoot());
}
subtitleModel->parseSubtitle(subPath);
getMainTimeline()->showSubtitles = true;
m_buttonSubtitleEditTool->setChecked(true);
getMainTimeline()->connectSubtitleModel(true);
......@@ -4216,7 +4220,7 @@ void MainWindow::slotEditSubtitle(const QString subPath)
void MainWindow::slotAddSubtitle()
{
if (pCore->currentDoc()->getSubtitleModel() == nullptr || !getMainTimeline()->showSubtitles) {
if (pCore->getSubtitleModel() == nullptr || !getMainTimeline()->showSubtitles) {
slotEditSubtitle();
m_buttonSubtitleEditTool->setChecked(true);
}
......@@ -4225,7 +4229,7 @@ void MainWindow::slotAddSubtitle()
void MainWindow::slotImportSubtitle()
{
if (pCore->currentDoc()->getSubtitleModel() == nullptr || !getMainTimeline()->showSubtitles) {
if (pCore->getSubtitleModel() == nullptr || !getMainTimeline()->showSubtitles) {
slotEditSubtitle();
m_buttonSubtitleEditTool->setChecked(true);
}
......@@ -4234,7 +4238,7 @@ void MainWindow::slotImportSubtitle()
void MainWindow::slotExportSubtitle()
{
if (pCore->currentDoc()->getSubtitleModel() == nullptr) {
if (pCore->getSubtitleModel() == nullptr) {
pCore->displayMessage(i18n("No subtitles in current project"), InformationMessage);
return;
}
......
......@@ -298,7 +298,7 @@ public slots:
void slotSwitchTimelineZone(bool toggled);
/** @brief Open the online services search dialog. */
void slotDownloadResources();
void slotEditSubtitle(const QString subPath = QString());
void slotEditSubtitle(QString subPath = QString());
private slots:
/** @brief Shows the shortcut dialog. */
......
......@@ -27,7 +27,6 @@ the Free Software Foundation, either version 3 of the License, or
// Temporary for testing
#include "bin/model/markerlistmodel.hpp"
#include "bin/model/subtitlemodel.hpp"
#include "profiles/profilerepository.hpp"
#include "project/notesplugin.h"
......@@ -1092,8 +1091,3 @@ void ProjectManager::addAudioTracks(int tracksCount)
{
pCore->window()->getMainTimeline()->controller()->addTracks(0, tracksCount);
}
std::shared_ptr<SubtitleModel> ProjectManager::getSubtitleModel()
{
return current()->getSubtitleModel();
}
......@@ -30,7 +30,6 @@ class KAutoSaveFile;
class KJob;
class KdenliveDoc;
class MarkerListModel;
class SubtitleModel;
class NotesPlugin;
class Project;
class QAction;
......@@ -98,11 +97,6 @@ public:
*/
void addAudioTracks(int tracksCount);
/** @brief Return the current Subtitle Model
The method is virtual to allow mocking
*/
virtual std::shared_ptr<SubtitleModel> getSubtitleModel();
public slots:
void newFile(QString profileName, bool showProjectSettings = true);
void newFile(bool showProjectSettings = true);
......
......@@ -145,6 +145,8 @@ void TimelineModel::prepareClose()
(*it)->unlock();
++it;
}
m_subtitleModel.reset();
//m_subtitleModel->removeAllSubtitles();
}
TimelineModel::~TimelineModel()
......@@ -1107,6 +1109,11 @@ bool TimelineModel::requestSubtitleMove(int clipId, int position, bool updateVie
return res;
}
std::shared_ptr<SubtitleModel> TimelineModel::getSubtitleModel()
{
return m_subtitleModel;
}
bool TimelineModel::requestClipMoveAttempt(int clipId, int trackId, int position)
{
QWriteLocker locker(&m_lock);
......@@ -5196,6 +5203,6 @@ bool TimelineModel::resizeStartMix(int cid, int duration, bool singleResize)
void TimelineModel::setSubModel(std::shared_ptr<SubtitleModel> model)
{
m_subtitleModel = model;
m_subtitleModel = std::move(model);
m_subtitleModel->registerSnap(std::static_pointer_cast<SnapInterface>(m_snaps));
}
......@@ -719,6 +719,7 @@ public:
/** @brief Create a mix selection with currently selected clip. If delta = -1, mix with previous clip, +1 with next clip and 0 will check cursor position*/
bool mixClip(int idToMove = -1, int delta = 0);
Q_INVOKABLE bool resizeStartMix(int cid, int duration, bool singleResize);
std::shared_ptr<SubtitleModel> getSubtitleModel();
protected:
/* @brief Register a new track. This is a call-back meant to be called from TrackModel
......
......@@ -1389,7 +1389,7 @@ void TimelineController::selectItems(const QVariantList &tracks, int startFrame,
itemsToSelect.insert(currentClips.begin(), currentClips.end());
}
if (selectSubTitles) {
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
auto subtitleModel = pCore->getSubtitleModel();
if (subtitleModel) {
auto currentSubs = subtitleModel->getItemsInRange(startFrame, endFrame);
itemsToSelect.insert(currentSubs.begin(), currentSubs.end());
......@@ -1417,7 +1417,7 @@ void TimelineController::cutClipUnderCursor(int position, int track)
const auto selection = m_model->getCurrentSelection();
if (track == -2) {
// Subtitle cut
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
auto subtitleModel = pCore->getSubtitleModel();
subtitleModel->cutSubtitle(position);
return;
}
......@@ -3750,7 +3750,7 @@ void TimelineController::editSubtitle(int startFrame, int endFrame, QString newT
if (oldText == newText) {
return;
}
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
auto subtitleModel = pCore->getSubtitleModel();
Fun local_redo = [subtitleModel, startFrame, endFrame, newText]() {
subtitleModel->editSubtitle(GenTime(startFrame, pCore->getCurrentFps()), newText, GenTime(endFrame, pCore->getCurrentFps()));
pCore->refreshProjectRange({startFrame, endFrame});
......@@ -3768,7 +3768,7 @@ void TimelineController::editSubtitle(int startFrame, int endFrame, QString newT
void TimelineController::resizeSubtitle(int startFrame, int endFrame, int oldEndFrame, bool refreshModel)
{
qDebug()<<"Editing existing subtitle in controller at:"<<startFrame;
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
auto subtitleModel = pCore->getSubtitleModel();
int max = qMax(endFrame, oldEndFrame);
Fun local_redo = [subtitleModel, startFrame, endFrame, max, refreshModel]() {
subtitleModel->editEndPos(GenTime(startFrame, pCore->getCurrentFps()), GenTime(endFrame, pCore->getCurrentFps()), refreshModel);
......@@ -3794,7 +3794,7 @@ void TimelineController::addSubtitle(int startframe)
}
int endframe = startframe + pCore->getDurationFromString(KdenliveSettings::subtitle_duration());
int id = TimelineModel::getNextId();
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
auto subtitleModel = pCore->getSubtitleModel();
Fun local_undo = [subtitleModel, id, startframe, endframe]() {
subtitleModel->removeSubtitle(id);
pCore->refreshProjectRange({startframe, endframe});
......@@ -3816,7 +3816,7 @@ void TimelineController::importSubtitle()
view.setupUi(d);
d->setWindowTitle(i18n("Import Subtitle"));
if (d->exec() == QDialog::Accepted && !view.subtitle_url->url().isEmpty()) {
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
auto subtitleModel = pCore->getSubtitleModel();
int offset = 0;
if (view.cursor_pos->isChecked()) {
offset = pCore->getTimelinePosition();
......@@ -3827,7 +3827,7 @@ void TimelineController::importSubtitle()
void TimelineController::exportSubtitle()
{
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
auto subtitleModel = pCore->getSubtitleModel();
if (subtitleModel == nullptr) {
return;
}
......@@ -3847,7 +3847,7 @@ void TimelineController::exportSubtitle()
void TimelineController::deleteSubtitle(int startframe, int endframe, QString text)
{
auto subtitleModel = pCore->projectManager()->current()->getSubtitleModel();
auto subtitleModel = pCore->getSubtitleModel();
int id = subtitleModel->getIdForStartPos(GenTime(startframe, pCore->getCurrentFps()));
Fun local_redo = [subtitleModel, id, startframe, endframe]() {
subtitleModel->removeSubtitle(id);
......
......@@ -174,7 +174,7 @@ void TimelineWidget::setModel(const std::shared_ptr<TimelineItemModel> &model, M
ft.setPointSize(QFontDatabase::systemFont(QFontDatabase::SmallestReadableFont).pointSize());
setFont(ft);
rootContext()->setContextProperty("miniFont", font());
rootContext()->setContextProperty("subtitleModel", pCore->projectManager()->current()->getSubtitleModel().get());
rootContext()->setContextProperty("subtitleModel", pCore->getSubtitleModel().get());
const QStringList effs = sortedItems(KdenliveSettings::favorite_effects(), false).values();
const QStringList trans = sortedItems(KdenliveSettings::favorite_transitions(), true).values();
......@@ -490,7 +490,7 @@ bool TimelineWidget::eventFilter(QObject *object, QEvent *event)
void TimelineWidget::connectSubtitleModel(bool firstConnect)
{
qDebug()<<"root context get sub model new function";
if (pCore->projectManager()->current()->getSubtitleModel().get() == nullptr) {
if (pCore->getSubtitleModel().get() == nullptr) {
//qDebug()<<"null ptr here at root context";
return;
}
......@@ -499,7 +499,7 @@ void TimelineWidget::connectSubtitleModel(bool firstConnect)
//qDebug()<<"null ptr NOT here at root context";
rootObject()->setProperty("showSubtitles", showSubtitles);
if (firstConnect) {
rootContext()->setContextProperty("subtitleModel", pCore->projectManager()->current()->getSubtitleModel().get());
rootContext()->setContextProperty("subtitleModel", pCore->getSubtitleModel().get());
}
}
}
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