GTableView.cpp 13 KB

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