SpreadsheetView.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (c) 2020-2022, 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. void SpreadsheetView::EditingDelegate::set_value(GUI::Variant const& value, GUI::ModelEditingDelegate::SelectionBehavior selection_behavior)
  21. {
  22. if (value.as_string().is_null()) {
  23. StringModelEditingDelegate::set_value("", selection_behavior);
  24. commit();
  25. return;
  26. }
  27. if (m_has_set_initial_value)
  28. return StringModelEditingDelegate::set_value(value, selection_behavior);
  29. m_has_set_initial_value = true;
  30. const auto option = m_sheet.at({ (size_t)index().column(), (size_t)index().row() });
  31. if (option)
  32. return StringModelEditingDelegate::set_value(option->source(), selection_behavior);
  33. StringModelEditingDelegate::set_value("", selection_behavior);
  34. }
  35. void InfinitelyScrollableTableView::did_scroll()
  36. {
  37. TableView::did_scroll();
  38. auto& vscrollbar = vertical_scrollbar();
  39. auto& hscrollbar = horizontal_scrollbar();
  40. if (!m_vertical_scroll_end_timer->is_active()) {
  41. if (vscrollbar.is_visible() && vscrollbar.value() == vscrollbar.max()) {
  42. m_vertical_scroll_end_timer->on_timeout = [&] {
  43. if (on_reaching_vertical_end)
  44. on_reaching_vertical_end();
  45. m_vertical_scroll_end_timer->stop();
  46. };
  47. m_vertical_scroll_end_timer->start(50);
  48. }
  49. }
  50. if (!m_horizontal_scroll_end_timer->is_active()) {
  51. if (hscrollbar.is_visible() && hscrollbar.value() == hscrollbar.max()) {
  52. m_horizontal_scroll_end_timer->on_timeout = [&] {
  53. if (on_reaching_horizontal_end)
  54. on_reaching_horizontal_end();
  55. m_horizontal_scroll_end_timer->stop();
  56. };
  57. m_horizontal_scroll_end_timer->start(50);
  58. }
  59. }
  60. }
  61. void InfinitelyScrollableTableView::mousemove_event(GUI::MouseEvent& event)
  62. {
  63. if (auto model = this->model()) {
  64. auto index = index_at_event_position(event.position());
  65. if (!index.is_valid())
  66. return TableView::mousemove_event(event);
  67. auto& sheet = static_cast<SheetModel&>(*model).sheet();
  68. sheet.disable_updates();
  69. ScopeGuard sheet_update_enabler { [&] { sheet.enable_updates(); } };
  70. m_is_hovering_cut_zone = false;
  71. m_is_hovering_extend_zone = false;
  72. if (selection().size() > 0 && !m_is_dragging_for_select) {
  73. // Get top-left and bottom-right most cells of selection
  74. auto bottom_right_most_index = selection().first();
  75. auto top_left_most_index = selection().first();
  76. selection().for_each_index([&](auto& index) {
  77. if (index.row() > bottom_right_most_index.row())
  78. bottom_right_most_index = index;
  79. else if (index.column() > bottom_right_most_index.column())
  80. bottom_right_most_index = index;
  81. if (index.row() < top_left_most_index.row())
  82. top_left_most_index = index;
  83. else if (index.column() < top_left_most_index.column())
  84. top_left_most_index = index;
  85. });
  86. auto top_left_rect = content_rect(top_left_most_index);
  87. auto bottom_right_rect = content_rect(bottom_right_most_index);
  88. auto distance_tl = top_left_rect.center() - event.position();
  89. auto distance_br = bottom_right_rect.center() - event.position();
  90. auto is_over_top_line = false;
  91. auto is_over_bottom_line = false;
  92. auto is_over_left_line = false;
  93. auto is_over_right_line = false;
  94. // If cursor is within the bounds of the selection
  95. auto select_padding = 2;
  96. if ((distance_br.y() >= -(bottom_right_rect.height() / 2 + select_padding)) && (distance_tl.y() <= (top_left_rect.height() / 2 + select_padding)) && (distance_br.x() >= -(bottom_right_rect.width() / 2 + select_padding)) && (distance_tl.x() <= (top_left_rect.width() / 2 + select_padding))) {
  97. if (distance_tl.y() >= (top_left_rect.height() / 2 - select_padding))
  98. is_over_top_line = true;
  99. else if (distance_br.y() <= -(bottom_right_rect.height() / 2 - select_padding))
  100. is_over_bottom_line = true;
  101. if (distance_tl.x() >= (top_left_rect.width() / 2 - select_padding))
  102. is_over_left_line = true;
  103. else if (distance_br.x() <= -(bottom_right_rect.width() / 2 - select_padding))
  104. is_over_right_line = true;
  105. }
  106. if (is_over_bottom_line && is_over_right_line)
  107. m_is_hovering_extend_zone = true;
  108. else if (is_over_top_line || is_over_bottom_line || is_over_left_line || is_over_right_line) {
  109. m_target_cell = top_left_most_index;
  110. m_is_hovering_cut_zone = true;
  111. }
  112. }
  113. if (m_is_hovering_cut_zone || m_is_dragging_for_copy)
  114. set_override_cursor(Gfx::StandardCursor::Drag);
  115. else if (m_is_hovering_extend_zone)
  116. set_override_cursor(Gfx::StandardCursor::Crosshair);
  117. else
  118. set_override_cursor(Gfx::StandardCursor::Arrow);
  119. auto holding_left_button = !!(event.buttons() & GUI::MouseButton::Primary);
  120. if (m_is_dragging_for_copy) {
  121. m_is_dragging_for_select = false;
  122. if (holding_left_button) {
  123. m_has_committed_to_cutting = true;
  124. }
  125. } else if (!m_is_dragging_for_select) {
  126. if (!holding_left_button) {
  127. m_starting_selection_index = index;
  128. } else {
  129. m_is_dragging_for_select = true;
  130. m_might_drag = false;
  131. }
  132. }
  133. if (holding_left_button && m_is_dragging_for_select && !m_has_committed_to_cutting) {
  134. if (!m_starting_selection_index.is_valid())
  135. m_starting_selection_index = index;
  136. Vector<GUI::ModelIndex> new_selection;
  137. for (auto i = min(m_starting_selection_index.row(), index.row()), imax = max(m_starting_selection_index.row(), index.row()); i <= imax; ++i) {
  138. for (auto j = min(m_starting_selection_index.column(), index.column()), jmax = max(m_starting_selection_index.column(), index.column()); j <= jmax; ++j) {
  139. auto index = model->index(i, j);
  140. if (index.is_valid())
  141. new_selection.append(move(index));
  142. }
  143. }
  144. if (!event.ctrl())
  145. selection().clear();
  146. selection().add_all(new_selection);
  147. }
  148. }
  149. TableView::mousemove_event(event);
  150. }
  151. void InfinitelyScrollableTableView::mousedown_event(GUI::MouseEvent& event)
  152. {
  153. // Override the mouse event so that the the cell that is 'clicked' is not
  154. // the one right beneath the cursor but instead the one that is referred to
  155. // when m_is_hovering_cut_zone as it can be the case that the user is targetting
  156. // a cell yet be outside of its bounding box due to the select_padding.
  157. if (m_is_hovering_cut_zone) {
  158. m_is_dragging_for_copy = true;
  159. auto rect = content_rect(m_target_cell);
  160. GUI::MouseEvent adjusted_event = { (GUI::Event::Type)event.type(), rect.center(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y() };
  161. AbstractTableView::mousedown_event(adjusted_event);
  162. } else {
  163. AbstractTableView::mousedown_event(event);
  164. auto index = index_at_event_position(event.position());
  165. AbstractTableView::set_cursor(index, SelectionUpdate::Set);
  166. }
  167. }
  168. void InfinitelyScrollableTableView::mouseup_event(GUI::MouseEvent& event)
  169. {
  170. m_is_dragging_for_select = false;
  171. m_is_dragging_for_copy = false;
  172. m_has_committed_to_cutting = false;
  173. if (m_is_hovering_cut_zone) {
  174. auto rect = content_rect(m_target_cell);
  175. GUI::MouseEvent adjusted_event = { (GUI::Event::Type)event.type(), rect.center(), event.buttons(), event.button(), event.modifiers(), event.wheel_delta_x(), event.wheel_delta_y() };
  176. TableView::mouseup_event(adjusted_event);
  177. } else {
  178. TableView::mouseup_event(event);
  179. }
  180. }
  181. void InfinitelyScrollableTableView::drop_event(GUI::DropEvent& event)
  182. {
  183. TableView::drop_event(event);
  184. m_is_dragging_for_copy = false;
  185. set_override_cursor(Gfx::StandardCursor::Arrow);
  186. auto drop_index = index_at_event_position(event.position());
  187. if (selection().size() > 0) {
  188. // Get top left index position of previous selection
  189. auto top_left_most_index = selection().first();
  190. selection().for_each_index([&](auto& index) {
  191. if (index.row() < top_left_most_index.row())
  192. top_left_most_index = index;
  193. else if (index.column() < top_left_most_index.column())
  194. top_left_most_index = index;
  195. });
  196. // Compare with drag location
  197. auto x_diff = drop_index.column() - top_left_most_index.column();
  198. auto y_diff = drop_index.row() - top_left_most_index.row();
  199. // Set new selection
  200. Vector<GUI::ModelIndex> new_selection;
  201. for (auto& index : selection().indices()) {
  202. auto new_index = model()->index(index.row() + y_diff, index.column() + x_diff);
  203. new_selection.append(move(new_index));
  204. }
  205. selection().clear();
  206. AbstractTableView::set_cursor(drop_index, SelectionUpdate::Set);
  207. selection().add_all(new_selection);
  208. }
  209. }
  210. void SpreadsheetView::update_with_model()
  211. {
  212. m_sheet_model->update();
  213. m_table_view->update();
  214. }
  215. SpreadsheetView::SpreadsheetView(Sheet& sheet)
  216. : m_sheet(sheet)
  217. , m_sheet_model(SheetModel::create(*m_sheet))
  218. {
  219. set_layout<GUI::VerticalBoxLayout>().set_margins(2);
  220. m_table_view = add<InfinitelyScrollableTableView>();
  221. m_table_view->set_grid_style(GUI::TableView::GridStyle::Both);
  222. m_table_view->set_selection_behavior(GUI::AbstractView::SelectionBehavior::SelectItems);
  223. m_table_view->set_edit_triggers(GUI::AbstractView::EditTrigger::EditKeyPressed | GUI::AbstractView::AnyKeyPressed | GUI::AbstractView::DoubleClicked);
  224. m_table_view->set_tab_key_navigation_enabled(true);
  225. m_table_view->row_header().set_visible(true);
  226. m_table_view->set_model(m_sheet_model);
  227. m_table_view->on_reaching_vertical_end = [&]() {
  228. for (size_t i = 0; i < 100; ++i) {
  229. auto index = m_sheet->add_row();
  230. m_table_view->set_column_painting_delegate(index, make<TableCellPainter>(*m_table_view));
  231. };
  232. update_with_model();
  233. };
  234. m_table_view->on_reaching_horizontal_end = [&]() {
  235. for (size_t i = 0; i < 10; ++i) {
  236. m_sheet->add_column();
  237. auto last_column_index = m_sheet->column_count() - 1;
  238. m_table_view->set_column_width(last_column_index, 50);
  239. m_table_view->set_default_column_width(last_column_index, 50);
  240. m_table_view->set_column_header_alignment(last_column_index, Gfx::TextAlignment::Center);
  241. m_table_view->set_column_painting_delegate(last_column_index, make<TableCellPainter>(*m_table_view));
  242. }
  243. update_with_model();
  244. };
  245. set_focus_proxy(m_table_view);
  246. // FIXME: This is dumb.
  247. for (size_t i = 0; i < m_sheet->column_count(); ++i) {
  248. m_table_view->set_column_painting_delegate(i, make<TableCellPainter>(*m_table_view));
  249. m_table_view->set_column_width(i, 50);
  250. m_table_view->set_default_column_width(i, 50);
  251. m_table_view->set_column_header_alignment(i, Gfx::TextAlignment::Center);
  252. }
  253. m_table_view->set_alternating_row_colors(false);
  254. m_table_view->set_highlight_selected_rows(false);
  255. m_table_view->set_editable(true);
  256. m_table_view->aid_create_editing_delegate = [this](auto&) {
  257. auto delegate = make<EditingDelegate>(*m_sheet);
  258. delegate->on_cursor_key_pressed = [this](auto& event) {
  259. m_table_view->stop_editing();
  260. m_table_view->dispatch_event(event);
  261. };
  262. delegate->on_cell_focusout = [this](auto& index, auto& value) {
  263. m_table_view->model()->set_data(index, value);
  264. };
  265. return delegate;
  266. };
  267. m_table_view->on_selection_change = [&] {
  268. m_sheet->selected_cells().clear();
  269. for (auto& index : m_table_view->selection().indices()) {
  270. Position position { (size_t)index.column(), (size_t)index.row() };
  271. m_sheet->selected_cells().set(position);
  272. }
  273. if (m_table_view->selection().is_empty() && on_selection_dropped)
  274. return on_selection_dropped();
  275. Vector<Position> selected_positions;
  276. selected_positions.ensure_capacity(m_table_view->selection().size());
  277. for (auto& selection : m_table_view->selection().indices())
  278. selected_positions.empend((size_t)selection.column(), (size_t)selection.row());
  279. if (on_selection_changed) {
  280. on_selection_changed(move(selected_positions));
  281. update_with_model();
  282. };
  283. };
  284. m_table_view->on_activation = [this](auto&) {
  285. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  286. };
  287. m_table_view->on_context_menu_request = [&](const GUI::ModelIndex&, const GUI::ContextMenuEvent& event) {
  288. // NOTE: We ignore the specific cell for now.
  289. m_cell_range_context_menu->popup(event.screen_position());
  290. };
  291. m_cell_range_context_menu = GUI::Menu::construct();
  292. m_cell_range_context_menu->add_action(GUI::Action::create("Type and Formatting...", [this](auto&) {
  293. Vector<Position> positions;
  294. for (auto& index : m_table_view->selection().indices()) {
  295. Position position { (size_t)index.column(), (size_t)index.row() };
  296. positions.append(move(position));
  297. }
  298. if (positions.is_empty()) {
  299. auto& index = m_table_view->cursor_index();
  300. Position position { (size_t)index.column(), (size_t)index.row() };
  301. positions.append(move(position));
  302. }
  303. auto dialog = CellTypeDialog::construct(positions, *m_sheet, window());
  304. if (dialog->exec() == GUI::Dialog::ExecOK) {
  305. for (auto& position : positions) {
  306. auto& cell = m_sheet->ensure(position);
  307. cell.set_type(dialog->type());
  308. cell.set_type_metadata(dialog->metadata());
  309. cell.set_conditional_formats(dialog->conditional_formats());
  310. }
  311. m_table_view->update();
  312. }
  313. }));
  314. m_table_view->on_drop = [&](const GUI::ModelIndex& index, const GUI::DropEvent& event) {
  315. if (!index.is_valid())
  316. return;
  317. ScopeGuard update_after_drop { [this] { update(); } };
  318. if (event.mime_data().has_format("text/x-spreadsheet-data")) {
  319. auto const& data = event.mime_data().data("text/x-spreadsheet-data");
  320. StringView urls { data.data(), data.size() };
  321. Vector<Position> source_positions, target_positions;
  322. for (auto& line : urls.lines(false)) {
  323. auto position = m_sheet->position_from_url(line);
  324. if (position.has_value())
  325. source_positions.append(position.release_value());
  326. }
  327. // Drop always has a single target.
  328. Position target { (size_t)index.column(), (size_t)index.row() };
  329. target_positions.append(move(target));
  330. if (source_positions.is_empty())
  331. return;
  332. auto first_position = source_positions.take_first();
  333. m_sheet->copy_cells(move(source_positions), move(target_positions), first_position);
  334. return;
  335. }
  336. if (event.mime_data().has_text()) {
  337. auto& target_cell = m_sheet->ensure({ (size_t)index.column(), (size_t)index.row() });
  338. target_cell.set_data(event.text());
  339. return;
  340. }
  341. };
  342. }
  343. void SpreadsheetView::hide_event(GUI::HideEvent&)
  344. {
  345. if (on_selection_dropped)
  346. on_selection_dropped();
  347. }
  348. void SpreadsheetView::show_event(GUI::ShowEvent&)
  349. {
  350. if (on_selection_changed && !m_table_view->selection().is_empty()) {
  351. Vector<Position> selected_positions;
  352. selected_positions.ensure_capacity(m_table_view->selection().size());
  353. for (auto& selection : m_table_view->selection().indices())
  354. selected_positions.empend((size_t)selection.column(), (size_t)selection.row());
  355. on_selection_changed(move(selected_positions));
  356. }
  357. }
  358. void SpreadsheetView::move_cursor(GUI::AbstractView::CursorMovement direction)
  359. {
  360. m_table_view->move_cursor(direction, GUI::AbstractView::SelectionUpdate::Set);
  361. }
  362. void SpreadsheetView::TableCellPainter::paint(GUI::Painter& painter, const Gfx::IntRect& rect, const Gfx::Palette& palette, const GUI::ModelIndex& index)
  363. {
  364. // Draw a border.
  365. // Undo the horizontal padding done by the table view...
  366. auto cell_rect = rect.inflated(m_table_view.horizontal_padding() * 2, 0);
  367. if (auto bg = index.data(GUI::ModelRole::BackgroundColor); bg.is_color())
  368. painter.fill_rect(cell_rect, bg.as_color());
  369. if (m_table_view.selection().contains(index)) {
  370. Color fill_color = palette.selection();
  371. fill_color.set_alpha(80);
  372. painter.fill_rect(cell_rect, fill_color);
  373. }
  374. auto text_color = index.data(GUI::ModelRole::ForegroundColor).to_color(palette.color(m_table_view.foreground_role()));
  375. auto data = index.data();
  376. auto text_alignment = index.data(GUI::ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterRight);
  377. painter.draw_text(rect, data.to_string(), m_table_view.font_for_index(index), text_alignment, text_color, Gfx::TextElision::Right);
  378. }
  379. }