SpreadsheetWidget.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 "LibGUI/InputBox.h"
  30. #include <LibCore/File.h>
  31. #include <LibGUI/BoxLayout.h>
  32. #include <LibGUI/Button.h>
  33. #include <LibGUI/FilePicker.h>
  34. #include <LibGUI/Label.h>
  35. #include <LibGUI/Menu.h>
  36. #include <LibGUI/MessageBox.h>
  37. #include <LibGUI/Splitter.h>
  38. #include <LibGUI/TabWidget.h>
  39. #include <LibGUI/TextEditor.h>
  40. #include <LibGfx/FontDatabase.h>
  41. #include <string.h>
  42. namespace Spreadsheet {
  43. SpreadsheetWidget::SpreadsheetWidget(NonnullRefPtrVector<Sheet>&& sheets, bool should_add_sheet_if_empty)
  44. : m_workbook(make<Workbook>(move(sheets)))
  45. {
  46. set_fill_with_background_color(true);
  47. set_layout<GUI::VerticalBoxLayout>().set_margins({ 2, 2, 2, 2 });
  48. auto& container = add<GUI::VerticalSplitter>();
  49. auto& top_bar = container.add<GUI::Frame>();
  50. top_bar.set_layout<GUI::HorizontalBoxLayout>().set_spacing(1);
  51. top_bar.set_fixed_height(26);
  52. auto& current_cell_label = top_bar.add<GUI::Label>("");
  53. current_cell_label.set_fixed_width(50);
  54. auto& help_button = top_bar.add<GUI::Button>("🛈");
  55. help_button.set_fixed_size(20, 20);
  56. help_button.on_click = [&](auto) {
  57. auto docs = m_selected_view->sheet().gather_documentation();
  58. auto help_window = HelpWindow::the(window());
  59. help_window->set_docs(move(docs));
  60. help_window->show();
  61. };
  62. auto& cell_value_editor = top_bar.add<GUI::TextEditor>(GUI::TextEditor::Type::SingleLine);
  63. cell_value_editor.set_font(Gfx::FontDatabase::default_fixed_width_font());
  64. cell_value_editor.set_scrollbars_enabled(false);
  65. cell_value_editor.set_syntax_highlighter(make<CellSyntaxHighlighter>());
  66. cell_value_editor.set_enabled(false);
  67. current_cell_label.set_enabled(false);
  68. m_tab_widget = container.add<GUI::TabWidget>();
  69. m_tab_widget->set_tab_position(GUI::TabWidget::TabPosition::Bottom);
  70. m_cell_value_editor = cell_value_editor;
  71. m_current_cell_label = current_cell_label;
  72. m_inline_documentation_window = GUI::Window::construct(window());
  73. m_inline_documentation_window->set_rect(m_cell_value_editor->rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
  74. m_inline_documentation_window->set_window_type(GUI::WindowType::Tooltip);
  75. m_inline_documentation_window->set_resizable(false);
  76. auto& inline_widget = m_inline_documentation_window->set_main_widget<GUI::Frame>();
  77. inline_widget.set_fill_with_background_color(true);
  78. inline_widget.set_layout<GUI::VerticalBoxLayout>().set_margins({ 4, 4, 4, 4 });
  79. inline_widget.set_frame_shape(Gfx::FrameShape::Box);
  80. m_inline_documentation_label = inline_widget.add<GUI::Label>();
  81. m_inline_documentation_label->set_fill_with_background_color(true);
  82. m_inline_documentation_label->set_autosize(false);
  83. m_inline_documentation_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
  84. if (!m_workbook->has_sheets() && should_add_sheet_if_empty)
  85. m_workbook->add_sheet("Sheet 1");
  86. m_tab_context_menu = GUI::Menu::construct();
  87. auto rename_action = GUI::Action::create("Rename...", [this](auto&) {
  88. ASSERT(m_tab_context_menu_sheet_view);
  89. auto& sheet = m_tab_context_menu_sheet_view->sheet();
  90. String new_name;
  91. if (GUI::InputBox::show(window(), new_name, String::formatted("New name for '{}'", sheet.name()), "Rename sheet") == GUI::Dialog::ExecOK) {
  92. sheet.set_name(new_name);
  93. sheet.update();
  94. m_tab_widget->set_tab_title(static_cast<GUI::Widget&>(*m_tab_context_menu_sheet_view), new_name);
  95. }
  96. });
  97. m_tab_context_menu->add_action(rename_action);
  98. m_tab_context_menu->add_action(GUI::Action::create("Add new sheet...", [this](auto&) {
  99. String name;
  100. if (GUI::InputBox::show(window(), name, "Name for new sheet", "Create sheet") == GUI::Dialog::ExecOK) {
  101. NonnullRefPtrVector<Sheet> new_sheets;
  102. new_sheets.append(m_workbook->add_sheet(name));
  103. setup_tabs(move(new_sheets));
  104. }
  105. }));
  106. setup_tabs(m_workbook->sheets());
  107. }
  108. void SpreadsheetWidget::resize_event(GUI::ResizeEvent& event)
  109. {
  110. GUI::Widget::resize_event(event);
  111. if (m_inline_documentation_window && m_cell_value_editor && window())
  112. m_inline_documentation_window->set_rect(m_cell_value_editor->screen_relative_rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
  113. }
  114. void SpreadsheetWidget::setup_tabs(NonnullRefPtrVector<Sheet> new_sheets)
  115. {
  116. RefPtr<GUI::Widget> first_tab_widget;
  117. for (auto& sheet : new_sheets) {
  118. auto& tab = m_tab_widget->add_tab<SpreadsheetView>(sheet.name(), sheet);
  119. if (!first_tab_widget)
  120. first_tab_widget = &tab;
  121. }
  122. auto change = [&](auto& selected_widget) {
  123. if (m_selected_view) {
  124. m_selected_view->on_selection_changed = nullptr;
  125. m_selected_view->on_selection_dropped = nullptr;
  126. };
  127. m_selected_view = &static_cast<SpreadsheetView&>(selected_widget);
  128. m_selected_view->on_selection_changed = [&](Vector<Position>&& selection) {
  129. if (selection.is_empty()) {
  130. m_current_cell_label->set_enabled(false);
  131. m_current_cell_label->set_text({});
  132. m_cell_value_editor->on_change = nullptr;
  133. m_cell_value_editor->on_focusin = nullptr;
  134. m_cell_value_editor->on_focusout = nullptr;
  135. m_cell_value_editor->set_text("");
  136. m_cell_value_editor->set_enabled(false);
  137. return;
  138. }
  139. if (selection.size() == 1) {
  140. auto& position = selection.first();
  141. StringBuilder builder;
  142. builder.append(position.column);
  143. builder.appendff("{}", position.row);
  144. m_current_cell_label->set_enabled(true);
  145. m_current_cell_label->set_text(builder.string_view());
  146. auto& cell = m_selected_view->sheet().ensure(position);
  147. m_cell_value_editor->on_change = nullptr;
  148. m_cell_value_editor->set_text(cell.source());
  149. m_cell_value_editor->on_change = [&] {
  150. auto text = m_cell_value_editor->text();
  151. // FIXME: Lines?
  152. auto offset = m_cell_value_editor->cursor().column();
  153. try_generate_tip_for_input_expression(text, offset);
  154. cell.set_data(move(text));
  155. m_selected_view->sheet().update();
  156. update();
  157. };
  158. m_cell_value_editor->set_enabled(true);
  159. static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(&cell);
  160. return;
  161. }
  162. // There are many cells selected, change all of them.
  163. StringBuilder builder;
  164. builder.appendff("<{}>", selection.size());
  165. m_current_cell_label->set_enabled(true);
  166. m_current_cell_label->set_text(builder.string_view());
  167. Vector<Cell*> cells;
  168. for (auto& position : selection)
  169. cells.append(&m_selected_view->sheet().ensure(position));
  170. auto first_cell = cells.first();
  171. m_cell_value_editor->on_change = nullptr;
  172. m_cell_value_editor->set_text("");
  173. m_should_change_selected_cells = false;
  174. m_cell_value_editor->on_focusin = [this] { m_should_change_selected_cells = true; };
  175. m_cell_value_editor->on_focusout = [this] { m_should_change_selected_cells = false; };
  176. m_cell_value_editor->on_change = [cells = move(cells), this] {
  177. if (m_should_change_selected_cells) {
  178. auto text = m_cell_value_editor->text();
  179. // FIXME: Lines?
  180. auto offset = m_cell_value_editor->cursor().column();
  181. try_generate_tip_for_input_expression(text, offset);
  182. for (auto* cell : cells)
  183. cell->set_data(text);
  184. m_selected_view->sheet().update();
  185. update();
  186. }
  187. };
  188. m_cell_value_editor->set_enabled(true);
  189. static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(first_cell);
  190. };
  191. m_selected_view->on_selection_dropped = [&]() {
  192. m_cell_value_editor->set_enabled(false);
  193. static_cast<CellSyntaxHighlighter*>(const_cast<Syntax::Highlighter*>(m_cell_value_editor->syntax_highlighter()))->set_cell(nullptr);
  194. m_cell_value_editor->set_text("");
  195. m_current_cell_label->set_enabled(false);
  196. m_current_cell_label->set_text("");
  197. };
  198. };
  199. if (first_tab_widget)
  200. change(*first_tab_widget);
  201. m_tab_widget->on_change = [change = move(change)](auto& selected_widget) {
  202. change(selected_widget);
  203. };
  204. m_tab_widget->on_context_menu_request = [&](auto& widget, auto& event) {
  205. m_tab_context_menu_sheet_view = widget;
  206. m_tab_context_menu->popup(event.screen_position());
  207. };
  208. }
  209. void SpreadsheetWidget::try_generate_tip_for_input_expression(StringView source, size_t cursor_offset)
  210. {
  211. m_inline_documentation_window->set_rect(m_cell_value_editor->screen_relative_rect().translated(0, m_cell_value_editor->height() + 7).inflated(6, 6));
  212. if (!m_selected_view || !source.starts_with('=')) {
  213. m_inline_documentation_window->hide();
  214. return;
  215. }
  216. auto maybe_function_and_argument = get_function_and_argument_index(source.substring_view(0, cursor_offset));
  217. if (!maybe_function_and_argument.has_value()) {
  218. m_inline_documentation_window->hide();
  219. return;
  220. }
  221. auto& [name, index] = maybe_function_and_argument.value();
  222. auto& sheet = m_selected_view->sheet();
  223. auto text = sheet.generate_inline_documentation_for(name, index);
  224. if (text.is_empty()) {
  225. m_inline_documentation_window->hide();
  226. } else {
  227. m_inline_documentation_label->set_text(move(text));
  228. m_inline_documentation_window->show();
  229. }
  230. }
  231. void SpreadsheetWidget::save(const StringView& filename)
  232. {
  233. auto result = m_workbook->save(filename);
  234. if (result.is_error())
  235. GUI::MessageBox::show_error(window(), result.error());
  236. }
  237. void SpreadsheetWidget::load(const StringView& filename)
  238. {
  239. auto result = m_workbook->load(filename);
  240. if (result.is_error()) {
  241. GUI::MessageBox::show_error(window(), result.error());
  242. return;
  243. }
  244. m_tab_widget->on_change = nullptr;
  245. m_cell_value_editor->on_change = nullptr;
  246. m_current_cell_label->set_text("");
  247. m_should_change_selected_cells = false;
  248. while (auto* widget = m_tab_widget->active_widget()) {
  249. m_tab_widget->remove_tab(*widget);
  250. }
  251. setup_tabs(m_workbook->sheets());
  252. }
  253. bool SpreadsheetWidget::request_close()
  254. {
  255. if (!m_workbook->dirty())
  256. return true;
  257. auto result = GUI::MessageBox::show(window(), "The spreadsheet has been modified. Would you like to save?", "Unsaved changes", GUI::MessageBox::Type::Warning, GUI::MessageBox::InputType::YesNoCancel);
  258. if (result == GUI::MessageBox::ExecYes) {
  259. if (current_filename().is_empty()) {
  260. String name = "workbook";
  261. Optional<String> save_path = GUI::FilePicker::get_save_filepath(window(), name, "sheets");
  262. if (!save_path.has_value())
  263. return false;
  264. save(save_path.value());
  265. } else {
  266. save(current_filename());
  267. }
  268. return true;
  269. }
  270. if (result == GUI::MessageBox::ExecNo)
  271. return true;
  272. return false;
  273. }
  274. void SpreadsheetWidget::add_sheet()
  275. {
  276. StringBuilder name;
  277. name.append("Sheet");
  278. name.appendff(" {}", m_workbook->sheets().size() + 1);
  279. NonnullRefPtrVector<Sheet> new_sheets;
  280. new_sheets.append(m_workbook->add_sheet(name.string_view()));
  281. setup_tabs(move(new_sheets));
  282. }
  283. void SpreadsheetWidget::add_sheet(NonnullRefPtr<Sheet>&& sheet)
  284. {
  285. ASSERT(m_workbook == &sheet->workbook());
  286. NonnullRefPtrVector<Sheet> new_sheets;
  287. new_sheets.append(move(sheet));
  288. m_workbook->sheets().append(new_sheets);
  289. setup_tabs(new_sheets);
  290. }
  291. void SpreadsheetWidget::set_filename(const String& filename)
  292. {
  293. if (m_workbook->set_filename(filename)) {
  294. StringBuilder builder;
  295. builder.append("Spreadsheet - ");
  296. builder.append(current_filename());
  297. window()->set_title(builder.string_view());
  298. window()->update();
  299. }
  300. }
  301. SpreadsheetWidget::~SpreadsheetWidget()
  302. {
  303. }
  304. }