GTableView.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include <LibGUI/GTableView.h>
  2. #include <LibGUI/GTableModel.h>
  3. #include <LibGUI/GScrollBar.h>
  4. #include <SharedGraphics/Painter.h>
  5. #include <Kernel/KeyCode.h>
  6. GTableView::GTableView(GWidget* parent)
  7. : GWidget(parent)
  8. {
  9. set_fill_with_background_color(false);
  10. m_vertical_scrollbar = new GScrollBar(Orientation::Vertical, this);
  11. m_vertical_scrollbar->set_step(4);
  12. m_vertical_scrollbar->on_change = [this] (int) {
  13. update();
  14. };
  15. m_horizontal_scrollbar = new GScrollBar(Orientation::Horizontal, this);
  16. m_horizontal_scrollbar->set_step(4);
  17. m_horizontal_scrollbar->set_big_step(30);
  18. m_horizontal_scrollbar->on_change = [this] (int) {
  19. update();
  20. };
  21. }
  22. GTableView::~GTableView()
  23. {
  24. }
  25. void GTableView::set_model(OwnPtr<GTableModel>&& model)
  26. {
  27. if (model.ptr() == m_model.ptr())
  28. return;
  29. if (m_model)
  30. m_model->unregister_view(Badge<GTableView>(), *this);
  31. m_model = move(model);
  32. if (m_model)
  33. m_model->register_view(Badge<GTableView>(), *this);
  34. }
  35. void GTableView::resize_event(GResizeEvent& event)
  36. {
  37. update_scrollbar_ranges();
  38. m_vertical_scrollbar->set_relative_rect(event.size().width() - m_vertical_scrollbar->preferred_size().width(), 0, m_vertical_scrollbar->preferred_size().width(), event.size().height() - m_horizontal_scrollbar->preferred_size().height());
  39. m_horizontal_scrollbar->set_relative_rect(0, event.size().height() - m_horizontal_scrollbar->preferred_size().height(), event.size().width() - m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height());
  40. }
  41. void GTableView::update_scrollbar_ranges()
  42. {
  43. int available_height = height() - header_height() - m_horizontal_scrollbar->height();
  44. int excess_height = max(0, (item_count() * item_height()) - available_height);
  45. m_vertical_scrollbar->set_range(0, excess_height);
  46. int available_width = width() - m_vertical_scrollbar->width();
  47. int excess_width = max(0, content_width() - available_width);
  48. m_horizontal_scrollbar->set_range(0, excess_width);
  49. m_vertical_scrollbar->set_big_step(visible_content_rect().height() - item_height());
  50. }
  51. int GTableView::content_width() const
  52. {
  53. if (!m_model)
  54. return 0;
  55. int width = 0;
  56. int column_count = m_model->column_count();
  57. for (int i = 0; i < column_count; ++i)
  58. width += m_model->column_metadata(i).preferred_width + horizontal_padding() * 2;
  59. return width;
  60. }
  61. void GTableView::model_notification(const GModelNotification&)
  62. {
  63. }
  64. void GTableView::did_update_model()
  65. {
  66. update_scrollbar_ranges();
  67. update();
  68. model_notification(GModelNotification(GModelNotification::ModelUpdated));
  69. }
  70. Rect GTableView::row_rect(int item_index) const
  71. {
  72. return { 0, header_height() + (item_index * item_height()), max(content_width(), width()), item_height() };
  73. }
  74. void GTableView::mousedown_event(GMouseEvent& event)
  75. {
  76. auto adjusted_position = event.position().translated(0, m_vertical_scrollbar->value());
  77. if (event.button() == GMouseButton::Left) {
  78. for (int i = 0; i < item_count(); ++i) {
  79. if (row_rect(i).contains(adjusted_position)) {
  80. m_model->set_selected_index({ i, 0 });
  81. update();
  82. return;
  83. }
  84. }
  85. }
  86. m_model->set_selected_index({ });
  87. update();
  88. }
  89. void GTableView::paint_event(GPaintEvent& event)
  90. {
  91. Painter painter(*this);
  92. painter.set_clip_rect(event.rect());
  93. painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
  94. int exposed_width = max(content_width(), width());
  95. int painted_item_index = 0;
  96. int y_offset = header_height();
  97. for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
  98. int y = y_offset + painted_item_index * item_height();
  99. Color background_color;
  100. Color text_color;
  101. if (row_index == m_model->selected_index().row()) {
  102. background_color = Color::from_rgb(0x84351a);
  103. text_color = Color::White;
  104. } else {
  105. background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
  106. text_color = Color::Black;
  107. }
  108. painter.fill_rect(row_rect(painted_item_index), background_color);
  109. int x_offset = 0;
  110. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  111. auto column_metadata = m_model->column_metadata(column_index);
  112. int column_width = column_metadata.preferred_width;
  113. Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
  114. auto data = m_model->data(row_index, column_index);
  115. if (data.is_bitmap())
  116. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  117. else
  118. painter.draw_text(cell_rect, data.to_string(), column_metadata.text_alignment, text_color);
  119. x_offset += column_width + horizontal_padding() * 2;
  120. }
  121. ++painted_item_index;
  122. };
  123. Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
  124. painter.fill_rect(unpainted_rect, Color::White);
  125. // Untranslate the painter and paint the column headers.
  126. painter.translate(0, m_vertical_scrollbar->value());
  127. painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
  128. int x_offset = 0;
  129. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  130. auto column_metadata = m_model->column_metadata(column_index);
  131. int column_width = column_metadata.preferred_width;
  132. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, item_height());
  133. painter.set_font(Font::default_bold_font());
  134. painter.draw_text(cell_rect.translated(horizontal_padding(), 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
  135. x_offset += column_width + horizontal_padding() * 2;
  136. painter.draw_line(cell_rect.top_left(), cell_rect.bottom_left(), Color::White);
  137. painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right(), Color::DarkGray);
  138. }
  139. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
  140. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::DarkGray);
  141. // Then untranslate and fill in the scroll corner. This is pretty messy, tbh.
  142. painter.translate(m_horizontal_scrollbar->value(), 0);
  143. painter.fill_rect({ m_horizontal_scrollbar->relative_rect().top_right().translated(1, 0), { m_vertical_scrollbar->preferred_size().width(), m_horizontal_scrollbar->preferred_size().height() } }, Color::LightGray);
  144. }
  145. int GTableView::item_count() const
  146. {
  147. return m_model->row_count();
  148. }
  149. void GTableView::keydown_event(GKeyEvent& event)
  150. {
  151. if (!model())
  152. return;
  153. auto& model = *this->model();
  154. if (event.key() == KeyCode::Key_Return) {
  155. model.activate(model.selected_index());
  156. return;
  157. }
  158. if (event.key() == KeyCode::Key_Up) {
  159. GModelIndex new_index(model.selected_index().row() - 1, model.selected_index().column());
  160. if (model.is_valid(new_index)) {
  161. model.set_selected_index(new_index);
  162. scroll_into_view(new_index, Orientation::Vertical);
  163. update();
  164. }
  165. return;
  166. }
  167. if (event.key() == KeyCode::Key_Down) {
  168. GModelIndex new_index(model.selected_index().row() + 1, model.selected_index().column());
  169. if (model.is_valid(new_index)) {
  170. model.set_selected_index(new_index);
  171. scroll_into_view(new_index, Orientation::Vertical);
  172. update();
  173. }
  174. return;
  175. }
  176. if (event.key() == KeyCode::Key_PageUp) {
  177. int items_per_page = visible_content_rect().height() / item_height();
  178. GModelIndex new_index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
  179. if (model.is_valid(new_index)) {
  180. model.set_selected_index(new_index);
  181. scroll_into_view(new_index, Orientation::Vertical);
  182. update();
  183. }
  184. return;
  185. }
  186. if (event.key() == KeyCode::Key_PageDown) {
  187. int items_per_page = visible_content_rect().height() / item_height();
  188. GModelIndex new_index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
  189. if (model.is_valid(new_index)) {
  190. model.set_selected_index(new_index);
  191. scroll_into_view(new_index, Orientation::Vertical);
  192. update();
  193. }
  194. return;
  195. }
  196. return GWidget::keydown_event(event);
  197. }
  198. Rect GTableView::visible_content_rect() const
  199. {
  200. return {
  201. m_horizontal_scrollbar->value(),
  202. m_vertical_scrollbar->value(),
  203. width() - m_vertical_scrollbar->width(),
  204. height() - header_height() - m_horizontal_scrollbar->height()
  205. };
  206. }
  207. void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  208. {
  209. auto visible_content_rect = this->visible_content_rect();
  210. auto rect = row_rect(index.row()).translated(0, -header_height());
  211. if (visible_content_rect.contains(rect))
  212. return;
  213. if (orientation == Orientation::Vertical) {
  214. if (rect.top() < visible_content_rect.top())
  215. m_vertical_scrollbar->set_value(rect.top());
  216. else if (rect.bottom() > visible_content_rect.bottom())
  217. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
  218. } else {
  219. if (rect.left() < visible_content_rect.left())
  220. m_horizontal_scrollbar->set_value(rect.left());
  221. else if (rect.right() > visible_content_rect.right())
  222. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
  223. }
  224. }