SpreadsheetView.cpp 13 KB

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