1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:57:35 +00:00

Spreadsheet: Add support for changing multiple cells at once

Just select many cells and use the cell editor! so easy!
This commit is contained in:
AnotherTest 2020-08-27 11:56:26 +04:30 committed by Andreas Kling
parent 7465c51ef2
commit a5a3e5a178
3 changed files with 47 additions and 20 deletions

View file

@ -81,22 +81,23 @@ SpreadsheetView::SpreadsheetView(Sheet& sheet)
m_table_view->on_selection_change = [&] { m_table_view->on_selection_change = [&] {
m_sheet->selected_cells().clear(); m_sheet->selected_cells().clear();
for (auto& index : m_table_view->selection().indexes()) { for (auto& index : m_table_view->selection().indexes()) {
Position position {m_sheet->column(index.column()), (size_t)index.row()}; Position position { m_sheet->column(index.column()), (size_t)index.row() };
m_sheet->selected_cells().set(position); m_sheet->selected_cells().set(position);
} }
if (m_table_view->selection().is_empty() && on_selection_dropped) if (m_table_view->selection().is_empty() && on_selection_dropped)
return on_selection_dropped(); return on_selection_dropped();
auto selection = m_table_view->selection().first(); Vector<Position> selected_positions;
selected_positions.ensure_capacity(m_table_view->selection().size());
for (auto& selection : m_table_view->selection().indexes())
selected_positions.empend(m_sheet->column(selection.column()), (size_t)selection.row());
Position position { m_sheet->column(selection.column()), (size_t)selection.row() }; if (on_selection_changed) {
auto& cell = m_sheet->ensure(position); on_selection_changed(move(selected_positions));
if (on_selection_changed) m_table_view->model()->update();
on_selection_changed(position, cell); m_table_view->update();
};
m_table_view->model()->update();
m_table_view->update();
}; };
} }
@ -109,10 +110,12 @@ void SpreadsheetView::hide_event(GUI::HideEvent&)
void SpreadsheetView::show_event(GUI::ShowEvent&) void SpreadsheetView::show_event(GUI::ShowEvent&)
{ {
if (on_selection_changed && !m_table_view->selection().is_empty()) { if (on_selection_changed && !m_table_view->selection().is_empty()) {
auto selection = m_table_view->selection().first(); Vector<Position> selected_positions;
Position position { m_sheet->column(selection.column()), (size_t)selection.row() }; selected_positions.ensure_capacity(m_table_view->selection().size());
auto& cell = m_sheet->ensure(position); for (auto& selection : m_table_view->selection().indexes())
on_selection_changed(position, cell); selected_positions.empend(m_sheet->column(selection.column()), (size_t)selection.row());
on_selection_changed(move(selected_positions));
} }
} }

View file

@ -43,7 +43,7 @@ public:
const Sheet& sheet() const { return *m_sheet; } const Sheet& sheet() const { return *m_sheet; }
Sheet& sheet() { return *m_sheet; } Sheet& sheet() { return *m_sheet; }
Function<void(const Position&, Cell&)> on_selection_changed; Function<void(Vector<Position>&&)> on_selection_changed;
Function<void()> on_selection_dropped; Function<void()> on_selection_dropped;
private: private:

View file

@ -99,17 +99,41 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
m_selected_view->on_selection_dropped = nullptr; m_selected_view->on_selection_dropped = nullptr;
}; };
m_selected_view = &static_cast<SpreadsheetView&>(selected_widget); m_selected_view = &static_cast<SpreadsheetView&>(selected_widget);
m_selected_view->on_selection_changed = [&](const Position& position, Cell& cell) { m_selected_view->on_selection_changed = [&](Vector<Position>&& selection) {
if (selection.size() == 1) {
auto& position = selection.first();
StringBuilder builder;
builder.append(position.column);
builder.appendf("%zu", position.row);
m_current_cell_label->set_enabled(true);
m_current_cell_label->set_text(builder.string_view());
auto& cell = m_selected_view->sheet().ensure(position);
m_cell_value_editor->on_change = nullptr;
m_cell_value_editor->set_text(cell.source());
m_cell_value_editor->on_change = [&] {
cell.set_data(m_cell_value_editor->text());
m_selected_view->sheet().update();
};
m_cell_value_editor->set_enabled(true);
return;
}
// There are many cells selected, change all of them.
StringBuilder builder; StringBuilder builder;
builder.append(position.column); builder.appendf("<%zu>", selection.size());
builder.appendf("%zu", position.row);
m_current_cell_label->set_enabled(true); m_current_cell_label->set_enabled(true);
m_current_cell_label->set_text(builder.string_view()); m_current_cell_label->set_text(builder.string_view());
Vector<Cell*> cells;
for (auto& position : selection)
cells.append(&m_selected_view->sheet().ensure(position));
m_cell_value_editor->on_change = nullptr; m_cell_value_editor->on_change = nullptr;
m_cell_value_editor->set_text(cell.source()); m_cell_value_editor->set_text("");
m_cell_value_editor->on_change = [&] { m_cell_value_editor->on_change = [cells = move(cells), this] {
cell.set_data(m_cell_value_editor->text()); for (auto* cell : cells)
cell->set_data(m_cell_value_editor->text());
m_selected_view->sheet().update(); m_selected_view->sheet().update();
}; };
m_cell_value_editor->set_enabled(true); m_cell_value_editor->set_enabled(true);