Subtitles: when cutting from subtitle widget, split text at cursor position

parent 6e639f6a
......@@ -396,12 +396,12 @@ bool SubtitleModel::setText(int id, const QString text)
QString oldText = m_subtitleList.at(start).first;
m_subtitleList[start].first = text;
Fun local_redo = [this, start, end, text]() {
editSubtitle(start, text, end);
editSubtitle(start, text);
pCore->refreshProjectRange({start.frames(pCore->getCurrentFps()), end.frames(pCore->getCurrentFps())});
return true;
};
Fun local_undo = [this, start, end, oldText]() {
editSubtitle(start, oldText, end);
editSubtitle(start, oldText);
pCore->refreshProjectRange({start.frames(pCore->getCurrentFps()), end.frames(pCore->getCurrentFps())});
return true;
};
......@@ -641,19 +641,14 @@ bool SubtitleModel::requestResize(int id, int size, bool right, Fun &undo, Fun &
return true;
}
void SubtitleModel::editSubtitle(GenTime startPos, QString newSubtitleText, GenTime endPos)
void SubtitleModel::editSubtitle(GenTime startPos, QString newSubtitleText)
{
if(startPos.frames(pCore->getCurrentFps()) < 0 || endPos.frames(pCore->getCurrentFps()) < 0) {
if(startPos.frames(pCore->getCurrentFps()) < 0) {
qDebug()<<"Time error: is negative";
return;
}
if(startPos.frames(pCore->getCurrentFps()) > endPos.frames(pCore->getCurrentFps())) {
qDebug()<<"Time error: start should be less than end";
return;
}
qDebug()<<"Editing existing subtitle in model";
m_subtitleList[startPos].first = newSubtitleText ;
m_subtitleList[startPos].second = endPos;
int id = getIdForStartPos(startPos);
qDebug()<<startPos.frames(pCore->getCurrentFps())<<m_subtitleList[startPos].first<<m_subtitleList[startPos].second.frames(pCore->getCurrentFps());
int row = m_timeline->getSubtitleIndex(id);
......
......@@ -83,12 +83,11 @@ public:
bool requestResize(int id, int size, bool right);
bool requestResize(int id, int size, bool right, Fun &undo, Fun &redo, bool logUndo);
/** @brief Edit subtitle , i.e. text and/or end time
/** @brief Edit subtitle text
@param startPos is start timing position of subtitles
@param newSubtitleText is (new) subtitle text
@param endPos defines the (new) position of the end time
*/
void editSubtitle(GenTime startPos, QString newSubtitleText, GenTime endPos);
void editSubtitle(GenTime startPos, QString newSubtitleText);
/** @brief Remove subtitle at start position (pos) */
bool removeSubtitle(int id, bool temporary = false);
......
......@@ -193,6 +193,11 @@ void Core::initGUI(const QUrl &Url, const QString &clipsToLoad)
m_mainWindow->getCurrentTimeline()->controller()->addSubtitle();
}
});
connect(m_subtitleWidget, &SubtitleEdit::cutSubtitle, [this](int id, int cursorPos) {
if (m_guiConstructed && m_mainWindow->getCurrentTimeline()->controller()) {
m_mainWindow->getCurrentTimeline()->controller()->cutSubtitle(id, cursorPos);
}
});
// Producer queue, creating MLT::Producers on request
/*
......
......@@ -61,12 +61,16 @@ SubtitleEdit::SubtitleEdit(QWidget *parent)
setupUi(this);
buttonApply->setIcon(QIcon::fromTheme(QStringLiteral("dialog-ok-apply")));
buttonAdd->setIcon(QIcon::fromTheme(QStringLiteral("list-add")));
buttonCut->setIcon(QIcon::fromTheme(QStringLiteral("edit-cut")));
auto *keyFilter = new ShiftEnterFilter(this);
subText->installEventFilter(keyFilter);
connect(keyFilter, &ShiftEnterFilter::triggerUpdate, this, &SubtitleEdit::updateSubtitle);
connect(subText, &QPlainTextEdit::textChanged, [this]() {
buttonApply->setEnabled(true);
});
connect(subText, &QPlainTextEdit::cursorPositionChanged, [this]() {
buttonCut->setEnabled(true);
});
m_position = new TimecodeDisplay(pCore->timecode(), this);
m_endPosition = new TimecodeDisplay(pCore->timecode(), this);
......@@ -99,6 +103,14 @@ SubtitleEdit::SubtitleEdit(QWidget *parent)
m_model->requestResize(m_activeSub, value, true);
});
connect(buttonAdd, &QToolButton::clicked, this, &SubtitleEdit::addSubtitle);
connect(buttonCut, &QToolButton::clicked, [this]() {
qDebug()<<"=== READY TO CUT SUB";
if (m_activeSub > -1 && subText->hasFocus()) {
int pos = subText->textCursor().position();
qDebug()<<"=== READY TO CUT SUB AT : "<<pos;
emit cutSubtitle(m_activeSub, pos);
}
});
connect(buttonApply, &QToolButton::clicked, this, &SubtitleEdit::updateSubtitle);
connect(buttonPrev, &QToolButton::clicked, this, &SubtitleEdit::goToPrevious);
connect(buttonNext, &QToolButton::clicked, this, &SubtitleEdit::goToNext);
......@@ -110,6 +122,7 @@ void SubtitleEdit::setModel(std::shared_ptr<SubtitleModel> model)
m_activeSub = -1;
subText->setEnabled(false);
buttonApply->setEnabled(false);
buttonCut->setEnabled(false);
if (m_model == nullptr) {
QSignalBlocker bk(subText);
subText->clear();
......@@ -134,9 +147,10 @@ void SubtitleEdit::updateSubtitle()
void SubtitleEdit::setActiveSubtitle(int id)
{
m_activeSub = id;
buttonApply->setEnabled(false);
buttonCut->setEnabled(false);
if (m_model && id > -1) {
subText->setEnabled(true);
buttonApply->setEnabled(false);
QSignalBlocker bk(subText);
m_position->setEnabled(true);
m_endPosition->setEnabled(true);
......@@ -153,7 +167,6 @@ void SubtitleEdit::setActiveSubtitle(int id)
m_duration->setValue(duration);
} else {
subText->setEnabled(false);
buttonApply->setEnabled(false);
m_position->setEnabled(false);
m_endPosition->setEnabled(false);
m_duration->setEnabled(false);
......
......@@ -75,6 +75,7 @@ private:
signals:
void addSubtitle();
void cutSubtitle(int id, int cursorPos);
};
#endif
......@@ -1449,6 +1449,46 @@ void TimelineController::cutClipUnderCursor(int position, int track)
}
}
void TimelineController::cutSubtitle(int id, int cursorPos)
{
qDebug()<<"== READY TO CUT AT: "<<cursorPos;
Q_ASSERT(m_model->isSubTitle(id));
if (cursorPos <= 0) {
return requestClipCut(id, -1);
}
// Cut subtitle at edit position
int timelinePos = pCore->getTimelinePosition();
GenTime position(timelinePos, pCore->getCurrentFps());
GenTime start = m_model->m_allSubtitles.at(id);
auto subtitleModel = pCore->getSubtitleModel();
SubtitledTime subData = subtitleModel->getSubtitle(start);
if (position > start && position < subData.end()) {
QString originalText = subData.subtitle();
QString firstText = originalText;
QString secondText = originalText;
firstText.truncate(cursorPos);
secondText.remove(0, originalText.length() - cursorPos);
Fun undo = []() { return true; };
Fun redo = []() { return true; };
bool res = subtitleModel->cutSubtitle(timelinePos, undo, redo);
qDebug()<<"== DO CUT SUCCESS: "<<res;
if (res) {
Fun local_redo = [subtitleModel, start, position, firstText, secondText]() {
subtitleModel->editSubtitle(start, firstText);
subtitleModel->editSubtitle(position, secondText);
return true;
};
Fun local_undo = [subtitleModel, start, originalText]() {
subtitleModel->editSubtitle(start, originalText);
return true;
};
local_redo();
UPDATE_UNDO_REDO_NOLOCK(local_redo, local_undo, undo, redo);
pCore->pushUndo(undo, redo, i18n("Cut clip"));
}
}
}
void TimelineController::cutAllClipsUnderCursor(int position)
{
if (position == -1) {
......@@ -3753,12 +3793,12 @@ void TimelineController::editSubtitle(int startFrame, int endFrame, QString newT
}
auto subtitleModel = pCore->getSubtitleModel();
Fun local_redo = [subtitleModel, startFrame, endFrame, newText]() {
subtitleModel->editSubtitle(GenTime(startFrame, pCore->getCurrentFps()), newText, GenTime(endFrame, pCore->getCurrentFps()));
subtitleModel->editSubtitle(GenTime(startFrame, pCore->getCurrentFps()), newText);
pCore->refreshProjectRange({startFrame, endFrame});
return true;
};
Fun local_undo = [subtitleModel, startFrame, endFrame, oldText]() {
subtitleModel->editSubtitle(GenTime(startFrame, pCore->getCurrentFps()), oldText, GenTime(endFrame, pCore->getCurrentFps()));
subtitleModel->editSubtitle(GenTime(startFrame, pCore->getCurrentFps()), oldText);
pCore->refreshProjectRange({startFrame, endFrame});
return true;
};
......
......@@ -572,6 +572,8 @@ public:
Q_INVOKABLE void resizeSubtitle(int startFrame, int endFrame, int oldEndFrame, bool refreshModel);
/** @brief Add subtitle clip at cursor's position in timeline */
Q_INVOKABLE void addSubtitle(int startframe = -1);
/** @brief Cut a subtitle and split the text at @param pos */
void cutSubtitle(int id, int cursorPos);
/** @brief Delete subtitle clip with frame as start position*/
Q_INVOKABLE void deleteSubtitle(int frameframe, int endframe, QString Ctext);
/** @brief Import a subtitle file*/
......
......@@ -74,6 +74,13 @@
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="buttonCut">
<property name="text">
<string>...</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="buttonApply">
<property name="text">
......
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