mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 20:47:45 +00:00
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:
parent
7bd0ebb1ab
commit
22575c9370
7 changed files with 59 additions and 31 deletions
|
@ -191,21 +191,4 @@ void Cell::copy_from(Cell const& other)
|
||||||
m_thrown_value = other.m_thrown_value;
|
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,17 +121,4 @@ private:
|
||||||
Format m_evaluated_formats;
|
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;
|
|
||||||
};
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -751,4 +751,11 @@ URL Position::to_url(Sheet const& sheet) const
|
||||||
return url;
|
return url;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CellChange::CellChange(Cell& cell, String const& previous_data)
|
||||||
|
: m_cell(cell)
|
||||||
|
, m_previous_data(previous_data)
|
||||||
|
{
|
||||||
|
m_new_data = cell.data();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,6 +22,20 @@
|
||||||
|
|
||||||
namespace Spreadsheet {
|
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 {
|
class Sheet : public Core::Object {
|
||||||
C_OBJECT(Sheet);
|
C_OBJECT(Sheet);
|
||||||
|
|
||||||
|
|
|
@ -165,4 +165,29 @@ void SheetModel::update()
|
||||||
m_sheet->update();
|
m_sheet->update();
|
||||||
did_update(UpdateFlag::DontInvalidateIndices);
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,4 +40,16 @@ private:
|
||||||
NonnullRefPtr<Sheet> m_sheet;
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -281,7 +281,7 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
|
||||||
for (auto& sheet : new_sheets) {
|
for (auto& sheet : new_sheets) {
|
||||||
auto& new_view = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet);
|
auto& new_view = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet);
|
||||||
new_view.model()->on_cell_data_change = [&](auto& cell, auto& previous_data) {
|
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);
|
window()->set_modified(true);
|
||||||
};
|
};
|
||||||
new_view.on_selection_changed = [&](Vector<Position>&& selection) {
|
new_view.on_selection_changed = [&](Vector<Position>&& selection) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue