AbstractTableView.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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/HeaderView.h>
  31. #include <LibGUI/Menu.h>
  32. #include <LibGUI/Model.h>
  33. #include <LibGUI/Painter.h>
  34. #include <LibGUI/ScrollBar.h>
  35. #include <LibGUI/Window.h>
  36. #include <LibGfx/Palette.h>
  37. namespace GUI {
  38. AbstractTableView::AbstractTableView()
  39. {
  40. m_column_header = add<HeaderView>(*this, Gfx::Orientation::Horizontal);
  41. m_column_header->move_to_back();
  42. set_should_hide_unnecessary_scrollbars(true);
  43. }
  44. AbstractTableView::~AbstractTableView()
  45. {
  46. }
  47. void AbstractTableView::select_all()
  48. {
  49. selection().clear();
  50. for (int item_index = 0; item_index < item_count(); ++item_index) {
  51. auto index = model()->index(item_index);
  52. selection().add(index);
  53. }
  54. }
  55. void AbstractTableView::update_column_sizes()
  56. {
  57. if (!model())
  58. return;
  59. auto& model = *this->model();
  60. int column_count = model.column_count();
  61. int row_count = model.row_count();
  62. for (int column = 0; column < column_count; ++column) {
  63. if (!column_header().is_section_visible(column))
  64. continue;
  65. int header_width = m_column_header->font().width(model.column_name(column));
  66. if (column == m_key_column && model.is_column_sortable(column))
  67. header_width += font().width(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
  68. int column_width = header_width;
  69. for (int row = 0; row < row_count; ++row) {
  70. auto cell_data = model.index(row, column).data();
  71. int cell_width = 0;
  72. if (cell_data.is_icon()) {
  73. cell_width = item_height();
  74. } else if (cell_data.is_bitmap()) {
  75. cell_width = cell_data.as_bitmap().width();
  76. } else if (cell_data.is_valid()) {
  77. cell_width = font().width(cell_data.to_string());
  78. }
  79. column_width = max(column_width, cell_width);
  80. }
  81. column_header().set_section_size(column, max(m_column_header->section_size(column), column_width));
  82. }
  83. }
  84. void AbstractTableView::update_content_size()
  85. {
  86. if (!model())
  87. return set_content_size({});
  88. int content_width = 0;
  89. int column_count = model()->column_count();
  90. for (int i = 0; i < column_count; ++i) {
  91. if (column_header().is_section_visible(i))
  92. content_width += column_width(i) + horizontal_padding() * 2;
  93. }
  94. int content_height = item_count() * item_height();
  95. set_content_size({ content_width, content_height });
  96. column_header().set_width(content_width);
  97. set_size_occupied_by_fixed_elements({ 0, m_column_header->height() });
  98. }
  99. TableCellPaintingDelegate* AbstractTableView::column_painting_delegate(int column) const
  100. {
  101. // FIXME: This should return a const pointer I think..
  102. return const_cast<TableCellPaintingDelegate*>(m_column_painting_delegate.get(column).value_or(nullptr));
  103. }
  104. void AbstractTableView::set_column_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate> delegate)
  105. {
  106. if (!delegate)
  107. m_column_painting_delegate.remove(column);
  108. else
  109. m_column_painting_delegate.set(column, move(delegate));
  110. }
  111. int AbstractTableView::column_width(int column_index) const
  112. {
  113. if (!model())
  114. return 0;
  115. return m_column_header->section_size(column_index);
  116. }
  117. void AbstractTableView::set_column_width(int column, int width)
  118. {
  119. column_header().set_section_size(column, width);
  120. }
  121. Gfx::TextAlignment AbstractTableView::column_header_alignment(int column_index) const
  122. {
  123. if (!model())
  124. return Gfx::TextAlignment::CenterLeft;
  125. return m_column_header->section_alignment(column_index);
  126. }
  127. void AbstractTableView::set_column_header_alignment(int column, Gfx::TextAlignment alignment)
  128. {
  129. column_header().set_section_alignment(column, alignment);
  130. }
  131. void AbstractTableView::mousedown_event(MouseEvent& event)
  132. {
  133. if (!model())
  134. return AbstractView::mousedown_event(event);
  135. if (event.button() != MouseButton::Left)
  136. return AbstractView::mousedown_event(event);
  137. auto adjusted_position = this->adjusted_position(event.position());
  138. Gfx::IntPoint horizontally_adjusted_position(adjusted_position.x(), event.position().y());
  139. bool is_toggle;
  140. auto index = index_at_event_position(event.position(), is_toggle);
  141. if (index.is_valid() && is_toggle && model()->row_count(index)) {
  142. toggle_index(index);
  143. return;
  144. }
  145. AbstractView::mousedown_event(event);
  146. }
  147. ModelIndex AbstractTableView::index_at_event_position(const Gfx::IntPoint& position, bool& is_toggle) const
  148. {
  149. is_toggle = false;
  150. if (!model())
  151. return {};
  152. auto adjusted_position = this->adjusted_position(position);
  153. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  154. if (!row_rect(row).contains(adjusted_position))
  155. continue;
  156. for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) {
  157. if (!content_rect(row, column).contains(adjusted_position))
  158. continue;
  159. return model()->index(row, column);
  160. }
  161. return model()->index(row, 0);
  162. }
  163. return {};
  164. }
  165. ModelIndex AbstractTableView::index_at_event_position(const Gfx::IntPoint& position) const
  166. {
  167. bool is_toggle;
  168. auto index = index_at_event_position(position, is_toggle);
  169. return is_toggle ? ModelIndex() : index;
  170. }
  171. int AbstractTableView::item_count() const
  172. {
  173. if (!model())
  174. return 0;
  175. return model()->row_count();
  176. }
  177. void AbstractTableView::move_selection(int vertical_steps, int horizontal_steps)
  178. {
  179. if (!model())
  180. return;
  181. auto& model = *this->model();
  182. ModelIndex new_index;
  183. if (!selection().is_empty()) {
  184. auto old_index = selection().first();
  185. new_index = model.index(old_index.row() + vertical_steps, old_index.column() + horizontal_steps);
  186. } else {
  187. new_index = model.index(0, 0);
  188. }
  189. if (model.is_valid(new_index)) {
  190. selection().set(new_index);
  191. scroll_into_view(new_index, Orientation::Vertical);
  192. update();
  193. }
  194. }
  195. void AbstractTableView::scroll_into_view(const ModelIndex& index, Orientation orientation)
  196. {
  197. auto rect = row_rect(index.row()).translated(0, -m_column_header->height());
  198. ScrollableWidget::scroll_into_view(rect, orientation);
  199. }
  200. void AbstractTableView::scroll_into_view(const ModelIndex& index, bool scroll_horizontally, bool scroll_vertically)
  201. {
  202. auto rect = row_rect(index.row()).translated(0, -m_column_header->height());
  203. ScrollableWidget::scroll_into_view(rect, scroll_horizontally, scroll_vertically);
  204. }
  205. void AbstractTableView::doubleclick_event(MouseEvent& event)
  206. {
  207. if (!model())
  208. return;
  209. if (event.button() == MouseButton::Left) {
  210. if (!selection().is_empty())
  211. activate_or_edit_selected();
  212. }
  213. }
  214. void AbstractTableView::context_menu_event(ContextMenuEvent& event)
  215. {
  216. if (!model())
  217. return;
  218. bool is_toggle;
  219. auto index = index_at_event_position(event.position(), is_toggle);
  220. if (index.is_valid()) {
  221. if (!selection().contains(index))
  222. selection().set(index);
  223. } else {
  224. selection().clear();
  225. }
  226. if (on_context_menu_request)
  227. on_context_menu_request(index, event);
  228. }
  229. Gfx::IntRect AbstractTableView::content_rect(int row, int column) const
  230. {
  231. auto row_rect = this->row_rect(row);
  232. int x = 0;
  233. for (int i = 0; i < column; ++i)
  234. x += column_width(i) + horizontal_padding() * 2;
  235. return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, item_height() };
  236. }
  237. Gfx::IntRect AbstractTableView::content_rect(const ModelIndex& index) const
  238. {
  239. return content_rect(index.row(), index.column());
  240. }
  241. Gfx::IntRect AbstractTableView::row_rect(int item_index) const
  242. {
  243. return { 0, m_column_header->height() + (item_index * item_height()), max(content_size().width(), width()), item_height() };
  244. }
  245. Gfx::IntPoint AbstractTableView::adjusted_position(const Gfx::IntPoint& position) const
  246. {
  247. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  248. }
  249. void AbstractTableView::did_update_model(unsigned flags)
  250. {
  251. AbstractView::did_update_model(flags);
  252. update_column_sizes();
  253. update_content_size();
  254. update();
  255. }
  256. void AbstractTableView::resize_event(ResizeEvent& event)
  257. {
  258. AbstractView::resize_event(event);
  259. if (column_header().is_visible())
  260. column_header().set_relative_rect(frame_thickness(), frame_thickness(), content_width(), column_header().preferred_size().height());
  261. }
  262. void AbstractTableView::header_did_change_section_size(Badge<HeaderView>, Gfx::Orientation, int, int)
  263. {
  264. update_content_size();
  265. update();
  266. }
  267. void AbstractTableView::header_did_change_section_visibility(Badge<HeaderView>, Gfx::Orientation, int, bool)
  268. {
  269. update_content_size();
  270. update();
  271. }
  272. void AbstractTableView::set_column_hidden(int column, bool hidden)
  273. {
  274. column_header().set_section_visible(column, !hidden);
  275. }
  276. void AbstractTableView::set_column_headers_visible(bool visible)
  277. {
  278. column_header().set_visible(visible);
  279. }
  280. void AbstractTableView::did_scroll()
  281. {
  282. AbstractView::did_scroll();
  283. column_header().set_x(frame_thickness() + -horizontal_scrollbar().value());
  284. }
  285. }