Move all selected rows up/down rather than just the active row

Originally committed to SVN as r6288.
This commit is contained in:
Thomas Goyne 2012-01-13 20:19:06 +00:00
parent 7e557c1dad
commit 789f25aff2
1 changed files with 27 additions and 27 deletions

View File

@ -197,59 +197,59 @@ struct grid_tags_simplify : public Command {
}; };
template<class T, class U> template<class T, class U>
static bool move_one(T begin, T end, U value) { static bool move_one(T begin, T end, U const& value) {
T it = find(begin, end, value); size_t move_count = 0;
assert(it != end); T prev = end;
for (; begin != end; ++begin) {
T prev = it; typename U::key_type cur = dynamic_cast<typename U::key_type>(*begin);
++prev; bool in_set = !!value.count(cur);
prev = find_if(prev, end, cast<U>()); if (!in_set && cur)
prev = begin;
if (prev != end) { else if (in_set && prev != end) {
using std::swap; using std::swap;
swap(*it, *prev); swap(*begin, *prev);
return true; prev = begin;
if (++move_count == value.size())
break;
}
} }
return false;
return move_count > 0;
} }
/// Move the active line up one row /// Move the selected lines up one row
struct grid_move_up : public Command { struct grid_move_up : public Command {
CMD_NAME("grid/move/up") CMD_NAME("grid/move/up")
STR_MENU("Move line up") STR_MENU("Move line up")
STR_DISP("Move line up") STR_DISP("Move line up")
STR_HELP("Move the selected line up one row") STR_HELP("Move the selected lines up one row")
CMD_TYPE(COMMAND_VALIDATE) CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) { bool Validate(const agi::Context *c) {
return c->selectionController->GetActiveLine() != 0; return c->selectionController->GetSelectedSet().size() != 0;
} }
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
if (AssDialogue *line = c->selectionController->GetActiveLine()) { if (move_one(c->ass->Line.begin(), c->ass->Line.end(), c->selectionController->GetSelectedSet()))
if (move_one(c->ass->Line.rbegin(), c->ass->Line.rend(), line)) c->ass->Commit(_("move lines"), AssFile::COMMIT_ORDER);
c->ass->Commit(_("move lines"), AssFile::COMMIT_ORDER);
}
} }
}; };
/// Move the active line down one row /// Move the selected lines down one row
struct grid_move_down : public Command { struct grid_move_down : public Command {
CMD_NAME("grid/move/down") CMD_NAME("grid/move/down")
STR_MENU("Move line down") STR_MENU("Move line down")
STR_DISP("Move line down") STR_DISP("Move line down")
STR_HELP("Move the selected line down one row") STR_HELP("Move the selected lines down one row")
CMD_TYPE(COMMAND_VALIDATE) CMD_TYPE(COMMAND_VALIDATE)
bool Validate(const agi::Context *c) { bool Validate(const agi::Context *c) {
return c->selectionController->GetActiveLine() != 0; return c->selectionController->GetSelectedSet().size() != 0;
} }
void operator()(agi::Context *c) { void operator()(agi::Context *c) {
if (AssDialogue *line = c->selectionController->GetActiveLine()) { if (move_one(c->ass->Line.rbegin(), c->ass->Line.rend(), c->selectionController->GetSelectedSet()))
if (move_one(c->ass->Line.begin(), c->ass->Line.end(), line)) c->ass->Commit(_("move lines"), AssFile::COMMIT_ORDER);
c->ass->Commit(_("move lines"), AssFile::COMMIT_ORDER);
}
} }
}; };