GTableView.cpp 21 KB

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