GTableView.cpp 12 KB

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