GTableView.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  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. : GScrollableWidget(parent)
  8. {
  9. }
  10. GTableView::~GTableView()
  11. {
  12. }
  13. void GTableView::set_model(OwnPtr<GTableModel>&& model)
  14. {
  15. if (model.ptr() == m_model.ptr())
  16. return;
  17. if (m_model)
  18. m_model->unregister_view(Badge<GTableView>(), *this);
  19. m_model = move(model);
  20. if (m_model)
  21. m_model->register_view(Badge<GTableView>(), *this);
  22. update_content_size();
  23. }
  24. void GTableView::model_notification(const GModelNotification&)
  25. {
  26. }
  27. void GTableView::update_content_size()
  28. {
  29. if (!m_model)
  30. return set_content_size({ });
  31. int content_width = 0;
  32. int column_count = m_model->column_count();
  33. for (int i = 0; i < column_count; ++i)
  34. content_width += m_model->column_metadata(i).preferred_width + horizontal_padding() * 2;
  35. int content_height = item_count() * item_height();
  36. set_content_size({ content_width, content_height });
  37. set_size_occupied_by_fixed_elements({ 0, header_height() });
  38. }
  39. void GTableView::did_update_model()
  40. {
  41. update_content_size();
  42. update();
  43. model_notification(GModelNotification(GModelNotification::ModelUpdated));
  44. }
  45. Rect GTableView::row_rect(int item_index) const
  46. {
  47. return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() };
  48. }
  49. int GTableView::column_width(int column_index) const
  50. {
  51. return m_model->column_metadata(column_index).preferred_width;
  52. }
  53. Rect GTableView::header_rect(int column_index) const
  54. {
  55. int x_offset = 0;
  56. for (int i = 0; i < column_index; ++i)
  57. x_offset += column_width(i) + horizontal_padding() * 2;
  58. auto column_metadata = m_model->column_metadata(column_index);
  59. int column_width = column_metadata.preferred_width;
  60. return { x_offset, 0, column_width + horizontal_padding() * 2, header_height() };
  61. }
  62. void GTableView::mousedown_event(GMouseEvent& event)
  63. {
  64. if (event.y() < header_height()) {
  65. auto adjusted_position = event.position().translated(horizontal_scrollbar().value(), 0);
  66. for (int i = 0; i < m_model->column_count(); ++i) {
  67. auto header_rect = this->header_rect(i);
  68. if (header_rect.contains(adjusted_position)) {
  69. auto new_sort_order = GSortOrder::Ascending;
  70. if (m_model->key_column() == i)
  71. new_sort_order = m_model->sort_order() == GSortOrder::Ascending
  72. ? GSortOrder::Descending
  73. : GSortOrder::Ascending;
  74. m_model->set_key_column_and_sort_order(i, new_sort_order);
  75. return;
  76. }
  77. }
  78. return;
  79. }
  80. if (event.button() == GMouseButton::Left) {
  81. auto adjusted_position = event.position().translated(0, vertical_scrollbar().value());
  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. m_model->set_selected_index({ });
  90. update();
  91. }
  92. }
  93. void GTableView::paint_event(GPaintEvent& event)
  94. {
  95. Painter painter(*this);
  96. painter.set_clip_rect(event.rect());
  97. painter.save();
  98. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  99. int exposed_width = max(content_size().width(), width());
  100. int painted_item_index = 0;
  101. int y_offset = header_height();
  102. for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
  103. int y = y_offset + painted_item_index * item_height();
  104. Color background_color;
  105. Color key_column_background_color;
  106. Color text_color;
  107. if (row_index == m_model->selected_index().row()) {
  108. background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  109. key_column_background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  110. text_color = Color::White;
  111. } else {
  112. if (alternating_row_colors() && (painted_item_index % 2)) {
  113. background_color = Color(210, 210, 210);
  114. key_column_background_color = Color(190, 190, 190);
  115. } else {
  116. background_color = Color::White;
  117. key_column_background_color = Color(235, 235, 235);
  118. }
  119. text_color = Color::Black;
  120. }
  121. painter.fill_rect(row_rect(painted_item_index), background_color);
  122. int x_offset = 0;
  123. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  124. auto column_metadata = m_model->column_metadata(column_index);
  125. int column_width = column_metadata.preferred_width;
  126. const Font& font = column_metadata.font ? *column_metadata.font : this->font();
  127. bool is_key_column = m_model->key_column() == column_index;
  128. Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
  129. if (is_key_column) {
  130. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  131. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  132. }
  133. auto data = m_model->data({ row_index, column_index });
  134. if (data.is_bitmap())
  135. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  136. else
  137. painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color);
  138. x_offset += column_width + horizontal_padding() * 2;
  139. }
  140. ++painted_item_index;
  141. };
  142. Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
  143. painter.fill_rect(unpainted_rect, Color::White);
  144. // Untranslate the painter vertically and do the column headers.
  145. painter.translate(0, vertical_scrollbar().value());
  146. if (headers_visible())
  147. paint_headers(painter);
  148. painter.restore();
  149. if (is_focused()) {
  150. Rect item_area_rect {
  151. 0,
  152. header_height(),
  153. width() - vertical_scrollbar().width(),
  154. height() - header_height() - horizontal_scrollbar().height()
  155. };
  156. painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
  157. };
  158. }
  159. void GTableView::paint_headers(Painter& painter)
  160. {
  161. int exposed_width = max(content_size().width(), width());
  162. painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
  163. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
  164. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::DarkGray);
  165. int x_offset = 0;
  166. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  167. auto column_metadata = m_model->column_metadata(column_index);
  168. int column_width = column_metadata.preferred_width;
  169. bool is_key_column = m_model->key_column() == column_index;
  170. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
  171. if (is_key_column) {
  172. painter.fill_rect(cell_rect.shrunken(2, 2), Color::from_rgb(0xdddddd));
  173. }
  174. painter.draw_text(cell_rect.translated(horizontal_padding(), 0), m_model->column_name(column_index), Font::default_bold_font(), TextAlignment::CenterLeft, Color::Black);
  175. x_offset += column_width + horizontal_padding() * 2;
  176. // Draw column separator.
  177. painter.draw_line(cell_rect.top_left().translated(0, 1), cell_rect.bottom_left().translated(0, -1), Color::White);
  178. painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right().translated(0, -1), Color::DarkGray);
  179. }
  180. // Draw the "start" of a new column to make the last separator look right.
  181. painter.draw_line({ x_offset, 1 }, { x_offset, header_height() - 2 }, Color::White);
  182. }
  183. int GTableView::item_count() const
  184. {
  185. return m_model->row_count();
  186. }
  187. void GTableView::keydown_event(GKeyEvent& event)
  188. {
  189. if (!model())
  190. return;
  191. auto& model = *this->model();
  192. if (event.key() == KeyCode::Key_Return) {
  193. model.activate(model.selected_index());
  194. return;
  195. }
  196. if (event.key() == KeyCode::Key_Up) {
  197. GModelIndex new_index;
  198. if (model.selected_index().is_valid())
  199. new_index = { model.selected_index().row() - 1, model.selected_index().column() };
  200. else
  201. new_index = { 0, 0 };
  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. if (event.key() == KeyCode::Key_Down) {
  210. GModelIndex new_index;
  211. if (model.selected_index().is_valid())
  212. new_index = { model.selected_index().row() + 1, model.selected_index().column() };
  213. else
  214. new_index = { 0, 0 };
  215. if (model.is_valid(new_index)) {
  216. model.set_selected_index(new_index);
  217. scroll_into_view(new_index, Orientation::Vertical);
  218. update();
  219. }
  220. return;
  221. }
  222. if (event.key() == KeyCode::Key_PageUp) {
  223. int items_per_page = visible_content_rect().height() / item_height();
  224. GModelIndex new_index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
  225. if (model.is_valid(new_index)) {
  226. model.set_selected_index(new_index);
  227. scroll_into_view(new_index, Orientation::Vertical);
  228. update();
  229. }
  230. return;
  231. }
  232. if (event.key() == KeyCode::Key_PageDown) {
  233. int items_per_page = visible_content_rect().height() / item_height();
  234. GModelIndex new_index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
  235. if (model.is_valid(new_index)) {
  236. model.set_selected_index(new_index);
  237. scroll_into_view(new_index, Orientation::Vertical);
  238. update();
  239. }
  240. return;
  241. }
  242. return GWidget::keydown_event(event);
  243. }
  244. void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  245. {
  246. auto rect = row_rect(index.row()).translated(0, -header_height());
  247. GScrollableWidget::scroll_into_view(rect, orientation);
  248. }