AbstractTableView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  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 <AK/StringBuilder.h>
  27. #include <AK/Vector.h>
  28. #include <LibGUI/AbstractTableView.h>
  29. #include <LibGUI/Action.h>
  30. #include <LibGUI/Button.h>
  31. #include <LibGUI/HeaderView.h>
  32. #include <LibGUI/Menu.h>
  33. #include <LibGUI/Model.h>
  34. #include <LibGUI/Painter.h>
  35. #include <LibGUI/ScrollBar.h>
  36. #include <LibGUI/Window.h>
  37. #include <LibGfx/Palette.h>
  38. namespace GUI {
  39. AbstractTableView::AbstractTableView()
  40. {
  41. m_corner_button = add<Button>();
  42. m_corner_button->move_to_back();
  43. m_corner_button->set_background_role(Gfx::ColorRole::ThreedShadow1);
  44. m_corner_button->set_fill_with_background_color(true);
  45. m_column_header = add<HeaderView>(*this, Gfx::Orientation::Horizontal);
  46. m_column_header->move_to_back();
  47. m_row_header = add<HeaderView>(*this, Gfx::Orientation::Vertical);
  48. m_row_header->move_to_back();
  49. m_row_header->set_visible(false);
  50. set_should_hide_unnecessary_scrollbars(true);
  51. }
  52. AbstractTableView::~AbstractTableView()
  53. {
  54. }
  55. void AbstractTableView::select_all()
  56. {
  57. selection().clear();
  58. for (int item_index = 0; item_index < item_count(); ++item_index) {
  59. auto index = model()->index(item_index);
  60. selection().add(index);
  61. }
  62. }
  63. void AbstractTableView::update_column_sizes()
  64. {
  65. if (!model())
  66. return;
  67. auto& model = *this->model();
  68. int column_count = model.column_count();
  69. int row_count = model.row_count();
  70. for (int column = 0; column < column_count; ++column) {
  71. if (!column_header().is_section_visible(column))
  72. continue;
  73. int header_width = m_column_header->font().width(model.column_name(column));
  74. if (column == m_key_column && model.is_column_sortable(column))
  75. header_width += font().width(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
  76. int column_width = header_width;
  77. for (int row = 0; row < row_count; ++row) {
  78. auto cell_data = model.index(row, column).data();
  79. int cell_width = 0;
  80. if (cell_data.is_icon()) {
  81. cell_width = row_height();
  82. } else if (cell_data.is_bitmap()) {
  83. cell_width = cell_data.as_bitmap().width();
  84. } else if (cell_data.is_valid()) {
  85. cell_width = font().width(cell_data.to_string());
  86. }
  87. column_width = max(column_width, cell_width);
  88. }
  89. column_header().set_section_size(column, max(m_column_header->section_size(column), column_width));
  90. }
  91. }
  92. void AbstractTableView::update_row_sizes()
  93. {
  94. if (!model())
  95. return;
  96. auto& model = *this->model();
  97. int row_count = model.row_count();
  98. for (int row = 0; row < row_count; ++row) {
  99. if (!column_header().is_section_visible(row))
  100. continue;
  101. row_header().set_section_size(row, row_height());
  102. }
  103. }
  104. void AbstractTableView::update_content_size()
  105. {
  106. if (!model())
  107. return set_content_size({});
  108. int content_width = 0;
  109. int column_count = model()->column_count();
  110. for (int i = 0; i < column_count; ++i) {
  111. if (column_header().is_section_visible(i))
  112. content_width += column_width(i) + horizontal_padding() * 2;
  113. }
  114. int content_height = item_count() * row_height();
  115. set_content_size({ content_width, content_height });
  116. set_size_occupied_by_fixed_elements({ row_header().width(), column_header().height() });
  117. layout_headers();
  118. }
  119. TableCellPaintingDelegate* AbstractTableView::column_painting_delegate(int column) const
  120. {
  121. // FIXME: This should return a const pointer I think..
  122. return const_cast<TableCellPaintingDelegate*>(m_column_painting_delegate.get(column).value_or(nullptr));
  123. }
  124. void AbstractTableView::set_column_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate> delegate)
  125. {
  126. if (!delegate)
  127. m_column_painting_delegate.remove(column);
  128. else
  129. m_column_painting_delegate.set(column, move(delegate));
  130. }
  131. int AbstractTableView::column_width(int column_index) const
  132. {
  133. if (!model())
  134. return 0;
  135. return m_column_header->section_size(column_index);
  136. }
  137. void AbstractTableView::set_column_width(int column, int width)
  138. {
  139. column_header().set_section_size(column, width);
  140. }
  141. Gfx::TextAlignment AbstractTableView::column_header_alignment(int column_index) const
  142. {
  143. if (!model())
  144. return Gfx::TextAlignment::CenterLeft;
  145. return m_column_header->section_alignment(column_index);
  146. }
  147. void AbstractTableView::set_column_header_alignment(int column, Gfx::TextAlignment alignment)
  148. {
  149. column_header().set_section_alignment(column, alignment);
  150. }
  151. void AbstractTableView::mousedown_event(MouseEvent& event)
  152. {
  153. if (!model())
  154. return AbstractView::mousedown_event(event);
  155. if (event.button() != MouseButton::Left)
  156. return AbstractView::mousedown_event(event);
  157. bool is_toggle;
  158. auto index = index_at_event_position(event.position(), is_toggle);
  159. if (index.is_valid() && is_toggle && model()->row_count(index)) {
  160. toggle_index(index);
  161. return;
  162. }
  163. AbstractView::mousedown_event(event);
  164. }
  165. ModelIndex AbstractTableView::index_at_event_position(const Gfx::IntPoint& position, bool& is_toggle) const
  166. {
  167. is_toggle = false;
  168. if (!model())
  169. return {};
  170. auto adjusted_position = this->adjusted_position(position);
  171. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  172. if (!row_rect(row).contains(adjusted_position))
  173. continue;
  174. for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) {
  175. if (!content_rect(row, column).contains(adjusted_position))
  176. continue;
  177. return model()->index(row, column);
  178. }
  179. return model()->index(row, 0);
  180. }
  181. return {};
  182. }
  183. ModelIndex AbstractTableView::index_at_event_position(const Gfx::IntPoint& position) const
  184. {
  185. bool is_toggle;
  186. auto index = index_at_event_position(position, is_toggle);
  187. return is_toggle ? ModelIndex() : index;
  188. }
  189. int AbstractTableView::item_count() const
  190. {
  191. if (!model())
  192. return 0;
  193. return model()->row_count();
  194. }
  195. void AbstractTableView::move_cursor_relative(int vertical_steps, int horizontal_steps, SelectionUpdate selection_update)
  196. {
  197. if (!model())
  198. return;
  199. auto& model = *this->model();
  200. ModelIndex new_index;
  201. if (cursor_index().is_valid()) {
  202. new_index = model.index(cursor_index().row() + vertical_steps, cursor_index().column() + horizontal_steps);
  203. } else {
  204. new_index = model.index(0, 0);
  205. }
  206. set_cursor(new_index, selection_update);
  207. }
  208. void AbstractTableView::scroll_into_view(const ModelIndex& index, bool scroll_horizontally, bool scroll_vertically)
  209. {
  210. auto rect = row_rect(index.row()).translated(0, -m_column_header->height());
  211. ScrollableWidget::scroll_into_view(rect, scroll_horizontally, scroll_vertically);
  212. }
  213. void AbstractTableView::doubleclick_event(MouseEvent& event)
  214. {
  215. if (!model())
  216. return;
  217. if (event.button() == MouseButton::Left) {
  218. if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
  219. begin_editing(cursor_index());
  220. else
  221. activate(cursor_index());
  222. }
  223. }
  224. void AbstractTableView::context_menu_event(ContextMenuEvent& event)
  225. {
  226. if (!model())
  227. return;
  228. bool is_toggle;
  229. auto index = index_at_event_position(event.position(), is_toggle);
  230. if (index.is_valid()) {
  231. if (!selection().contains(index))
  232. selection().set(index);
  233. } else {
  234. selection().clear();
  235. }
  236. if (on_context_menu_request)
  237. on_context_menu_request(index, event);
  238. }
  239. Gfx::IntRect AbstractTableView::content_rect(int row, int column) const
  240. {
  241. auto row_rect = this->row_rect(row);
  242. int x = 0;
  243. for (int i = 0; i < column; ++i)
  244. x += column_width(i) + horizontal_padding() * 2;
  245. return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, row_height() };
  246. }
  247. Gfx::IntRect AbstractTableView::content_rect(const ModelIndex& index) const
  248. {
  249. return content_rect(index.row(), index.column());
  250. }
  251. Gfx::IntRect AbstractTableView::row_rect(int item_index) const
  252. {
  253. return { row_header().is_visible() ? row_header().width() : 0, column_header().height() + (item_index * row_height()), max(content_size().width(), width()), row_height() };
  254. }
  255. Gfx::IntPoint AbstractTableView::adjusted_position(const Gfx::IntPoint& position) const
  256. {
  257. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  258. }
  259. void AbstractTableView::did_update_model(unsigned flags)
  260. {
  261. AbstractView::did_update_model(flags);
  262. update_row_sizes();
  263. update_column_sizes();
  264. update_content_size();
  265. update();
  266. }
  267. void AbstractTableView::resize_event(ResizeEvent& event)
  268. {
  269. AbstractView::resize_event(event);
  270. layout_headers();
  271. }
  272. void AbstractTableView::header_did_change_section_size(Badge<HeaderView>, Gfx::Orientation, int, int)
  273. {
  274. update_content_size();
  275. update();
  276. }
  277. void AbstractTableView::header_did_change_section_visibility(Badge<HeaderView>, Gfx::Orientation, int, bool)
  278. {
  279. update_content_size();
  280. update();
  281. }
  282. void AbstractTableView::set_column_hidden(int column, bool hidden)
  283. {
  284. column_header().set_section_visible(column, !hidden);
  285. }
  286. void AbstractTableView::set_column_headers_visible(bool visible)
  287. {
  288. column_header().set_visible(visible);
  289. }
  290. void AbstractTableView::did_scroll()
  291. {
  292. AbstractView::did_scroll();
  293. layout_headers();
  294. }
  295. void AbstractTableView::layout_headers()
  296. {
  297. if (column_header().is_visible()) {
  298. int x = frame_thickness() + (row_header().is_visible() ? row_header().width() : 0) + -horizontal_scrollbar().value();
  299. int y = frame_thickness();
  300. column_header().set_relative_rect(x, y, content_width(), column_header().preferred_size().height());
  301. }
  302. if (row_header().is_visible()) {
  303. int x = frame_thickness();
  304. int y = (frame_thickness() + (column_header().is_visible() ? column_header().height() : 0)) + -vertical_scrollbar().value();
  305. row_header().set_relative_rect(x, y, row_header().preferred_size().width(), content_height());
  306. }
  307. if (row_header().is_visible() && column_header().is_visible()) {
  308. m_corner_button->set_relative_rect(frame_thickness(), frame_thickness(), row_header().width(), column_header().height());
  309. m_corner_button->set_visible(true);
  310. } else {
  311. m_corner_button->set_visible(false);
  312. }
  313. }
  314. void AbstractTableView::set_row_height(int height)
  315. {
  316. if (m_row_height == height)
  317. return;
  318. m_row_height = height;
  319. update_row_sizes();
  320. }
  321. void AbstractTableView::keydown_event(KeyEvent& event)
  322. {
  323. if (is_tab_key_navigation_enabled()) {
  324. if (event.modifiers() == KeyModifier::Mod_Shift && event.key() == KeyCode::Key_Tab) {
  325. move_cursor(CursorMovement::Left, SelectionUpdate::Set);
  326. event.accept();
  327. return;
  328. }
  329. if (!event.modifiers() && event.key() == KeyCode::Key_Tab) {
  330. move_cursor(CursorMovement::Right, SelectionUpdate::Set);
  331. event.accept();
  332. return;
  333. }
  334. }
  335. return AbstractView::keydown_event(event);
  336. }
  337. }