AbstractTableView.cpp 13 KB

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