GTableView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. int GTableView::column_width(int column_index) const
  75. {
  76. return m_model->column_metadata(column_index).preferred_width;
  77. }
  78. Rect GTableView::header_rect(int column_index) const
  79. {
  80. int x_offset = 0;
  81. for (int i = 0; i < column_index; ++i)
  82. x_offset += column_width(i) + horizontal_padding() * 2;
  83. auto column_metadata = m_model->column_metadata(column_index);
  84. int column_width = column_metadata.preferred_width;
  85. return { x_offset, 0, column_width + horizontal_padding() * 2, header_height() };
  86. }
  87. void GTableView::mousedown_event(GMouseEvent& event)
  88. {
  89. if (event.y() < header_height()) {
  90. auto adjusted_position = event.position().translated(m_horizontal_scrollbar->value(), 0);
  91. for (int i = 0; i < m_model->column_count(); ++i) {
  92. auto header_rect = this->header_rect(i);
  93. if (header_rect.contains(adjusted_position)) {
  94. auto new_sort_order = GSortOrder::Ascending;
  95. if (m_model->key_column() == i)
  96. new_sort_order = m_model->sort_order() == GSortOrder::Ascending
  97. ? GSortOrder::Descending
  98. : GSortOrder::Ascending;
  99. m_model->set_key_column_and_sort_order(i, new_sort_order);
  100. return;
  101. }
  102. }
  103. return;
  104. }
  105. if (event.button() == GMouseButton::Left) {
  106. auto adjusted_position = event.position().translated(0, m_vertical_scrollbar->value());
  107. for (int i = 0; i < item_count(); ++i) {
  108. if (row_rect(i).contains(adjusted_position)) {
  109. m_model->set_selected_index({ i, 0 });
  110. update();
  111. return;
  112. }
  113. }
  114. m_model->set_selected_index({ });
  115. update();
  116. }
  117. }
  118. void GTableView::paint_event(GPaintEvent& event)
  119. {
  120. Painter painter(*this);
  121. painter.set_clip_rect(event.rect());
  122. painter.translate(-m_horizontal_scrollbar->value(), -m_vertical_scrollbar->value());
  123. int exposed_width = max(content_width(), width());
  124. int painted_item_index = 0;
  125. int y_offset = header_height();
  126. for (int row_index = 0; row_index < m_model->row_count(); ++row_index) {
  127. int y = y_offset + painted_item_index * item_height();
  128. Color background_color;
  129. Color key_column_background_color;
  130. Color text_color;
  131. if (row_index == m_model->selected_index().row()) {
  132. background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  133. key_column_background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  134. text_color = Color::White;
  135. } else {
  136. background_color = painted_item_index % 2 ? Color(210, 210, 210) : Color::White;
  137. key_column_background_color = painted_item_index % 2 ? Color(190, 190, 190) : Color(235, 235, 235);
  138. text_color = Color::Black;
  139. }
  140. painter.fill_rect(row_rect(painted_item_index), background_color);
  141. int x_offset = 0;
  142. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  143. auto column_metadata = m_model->column_metadata(column_index);
  144. int column_width = column_metadata.preferred_width;
  145. bool is_key_column = m_model->key_column() == column_index;
  146. Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
  147. if (is_key_column) {
  148. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  149. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  150. }
  151. auto data = m_model->data({ row_index, column_index });
  152. if (data.is_bitmap())
  153. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  154. else
  155. painter.draw_text(cell_rect, data.to_string(), column_metadata.text_alignment, text_color);
  156. x_offset += column_width + horizontal_padding() * 2;
  157. }
  158. ++painted_item_index;
  159. };
  160. Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
  161. painter.fill_rect(unpainted_rect, Color::White);
  162. // Untranslate the painter and paint the column headers.
  163. painter.translate(0, m_vertical_scrollbar->value());
  164. painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
  165. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
  166. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::DarkGray);
  167. int x_offset = 0;
  168. for (int column_index = 0; column_index < m_model->column_count(); ++column_index) {
  169. auto column_metadata = m_model->column_metadata(column_index);
  170. int column_width = column_metadata.preferred_width;
  171. bool is_key_column = m_model->key_column() == column_index;
  172. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
  173. if (is_key_column) {
  174. painter.fill_rect(cell_rect.shrunken(2, 2), Color::from_rgb(0xdddddd));
  175. }
  176. painter.set_font(Font::default_bold_font());
  177. painter.draw_text(cell_rect.translated(horizontal_padding(), 0), m_model->column_name(column_index), TextAlignment::CenterLeft, Color::Black);
  178. x_offset += column_width + horizontal_padding() * 2;
  179. // Draw column separator.
  180. painter.draw_line(cell_rect.top_left().translated(0, 1), cell_rect.bottom_left().translated(0, -1), Color::White);
  181. painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right().translated(0, -1), Color::DarkGray);
  182. }
  183. // Draw the "start" of a new column to make the last separator look right.
  184. painter.draw_line({ x_offset, 1 }, { x_offset, header_height() - 2 }, Color::White);
  185. // Then untranslate and fill in the scroll corner. This is pretty messy, tbh.
  186. painter.translate(m_horizontal_scrollbar->value(), 0);
  187. 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);
  188. if (is_focused()) {
  189. Rect item_area_rect {
  190. 0,
  191. header_height(),
  192. width() - m_vertical_scrollbar->width(),
  193. height() - header_height() - m_horizontal_scrollbar->height()
  194. };
  195. painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
  196. };
  197. }
  198. int GTableView::item_count() const
  199. {
  200. return m_model->row_count();
  201. }
  202. void GTableView::keydown_event(GKeyEvent& event)
  203. {
  204. if (!model())
  205. return;
  206. auto& model = *this->model();
  207. if (event.key() == KeyCode::Key_Return) {
  208. model.activate(model.selected_index());
  209. return;
  210. }
  211. if (event.key() == KeyCode::Key_Up) {
  212. GModelIndex new_index;
  213. if (model.selected_index().is_valid())
  214. new_index = { model.selected_index().row() - 1, model.selected_index().column() };
  215. else
  216. new_index = { 0, 0 };
  217. if (model.is_valid(new_index)) {
  218. model.set_selected_index(new_index);
  219. scroll_into_view(new_index, Orientation::Vertical);
  220. update();
  221. }
  222. return;
  223. }
  224. if (event.key() == KeyCode::Key_Down) {
  225. GModelIndex new_index;
  226. if (model.selected_index().is_valid())
  227. new_index = { model.selected_index().row() + 1, model.selected_index().column() };
  228. else
  229. new_index = { 0, 0 };
  230. if (model.is_valid(new_index)) {
  231. model.set_selected_index(new_index);
  232. scroll_into_view(new_index, Orientation::Vertical);
  233. update();
  234. }
  235. return;
  236. }
  237. if (event.key() == KeyCode::Key_PageUp) {
  238. int items_per_page = visible_content_rect().height() / item_height();
  239. GModelIndex new_index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
  240. if (model.is_valid(new_index)) {
  241. model.set_selected_index(new_index);
  242. scroll_into_view(new_index, Orientation::Vertical);
  243. update();
  244. }
  245. return;
  246. }
  247. if (event.key() == KeyCode::Key_PageDown) {
  248. int items_per_page = visible_content_rect().height() / item_height();
  249. GModelIndex new_index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
  250. if (model.is_valid(new_index)) {
  251. model.set_selected_index(new_index);
  252. scroll_into_view(new_index, Orientation::Vertical);
  253. update();
  254. }
  255. return;
  256. }
  257. return GWidget::keydown_event(event);
  258. }
  259. Rect GTableView::visible_content_rect() const
  260. {
  261. return {
  262. m_horizontal_scrollbar->value(),
  263. m_vertical_scrollbar->value(),
  264. width() - m_vertical_scrollbar->width(),
  265. height() - header_height() - m_horizontal_scrollbar->height()
  266. };
  267. }
  268. void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  269. {
  270. auto visible_content_rect = this->visible_content_rect();
  271. auto rect = row_rect(index.row()).translated(0, -header_height());
  272. if (visible_content_rect.contains(rect))
  273. return;
  274. if (orientation == Orientation::Vertical) {
  275. if (rect.top() < visible_content_rect.top())
  276. m_vertical_scrollbar->set_value(rect.top());
  277. else if (rect.bottom() > visible_content_rect.bottom())
  278. m_vertical_scrollbar->set_value(rect.bottom() - visible_content_rect.height());
  279. } else {
  280. if (rect.left() < visible_content_rect.left())
  281. m_horizontal_scrollbar->set_value(rect.left());
  282. else if (rect.right() > visible_content_rect.right())
  283. m_horizontal_scrollbar->set_value(rect.right() - visible_content_rect.width());
  284. }
  285. }