AbstractTableView.cpp 19 KB

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