GTableView.cpp 13 KB

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