SpreadsheetView.cpp 14 KB

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