GTableView.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <LibGUI/GTableView.h>
  2. #include <LibGUI/GTableModel.h>
  3. #include <LibGUI/GScrollBar.h>
  4. #include <SharedGraphics/Painter.h>
  5. GTableView::GTableView(GWidget* parent)
  6. : GWidget(parent)
  7. {
  8. set_fill_with_background_color(false);
  9. m_scrollbar = new GScrollBar(Orientation::Vertical, this);
  10. m_scrollbar->set_step(4);
  11. m_scrollbar->set_big_step(30);
  12. m_scrollbar->on_change = [this] (int) {
  13. update();
  14. };
  15. }
  16. GTableView::~GTableView()
  17. {
  18. }
  19. void GTableView::set_model(OwnPtr<GTableModel>&& model)
  20. {
  21. if (model.ptr() == m_model.ptr())
  22. return;
  23. if (m_model)
  24. m_model->unregister_view(Badge<GTableView>(), *this);
  25. m_model = move(model);
  26. if (m_model)
  27. m_model->register_view(Badge<GTableView>(), *this);
  28. }
  29. void GTableView::resize_event(GResizeEvent& event)
  30. {
  31. m_scrollbar->set_relative_rect(event.size().width() - m_scrollbar->preferred_size().width(), 0, m_scrollbar->preferred_size().width(), event.size().height());
  32. update_scrollbar_range();
  33. }
  34. void GTableView::update_scrollbar_range()
  35. {
  36. int excess_height = max(0, (item_count() * item_height()) - height());
  37. m_scrollbar->set_range(0, excess_height);
  38. }
  39. void GTableView::did_update_model()
  40. {
  41. update_scrollbar_range();
  42. update();
  43. }
  44. Rect GTableView::row_rect(int item_index) const
  45. {
  46. return { 0, header_height() + (item_index * item_height()), width(), item_height() };
  47. }
  48. void GTableView::mousedown_event(GMouseEvent& event)
  49. {
  50. auto adjusted_position = event.position().translated(0, m_scrollbar->value());
  51. if (event.button() == GMouseButton::Left) {
  52. for (int i = 0; i < item_count(); ++i) {
  53. if (row_rect(i).contains(adjusted_position)) {
  54. m_model->set_selected_index({ i, 0 });
  55. update();
  56. return;
  57. }
  58. }
  59. }
  60. m_model->set_selected_index({ });
  61. update();
  62. }
  63. void GTableView::paint_event(GPaintEvent&)
  64. {
  65. Painter painter(*this);
  66. painter.translate(0, -m_scrollbar->value());
  67. int horizontal_padding = 5;
  68. int painted_item_index = 0;
  69. int y_offset = header_height();
  70. for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
  71. int y = y_offset + painted_item_index * item_height();
  72. Color background_color;
  73. Color text_color;
  74. if (row_index == m_model->selected_index().row()) {
  75. background_color = Color::from_rgb(0x84351a);
  76. text_color = Color::White;
  77. } else {
  78. background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
  79. text_color = Color::Black;
  80. }
  81. painter.fill_rect(row_rect(painted_item_index), background_color);
  82. int x_offset = 0;
  83. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  84. auto column_metadata = m_model->column_metadata(column_index);
  85. int column_width = column_metadata.preferred_width;
  86. Rect cell_rect(horizontal_padding + x_offset, y, column_width, item_height());
  87. painter.draw_text(cell_rect, m_model->data(row_index, column_index), column_metadata.text_alignment, text_color);
  88. x_offset += column_width + horizontal_padding * 2;
  89. }
  90. ++painted_item_index;
  91. };
  92. Rect unpainted_rect(0, painted_item_index * item_height(), width(), height());
  93. unpainted_rect.intersect(rect());
  94. painter.fill_rect(unpainted_rect, Color::White);
  95. // Untranslate the painter and paint the column headers.
  96. painter.translate(0, m_scrollbar->value());
  97. painter.fill_rect({ 0, 0, width(), header_height() }, Color::LightGray);
  98. int x_offset = 0;
  99. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  100. auto column_metadata = m_model->column_metadata(column_index);
  101. int column_width = column_metadata.preferred_width;
  102. Rect cell_rect(x_offset, 0, column_width + horizontal_padding * 2, item_height());
  103. painter.draw_text(cell_rect.translated(horizontal_padding, 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
  104. x_offset += column_width + horizontal_padding * 2;
  105. painter.draw_line(cell_rect.top_left(), cell_rect.bottom_left(), Color::White);
  106. painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right(), Color::DarkGray);
  107. }
  108. painter.draw_line({ 0, 0 }, { width() - 1, 0 }, Color::White);
  109. painter.draw_line({ 0, header_height() - 1 }, { width() - 1, header_height() - 1 }, Color::DarkGray);
  110. }
  111. int GTableView::item_count() const
  112. {
  113. return m_model->row_count();
  114. }