AbstractTableView.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. ScrollableWidget::scroll_into_view(content_rect(index), scroll_horizontally, scroll_vertically);
  211. }
  212. void AbstractTableView::doubleclick_event(MouseEvent& event)
  213. {
  214. if (!model())
  215. return;
  216. if (event.button() == MouseButton::Left) {
  217. if (is_editable() && edit_triggers() & EditTrigger::DoubleClicked)
  218. begin_editing(cursor_index());
  219. else
  220. activate(cursor_index());
  221. }
  222. }
  223. void AbstractTableView::context_menu_event(ContextMenuEvent& event)
  224. {
  225. if (!model())
  226. return;
  227. bool is_toggle;
  228. auto index = index_at_event_position(event.position(), is_toggle);
  229. if (index.is_valid()) {
  230. if (!selection().contains(index))
  231. selection().set(index);
  232. } else {
  233. selection().clear();
  234. }
  235. if (on_context_menu_request)
  236. on_context_menu_request(index, event);
  237. }
  238. Gfx::IntRect AbstractTableView::content_rect(int row, int column) const
  239. {
  240. auto row_rect = this->row_rect(row);
  241. int x = 0;
  242. for (int i = 0; i < column; ++i)
  243. x += column_width(i) + horizontal_padding() * 2;
  244. return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, row_height() };
  245. }
  246. Gfx::IntRect AbstractTableView::content_rect(const ModelIndex& index) const
  247. {
  248. return content_rect(index.row(), index.column());
  249. }
  250. Gfx::IntRect AbstractTableView::row_rect(int item_index) const
  251. {
  252. return { row_header().is_visible() ? row_header().width() : 0,
  253. (column_header().is_visible() ? column_header().height() : 0) + (item_index * row_height()),
  254. max(content_size().width(), width()),
  255. row_height() };
  256. }
  257. Gfx::IntPoint AbstractTableView::adjusted_position(const Gfx::IntPoint& position) const
  258. {
  259. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  260. }
  261. void AbstractTableView::did_update_model(unsigned flags)
  262. {
  263. AbstractView::did_update_model(flags);
  264. update_row_sizes();
  265. update_column_sizes();
  266. update_content_size();
  267. update();
  268. }
  269. void AbstractTableView::resize_event(ResizeEvent& event)
  270. {
  271. AbstractView::resize_event(event);
  272. layout_headers();
  273. }
  274. void AbstractTableView::header_did_change_section_size(Badge<HeaderView>, Gfx::Orientation, int, int)
  275. {
  276. update_content_size();
  277. update();
  278. }
  279. void AbstractTableView::header_did_change_section_visibility(Badge<HeaderView>, Gfx::Orientation, int, bool)
  280. {
  281. update_content_size();
  282. update();
  283. }
  284. void AbstractTableView::set_column_hidden(int column, bool hidden)
  285. {
  286. column_header().set_section_visible(column, !hidden);
  287. }
  288. void AbstractTableView::set_column_headers_visible(bool visible)
  289. {
  290. column_header().set_visible(visible);
  291. }
  292. void AbstractTableView::did_scroll()
  293. {
  294. AbstractView::did_scroll();
  295. layout_headers();
  296. }
  297. void AbstractTableView::layout_headers()
  298. {
  299. if (column_header().is_visible()) {
  300. int row_header_width = row_header().is_visible() ? row_header().width() : 0;
  301. int vertical_scrollbar_width = vertical_scrollbar().is_visible() ? vertical_scrollbar().width() : 0;
  302. int x = frame_thickness() + row_header_width - horizontal_scrollbar().value();
  303. int y = frame_thickness();
  304. int width = AK::max(content_width(), rect().width() - frame_thickness() * 2 - row_header_width - vertical_scrollbar_width);
  305. column_header().set_relative_rect(x, y, width, column_header().preferred_size().height());
  306. }
  307. if (row_header().is_visible()) {
  308. int column_header_height = column_header().is_visible() ? column_header().height() : 0;
  309. int horizontal_scrollbar_height = horizontal_scrollbar().is_visible() ? horizontal_scrollbar().height() : 0;
  310. int x = frame_thickness();
  311. int y = frame_thickness() + column_header_height - vertical_scrollbar().value();
  312. int height = AK::max(content_height(), rect().height() - frame_thickness() * 2 - column_header_height - horizontal_scrollbar_height);
  313. row_header().set_relative_rect(x, y, row_header().preferred_size().width(), height);
  314. }
  315. if (row_header().is_visible() && column_header().is_visible()) {
  316. m_corner_button->set_relative_rect(frame_thickness(), frame_thickness(), row_header().width(), column_header().height());
  317. m_corner_button->set_visible(true);
  318. } else {
  319. m_corner_button->set_visible(false);
  320. }
  321. }
  322. void AbstractTableView::set_row_height(int height)
  323. {
  324. if (m_row_height == height)
  325. return;
  326. m_row_height = height;
  327. update_row_sizes();
  328. }
  329. void AbstractTableView::keydown_event(KeyEvent& event)
  330. {
  331. if (is_tab_key_navigation_enabled()) {
  332. if (event.modifiers() == KeyModifier::Mod_Shift && event.key() == KeyCode::Key_Tab) {
  333. move_cursor(CursorMovement::Left, SelectionUpdate::Set);
  334. event.accept();
  335. return;
  336. }
  337. if (!event.modifiers() && event.key() == KeyCode::Key_Tab) {
  338. move_cursor(CursorMovement::Right, SelectionUpdate::Set);
  339. event.accept();
  340. return;
  341. }
  342. }
  343. AbstractView::keydown_event(event);
  344. }
  345. }