GTableView.cpp 15 KB

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