SpreadsheetView.cpp 13 KB

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