Fix undo import subtitle file, and improve subtitle group operations (only...

Fix undo import subtitle file, and improve subtitle group operations (only reload subtitle file once)
parent a4c5e0bf
......@@ -73,8 +73,6 @@ void SubtitleModel::setup()
connect(this, &SubtitleModel::columnsRemoved, this, &SubtitleModel::modelChanged);
connect(this, &SubtitleModel::columnsInserted, this, &SubtitleModel::modelChanged);
connect(this, &SubtitleModel::rowsMoved, this, &SubtitleModel::modelChanged);
connect(this, &SubtitleModel::rowsRemoved, this, &SubtitleModel::modelChanged);
connect(this, &SubtitleModel::rowsInserted, this, &SubtitleModel::modelChanged);
connect(this, &SubtitleModel::modelReset, this, &SubtitleModel::modelChanged);
}
......@@ -91,6 +89,11 @@ void SubtitleModel::importSubtitle(const QString filePath, int offset, bool exte
*/
if (filePath.isEmpty())
return;
Fun redo = []() { return true; };
Fun undo = [this]() {
emit modelChanged();
return true;
};
GenTime subtitleOffset(offset, pCore->getCurrentFps());
if (filePath.endsWith(".srt")) {
QFile srtFile(filePath);
......@@ -132,7 +135,7 @@ void SubtitleModel::importSubtitle(const QString filePath, int offset, bool exte
}
turn++;
} else {
addSubtitle(TimelineModel::getNextId(), startPos + subtitleOffset, endPos + subtitleOffset, comment);
addSubtitle(startPos + subtitleOffset, endPos + subtitleOffset, comment, undo, redo, false);
//reinitialize
comment = timeLine = "";
turn = 0; r = 0;
......@@ -231,7 +234,7 @@ void SubtitleModel::importSubtitle(const QString filePath, int offset, bool exte
// Text
comment = dialogue[9]+ remainingStr;
//qDebug()<<"Start: "<< start << "End: "<<end << comment;
addSubtitle(TimelineModel::getNextId(), startPos + subtitleOffset, endPos + subtitleOffset, comment);
addSubtitle(startPos + subtitleOffset, endPos + subtitleOffset, comment, undo, redo, false);
}
}
turn++;
......@@ -242,8 +245,14 @@ void SubtitleModel::importSubtitle(const QString filePath, int offset, bool exte
}
assFile.close();
}
Fun update_model= [this]() {
emit modelChanged();
return true;
};
PUSH_LAMBDA(update_model, redo);
redo();
if (externalImport) {
jsontoSubtitle(toJson());
pCore->pushUndo(undo, redo, i18n("Edit subtitle"));
}
}
......@@ -284,7 +293,24 @@ GenTime SubtitleModel::stringtoTime(QString &str)
return pos;
}
bool SubtitleModel::addSubtitle(int id, GenTime start, GenTime end, const QString str, bool temporary)
bool SubtitleModel::addSubtitle(GenTime start, GenTime end, const QString str, Fun &undo, Fun &redo, bool updateFilter)
{
int id = TimelineModel::getNextId();
Fun local_redo = [this, id, start, end, str, updateFilter]() {
addSubtitle(id, start, end, str, false, updateFilter);
pCore->refreshProjectRange({start.frames(pCore->getCurrentFps()), end.frames(pCore->getCurrentFps())});
return true;
};
Fun local_undo = [this, id, start, end, updateFilter]() {
removeSubtitle(id, false, updateFilter);
pCore->refreshProjectRange({start.frames(pCore->getCurrentFps()), end.frames(pCore->getCurrentFps())});
return true;
};
UPDATE_UNDO_REDO(local_redo, local_undo, undo, redo);
return true;
}
bool SubtitleModel::addSubtitle(int id, GenTime start, GenTime end, const QString str, bool temporary, bool updateFilter)
{
if (start.frames(pCore->getCurrentFps()) < 0 || end.frames(pCore->getCurrentFps()) < 0) {
qDebug()<<"Time error: is negative";
......@@ -307,6 +333,9 @@ bool SubtitleModel::addSubtitle(int id, GenTime start, GenTime end, const QStrin
addSnapPoint(start);
addSnapPoint(end);
qDebug()<<"Added to model";
if (updateFilter) {
emit modelChanged();
}
return true;
}
......@@ -657,7 +686,7 @@ void SubtitleModel::editSubtitle(GenTime startPos, QString newSubtitleText)
return;
}
bool SubtitleModel::removeSubtitle(int id, bool temporary)
bool SubtitleModel::removeSubtitle(int id, bool temporary, bool updateFilter)
{
qDebug()<<"Deleting subtitle in model";
if (m_timeline->m_allSubtitles.find( id ) == m_timeline->m_allSubtitles.end()) {
......@@ -677,6 +706,9 @@ bool SubtitleModel::removeSubtitle(int id, bool temporary)
endRemoveRows();
removeSnapPoint(start);
removeSnapPoint(end);
if (updateFilter) {
emit modelChanged();
}
return true;
}
......
......@@ -55,7 +55,8 @@ public:
enum { SubtitleRole = Qt::UserRole + 1, StartPosRole, EndPosRole, StartFrameRole, EndFrameRole, IdRole, SelectedRole };
/** @brief Function that parses through a subtitle file */
bool addSubtitle(int id, GenTime start,GenTime end, const QString str, bool temporary = false);
bool addSubtitle(int id, GenTime start,GenTime end, const QString str, bool temporary = false, bool updateFilter = true);
bool addSubtitle(GenTime start, GenTime end, const QString str, Fun &undo, Fun &redo, bool updateFilter = true);
/** @brief Converts string of time to GenTime */
GenTime stringtoTime(QString &str);
/** @brief Return model data item according to the role passed */
......@@ -90,7 +91,7 @@ public:
void editSubtitle(GenTime startPos, QString newSubtitleText);
/** @brief Remove subtitle at start position (pos) */
bool removeSubtitle(int id, bool temporary = false);
bool removeSubtitle(int id, bool temporary = false, bool updateFilter = true);
/** @brief Remove all subtitles from subtitle model */
void removeAllSubtitles();
......
......@@ -374,7 +374,7 @@ bool TimelineFunctions::requestSpacerEndOperation(const std::shared_ptr<Timeline
} else if (timeline->isComposition(itemId)) {
final = timeline->requestCompositionMove(itemId, track, -1, endPosition, true, true, undo, redo);
} else {
final = timeline->requestSubtitleMove(itemId, endPosition, true, true, true, undo, redo);
final = timeline->requestSubtitleMove(itemId, endPosition, true, true, true, true, undo, redo);
}
}
}
......
......@@ -1077,23 +1077,23 @@ bool TimelineModel::requestSubtitleMove(int clipId, int position, bool updateVie
}
std::function<bool(void)> undo = []() { return true; };
std::function<bool(void)> redo = []() { return true; };
bool res = requestSubtitleMove(clipId, position, updateView, logUndo, invalidateTimeline, undo, redo);
bool res = requestSubtitleMove(clipId, position, updateView, logUndo, logUndo, invalidateTimeline, undo, redo);
if (res && logUndo) {
PUSH_UNDO(undo, redo, i18n("Move subtitle"));
}
return res;
}
bool TimelineModel::requestSubtitleMove(int clipId, int position, bool updateView, bool logUndo, bool invalidateTimeline, Fun &undo, Fun &redo)
bool TimelineModel::requestSubtitleMove(int clipId, int position, bool updateView, bool first, bool last, bool invalidateTimeline, Fun &undo, Fun &redo)
{
QWriteLocker locker(&m_lock);
GenTime oldPos = m_allSubtitles.at(clipId);
GenTime newPos(position, pCore->getCurrentFps());
Fun local_redo = [this, clipId, newPos, logUndo, updateView]() {
return m_subtitleModel->moveSubtitle(clipId, newPos, logUndo, updateView);
Fun local_redo = [this, clipId, newPos, last, updateView]() {
return m_subtitleModel->moveSubtitle(clipId, newPos, last, updateView);
};
Fun local_undo = [this, oldPos, clipId, logUndo, updateView]() {
return m_subtitleModel->moveSubtitle(clipId, oldPos, logUndo, updateView);
Fun local_undo = [this, oldPos, clipId, first, updateView]() {
return m_subtitleModel->moveSubtitle(clipId, oldPos, first, updateView);
};
bool res = local_redo();
if (res) {
......@@ -1838,7 +1838,7 @@ bool TimelineModel::requestItemDeletion(int itemId, Fun &undo, Fun &redo, bool l
return requestCompositionDeletion(itemId, undo, redo);
}
if (isSubTitle(itemId)) {
return requestSubtitleDeletion(itemId, undo, redo);
return requestSubtitleDeletion(itemId, undo, redo, true, true);
}
Q_ASSERT(false);
return false;
......@@ -1904,18 +1904,18 @@ bool TimelineModel::requestClipDeletion(int clipId, Fun &undo, Fun &redo)
return false;
}
bool TimelineModel::requestSubtitleDeletion(int clipId, Fun &undo, Fun &redo)
bool TimelineModel::requestSubtitleDeletion(int clipId, Fun &undo, Fun &redo, bool first, bool last)
{
GenTime startTime = m_allSubtitles.at(clipId);
SubtitledTime sub = m_subtitleModel->getSubtitle(startTime);
Fun operation = [this, clipId] () {
return m_subtitleModel->removeSubtitle(clipId);
Fun operation = [this, clipId, last] () {
return m_subtitleModel->removeSubtitle(clipId, false, last);
};
GenTime start = sub.start();
GenTime end = sub.end();
QString text = sub.subtitle();
Fun reverse = [this, clipId, start, end, text]() {
return m_subtitleModel->addSubtitle(clipId, start, end, text);
Fun reverse = [this, clipId, start, end, text, first]() {
return m_subtitleModel->addSubtitle(clipId, start, end, text, false, first);
};
if (operation()) {
UPDATE_UNDO_REDO(operation, reverse, undo, redo);
......@@ -2229,12 +2229,12 @@ bool TimelineModel::requestGroupMove(int itemId, int groupId, int delta_track, i
}
return true;
};
Fun redo_subs = []() { return true; };
Fun undo_subs = []() { return true; };
// Move subtitles
if (!sorted_subtitles.empty()) {
for (auto &item : sorted_subtitles) {
requestSubtitleMove(item.first, item.second.frames(pCore->getCurrentFps()) + delta_pos, updateView, finalMove, finalMove, local_undo, local_redo);
std::vector<std::pair<int, GenTime>>::iterator ptr;
std::vector<std::pair<int, GenTime>>::iterator last = std::prev(sorted_subtitles.end());
for (ptr = sorted_subtitles.begin(); ptr < sorted_subtitles.end(); ptr++) {
requestSubtitleMove((*ptr).first, (*ptr).second.frames(pCore->getCurrentFps()) + delta_pos, updateView, ptr == sorted_subtitles.begin(), ptr == last, finalMove, local_undo, local_redo);
}
}
......@@ -2598,7 +2598,7 @@ bool TimelineModel::requestGroupDeletion(int clipId, Fun &undo, Fun &redo)
}
std::set<int>::reverse_iterator rit;
for (rit = all_subtitles.rbegin(); rit != all_subtitles.rend(); ++rit) {
bool res = requestSubtitleDeletion(*rit, undo, redo);
bool res = requestSubtitleDeletion(*rit, undo, redo, rit == all_subtitles.rbegin(), rit == std::prev(all_subtitles.rend()));
if (!res) {
undo();
return false;
......
......@@ -366,7 +366,7 @@ public:
*/
Q_INVOKABLE bool requestClipMove(int clipId, int trackId, int position, bool moveMirrorTracks = true, bool updateView = true, bool logUndo = true, bool invalidateTimeline = false);
Q_INVOKABLE bool requestSubtitleMove(int clipId, int position, bool updateView = true, bool logUndo = true, bool invalidateTimeline = false);
bool requestSubtitleMove(int clipId, int position, bool updateView, bool logUndo, bool invalidateTimeline, Fun &undo, Fun &redo);
bool requestSubtitleMove(int clipId, int position, bool updateView, bool first, bool last, bool invalidateTimeline, Fun &undo, Fun &redo);
bool cutSubtitle(int position, Fun &undo, Fun &redo);
bool requestClipMix(std::pair<int, int> clipIds, int trackId, int position, bool updateView, bool invalidateTimeline, bool finalMove, Fun &undo, Fun &redo, bool groupMove);
......@@ -786,7 +786,7 @@ protected:
/* Internal functions to delete a clip or a composition. In general, you should call requestItemDeletion */
bool requestClipDeletion(int clipId, Fun &undo, Fun &redo);
bool requestCompositionDeletion(int compositionId, Fun &undo, Fun &redo);
bool requestSubtitleDeletion(int clipId, Fun &undo, Fun &redo);
bool requestSubtitleDeletion(int clipId, Fun &undo, Fun &redo, bool first, bool last);
/** @brief Check tracks duration and update black track accordingly */
void updateDuration();
......
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