SpreadsheetView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_default_column_width(last_column_index, 50);
  155. m_table_view->set_column_header_alignment(last_column_index, Gfx::TextAlignment::Center);
  156. }
  157. update_with_model();
  158. };
  159. set_focus_proxy(m_table_view);
  160. // FIXME: This is dumb.
  161. for (size_t i = 0; i < m_sheet->column_count(); ++i) {
  162. m_table_view->set_column_painting_delegate(i, make<TableCellPainter>(*m_table_view));
  163. m_table_view->set_column_width(i, 50);
  164. m_table_view->set_default_column_width(i, 50);
  165. m_table_view->set_column_header_alignment(i, Gfx::TextAlignment::Center);
  166. }
  167. m_table_view->set_alternating_row_colors(false);
  168. m_table_view->set_highlight_selected_rows(false);
  169. m_table_view->set_editable(true);
  170. m_table_view->aid_create_editing_delegate = [this](auto&) {
  171. auto delegate = make<EditingDelegate>(*m_sheet);
  172. delegate->on_cursor_key_pressed = [this](auto& event) {
  173. m_table_view->stop_editing();
  174. m_table_view->event(event);
  175. };
  176. return delegate;
  177. };
  178. m_table_view->on_selection_change = [&] {
  179. m_sheet->selected_cells().clear();
  180. for (auto& index : m_table_view->selection().indexes()) {
  181. Position position { (size_t)index.column(), (size_t)index.row() };
  182. m_sheet->selected_cells().set(position);
  183. }
  184. if (m_table_view->selection().is_empty() && on_selection_dropped)
  185. return on_selection_dropped();
  186. Vector<Position> selected_positions;
  187. selected_positions.ensure_capacity(m_table_view->selection().size());
  188. for (auto& selection : m_table_view->selection().indexes())
  189. selected_positions.empend((size_t)selection.column(), (size_t)selection.row());
  190. if (on_selection_changed) {
  191. on_selection_changed(move(selected_positions));
  192. update_with_model();
  193. };
  194. };
  195. m_table_view->on_activation = [this](auto&) {
  196. m_table_view->move_cursor(GUI::AbstractView::CursorMovement::Down, GUI::AbstractView::SelectionUpdate::Set);
  197. };
  198. m_table_view->on_context_menu_request = [&](const GUI::ModelIndex&, const GUI::ContextMenuEvent& event) {
  199. // NOTE: We ignore the specific cell for now.
  200. m_cell_range_context_menu->popup(event.screen_position());
  201. };
  202. m_cell_range_context_menu = GUI::Menu::construct();
  203. m_cell_range_context_menu->add_action(GUI::Action::create("Type and Formatting...", [this](auto&) {
  204. Vector<Position> positions;
  205. for (auto& index : m_table_view->selection().indexes()) {
  206. Position position { (size_t)index.column(), (size_t)index.row() };
  207. positions.append(move(position));
  208. }
  209. if (positions.is_empty()) {
  210. auto& index = m_table_view->cursor_index();
  211. Position position { (size_t)index.column(), (size_t)index.row() };
  212. positions.append(move(position));
  213. }
  214. auto dialog = CellTypeDialog::construct(positions, *m_sheet, window());
  215. if (dialog->exec() == GUI::Dialog::ExecOK) {
  216. for (auto& position : positions) {
  217. auto& cell = m_sheet->ensure(position);
  218. cell.set_type(dialog->type());
  219. cell.set_type_metadata(dialog->metadata());
  220. cell.set_conditional_formats(dialog->conditional_formats());
  221. }
  222. m_table_view->update();
  223. }
  224. }));
  225. m_table_view->on_drop = [&](const GUI::ModelIndex& index, const GUI::DropEvent& event) {
  226. if (!index.is_valid())
  227. return;
  228. ScopeGuard update_after_drop { [this] { update(); } };
  229. if (event.mime_data().has_format("text/x-spreadsheet-data")) {
  230. auto data = event.mime_data().data("text/x-spreadsheet-data");
  231. StringView urls { data.data(), data.size() };
  232. Vector<Position> source_positions, target_positions;
  233. for (auto& line : urls.lines(false)) {
  234. auto position = m_sheet->position_from_url(line);
  235. if (position.has_value())
  236. source_positions.append(position.release_value());
  237. }
  238. // Drop always has a single target.
  239. Position target { (size_t)index.column(), (size_t)index.row() };
  240. target_positions.append(move(target));
  241. if (source_positions.is_empty())
  242. return;
  243. auto first_position = source_positions.take_first();
  244. m_sheet->copy_cells(move(source_positions), move(target_positions), first_position);
  245. return;
  246. }
  247. if (event.mime_data().has_text()) {
  248. auto* target_cell = m_sheet->at({ (size_t)index.column(), (size_t)index.row() });
  249. VERIFY(target_cell);
  250. target_cell->set_data(event.text());
  251. return;
  252. }
  253. };
  254. }
  255. void SpreadsheetView::hide_event(GUI::HideEvent&)
  256. {
  257. if (on_selection_dropped)
  258. on_selection_dropped();
  259. }
  260. void SpreadsheetView::show_event(GUI::ShowEvent&)
  261. {
  262. if (on_selection_changed && !m_table_view->selection().is_empty()) {
  263. Vector<Position> selected_positions;
  264. selected_positions.ensure_capacity(m_table_view->selection().size());
  265. for (auto& selection : m_table_view->selection().indexes())
  266. selected_positions.empend((size_t)selection.column(), (size_t)selection.row());
  267. on_selection_changed(move(selected_positions));
  268. }
  269. }
  270. void SpreadsheetView::TableCellPainter::paint(GUI::Painter& painter, const Gfx::IntRect& rect, const Gfx::Palette& palette, const GUI::ModelIndex& index)
  271. {
  272. // Draw a border.
  273. // Undo the horizontal padding done by the table view...
  274. auto cell_rect = rect.inflated(m_table_view.horizontal_padding() * 2, 0);
  275. if (auto bg = index.data(GUI::ModelRole::BackgroundColor); bg.is_color())
  276. painter.fill_rect(cell_rect, bg.as_color());
  277. if (m_table_view.selection().contains(index)) {
  278. Color fill_color = palette.selection();
  279. fill_color.set_alpha(80);
  280. painter.fill_rect(cell_rect, fill_color);
  281. }
  282. auto text_color = index.data(GUI::ModelRole::ForegroundColor).to_color(palette.color(m_table_view.foreground_role()));
  283. auto data = index.data();
  284. auto text_alignment = index.data(GUI::ModelRole::TextAlignment).to_text_alignment(Gfx::TextAlignment::CenterRight);
  285. painter.draw_text(rect, data.to_string(), m_table_view.font_for_index(index), text_alignment, text_color, Gfx::TextElision::Right);
  286. }
  287. }