GTableView.cpp 4.3 KB

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