Extract adjoin lines logic from SubtitlesGrid

This commit is contained in:
Thomas Goyne 2013-06-14 09:26:19 -07:00
parent e0fcfef32e
commit bda127144d
3 changed files with 31 additions and 49 deletions

View file

@ -69,7 +69,7 @@ namespace {
CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) {
SubtitleSelection sel = c->selectionController->GetSelectedSet();
if (sel.size() < 2) return false;
if (sel.size() < 2) return !sel.empty();
size_t found = 0;
for (auto diag : c->ass->Line | agi::of_type<AssDialogue>()) {
@ -87,6 +87,34 @@ namespace {
/// @defgroup cmd-time Time manipulation commands.
/// @{
static void adjoin_lines(agi::Context *c, bool set_start) {
auto sel = c->selectionController->GetSelectedSet();
AssDialogue *prev = nullptr;
size_t seen = 0;
bool prev_sel = false;
for (auto diag : c->ass->Line | agi::of_type<AssDialogue>()) {
bool cur_sel = !!sel.count(diag);
if (prev) {
// One row selections act as if the previous or next line was selected
if (set_start && cur_sel && (sel.size() == 1 || prev_sel))
diag->Start = prev->End;
else if (!set_start && prev_sel && (cur_sel || sel.size() == 1))
prev->End = diag->Start;
}
if (seen == sel.size())
break;
if (cur_sel)
++seen;
prev = diag;
prev_sel = cur_sel;
}
c->ass->Commit(_("adjoin"), AssFile::COMMIT_DIAG_TIME);
}
/// Changes times of subs so end times begin on next's start time.
struct time_continuous_end : public validate_adjoinable {
CMD_NAME("time/continuous/end")
@ -95,12 +123,10 @@ struct time_continuous_end : public validate_adjoinable {
STR_HELP("Changes times of subs so end times begin on next's start time")
void operator()(agi::Context *c) {
wxArrayInt sels = c->subsGrid->GetSelection();
c->subsGrid->AdjoinLines(sels.front(), sels.back(), false);
adjoin_lines(c, false);
}
};
/// Changes times of subs so start times begin on previous's end time.
struct time_continuous_start : public validate_adjoinable {
CMD_NAME("time/continuous/start")
@ -109,10 +135,8 @@ struct time_continuous_start : public validate_adjoinable {
STR_HELP("Changes times of subs so start times begin on previous's end time")
void operator()(agi::Context *c) {
wxArrayInt sels = c->subsGrid->GetSelection();
c->subsGrid->AdjoinLines(sels.front(), sels.back(), true);
adjoin_lines(c, true);
}
};

View file

@ -151,39 +151,3 @@ void SubtitlesGrid::RecombineLines() {
context->ass->Commit(_("combining"), AssFile::COMMIT_DIAG_ADDREM | AssFile::COMMIT_DIAG_FULL);
}
void SubtitlesGrid::AdjoinLines(int n1,int n2,bool setStart) {
if (n1 == n2) {
if (setStart) {
--n1;
}
else {
++n2;
}
}
// Set start
if (setStart) {
AssDialogue *prev = GetDialogue(n1);
AssDialogue *cur;
for (int i=n1+1;i<=n2;i++) {
cur = GetDialogue(i);
if (!cur) return;
cur->Start = prev->End;
prev = cur;
}
}
// Set end
else {
AssDialogue *next;
AssDialogue *cur = GetDialogue(n1);
for (int i=n1;i<n2;i++) {
next = GetDialogue(i+1);
if (!next) return;
cur->End = next->Start;
cur = next;
}
}
context->ass->Commit(_("adjoin"), AssFile::COMMIT_DIAG_TIME);
}

View file

@ -38,11 +38,5 @@ class SubtitlesGrid: public BaseGrid {
public:
SubtitlesGrid(wxWindow *parent, agi::Context *context);
/// @brief Adjoins selected lines, setting each line's start time to the previous line's end time
/// @param n1 First line to adjoin
/// @param n2 Last line to adjoin
/// @param setStart Set the start times (rather than end times)
void AdjoinLines(int first,int last,bool setStart);
void RecombineLines();
};