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

@ -88,16 +88,17 @@ SpreadsheetView::SpreadsheetView(Sheet& sheet)
if (m_table_view->selection().is_empty() && on_selection_dropped)
return on_selection_dropped();
auto selection = m_table_view->selection().first();
Position position { m_sheet->column(selection.column()), (size_t)selection.row() };
auto& cell = m_sheet->ensure(position);
if (on_selection_changed)
on_selection_changed(position, cell);
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());
if (on_selection_changed) {
on_selection_changed(move(selected_positions));
m_table_view->model()->update();
m_table_view->update();
};
};
}
void SpreadsheetView::hide_event(GUI::HideEvent&)
@ -109,10 +110,12 @@ void SpreadsheetView::hide_event(GUI::HideEvent&)
void SpreadsheetView::show_event(GUI::ShowEvent&)
{
if (on_selection_changed && !m_table_view->selection().is_empty()) {
auto selection = m_table_view->selection().first();
Position position { m_sheet->column(selection.column()), (size_t)selection.row() };
auto& cell = m_sheet->ensure(position);
on_selection_changed(position, cell);
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());
on_selection_changed(move(selected_positions));
}
}

View file

@ -43,7 +43,7 @@ public:
const Sheet& sheet() const { 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;
private:

View file

@ -99,13 +99,16 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
m_selected_view->on_selection_dropped = nullptr;
};
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 = [&] {
@ -113,6 +116,27 @@ void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
m_selected_view->sheet().update();
};
m_cell_value_editor->set_enabled(true);
return;
}
// There are many cells selected, change all of them.
StringBuilder builder;
builder.appendf("<%zu>", selection.size());
m_current_cell_label->set_enabled(true);
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->set_text("");
m_cell_value_editor->on_change = [cells = move(cells), this] {
for (auto* cell : cells)
cell->set_data(m_cell_value_editor->text());
m_selected_view->sheet().update();
};
m_cell_value_editor->set_enabled(true);
};
m_selected_view->on_selection_dropped = [&]() {
m_cell_value_editor->set_enabled(false);