GTableView.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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 painted_item_index = 0;
  231. int y_offset = header_height();
  232. for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
  233. bool is_selected_row = selection().contains_row(row_index);
  234. int y = y_offset + painted_item_index * item_height();
  235. Color background_color;
  236. Color key_column_background_color;
  237. if (is_selected_row) {
  238. background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  239. key_column_background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  240. } else {
  241. if (alternating_row_colors() && (painted_item_index % 2)) {
  242. background_color = Color(210, 210, 210);
  243. key_column_background_color = Color(180, 180, 180);
  244. } else {
  245. background_color = Color::White;
  246. key_column_background_color = Color(210, 210, 210);
  247. }
  248. }
  249. painter.fill_rect(row_rect(painted_item_index), background_color);
  250. int x_offset = 0;
  251. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  252. if (is_column_hidden(column_index))
  253. continue;
  254. auto column_metadata = model()->column_metadata(column_index);
  255. int column_width = this->column_width(column_index);
  256. const Font& font = column_metadata.font ? *column_metadata.font : this->font();
  257. bool is_key_column = model()->key_column() == column_index;
  258. Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
  259. if (is_key_column) {
  260. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  261. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  262. }
  263. auto cell_index = model()->index(row_index, column_index);
  264. if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) {
  265. delegate->paint(painter, cell_rect, *model(), cell_index);
  266. } else {
  267. auto data = model()->data(cell_index);
  268. if (data.is_bitmap()) {
  269. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  270. } else if (data.is_icon()) {
  271. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  272. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  273. } else {
  274. Color text_color;
  275. if (is_selected_row)
  276. text_color = Color::White;
  277. else
  278. text_color = model()->data(cell_index, GModel::Role::ForegroundColor).to_color(Color::Black);
  279. painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color, TextElision::Right);
  280. }
  281. }
  282. x_offset += column_width + horizontal_padding() * 2;
  283. }
  284. ++painted_item_index;
  285. };
  286. Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
  287. painter.fill_rect(unpainted_rect, Color::White);
  288. // Untranslate the painter vertically and do the column headers.
  289. painter.translate(0, vertical_scrollbar().value());
  290. if (headers_visible())
  291. paint_headers(painter);
  292. }
  293. void GTableView::paint_headers(Painter& painter)
  294. {
  295. int exposed_width = max(content_size().width(), width());
  296. painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::WarmGray);
  297. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
  298. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::MidGray);
  299. int x_offset = 0;
  300. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  301. if (is_column_hidden(column_index))
  302. continue;
  303. int column_width = this->column_width(column_index);
  304. bool is_key_column = model()->key_column() == column_index;
  305. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
  306. StylePainter::paint_button(painter, cell_rect, ButtonStyle::Normal, false);
  307. String text;
  308. if (is_key_column) {
  309. StringBuilder builder;
  310. builder.append(model()->column_name(column_index));
  311. auto sort_order = model()->sort_order();
  312. if (sort_order == GSortOrder::Ascending)
  313. builder.append(" \xc3\xb6");
  314. else if (sort_order == GSortOrder::Descending)
  315. builder.append(" \xc3\xb7");
  316. text = builder.to_string();
  317. } else {
  318. text = model()->column_name(column_index);
  319. }
  320. auto text_rect = cell_rect.translated(horizontal_padding(), 0);
  321. painter.draw_text(text_rect, text, header_font(), TextAlignment::CenterLeft, Color::Black);
  322. x_offset += column_width + horizontal_padding() * 2;
  323. }
  324. }
  325. int GTableView::item_count() const
  326. {
  327. if (!model())
  328. return 0;
  329. return model()->row_count();
  330. }
  331. void GTableView::keydown_event(GKeyEvent& event)
  332. {
  333. if (!model())
  334. return;
  335. auto& model = *this->model();
  336. if (event.key() == KeyCode::Key_Return) {
  337. selection().for_each_index([this](auto& index) {
  338. activate(index);
  339. });
  340. return;
  341. }
  342. if (event.key() == KeyCode::Key_Up) {
  343. GModelIndex new_index;
  344. if (!selection().is_empty()) {
  345. auto old_index = selection().first();
  346. new_index = model.index(old_index.row() - 1, old_index.column());
  347. } else {
  348. new_index = model.index(0, 0);
  349. }
  350. if (model.is_valid(new_index)) {
  351. selection().set(new_index);
  352. scroll_into_view(new_index, Orientation::Vertical);
  353. update();
  354. }
  355. return;
  356. }
  357. if (event.key() == KeyCode::Key_Down) {
  358. GModelIndex new_index;
  359. if (!selection().is_empty()) {
  360. auto old_index = selection().first();
  361. new_index = model.index(old_index.row() + 1, old_index.column());
  362. } else {
  363. new_index = model.index(0, 0);
  364. }
  365. if (model.is_valid(new_index)) {
  366. selection().set(new_index);
  367. scroll_into_view(new_index, Orientation::Vertical);
  368. update();
  369. }
  370. return;
  371. }
  372. if (event.key() == KeyCode::Key_PageUp) {
  373. int items_per_page = visible_content_rect().height() / item_height();
  374. auto old_index = selection().first();
  375. auto new_index = model.index(max(0, old_index.row() - items_per_page), old_index.column());
  376. if (model.is_valid(new_index)) {
  377. selection().set(new_index);
  378. scroll_into_view(new_index, Orientation::Vertical);
  379. update();
  380. }
  381. return;
  382. }
  383. if (event.key() == KeyCode::Key_PageDown) {
  384. int items_per_page = visible_content_rect().height() / item_height();
  385. auto old_index = selection().first();
  386. auto new_index = model.index(min(model.row_count() - 1, old_index.row() + items_per_page), old_index.column());
  387. if (model.is_valid(new_index)) {
  388. selection().set(new_index);
  389. scroll_into_view(new_index, Orientation::Vertical);
  390. update();
  391. }
  392. return;
  393. }
  394. return GWidget::keydown_event(event);
  395. }
  396. void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  397. {
  398. auto rect = row_rect(index.row()).translated(0, -header_height());
  399. GScrollableWidget::scroll_into_view(rect, orientation);
  400. }
  401. GTableView::ColumnData& GTableView::column_data(int column) const
  402. {
  403. if (column >= m_column_data.size())
  404. m_column_data.resize(column + 1);
  405. return m_column_data.at(column);
  406. }
  407. bool GTableView::is_column_hidden(int column) const
  408. {
  409. return !column_data(column).visibility;
  410. }
  411. void GTableView::set_column_hidden(int column, bool hidden)
  412. {
  413. auto& column_data = this->column_data(column);
  414. if (column_data.visibility == !hidden)
  415. return;
  416. column_data.visibility = !hidden;
  417. update_content_size();
  418. update();
  419. }
  420. void GTableView::doubleclick_event(GMouseEvent& event)
  421. {
  422. if (!model())
  423. return;
  424. if (event.button() == GMouseButton::Left) {
  425. if (event.y() < header_height())
  426. return;
  427. if (!selection().is_empty()) {
  428. if (is_editable()) {
  429. begin_editing(selection().first());
  430. } else {
  431. selection().for_each_index([this](auto& index) {
  432. activate(index);
  433. });
  434. }
  435. }
  436. }
  437. }
  438. GMenu& GTableView::ensure_header_context_menu()
  439. {
  440. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  441. // or if the column count/names change.
  442. if (!m_header_context_menu) {
  443. ASSERT(model());
  444. m_header_context_menu = make<GMenu>();
  445. for (int column = 0; column < model()->column_count(); ++column) {
  446. auto& column_data = this->column_data(column);
  447. auto name = model()->column_name(column);
  448. column_data.visibility_action = GAction::create(name, [this, column](GAction& action) {
  449. action.set_checked(!action.is_checked());
  450. set_column_hidden(column, !action.is_checked());
  451. });
  452. column_data.visibility_action->set_checkable(true);
  453. column_data.visibility_action->set_checked(true);
  454. m_header_context_menu->add_action(*column_data.visibility_action);
  455. }
  456. }
  457. return *m_header_context_menu;
  458. }
  459. void GTableView::context_menu_event(GContextMenuEvent& event)
  460. {
  461. if (!model())
  462. return;
  463. if (event.position().y() < header_height()) {
  464. ensure_header_context_menu().popup(event.screen_position());
  465. return;
  466. }
  467. auto index = index_at_event_position(event.position());
  468. if (index.is_valid()) {
  469. if (!selection().contains(index))
  470. selection().set(index);
  471. } else {
  472. selection().clear();
  473. }
  474. if (on_context_menu_request)
  475. on_context_menu_request(index, event);
  476. }
  477. void GTableView::leave_event(CEvent&)
  478. {
  479. window()->set_override_cursor(GStandardCursor::None);
  480. }
  481. const Font& GTableView::header_font()
  482. {
  483. return Font::default_bold_font();
  484. }
  485. void GTableView::set_cell_painting_delegate(int column, OwnPtr<GTableCellPaintingDelegate>&& delegate)
  486. {
  487. column_data(column).cell_painting_delegate = move(delegate);
  488. }