AbstractTableView.cpp 14 KB

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