AbstractTableView.cpp 18 KB

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