summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnotherTest <ali.mpfard@gmail.com>2020-08-24 19:51:25 +0430
committerAndreas Kling <kling@serenityos.org>2020-08-24 18:21:33 +0200
commit12cf3e13c0fd7a1b68251d605d5a29d1e77e2526 (patch)
tree232a490706211cb78a844f9b47fe2e68be7dad14
parenta6ebd29aa5c95712c964ff0301398f004d6d2011 (diff)
downloadserenity-12cf3e13c0fd7a1b68251d605d5a29d1e77e2526.zip
Spreadsheet: Add a topbar with a text editor
-rw-r--r--Applications/Spreadsheet/Spreadsheet.cpp1
-rw-r--r--Applications/Spreadsheet/SpreadsheetView.cpp14
-rw-r--r--Applications/Spreadsheet/SpreadsheetView.h4
-rw-r--r--Applications/Spreadsheet/SpreadsheetWidget.cpp56
-rw-r--r--Applications/Spreadsheet/SpreadsheetWidget.h1
5 files changed, 74 insertions, 2 deletions
diff --git a/Applications/Spreadsheet/Spreadsheet.cpp b/Applications/Spreadsheet/Spreadsheet.cpp
index 40c92ac341..e38c34564b 100644
--- a/Applications/Spreadsheet/Spreadsheet.cpp
+++ b/Applications/Spreadsheet/Spreadsheet.cpp
@@ -208,6 +208,7 @@ JS::Value Sheet::evaluate(const StringView& source, Cell* on_behalf_of)
void Cell::update_data()
{
+ dbg() << "Update cell " << this << ", dirty=" << dirty;
TemporaryChange cell_change { sheet->current_evaluated_cell(), this };
if (!dirty)
return;
diff --git a/Applications/Spreadsheet/SpreadsheetView.cpp b/Applications/Spreadsheet/SpreadsheetView.cpp
index 42ac1422a7..6c04212723 100644
--- a/Applications/Spreadsheet/SpreadsheetView.cpp
+++ b/Applications/Spreadsheet/SpreadsheetView.cpp
@@ -71,6 +71,20 @@ SpreadsheetView::SpreadsheetView(Sheet& sheet)
m_table_view->aid_create_editing_delegate = [&](auto&) {
return make<EditingDelegate>(*m_sheet);
};
+
+ m_table_view->on_selection_change = [&] {
+ 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() - 1), (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();
+ };
}
void SpreadsheetView::TableCellPainter::paint(GUI::Painter& painter, const Gfx::IntRect& rect, const Gfx::Palette& palette, const GUI::ModelIndex& index)
diff --git a/Applications/Spreadsheet/SpreadsheetView.h b/Applications/Spreadsheet/SpreadsheetView.h
index eecfe67bb3..88192d5961 100644
--- a/Applications/Spreadsheet/SpreadsheetView.h
+++ b/Applications/Spreadsheet/SpreadsheetView.h
@@ -41,6 +41,10 @@ public:
~SpreadsheetView();
const Sheet& sheet() const { return *m_sheet; }
+ Sheet& sheet() { return *m_sheet; }
+
+ Function<void(const Position&, Cell&)> on_selection_changed;
+ Function<void()> on_selection_dropped;
private:
SpreadsheetView(Sheet&);
diff --git a/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Applications/Spreadsheet/SpreadsheetWidget.cpp
index da1cc3bae1..64759054e0 100644
--- a/Applications/Spreadsheet/SpreadsheetWidget.cpp
+++ b/Applications/Spreadsheet/SpreadsheetWidget.cpp
@@ -30,8 +30,11 @@
#include <AK/JsonObjectSerializer.h>
#include <LibCore/File.h>
#include <LibGUI/BoxLayout.h>
+#include <LibGUI/Label.h>
#include <LibGUI/MessageBox.h>
+#include <LibGUI/Splitter.h>
#include <LibGUI/TabWidget.h>
+#include <LibGUI/TextEditor.h>
#include <string.h>
namespace Spreadsheet {
@@ -40,11 +43,60 @@ SpreadsheetWidget::SpreadsheetWidget()
{
set_fill_with_background_color(true);
set_layout<GUI::VerticalBoxLayout>().set_margins({ 2, 2, 2, 2 });
- m_tab_widget = add<GUI::TabWidget>();
+ auto& container = add<GUI::VerticalSplitter>();
+
+ auto& top_bar = container.add<GUI::Frame>();
+ top_bar.set_layout<GUI::HorizontalBoxLayout>().set_spacing(1);
+ top_bar.set_preferred_size(0, 50);
+ top_bar.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
+ auto& current_cell_label = top_bar.add<GUI::Label>("");
+ current_cell_label.set_preferred_size(50, 0);
+ current_cell_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
+ auto& cell_value_editor = top_bar.add<GUI::TextEditor>(GUI::TextEditor::Type::SingleLine);
+ cell_value_editor.set_scrollbars_enabled(false);
+
+ cell_value_editor.set_enabled(false);
+ current_cell_label.set_enabled(false);
+
+ m_tab_widget = container.add<GUI::TabWidget>();
m_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
m_sheets.append(Sheet::construct("Sheet 1"));
- m_tab_widget->add_tab<SpreadsheetView>(m_sheets.first().name(), m_sheets.first());
+ auto& tab = m_tab_widget->add_tab<SpreadsheetView>(m_sheets.first().name(), m_sheets.first());
+
+ auto change = [&](auto& selected_widget) {
+ if (m_selected_view) {
+ m_selected_view->on_selection_changed = nullptr;
+ 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) {
+ StringBuilder builder;
+ builder.append(position.column);
+ builder.appendf("%zu", position.row);
+ current_cell_label.set_enabled(true);
+ current_cell_label.set_text(builder.string_view());
+
+ cell_value_editor.on_change = nullptr;
+ cell_value_editor.set_text(cell.source());
+ cell_value_editor.on_change = [&] {
+ cell.set_data(cell_value_editor.text());
+ m_selected_view->sheet().update();
+ };
+ cell_value_editor.set_enabled(true);
+ };
+ m_selected_view->on_selection_dropped = [&]() {
+ cell_value_editor.set_enabled(false);
+ cell_value_editor.set_text("");
+ current_cell_label.set_enabled(false);
+ current_cell_label.set_text("");
+ };
+ };
+
+ change(tab);
+ m_tab_widget->on_change = [change = move(change)](auto& selected_widget) {
+ change(selected_widget);
+ };
}
SpreadsheetWidget::~SpreadsheetWidget()
diff --git a/Applications/Spreadsheet/SpreadsheetWidget.h b/Applications/Spreadsheet/SpreadsheetWidget.h
index aa14c0a8e2..22c2730883 100644
--- a/Applications/Spreadsheet/SpreadsheetWidget.h
+++ b/Applications/Spreadsheet/SpreadsheetWidget.h
@@ -44,6 +44,7 @@ private:
SpreadsheetWidget();
NonnullRefPtrVector<Sheet> m_sheets;
+ SpreadsheetView* m_selected_view { nullptr };
RefPtr<GUI::TabWidget> m_tab_widget;
};