SpreadsheetView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "SpreadsheetView.h"
  7. #include "CellTypeDialog.h"
  8. #include <AK/ScopeGuard.h>
  9. #include <AK/URL.h>
  10. #include <LibCore/MimeData.h>
  11. #include <LibGUI/BoxLayout.h>
  12. #include <LibGUI/HeaderView.h>
  13. #include <LibGUI/Menu.h>
  14. #include <LibGUI/ModelEditingDelegate.h>
  15. #include <LibGUI/Painter.h>
  16. #include <LibGUI/Scrollbar.h>
  17. #include <LibGUI/TableView.h>
  18. #include <LibGfx/Palette.h>
  19. namespace Spreadsheet {
  20. SpreadsheetView::~SpreadsheetView()
  21. {
  22. }
  23. void SpreadsheetView::EditingDelegate::set_value(GUI::Variant const& value, GUI::ModelEditingDelegate::SelectionBehavior selection_behavior)
  24. {
  25. if (value.as_string().is_null()) {
  26. StringModelEditingDelegate::set_value("", selection_behavior);
  27. commit();
  28. return;
  29. }
  30. if (m_has_set_initial_value)
  31. return StringModelEditingDelegate::set_value(value, selection_behavior);
  32. m_has_set_initial_value = true;
  33. const auto option = m_sheet.at({ (size_t)index().column(), (size_t)index().row() });
  34. if (option)
  35. return StringModelEditingDelegate::set_value(option->source(), selection_behavior);
  36. StringModelEditingDelegate::set_value("", selection_behavior);
  37. }
  38. void InfinitelyScrollableTableView::did_scroll()
  39. {
  40. TableView::did_scroll();
  41. auto& vscrollbar = vertical_scrollbar();
  42. auto& hscrollbar = horizontal_scrollbar();
  43. if (!m_vertical_scroll_end_timer->is_active()) {
  44. if (vscrollbar.is_visible() && vscrollbar.value() == vscrollbar.max()) {
  45. m_vertical_scroll_end_timer->on_timeout = [&] {
  46. if (on_reaching_vertical_end)
  47. on_reaching_vertical_end();
  48. m_vertical_scroll_end_timer->stop();
  49. };
  50. m_vertical_scroll_end_timer->start(50);
  51. }
  52. }
  53. if (!m_horizontal_scroll_end_timer->is_active()) {
  54. if (hscrollbar.is_visible() && hscrollbar.value() == hscrollbar.max()) {
  55. m_horizontal_scroll_end_timer->on_timeout = [&] {
  56. if (on_reaching_horizontal_end)
  57. on_reaching_horizontal_end();
  58. m_horizontal_scroll_end_timer->stop();
  59. };
  60. m_horizontal_scroll_end_timer->start(50);
  61. }
  62. }
  63. }
  64. void InfinitelyScrollableTableView::mousemove_event(GUI::MouseEvent& event)
  65. {
  66. if (auto model = this->model()) {
  67. auto index = index_at_event_position(event.position());
  68. if (!index.is_valid())
  69. return TableView::mousemove_event(event);
  70. auto& sheet = static_cast<SheetModel&>(*model).sheet();
  71. sheet.disable_updates();
  72. ScopeGuard sheet_update_enabler { [&] { sheet.enable_updates(); } };
  73. auto holding_left_button = !!(event.buttons() & GUI::MouseButton::Primary);
  74. if (m_is_dragging_for_copy) {
  75. set_override_cursor(Gfx::StandardCursor::Crosshair);
  76. m_should_intercept_drag = false;
  77. if (holding_left_button) {
  78. m_has_committed_to_dragging = true;
  79. // Force a drag to happen by moving the mousedown position to the center of the cell.
  80. auto rect = content_rect(index);
  81. m_left_mousedown_position = rect.center();
  82. }
  83. } else if (!m_should_intercept_drag) {
  84. set_override_cursor(Gfx::StandardCursor::Arrow);
  85. if (!holding_left_button) {
  86. m_starting_selection_index = index;
  87. } else {
  88. m_should_intercept_drag = true;
  89. m_might_drag = false;
  90. }
  91. }
  92. if (holding_left_button && m_should_intercept_drag && !m_has_committed_to_dragging) {
  93. if (!m_starting_selection_index.is_valid())
  94. m_starting_selection_index = index;
  95. Vector<GUI::ModelIndex> new_selection;
  96. for (auto i = min(m_starting_selection_index.row(), index.row()), imax = max(m_starting_selection_index.row(), index.row()); i <= imax; ++i) {
  97. for (auto j = min(m_starting_selection_index.column(), index.column()), jmax = max(m_starting_selection_index.column(), index.column()); j <= jmax; ++j) {
  98. auto index = model->index(i, j);
  99. if (index.is_valid())
  100. new_selection.append(move(index));
  101. }
  102. }
  103. if (!event.ctrl())
  104. selection().clear();
  105. selection().add_all(new_selection);
  106. }
  107. }
  108. TableView::mousemove_event(event);
  109. }
  110. void InfinitelyScrollableTableView::mousedown_event(GUI::MouseEvent& event)
  111. {
  112. if (this->model()) {
  113. auto index = index_at_event_position(event.position());
  114. auto rect = content_rect(index);
  115. auto distance = rect.center().absolute_relative_distance_to(event.position());
  116. m_is_dragging_for_copy = distance.x() >= rect.width() / 2 - 5 && distance.y() >= rect.height() / 2 - 5;
  117. }
  118. AbstractTableView::mousedown_event(event);
  119. }
  120. void InfinitelyScrollableTableView::mouseup_event(GUI::MouseEvent& event)
  121. {
  122. m_should_intercept_drag = false;
  123. m_has_committed_to_dragging = false;
  124. TableView::mouseup_event(event);
  125. }
  126. void SpreadsheetView::update_with_model()
  127. {
  128. m_sheet_model->update();
  129. m_table_view->update();
  130. }
  131. SpreadsheetView::SpreadsheetView(Sheet& sheet)
  132. : m_sheet(sheet)
  133. , m_sheet_model(SheetModel::create(*m_sheet))
  134. {
  135. set_layout<GUI::VerticalBoxLayout>().set_margins(2);
  136. m_table_view = add<InfinitelyScrollableTableView>();
  137. m_table_view->set_grid_style(GUI::TableView::GridStyle::Both);
  138. m_table_view->set_selection_behavior(GUI::AbstractView::SelectionBehavior::SelectItems);
  139. m_table_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed | GUI::AbstractView::AnyKeyPressed | GUI::AbstractView::DoubleClicked);
  140. m_table_view->set_tab_key_navigation_enabled(true);
  141. m_table_view->row_header().set_visible(true);
  142. m_table_view->set_model(m_sheet_model);
  143. m_table_view->on_reaching_vertical_end = [&]() {
  144. for (size_t i = 0; i < 100; ++i) {
  145. auto index = m_sheet->add_row();
  146. m_table_view->set_column_painting_delegate(index, make<TableCellPainter>(*m_table_view));
  147. };
  148. update_with_model();
  149. };
  150. m_table_view->on_reaching_horizontal_end = [&]() {
  151. for (size_t i = 0; i < 10; ++i) {
  152. m_sheet->add_column();
  153. auto last_column_index = m_sheet->column_count() - 1;
  154. m_table_view->set_column_width(last_column_index, 50);
  155. m_table_view->set_default_column_width(last_column_index, 50);
  156. m_table_view->set_column_header_alignment(last_column_index, Gfx::TextAlignment::Center);
  157. m_table_view->set_column_painting_delegate(last_column_index, make<TableCellPainter>(*m_table_view));
  158. }
  159. update_with_model();
  160. };
  161. set_focus_proxy(m_table_view);
  162. // FIXME: This is dumb.
  163. for (size_t i = 0; i < m_sheet->column_count(); ++i) {
  164. m_table_view->set_column_painting_delegate(i, make<TableCellPainter>(*m_table_view));
  165. m_table_view->set_column_width(i, 50);
  166. m_table_view->set_default_column_width(i, 50);
  167. m_table_view->set_column_header_alignment(i, Gfx::TextAlignment::Center);
  168. }
  169. m_table_view->set_alternating_row_colors(false);
  170. m_table_view->set_highlight_selected_rows(false);
  171. m_table_view->set_editable(true);
  172. m_table_view->aid_create_editing_delegate = [this](auto&) {
  173. auto delegate = make<EditingDelegate>(*m_sheet);
  174. delegate->on_cursor_key_pressed = [this](auto& event) {
  175. m_table_view->stop_editing();
  176. m_table_view->dispatch_event(event);
  177. };
  178. delegate->on_cell_focusout = [this](auto& index, auto& value) {
  179. m_table_view->model()->set_data(index, value);
  180. };
  181. return delegate;
  182. };
  183. m_table_view->on_selection_change = [&] {
  184. m_sheet->selected_cells().clear();
  185. for (auto& index : m_table_view->selection().indices()) {
  186. Position position { (size_t)index.column(), (size_t)index.row() };
  187. m_sheet->selected_cells().set(position);
  188. }
  189. if (m_table_view->selection().is_empty() && on_selection_dropped)
  190. return on_selection_dropped();
  191. Vector<Position> selected_positions;
  192. selected_positions.ensure_capacity(m_table_view->selection().size());
  193. for (auto& selection : m_table_view->selection().indices())
  194. selected_positions.empend((size_t)selection.column(), (size_t)selection.row());
  195. if (on_selection_changed) {
  196. on_selection_changed(move(selected_positions));
  197. update_with_model();
  198. };
  199. };
  200. m_table_view->on_activation = [this](auto&) {
  201. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  202. };
  203. m_table_view->on_context_menu_request = [&](const GUI::ModelIndex&, const GUI::ContextMenuEvent& event) {
  204. // NOTE: We ignore the specific cell for now.
  205. m_cell_range_context_menu->popup(event.screen_position());
  206. };
  207. m_cell_range_context_menu = GUI::Menu::construct();
  208. m_cell_range_context_menu->add_action(GUI::Action::create("Type and Formatting...", [this](auto&) {
  209. Vector<Position> positions;
  210. for (auto& index : m_table_view->selection().indices()) {
  211. Position position { (size_t)index.column(), (size_t)index.row() };
  212. positions.append(move(position));
  213. }
  214. if (positions.is_empty()) {
  215. auto& index = m_table_view->cursor_index();
  216. Position position { (size_t)index.column(), (size_t)index.row() };
  217. positions.append(move(position));
  218. }
  219. auto dialog = CellTypeDialog::construct(positions, *m_sheet, window());
  220. if (dialog->exec() == GUI::Dialog::ExecOK) {
  221. for (auto& position : positions) {
  222. auto& cell = m_sheet->ensure(position);
  223. cell.set_type(dialog->type());
  224. cell.set_type_metadata(dialog->metadata());
  225. cell.set_conditional_formats(dialog->conditional_formats());
  226. }
  227. m_table_view->update();
  228. }
  229. }));
  230. m_table_view->on_drop = [&](const GUI::ModelIndex& index, const GUI::DropEvent& event) {
  231. if (!index.is_valid())
  232. return;
  233. ScopeGuard update_after_drop { [this] { update(); } };
  234. if (event.mime_data().has_format("text/x-spreadsheet-data")) {
  235. auto const& data = event.mime_data().data("text/x-spreadsheet-data");
  236. StringView urls { data.data(), data.size() };
  237. Vector<Position> source_positions, target_positions;
  238. for (auto& line : urls.lines(false)) {
  239. auto position = m_sheet->position_from_url(line);
  240. if (position.has_value())
  241. source_positions.append(position.release_value());
  242. }
  243. // Drop always has a single target.
  244. Position target { (size_t)index.column(), (size_t)index.row() };
  245. target_positions.append(move(target));
  246. if (source_positions.is_empty())
  247. return;
  248. auto first_position = source_positions.take_first();
  249. m_sheet->copy_cells(move(source_positions), move(target_positions), first_position);
  250. return;
  251. }
  252. if (event.mime_data().has_text()) {
  253. auto& target_cell = m_sheet->ensure({ (size_t)index.column(), (size_t)index.row() });
  254. target_cell.set_data(event.text());
  255. return;
  256. }
  257. };
  258. }
  259. void SpreadsheetView::hide_event(GUI::HideEvent&)
  260. {
  261. if (on_selection_dropped)
  262. on_selection_dropped();
  263. }
  264. void SpreadsheetView::show_event(GUI::ShowEvent&)
  265. {
  266. if (on_selection_changed && !m_table_view->selection().is_empty()) {
  267. Vector<Position> selected_positions;
  268. selected_positions.ensure_capacity(m_table_view->selection().size());
  269. for (auto& selection : m_table_view->selection().indices())
  270. selected_positions.empend((size_t)selection.column(), (size_t)selection.row());
  271. on_selection_changed(move(selected_positions));
  272. }
  273. }
  274. void SpreadsheetView::move_cursor(GUI::AbstractView::CursorMovement direction)
  275. {
  276. m_table_view->move_cursor(direction, GUI::AbstractView::SelectionUpdate::Set);
  277. }
  278. void SpreadsheetView::TableCellPainter::paint(GUI::Painter& painter, const Gfx::IntRect& rect, const Gfx::Palette& palette, const GUI::ModelIndex& index)
  279. {
  280. // Draw a border.
  281. // Undo the horizontal padding done by the table view...
  282. auto cell_rect = rect.inflated(m_table_view.horizontal_padding() * 2, 0);
  283. if (auto bg = index.data(GUI::ModelRole::BackgroundColor); bg.is_color())
  284. painter.fill_rect(cell_rect, bg.as_color());
  285. if (m_table_view.selection().contains(index)) {
  286. Color fill_color = palette.selection();
  287. fill_color.set_alpha(80);
  288. painter.fill_rect(cell_rect, fill_color);
  289. }
  290. auto text_color = index.data(GUI::ModelRole::ForegroundColor).to_color(palette.color(m_table_view.foreground_role()));
  291. auto data = index.data();
  292. auto text_alignment = index.data(GUI::ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterRight);
  293. painter.draw_text(rect, data.to_string(), m_table_view.font_for_index(index), text_alignment, text_color, Gfx::TextElision::Right);
  294. }
  295. }