AbstractTableView.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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/Menu.h>
  31. #include <LibGUI/Model.h>
  32. #include <LibGUI/Painter.h>
  33. #include <LibGUI/ScrollBar.h>
  34. #include <LibGUI/Window.h>
  35. #include <LibGfx/Palette.h>
  36. namespace GUI {
  37. static const int minimum_column_width = 2;
  38. AbstractTableView::AbstractTableView()
  39. {
  40. set_should_hide_unnecessary_scrollbars(true);
  41. }
  42. AbstractTableView::~AbstractTableView()
  43. {
  44. }
  45. void AbstractTableView::select_all()
  46. {
  47. selection().clear();
  48. for (int item_index = 0; item_index < item_count(); ++item_index) {
  49. auto index = model()->index(item_index);
  50. selection().add(index);
  51. }
  52. }
  53. void AbstractTableView::update_column_sizes()
  54. {
  55. if (!m_size_columns_to_fit_content)
  56. return;
  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. int key_column = model.key_column();
  63. for (int column = 0; column < column_count; ++column) {
  64. if (is_column_hidden(column))
  65. continue;
  66. int header_width = header_font().width(model.column_name(column));
  67. if (column == key_column)
  68. header_width += font().width(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
  69. int column_width = header_width;
  70. for (int row = 0; row < row_count; ++row) {
  71. auto cell_data = model.data(model.index(row, column));
  72. int cell_width = 0;
  73. if (cell_data.is_bitmap()) {
  74. cell_width = cell_data.as_bitmap().width();
  75. } else {
  76. cell_width = font().width(cell_data.to_string());
  77. }
  78. column_width = max(column_width, cell_width);
  79. }
  80. auto& column_data = this->column_data(column);
  81. column_data.width = max(column_data.width, column_width);
  82. column_data.has_initialized_width = true;
  83. }
  84. }
  85. void AbstractTableView::update_content_size()
  86. {
  87. if (!model())
  88. return set_content_size({});
  89. int content_width = 0;
  90. int column_count = model()->column_count();
  91. for (int i = 0; i < column_count; ++i) {
  92. if (!is_column_hidden(i))
  93. content_width += column_width(i) + horizontal_padding() * 2;
  94. }
  95. int content_height = item_count() * item_height();
  96. set_content_size({ content_width, content_height });
  97. set_size_occupied_by_fixed_elements({ 0, header_height() });
  98. }
  99. Gfx::Rect AbstractTableView::header_rect(int column_index) const
  100. {
  101. if (!model())
  102. return {};
  103. if (is_column_hidden(column_index))
  104. return {};
  105. int x_offset = 0;
  106. for (int i = 0; i < column_index; ++i) {
  107. if (is_column_hidden(i))
  108. continue;
  109. x_offset += column_width(i) + horizontal_padding() * 2;
  110. }
  111. return { x_offset, 0, column_width(column_index) + horizontal_padding() * 2, header_height() };
  112. }
  113. void AbstractTableView::set_hovered_header_index(int index)
  114. {
  115. if (m_hovered_column_header_index == index)
  116. return;
  117. m_hovered_column_header_index = index;
  118. update_headers();
  119. }
  120. void AbstractTableView::paint_headers(Painter& painter)
  121. {
  122. if (!headers_visible())
  123. return;
  124. int exposed_width = max(content_size().width(), width());
  125. painter.fill_rect({ 0, 0, exposed_width, header_height() }, palette().button());
  126. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, palette().threed_highlight());
  127. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, palette().threed_shadow1());
  128. int x_offset = 0;
  129. int column_count = model()->column_count();
  130. for (int column_index = 0; column_index < column_count; ++column_index) {
  131. if (is_column_hidden(column_index))
  132. continue;
  133. int column_width = this->column_width(column_index);
  134. bool is_key_column = model()->key_column() == column_index;
  135. Gfx::Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
  136. bool pressed = column_index == m_pressed_column_header_index && m_pressed_column_header_is_pressed;
  137. bool hovered = column_index == m_hovered_column_header_index && model()->is_column_sortable(column_index);
  138. Gfx::StylePainter::paint_button(painter, cell_rect, palette(), Gfx::ButtonStyle::Normal, pressed, hovered);
  139. String text;
  140. if (is_key_column) {
  141. StringBuilder builder;
  142. builder.append(model()->column_name(column_index));
  143. auto sort_order = model()->sort_order();
  144. if (sort_order == SortOrder::Ascending)
  145. builder.append(" \xE2\xAC\x86"); // UPWARDS BLACK ARROW
  146. else if (sort_order == SortOrder::Descending)
  147. builder.append(" \xE2\xAC\x87"); // DOWNWARDS BLACK ARROW
  148. text = builder.to_string();
  149. } else {
  150. text = model()->column_name(column_index);
  151. }
  152. auto text_rect = cell_rect.translated(horizontal_padding(), 0);
  153. if (pressed)
  154. text_rect.move_by(1, 1);
  155. painter.draw_text(text_rect, text, header_font(), Gfx::TextAlignment::CenterLeft, palette().button_text());
  156. x_offset += column_width + horizontal_padding() * 2;
  157. }
  158. }
  159. bool AbstractTableView::is_column_hidden(int column) const
  160. {
  161. return !column_data(column).visibility;
  162. }
  163. void AbstractTableView::set_column_hidden(int column, bool hidden)
  164. {
  165. auto& column_data = this->column_data(column);
  166. if (column_data.visibility == !hidden)
  167. return;
  168. column_data.visibility = !hidden;
  169. if (column_data.visibility_action) {
  170. column_data.visibility_action->set_checked(!hidden);
  171. }
  172. update_content_size();
  173. update();
  174. }
  175. Menu& AbstractTableView::ensure_header_context_menu()
  176. {
  177. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  178. // or if the column count/names change.
  179. if (!m_header_context_menu) {
  180. ASSERT(model());
  181. m_header_context_menu = Menu::construct();
  182. for (int column = 0; column < model()->column_count(); ++column) {
  183. auto& column_data = this->column_data(column);
  184. auto name = model()->column_name(column);
  185. column_data.visibility_action = Action::create_checkable(name, [this, column](auto& action) {
  186. set_column_hidden(column, !action.is_checked());
  187. });
  188. column_data.visibility_action->set_checked(column_data.visibility);
  189. m_header_context_menu->add_action(*column_data.visibility_action);
  190. }
  191. }
  192. return *m_header_context_menu;
  193. }
  194. const Gfx::Font& AbstractTableView::header_font()
  195. {
  196. return Gfx::Font::default_bold_font();
  197. }
  198. void AbstractTableView::set_cell_painting_delegate(int column, OwnPtr<TableCellPaintingDelegate>&& delegate)
  199. {
  200. column_data(column).cell_painting_delegate = move(delegate);
  201. }
  202. void AbstractTableView::update_headers()
  203. {
  204. Gfx::Rect rect { 0, 0, frame_inner_rect().width(), header_height() };
  205. rect.move_by(frame_thickness(), frame_thickness());
  206. update(rect);
  207. }
  208. AbstractTableView::ColumnData& AbstractTableView::column_data(int column) const
  209. {
  210. if (static_cast<size_t>(column) >= m_column_data.size())
  211. m_column_data.resize(column + 1);
  212. return m_column_data.at(column);
  213. }
  214. Gfx::Rect AbstractTableView::column_resize_grabbable_rect(int column) const
  215. {
  216. if (!model())
  217. return {};
  218. auto header_rect = this->header_rect(column);
  219. return { header_rect.right() - 1, header_rect.top(), 4, header_rect.height() };
  220. }
  221. int AbstractTableView::column_width(int column_index) const
  222. {
  223. if (!model())
  224. return 0;
  225. auto& column_data = this->column_data(column_index);
  226. if (!column_data.has_initialized_width) {
  227. ASSERT(!m_size_columns_to_fit_content);
  228. column_data.has_initialized_width = true;
  229. column_data.width = model()->column_metadata(column_index).preferred_width;
  230. }
  231. return column_data.width;
  232. }
  233. void AbstractTableView::mousemove_event(MouseEvent& event)
  234. {
  235. if (!model())
  236. return AbstractView::mousemove_event(event);
  237. auto adjusted_position = this->adjusted_position(event.position());
  238. Gfx::Point horizontally_adjusted_position(adjusted_position.x(), event.position().y());
  239. if (m_in_column_resize) {
  240. auto delta = adjusted_position - m_column_resize_origin;
  241. int new_width = m_column_resize_original_width + delta.x();
  242. if (new_width <= minimum_column_width)
  243. new_width = minimum_column_width;
  244. ASSERT(m_resizing_column >= 0 && m_resizing_column < model()->column_count());
  245. auto& column_data = this->column_data(m_resizing_column);
  246. if (column_data.width != new_width) {
  247. column_data.width = new_width;
  248. update_content_size();
  249. update();
  250. }
  251. return;
  252. }
  253. if (m_pressed_column_header_index != -1) {
  254. auto header_rect = this->header_rect(m_pressed_column_header_index);
  255. if (header_rect.contains(horizontally_adjusted_position)) {
  256. set_hovered_header_index(m_pressed_column_header_index);
  257. if (!m_pressed_column_header_is_pressed)
  258. update_headers();
  259. m_pressed_column_header_is_pressed = true;
  260. } else {
  261. set_hovered_header_index(-1);
  262. if (m_pressed_column_header_is_pressed)
  263. update_headers();
  264. m_pressed_column_header_is_pressed = false;
  265. }
  266. return;
  267. }
  268. if (event.buttons() == 0) {
  269. int column_count = model()->column_count();
  270. bool found_hovered_header = false;
  271. for (int i = 0; i < column_count; ++i) {
  272. if (column_resize_grabbable_rect(i).contains(horizontally_adjusted_position)) {
  273. window()->set_override_cursor(StandardCursor::ResizeHorizontal);
  274. set_hovered_header_index(-1);
  275. return;
  276. }
  277. if (header_rect(i).contains(horizontally_adjusted_position)) {
  278. set_hovered_header_index(i);
  279. found_hovered_header = true;
  280. }
  281. }
  282. if (!found_hovered_header)
  283. set_hovered_header_index(-1);
  284. }
  285. window()->set_override_cursor(StandardCursor::None);
  286. AbstractView::mousemove_event(event);
  287. }
  288. void AbstractTableView::mouseup_event(MouseEvent& event)
  289. {
  290. auto adjusted_position = this->adjusted_position(event.position());
  291. Gfx::Point horizontally_adjusted_position(adjusted_position.x(), event.position().y());
  292. if (event.button() == MouseButton::Left) {
  293. if (m_in_column_resize) {
  294. if (!column_resize_grabbable_rect(m_resizing_column).contains(horizontally_adjusted_position))
  295. window()->set_override_cursor(StandardCursor::None);
  296. m_in_column_resize = false;
  297. return;
  298. }
  299. if (m_pressed_column_header_index != -1) {
  300. auto header_rect = this->header_rect(m_pressed_column_header_index);
  301. if (header_rect.contains(horizontally_adjusted_position)) {
  302. auto new_sort_order = SortOrder::Ascending;
  303. if (model()->key_column() == m_pressed_column_header_index)
  304. new_sort_order = model()->sort_order() == SortOrder::Ascending
  305. ? SortOrder::Descending
  306. : SortOrder::Ascending;
  307. model()->set_key_column_and_sort_order(m_pressed_column_header_index, new_sort_order);
  308. }
  309. m_pressed_column_header_index = -1;
  310. m_pressed_column_header_is_pressed = false;
  311. update_headers();
  312. return;
  313. }
  314. }
  315. AbstractView::mouseup_event(event);
  316. }
  317. void AbstractTableView::mousedown_event(MouseEvent& event)
  318. {
  319. if (!model())
  320. return AbstractView::mousedown_event(event);
  321. if (event.button() != MouseButton::Left)
  322. return AbstractView::mousedown_event(event);
  323. auto adjusted_position = this->adjusted_position(event.position());
  324. Gfx::Point horizontally_adjusted_position(adjusted_position.x(), event.position().y());
  325. if (event.y() < header_height()) {
  326. int column_count = model()->column_count();
  327. for (int i = 0; i < column_count; ++i) {
  328. if (column_resize_grabbable_rect(i).contains(horizontally_adjusted_position)) {
  329. m_resizing_column = i;
  330. m_in_column_resize = true;
  331. m_column_resize_original_width = column_width(i);
  332. m_column_resize_origin = adjusted_position;
  333. return;
  334. }
  335. auto header_rect = this->header_rect(i);
  336. if (header_rect.contains(horizontally_adjusted_position) && model()->is_column_sortable(i)) {
  337. m_pressed_column_header_index = i;
  338. m_pressed_column_header_is_pressed = true;
  339. update_headers();
  340. return;
  341. }
  342. }
  343. return;
  344. }
  345. bool is_toggle;
  346. auto index = index_at_event_position(event.position(), is_toggle);
  347. if (index.is_valid() && is_toggle && model()->row_count(index)) {
  348. toggle_index(index);
  349. return;
  350. }
  351. AbstractView::mousedown_event(event);
  352. }
  353. ModelIndex AbstractTableView::index_at_event_position(const Gfx::Point& position, bool& is_toggle) const
  354. {
  355. is_toggle = false;
  356. if (!model())
  357. return {};
  358. auto adjusted_position = this->adjusted_position(position);
  359. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  360. if (!row_rect(row).contains(adjusted_position))
  361. continue;
  362. for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) {
  363. if (!content_rect(row, column).contains(adjusted_position))
  364. continue;
  365. return model()->index(row, column);
  366. }
  367. return model()->index(row, 0);
  368. }
  369. return {};
  370. }
  371. ModelIndex AbstractTableView::index_at_event_position(const Gfx::Point& position) const
  372. {
  373. bool is_toggle;
  374. auto index = index_at_event_position(position, is_toggle);
  375. return is_toggle ? ModelIndex() : index;
  376. }
  377. int AbstractTableView::item_count() const
  378. {
  379. if (!model())
  380. return 0;
  381. return model()->row_count();
  382. }
  383. void AbstractTableView::move_selection(int steps)
  384. {
  385. if (!model())
  386. return;
  387. auto& model = *this->model();
  388. ModelIndex new_index;
  389. if (!selection().is_empty()) {
  390. auto old_index = selection().first();
  391. new_index = model.index(old_index.row() + steps, old_index.column());
  392. } else {
  393. new_index = model.index(0, 0);
  394. }
  395. if (model.is_valid(new_index)) {
  396. selection().set(new_index);
  397. scroll_into_view(new_index, Orientation::Vertical);
  398. update();
  399. }
  400. }
  401. void AbstractTableView::scroll_into_view(const ModelIndex& index, Orientation orientation)
  402. {
  403. auto rect = row_rect(index.row()).translated(0, -header_height());
  404. ScrollableWidget::scroll_into_view(rect, orientation);
  405. }
  406. void AbstractTableView::doubleclick_event(MouseEvent& event)
  407. {
  408. if (!model())
  409. return;
  410. if (event.button() == MouseButton::Left) {
  411. if (event.y() < header_height())
  412. return;
  413. if (!selection().is_empty()) {
  414. if (is_editable())
  415. begin_editing(selection().first());
  416. else
  417. activate_selected();
  418. }
  419. }
  420. }
  421. void AbstractTableView::context_menu_event(ContextMenuEvent& event)
  422. {
  423. if (!model())
  424. return;
  425. if (event.position().y() < header_height()) {
  426. ensure_header_context_menu().popup(event.screen_position());
  427. return;
  428. }
  429. bool is_toggle;
  430. auto index = index_at_event_position(event.position(), is_toggle);
  431. if (index.is_valid()) {
  432. if (!selection().contains(index))
  433. selection().set(index);
  434. } else {
  435. selection().clear();
  436. }
  437. if (on_context_menu_request)
  438. on_context_menu_request(index, event);
  439. }
  440. void AbstractTableView::leave_event(Core::Event& event)
  441. {
  442. AbstractView::leave_event(event);
  443. window()->set_override_cursor(StandardCursor::None);
  444. set_hovered_header_index(-1);
  445. }
  446. Gfx::Rect AbstractTableView::content_rect(int row, int column) const
  447. {
  448. auto row_rect = this->row_rect(row);
  449. int x = 0;
  450. for (int i = 0; i < column; ++i)
  451. x += column_width(i) + horizontal_padding() * 2;
  452. return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, item_height() };
  453. }
  454. Gfx::Rect AbstractTableView::content_rect(const ModelIndex& index) const
  455. {
  456. return content_rect(index.row(), index.column());
  457. }
  458. Gfx::Rect AbstractTableView::row_rect(int item_index) const
  459. {
  460. return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() };
  461. }
  462. Gfx::Point AbstractTableView::adjusted_position(const Gfx::Point& position) const
  463. {
  464. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  465. }
  466. void AbstractTableView::did_update_model(unsigned flags)
  467. {
  468. AbstractView::did_update_model(flags);
  469. update_column_sizes();
  470. update_content_size();
  471. update();
  472. }
  473. }