Spreadsheet: Make undo operation handle multiple cells at a time

Instead of having the undo operation only be able to undo one cell
for a given undo, make it able to handle multiple cells at a time.
 Please enter the commit message for your changes. Lines starting
This commit is contained in:
martinfalisse 2022-04-07 23:04:36 +02:00 committed by Ali Mohammad Pur
parent 7bd0ebb1ab
commit 22575c9370
Notes: sideshowbarker 2024-07-17 11:51:16 +09:00
7 changed files with 59 additions and 31 deletions

View file

@ -191,21 +191,4 @@ void Cell::copy_from(Cell const& other)
m_thrown_value = other.m_thrown_value;
}
CellUndoCommand::CellUndoCommand(Cell& cell, String const& previous_data)
: m_cell(cell)
, m_current_data(cell.data())
, m_previous_data(previous_data)
{
}
void CellUndoCommand::undo()
{
m_cell.set_data(m_previous_data);
}
void CellUndoCommand::redo()
{
m_cell.set_data(m_current_data);
}
}

View file

@ -121,17 +121,4 @@ private:
Format m_evaluated_formats;
};
class CellUndoCommand : public GUI::Command {
public:
CellUndoCommand(Cell&, String const&);
virtual void undo() override;
virtual void redo() override;
private:
Cell& m_cell;
String m_current_data;
String m_previous_data;
};
}

View file

@ -751,4 +751,11 @@ URL Position::to_url(Sheet const& sheet) const
return url;
}
CellChange::CellChange(Cell& cell, String const& previous_data)
: m_cell(cell)
, m_previous_data(previous_data)
{
m_new_data = cell.data();
}
}

View file

@ -22,6 +22,20 @@
namespace Spreadsheet {
class CellChange {
public:
CellChange(Cell&, String const&);
auto& cell() { return m_cell; }
auto& previous_data() { return m_previous_data; }
auto& new_data() { return m_new_data; }
private:
Cell& m_cell;
String m_previous_data;
String m_new_data;
};
class Sheet : public Core::Object {
C_OBJECT(Sheet);

View file

@ -165,4 +165,29 @@ void SheetModel::update()
m_sheet->update();
did_update(UpdateFlag::DontInvalidateIndices);
}
CellsUndoCommand::CellsUndoCommand(Vector<CellChange> cell_changes)
{
m_cell_changes = cell_changes;
}
CellsUndoCommand::CellsUndoCommand(Cell& cell, String const& previous_data)
{
m_cell_changes.append(CellChange(cell, previous_data));
}
void CellsUndoCommand::undo()
{
for (size_t i = 0; i < m_cell_changes.size(); ++i) {
m_cell_changes[i].cell().set_data(m_cell_changes[i].previous_data());
}
}
void CellsUndoCommand::redo()
{
for (size_t i = 0; i < m_cell_changes.size(); ++i) {
m_cell_changes[i].cell().set_data(m_cell_changes[i].new_data());
}
}
}

View file

@ -40,4 +40,16 @@ private:
NonnullRefPtr<Sheet> m_sheet;
};
class CellsUndoCommand : public GUI::Command {
public:
CellsUndoCommand(Cell&, String const&);
CellsUndoCommand(Vector<CellChange>);
virtual void undo() override;
virtual void redo() override;
private:
Vector<CellChange> m_cell_changes;
};
}

View file

@ -281,7 +281,7 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
for (auto& sheet : new_sheets) {
auto& new_view = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet);
new_view.model()->on_cell_data_change = [&](auto& cell, auto& previous_data) {
undo_stack().push(make<CellUndoCommand>(cell, previous_data));
undo_stack().push(make<CellsUndoCommand>(cell, previous_data));
window()->set_modified(true);
};
new_view.on_selection_changed = [&](Vector<Position>&& selection) {