GTableView.cpp 16 KB

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