GTableView.cpp 19 KB

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