summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-08-27 11:56:26 +0430
committerAndreas Kling <kling@serenityos.org>2020-08-27 10:27:20 +0200
commita5a3e5a178be80bc78106a6d407422103d15e3f1 (patch)
tree4cde65e1e08451f4e4f929c4f47aa68462a7feae
parent7465c51ef2208e8bac75f646222cd3495fa602a3 (diff)
downloadserenity-a5a3e5a178be80bc78106a6d407422103d15e3f1.zip
Spreadsheet: Add support for changing multiple cells at once
Just select many cells and use the cell editor! so easy!
-rw-r--r--Applications/Spreadsheet/SpreadsheetView.cpp29
-rw-r--r--Applications/Spreadsheet/SpreadsheetView.h2
-rw-r--r--Applications/Spreadsheet/SpreadsheetWidget.cpp36
3 files changed, 47 insertions, 20 deletions
diff --git a/Applications/Spreadsheet/SpreadsheetView.cpp b/Applications/Spreadsheet/SpreadsheetView.cpp
index ac63e61965..e84c6c3775 100644
--- a/Applications/Spreadsheet/SpreadsheetView.cpp
+++ b/Applications/Spreadsheet/SpreadsheetView.cpp
@@ -81,22 +81,23 @@ SpreadsheetView::SpreadsheetView(Sheet& sheet)
m_table_view->on_selection_change = [&] {
m_sheet->selected_cells().clear();
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);
}
if (m_table_view->selection().is_empty() && 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() };
- auto& cell = m_sheet->ensure(position);
- if (on_selection_changed)
- on_selection_changed(position, cell);
-
- m_table_view->model()->update();
- m_table_view->update();
+ if (on_selection_changed) {
+ on_selection_changed(move(selected_positions));
+ 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&)
{
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));
}
}
diff --git a/Applications/Spreadsheet/SpreadsheetView.h b/Applications/Spreadsheet/SpreadsheetView.h
index 8760cae123..23d27ebfa8 100644
--- a/Applications/Spreadsheet/SpreadsheetView.h
+++ b/Applications/Spreadsheet/SpreadsheetView.h
@@ -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:
diff --git a/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Applications/Spreadsheet/SpreadsheetWidget.cpp
index 7cf6149ee1..c3912e6684 100644
--- a/Applications/Spreadsheet/SpreadsheetWidget.cpp
+++ b/Applications/Spreadsheet/SpreadsheetWidget.cpp
@@ -99,17 +99,41 @@ 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 = [&] {
+ 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;
- builder.append(position.column);
- builder.appendf("%zu", position.row);
+ 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(cell.source());
- m_cell_value_editor->on_change = [&] {
- cell.set_data(m_cell_value_editor->text());
+ 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);