SpreadsheetWidget.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "SpreadsheetWidget.h"
  27. #include "CellSyntaxHighlighter.h"
  28. #include "HelpWindow.h"
  29. #include <LibCore/File.h>
  30. #include <LibGUI/BoxLayout.h>
  31. #include <LibGUI/Button.h>
  32. #include <LibGUI/Label.h>
  33. #include <LibGUI/MessageBox.h>
  34. #include <LibGUI/Splitter.h>
  35. #include <LibGUI/TabWidget.h>
  36. #include <LibGUI/TextEditor.h>
  37. #include <string.h>
  38. namespace Spreadsheet {
  39. SpreadsheetWidget::SpreadsheetWidget(NonnullRefPtrVector<Sheet>&& sheets, bool should_add_sheet_if_empty)
  40. : m_workbook(make<Workbook>(move(sheets)))
  41. {
  42. set_fill_with_background_color(true);
  43. set_layout<GUI::VerticalBoxLayout>().set_margins({ 2, 2, 2, 2 });
  44. auto& container = add<GUI::VerticalSplitter>();
  45. auto& top_bar = container.add<GUI::Frame>();
  46. top_bar.set_layout<GUI::HorizontalBoxLayout>().set_spacing(1);
  47. top_bar.set_preferred_size(0, 50);
  48. top_bar.set_size_policy(GUI::SizePolicy::Fill, GUI::SizePolicy::Fixed);
  49. auto& current_cell_label = top_bar.add<GUI::Label>("");
  50. current_cell_label.set_preferred_size(50, 0);
  51. current_cell_label.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fill);
  52. auto& help_button = top_bar.add<GUI::Button>("🛈");
  53. help_button.set_preferred_size(20, 20);
  54. help_button.set_size_policy(GUI::SizePolicy::Fixed, GUI::SizePolicy::Fixed);
  55. help_button.on_click = [&](auto) {
  56. auto docs = m_selected_view->sheet().gather_documentation();
  57. auto help_window = HelpWindow::the();
  58. help_window->set_docs(move(docs));
  59. help_window->show();
  60. };
  61. auto& cell_value_editor = top_bar.add<GUI::TextEditor>(GUI::TextEditor::Type::SingleLine);
  62. cell_value_editor.set_font(Gfx::Font::default_fixed_width_font());
  63. cell_value_editor.set_scrollbars_enabled(false);
  64. cell_value_editor.set_syntax_highlighter(make<CellSyntaxHighlighter>());
  65. cell_value_editor.set_enabled(false);
  66. current_cell_label.set_enabled(false);
  67. m_tab_widget = container.add<GUI::TabWidget>();
  68. m_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  69. m_cell_value_editor = cell_value_editor;
  70. m_current_cell_label = current_cell_label;
  71. if (!m_workbook->has_sheets() && should_add_sheet_if_empty)
  72. m_workbook->add_sheet("Sheet 1");
  73. setup_tabs(m_workbook->sheets());
  74. }
  75. void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
  76. {
  77. RefPtr<GUI::Widget> first_tab_widget;
  78. for (auto& sheet : new_sheets) {
  79. auto& tab = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet);
  80. if (!first_tab_widget)
  81. first_tab_widget = &tab;
  82. }
  83. auto change = [&](auto& selected_widget) {
  84. if (m_selected_view) {
  85. m_selected_view->on_selection_changed = nullptr;
  86. m_selected_view->on_selection_dropped = nullptr;
  87. };
  88. m_selected_view = &static_cast<SpreadsheetView&>(selected_widget);
  89. m_selected_view->on_selection_changed = [&](const Position& position, Cell& cell) {
  90. StringBuilder builder;
  91. builder.append(position.column);
  92. builder.appendf("%zu", position.row);
  93. m_current_cell_label->set_enabled(true);
  94. m_current_cell_label->set_text(builder.string_view());
  95. m_cell_value_editor->on_change = nullptr;
  96. m_cell_value_editor->set_text(cell.source());
  97. m_cell_value_editor->on_change = [&] {
  98. cell.set_data(m_cell_value_editor->text());
  99. m_selected_view->sheet().update();
  100. };
  101. m_cell_value_editor->set_enabled(true);
  102. };
  103. m_selected_view->on_selection_dropped = [&]() {
  104. m_cell_value_editor->set_enabled(false);
  105. m_cell_value_editor->set_text("");
  106. m_current_cell_label->set_enabled(false);
  107. m_current_cell_label->set_text("");
  108. };
  109. };
  110. if (first_tab_widget)
  111. change(*first_tab_widget);
  112. m_tab_widget->on_change = [change = move(change)](auto& selected_widget) {
  113. change(selected_widget);
  114. };
  115. }
  116. void SpreadsheetWidget::save(const StringView& filename)
  117. {
  118. auto result = m_workbook->save(filename);
  119. if (result.is_error())
  120. GUI::MessageBox::show_error(window(), result.error());
  121. }
  122. void SpreadsheetWidget::load(const StringView& filename)
  123. {
  124. auto result = m_workbook->load(filename);
  125. if (result.is_error()) {
  126. GUI::MessageBox::show_error(window(), result.error());
  127. return;
  128. }
  129. while (auto* widget = m_tab_widget->active_widget()) {
  130. m_tab_widget->remove_tab(*widget);
  131. }
  132. setup_tabs(m_workbook->sheets());
  133. }
  134. void SpreadsheetWidget::add_sheet()
  135. {
  136. StringBuilder name;
  137. name.append("Sheet");
  138. name.appendf(" %d", m_workbook->sheets().size() + 1);
  139. auto& sheet = m_workbook->add_sheet(name.string_view());
  140. NonnullRefPtrVector<Sheet> new_sheets;
  141. new_sheets.append(sheet);
  142. setup_tabs(new_sheets);
  143. }
  144. void SpreadsheetWidget::set_filename(const String& filename)
  145. {
  146. if (m_workbook->set_filename(filename)) {
  147. StringBuilder builder;
  148. builder.append("Spreadsheet - ");
  149. builder.append(current_filename());
  150. window()->set_title(builder.string_view());
  151. window()->update();
  152. }
  153. }
  154. SpreadsheetWidget::~SpreadsheetWidget()
  155. {
  156. }
  157. }