GTableView.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. #include <LibGUI/GTableView.h>
  2. #include <LibGUI/GModel.h>
  3. #include <LibGUI/GScrollBar.h>
  4. #include <LibGUI/GPainter.h>
  5. #include <Kernel/KeyCode.h>
  6. GTableView::GTableView(GWidget* parent)
  7. : GAbstractView(parent)
  8. {
  9. set_frame_shape(FrameShape::Container);
  10. set_frame_shadow(FrameShadow::Sunken);
  11. set_frame_thickness(2);
  12. }
  13. GTableView::~GTableView()
  14. {
  15. }
  16. void GTableView::update_content_size()
  17. {
  18. if (!model())
  19. return set_content_size({ });
  20. int content_width = 0;
  21. int column_count = model()->column_count();
  22. for (int i = 0; i < column_count; ++i)
  23. content_width += model()->column_metadata(i).preferred_width + horizontal_padding() * 2;
  24. int content_height = item_count() * item_height();
  25. set_content_size({ content_width, content_height });
  26. set_size_occupied_by_fixed_elements({ 0, header_height() });
  27. }
  28. void GTableView::did_update_model()
  29. {
  30. GAbstractView::did_update_model();
  31. update_content_size();
  32. update();
  33. }
  34. Rect GTableView::row_rect(int item_index) const
  35. {
  36. return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() };
  37. }
  38. int GTableView::column_width(int column_index) const
  39. {
  40. return model()->column_metadata(column_index).preferred_width;
  41. }
  42. Rect GTableView::header_rect(int column_index) const
  43. {
  44. if (is_column_hidden(column_index))
  45. return { };
  46. int x_offset = 0;
  47. for (int i = 0; i < column_index; ++i) {
  48. if (is_column_hidden(i))
  49. continue;
  50. x_offset += column_width(i) + horizontal_padding() * 2;
  51. }
  52. auto column_metadata = model()->column_metadata(column_index);
  53. int column_width = column_metadata.preferred_width;
  54. return { x_offset, 0, column_width + horizontal_padding() * 2, header_height() };
  55. }
  56. void GTableView::mousedown_event(GMouseEvent& event)
  57. {
  58. if (!model())
  59. return;
  60. if (event.y() < header_height()) {
  61. auto adjusted_position = event.position().translated(horizontal_scrollbar().value(), 0);
  62. for (int i = 0; i < model()->column_count(); ++i) {
  63. auto header_rect = this->header_rect(i);
  64. if (header_rect.contains(adjusted_position)) {
  65. auto new_sort_order = GSortOrder::Ascending;
  66. if (model()->key_column() == i)
  67. new_sort_order = model()->sort_order() == GSortOrder::Ascending
  68. ? GSortOrder::Descending
  69. : GSortOrder::Ascending;
  70. model()->set_key_column_and_sort_order(i, new_sort_order);
  71. return;
  72. }
  73. }
  74. return;
  75. }
  76. if (event.button() == GMouseButton::Left) {
  77. auto adjusted_position = event.position().translated(0, vertical_scrollbar().value());
  78. for (int i = 0; i < item_count(); ++i) {
  79. if (row_rect(i).contains(adjusted_position)) {
  80. model()->set_selected_index(model()->index(i, 0));
  81. update();
  82. return;
  83. }
  84. }
  85. model()->set_selected_index({ });
  86. update();
  87. }
  88. }
  89. void GTableView::paint_event(GPaintEvent& event)
  90. {
  91. GFrame::paint_event(event);
  92. if (!model())
  93. return;
  94. GPainter painter(*this);
  95. painter.add_clip_rect(frame_inner_rect());
  96. painter.add_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 < model()->row_count(); ++row_index) {
  103. bool is_selected_row = row_index == model()->selected_index().row();
  104. int y = y_offset + painted_item_index * item_height();
  105. Color background_color;
  106. Color key_column_background_color;
  107. if (is_selected_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. } else {
  111. if (alternating_row_colors() && (painted_item_index % 2)) {
  112. background_color = Color(210, 210, 210);
  113. key_column_background_color = Color(190, 190, 190);
  114. } else {
  115. background_color = Color::White;
  116. key_column_background_color = Color(235, 235, 235);
  117. }
  118. }
  119. painter.fill_rect(row_rect(painted_item_index), background_color);
  120. int x_offset = 0;
  121. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  122. if (is_column_hidden(column_index))
  123. continue;
  124. auto column_metadata = 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 = 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 cell_index = model()->index(row_index, column_index);
  134. auto data = model()->data(cell_index);
  135. if (data.is_bitmap()) {
  136. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  137. } else if (data.is_icon()) {
  138. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  139. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  140. } else {
  141. Color text_color;
  142. if (is_selected_row)
  143. text_color = Color::White;
  144. else
  145. text_color = model()->data(cell_index, GModel::Role::ForegroundColor).to_color(Color::Black);
  146. painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color);
  147. }
  148. x_offset += column_width + horizontal_padding() * 2;
  149. }
  150. ++painted_item_index;
  151. };
  152. Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
  153. painter.fill_rect(unpainted_rect, Color::White);
  154. // Untranslate the painter vertically and do the column headers.
  155. painter.translate(0, vertical_scrollbar().value());
  156. if (headers_visible())
  157. paint_headers(painter);
  158. painter.restore();
  159. if (is_focused()) {
  160. Rect item_area_rect {
  161. 0,
  162. header_height(),
  163. width() - vertical_scrollbar().width(),
  164. height() - header_height() - horizontal_scrollbar().height()
  165. };
  166. painter.draw_rect(item_area_rect, Color::from_rgb(0x84351a));
  167. };
  168. }
  169. void GTableView::paint_headers(Painter& painter)
  170. {
  171. int exposed_width = max(content_size().width(), width());
  172. painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::LightGray);
  173. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
  174. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::MidGray);
  175. int x_offset = 0;
  176. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  177. if (is_column_hidden(column_index))
  178. continue;
  179. auto column_metadata = model()->column_metadata(column_index);
  180. int column_width = column_metadata.preferred_width;
  181. bool is_key_column = model()->key_column() == column_index;
  182. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
  183. if (is_key_column) {
  184. painter.fill_rect(cell_rect.shrunken(2, 2), Color::from_rgb(0xdddddd));
  185. }
  186. painter.draw_text(cell_rect.translated(horizontal_padding(), 0), model()->column_name(column_index), Font::default_bold_font(), TextAlignment::CenterLeft, Color::Black);
  187. x_offset += column_width + horizontal_padding() * 2;
  188. // Draw column separator.
  189. painter.draw_line(cell_rect.top_left().translated(0, 1), cell_rect.bottom_left().translated(0, -1), Color::White);
  190. painter.draw_line(cell_rect.top_right(), cell_rect.bottom_right().translated(0, -1), Color::MidGray);
  191. }
  192. // Draw the "start" of a new column to make the last separator look right.
  193. painter.draw_line({ x_offset, 1 }, { x_offset, header_height() - 2 }, Color::White);
  194. }
  195. int GTableView::item_count() const
  196. {
  197. if (!model())
  198. return 0;
  199. return model()->row_count();
  200. }
  201. void GTableView::keydown_event(GKeyEvent& event)
  202. {
  203. if (!model())
  204. return;
  205. auto& model = *this->model();
  206. if (event.key() == KeyCode::Key_Return) {
  207. model.activate(model.selected_index());
  208. return;
  209. }
  210. if (event.key() == KeyCode::Key_Up) {
  211. GModelIndex new_index;
  212. if (model.selected_index().is_valid())
  213. new_index = model.index(model.selected_index().row() - 1, model.selected_index().column());
  214. else
  215. new_index = model.index(0, 0);
  216. if (model.is_valid(new_index)) {
  217. model.set_selected_index(new_index);
  218. scroll_into_view(new_index, Orientation::Vertical);
  219. update();
  220. }
  221. return;
  222. }
  223. if (event.key() == KeyCode::Key_Down) {
  224. GModelIndex new_index;
  225. if (model.selected_index().is_valid())
  226. new_index = model.index(model.selected_index().row() + 1, model.selected_index().column());
  227. else
  228. new_index = model.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_PageUp) {
  237. int items_per_page = visible_content_rect().height() / item_height();
  238. auto new_index = model.index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
  239. if (model.is_valid(new_index)) {
  240. model.set_selected_index(new_index);
  241. scroll_into_view(new_index, Orientation::Vertical);
  242. update();
  243. }
  244. return;
  245. }
  246. if (event.key() == KeyCode::Key_PageDown) {
  247. int items_per_page = visible_content_rect().height() / item_height();
  248. auto new_index = model.index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
  249. if (model.is_valid(new_index)) {
  250. model.set_selected_index(new_index);
  251. scroll_into_view(new_index, Orientation::Vertical);
  252. update();
  253. }
  254. return;
  255. }
  256. return GWidget::keydown_event(event);
  257. }
  258. void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  259. {
  260. auto rect = row_rect(index.row()).translated(0, -header_height());
  261. GScrollableWidget::scroll_into_view(rect, orientation);
  262. }
  263. bool GTableView::is_column_hidden(int column) const
  264. {
  265. if (column >= 0 && column < m_column_visibility.size())
  266. return !m_column_visibility[column];
  267. return false;
  268. }
  269. void GTableView::set_column_hidden(int column, bool hidden)
  270. {
  271. ASSERT(column >= 0);
  272. if (m_column_visibility.size() <= column) {
  273. int previous_column_count = m_column_visibility.size();
  274. m_column_visibility.resize(column + 1);
  275. for (int i = previous_column_count; i < m_column_visibility.size(); ++i)
  276. m_column_visibility[i] = true;
  277. }
  278. m_column_visibility[column] = !hidden;
  279. }
  280. void GTableView::doubleclick_event(GMouseEvent& event)
  281. {
  282. if (!model())
  283. return;
  284. if (event.button() == GMouseButton::Left) {
  285. mousedown_event(event);
  286. model()->activate(model()->selected_index());
  287. }
  288. }