GTableView.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. if (event.y() < header_height()) {
  77. // FIXME: Do something when clicking on a header.
  78. return;
  79. }
  80. auto adjusted_position = event.position().translated(0, m_vertical_scrollbar->value());
  81. if (event.button() == GMouseButton::Left) {
  82. for (int i = 0; i < item_count(); ++i) {
  83. if (row_rect(i).contains(adjusted_position)) {
  84. m_model->set_selected_index({ i, 0 });
  85. update();
  86. return;
  87. }
  88. }
  89. }
  90. m_model->set_selected_index({ });
  91. update();
  92. }
  93. void GTableView::paint_event(GPaintEvent& event)
  94. {
  95. Painter painter(*this);
  96. painter.set_clip_rect(event.rect());
  97. painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
  98. int exposed_width = max(content_width(), width());
  99. int painted_item_index = 0;
  100. int y_offset = header_height();
  101. for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
  102. int y = y_offset + painted_item_index * item_height();
  103. Color background_color;
  104. Color text_color;
  105. if (row_index == m_model->selected_index().row()) {
  106. background_color = Color::from_rgb(0x84351a);
  107. text_color = Color::White;
  108. } else {
  109. background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
  110. text_color = Color::Black;
  111. }
  112. painter.fill_rect(row_rect(painted_item_index), background_color);
  113. int x_offset = 0;
  114. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  115. auto column_metadata = m_model->column_metadata(column_index);
  116. int column_width = column_metadata.preferred_width;
  117. Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
  118. auto data = m_model->data(row_index, column_index);
  119. if (data.is_bitmap())
  120. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  121. else
  122. painter.draw_text(cell_rect, data.to_string(), column_metadata.text_alignment, text_color);
  123. x_offset += column_width + horizontal_padding() * 2;
  124. }
  125. ++painted_item_index;
  126. };
  127. Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
  128. painter.fill_rect(unpainted_rect, Color::White);
  129. // Untranslate the painter and paint the column headers.
  130. painter.translate(0, m_vertical_scrollbar->value());
  131. painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
  132. int x_offset = 0;
  133. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  134. auto column_metadata = m_model->column_metadata(column_index);
  135. int column_width = column_metadata.preferred_width;
  136. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, item_height());
  137. painter.set_font(Font::default_bold_font());
  138. painter.draw_text(cell_rect.translated(horizontal_padding(), 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
  139. x_offset += column_width + horizontal_padding() * 2;
  140. painter.draw_line(cell_rect.top_left(), cell_rect.bottom_left(), Color::White);
  141. painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right(), Color::DarkGray);
  142. }
  143. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
  144. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::DarkGray);
  145. // Then untranslate and fill in the scroll corner. This is pretty messy, tbh.
  146. painter.translate(m_horizontal_scrollbar->value(), 0);
  147. 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);
  148. if (is_focused()) {
  149. Rect item_area_rect {
  150. 0,
  151. header_height(),
  152. width() - m_vertical_scrollbar->width(),
  153. height() - header_height() - m_horizontal_scrollbar->height()
  154. };
  155. painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
  156. };
  157. }
  158. int GTableView::item_count() const
  159. {
  160. return m_model->row_count();
  161. }
  162. void GTableView::keydown_event(GKeyEvent& event)
  163. {
  164. if (!model())
  165. return;
  166. auto& model = *this->model();
  167. if (event.key() == KeyCode::Key_Return) {
  168. model.activate(model.selected_index());
  169. return;
  170. }
  171. if (event.key() == KeyCode::Key_Up) {
  172. GModelIndex new_index(model.selected_index().row() - 1, model.selected_index().column());
  173. if (model.is_valid(new_index)) {
  174. model.set_selected_index(new_index);
  175. scroll_into_view(new_index, Orientation::Vertical);
  176. update();
  177. }
  178. return;
  179. }
  180. if (event.key() == KeyCode::Key_Down) {
  181. GModelIndex new_index(model.selected_index().row() + 1, model.selected_index().column());
  182. if (model.is_valid(new_index)) {
  183. model.set_selected_index(new_index);
  184. scroll_into_view(new_index, Orientation::Vertical);
  185. update();
  186. }
  187. return;
  188. }
  189. if (event.key() == KeyCode::Key_PageUp) {
  190. int items_per_page = visible_content_rect().height() / item_height();
  191. GModelIndex new_index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
  192. if (model.is_valid(new_index)) {
  193. model.set_selected_index(new_index);
  194. scroll_into_view(new_index, Orientation::Vertical);
  195. update();
  196. }
  197. return;
  198. }
  199. if (event.key() == KeyCode::Key_PageDown) {
  200. int items_per_page = visible_content_rect().height() / item_height();
  201. GModelIndex new_index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
  202. if (model.is_valid(new_index)) {
  203. model.set_selected_index(new_index);
  204. scroll_into_view(new_index, Orientation::Vertical);
  205. update();
  206. }
  207. return;
  208. }
  209. return GWidget::keydown_event(event);
  210. }
  211. Rect GTableView::visible_content_rect() const
  212. {
  213. return {
  214. m_horizontal_scrollbar->value(),
  215. m_vertical_scrollbar->value(),
  216. width() - m_vertical_scrollbar->width(),
  217. height() - header_height() - m_horizontal_scrollbar->height()
  218. };
  219. }
  220. void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  221. {
  222. auto visible_content_rect = this->visible_content_rect();
  223. auto rect = row_rect(index.row()).translated(0, -header_height());
  224. if (visible_content_rect.contains(rect))
  225. return;
  226. if (orientation == Orientation::Vertical) {
  227. if (rect.top() < visible_content_rect.top())
  228. m_vertical_scrollbar->set_value(rect.top());
  229. else if (rect.bottom() > visible_content_rect.bottom())
  230. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
  231. } else {
  232. if (rect.left() < visible_content_rect.left())
  233. m_horizontal_scrollbar->set_value(rect.left());
  234. else if (rect.right() > visible_content_rect.right())
  235. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
  236. }
  237. }