diff options
-rw-r--r-- | Userland/Applications/Spreadsheet/Cell.cpp | 17 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/Cell.h | 13 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/Spreadsheet.cpp | 7 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/Spreadsheet.h | 14 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/SpreadsheetModel.cpp | 25 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/SpreadsheetModel.h | 12 | ||||
-rw-r--r-- | Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp | 2 |
7 files changed, 59 insertions, 31 deletions
diff --git a/Userland/Applications/Spreadsheet/Cell.cpp b/Userland/Applications/Spreadsheet/Cell.cpp index e9c41fdf61..ae49b1b1a5 100644 --- a/Userland/Applications/Spreadsheet/Cell.cpp +++ b/Userland/Applications/Spreadsheet/Cell.cpp @@ -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); -} - } diff --git a/Userland/Applications/Spreadsheet/Cell.h b/Userland/Applications/Spreadsheet/Cell.h index 6159b9ec2d..8395b25710 100644 --- a/Userland/Applications/Spreadsheet/Cell.h +++ b/Userland/Applications/Spreadsheet/Cell.h @@ -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; -}; - } diff --git a/Userland/Applications/Spreadsheet/Spreadsheet.cpp b/Userland/Applications/Spreadsheet/Spreadsheet.cpp index c8cee6ef0e..bf474d370f 100644 --- a/Userland/Applications/Spreadsheet/Spreadsheet.cpp +++ b/Userland/Applications/Spreadsheet/Spreadsheet.cpp @@ -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(); +} + } diff --git a/Userland/Applications/Spreadsheet/Spreadsheet.h b/Userland/Applications/Spreadsheet/Spreadsheet.h index 6aa5b0e3db..9bab004df0 100644 --- a/Userland/Applications/Spreadsheet/Spreadsheet.h +++ b/Userland/Applications/Spreadsheet/Spreadsheet.h @@ -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); diff --git a/Userland/Applications/Spreadsheet/SpreadsheetModel.cpp b/Userland/Applications/Spreadsheet/SpreadsheetModel.cpp index b190788b7a..e643f0cf95 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetModel.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetModel.cpp @@ -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()); + } +} + } diff --git a/Userland/Applications/Spreadsheet/SpreadsheetModel.h b/Userland/Applications/Spreadsheet/SpreadsheetModel.h index 5c6dcb36a5..708097e5df 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetModel.h +++ b/Userland/Applications/Spreadsheet/SpreadsheetModel.h @@ -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; +}; + } diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp index 9873868980..bf99c09810 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp @@ -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) { |