GTableView.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. model()->set_selected_index(index_at_event_position(event.position()));
  155. update();
  156. }
  157. GModelIndex GTableView::index_at_event_position(const Point& position) const
  158. {
  159. if (!model())
  160. return {};
  161. auto adjusted_position = this->adjusted_position(position);
  162. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  163. if (!row_rect(row).contains(adjusted_position))
  164. continue;
  165. for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) {
  166. if (!content_rect(row, column).contains(adjusted_position))
  167. continue;
  168. return model()->index(row, column);
  169. }
  170. return model()->index(row, 0);
  171. }
  172. return {};
  173. }
  174. void GTableView::mousemove_event(GMouseEvent& event)
  175. {
  176. if (!model())
  177. return;
  178. if (m_in_column_resize) {
  179. auto delta = event.position() - m_column_resize_origin;
  180. int new_width = m_column_resize_original_width + delta.x();
  181. ASSERT(m_resizing_column >= 0 && m_resizing_column < model()->column_count());
  182. auto& column_data = this->column_data(m_resizing_column);
  183. if (column_data.width != new_width) {
  184. column_data.width = new_width;
  185. dbg() << "New column width: " << new_width;
  186. update_content_size();
  187. update();
  188. }
  189. return;
  190. }
  191. if (event.buttons() == 0) {
  192. for (int i = 0; i < model()->column_count(); ++i) {
  193. if (column_resize_grabbable_rect(i).contains(event.position())) {
  194. window()->set_override_cursor(GStandardCursor::ResizeHorizontal);
  195. return;
  196. }
  197. }
  198. }
  199. window()->set_override_cursor(GStandardCursor::None);
  200. }
  201. void GTableView::mouseup_event(GMouseEvent& event)
  202. {
  203. auto adjusted_position = this->adjusted_position(event.position());
  204. if (event.button() == GMouseButton::Left) {
  205. if (m_in_column_resize) {
  206. if (!column_resize_grabbable_rect(m_resizing_column).contains(adjusted_position))
  207. window()->set_override_cursor(GStandardCursor::None);
  208. m_in_column_resize = false;
  209. }
  210. }
  211. }
  212. void GTableView::paint_event(GPaintEvent& event)
  213. {
  214. GFrame::paint_event(event);
  215. if (!model())
  216. return;
  217. GPainter painter(*this);
  218. painter.add_clip_rect(frame_inner_rect());
  219. painter.add_clip_rect(event.rect());
  220. painter.translate(frame_thickness(), frame_thickness());
  221. painter.translate(-horizontal_scrollbar().value(), -vertical_scrollbar().value());
  222. int exposed_width = max(content_size().width(), width());
  223. int painted_item_index = 0;
  224. int y_offset = header_height();
  225. for (int row_index = 0; row_index < model()->row_count(); ++row_index) {
  226. bool is_selected_row = row_index == model()->selected_index().row();
  227. int y = y_offset + painted_item_index * item_height();
  228. Color background_color;
  229. Color key_column_background_color;
  230. if (is_selected_row) {
  231. background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  232. key_column_background_color = is_focused() ? Color::from_rgb(0x84351a) : Color::from_rgb(0x606060);
  233. } else {
  234. if (alternating_row_colors() && (painted_item_index % 2)) {
  235. background_color = Color(210, 210, 210);
  236. key_column_background_color = Color(180, 180, 180);
  237. } else {
  238. background_color = Color::White;
  239. key_column_background_color = Color(210, 210, 210);
  240. }
  241. }
  242. painter.fill_rect(row_rect(painted_item_index), background_color);
  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. auto column_metadata = model()->column_metadata(column_index);
  248. int column_width = this->column_width(column_index);
  249. const Font& font = column_metadata.font ? *column_metadata.font : this->font();
  250. bool is_key_column = model()->key_column() == column_index;
  251. Rect cell_rect(horizontal_padding() + x_offset, y, column_width, item_height());
  252. if (is_key_column) {
  253. auto cell_rect_for_fill = cell_rect.inflated(horizontal_padding() * 2, 0);
  254. painter.fill_rect(cell_rect_for_fill, key_column_background_color);
  255. }
  256. auto cell_index = model()->index(row_index, column_index);
  257. if (auto* delegate = column_data(column_index).cell_painting_delegate.ptr()) {
  258. delegate->paint(painter, cell_rect, *model(), cell_index);
  259. } else {
  260. auto data = model()->data(cell_index);
  261. if (data.is_bitmap()) {
  262. painter.blit(cell_rect.location(), data.as_bitmap(), data.as_bitmap().rect());
  263. } else if (data.is_icon()) {
  264. if (auto bitmap = data.as_icon().bitmap_for_size(16))
  265. painter.blit(cell_rect.location(), *bitmap, bitmap->rect());
  266. } else {
  267. Color text_color;
  268. if (is_selected_row)
  269. text_color = Color::White;
  270. else
  271. text_color = model()->data(cell_index, GModel::Role::ForegroundColor).to_color(Color::Black);
  272. painter.draw_text(cell_rect, data.to_string(), font, column_metadata.text_alignment, text_color, TextElision::Right);
  273. }
  274. }
  275. x_offset += column_width + horizontal_padding() * 2;
  276. }
  277. ++painted_item_index;
  278. };
  279. Rect unpainted_rect(0, header_height() + painted_item_index * item_height(), exposed_width, height());
  280. painter.fill_rect(unpainted_rect, Color::White);
  281. // Untranslate the painter vertically and do the column headers.
  282. painter.translate(0, vertical_scrollbar().value());
  283. if (headers_visible())
  284. paint_headers(painter);
  285. }
  286. void GTableView::paint_headers(Painter& painter)
  287. {
  288. int exposed_width = max(content_size().width(), width());
  289. painter.fill_rect({ 0, 0, exposed_width, header_height() }, Color::WarmGray);
  290. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, Color::White);
  291. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, Color::MidGray);
  292. int x_offset = 0;
  293. for (int column_index = 0; column_index < model()->column_count(); ++column_index) {
  294. if (is_column_hidden(column_index))
  295. continue;
  296. int column_width = this->column_width(column_index);
  297. bool is_key_column = model()->key_column() == column_index;
  298. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
  299. StylePainter::paint_button(painter, cell_rect, ButtonStyle::Normal, false);
  300. String text;
  301. if (is_key_column) {
  302. StringBuilder builder;
  303. builder.append(model()->column_name(column_index));
  304. auto sort_order = model()->sort_order();
  305. if (sort_order == GSortOrder::Ascending)
  306. builder.append(" \xc3\xb6");
  307. else if (sort_order == GSortOrder::Descending)
  308. builder.append(" \xc3\xb7");
  309. text = builder.to_string();
  310. } else {
  311. text = model()->column_name(column_index);
  312. }
  313. auto text_rect = cell_rect.translated(horizontal_padding(), 0);
  314. painter.draw_text(text_rect, text, header_font(), TextAlignment::CenterLeft, Color::Black);
  315. x_offset += column_width + horizontal_padding() * 2;
  316. }
  317. }
  318. int GTableView::item_count() const
  319. {
  320. if (!model())
  321. return 0;
  322. return model()->row_count();
  323. }
  324. void GTableView::keydown_event(GKeyEvent& event)
  325. {
  326. if (!model())
  327. return;
  328. auto& model = *this->model();
  329. if (event.key() == KeyCode::Key_Return) {
  330. activate(model.selected_index());
  331. return;
  332. }
  333. if (event.key() == KeyCode::Key_Up) {
  334. GModelIndex new_index;
  335. if (model.selected_index().is_valid())
  336. new_index = model.index(model.selected_index().row() - 1, model.selected_index().column());
  337. else
  338. new_index = model.index(0, 0);
  339. if (model.is_valid(new_index)) {
  340. model.set_selected_index(new_index);
  341. scroll_into_view(new_index, Orientation::Vertical);
  342. update();
  343. }
  344. return;
  345. }
  346. if (event.key() == KeyCode::Key_Down) {
  347. GModelIndex new_index;
  348. if (model.selected_index().is_valid())
  349. new_index = model.index(model.selected_index().row() + 1, model.selected_index().column());
  350. else
  351. new_index = model.index(0, 0);
  352. if (model.is_valid(new_index)) {
  353. model.set_selected_index(new_index);
  354. scroll_into_view(new_index, Orientation::Vertical);
  355. update();
  356. }
  357. return;
  358. }
  359. if (event.key() == KeyCode::Key_PageUp) {
  360. int items_per_page = visible_content_rect().height() / item_height();
  361. auto new_index = model.index(max(0, model.selected_index().row() - items_per_page), model.selected_index().column());
  362. if (model.is_valid(new_index)) {
  363. model.set_selected_index(new_index);
  364. scroll_into_view(new_index, Orientation::Vertical);
  365. update();
  366. }
  367. return;
  368. }
  369. if (event.key() == KeyCode::Key_PageDown) {
  370. int items_per_page = visible_content_rect().height() / item_height();
  371. auto new_index = model.index(min(model.row_count() - 1, model.selected_index().row() + items_per_page), model.selected_index().column());
  372. if (model.is_valid(new_index)) {
  373. model.set_selected_index(new_index);
  374. scroll_into_view(new_index, Orientation::Vertical);
  375. update();
  376. }
  377. return;
  378. }
  379. return GWidget::keydown_event(event);
  380. }
  381. void GTableView::scroll_into_view(const GModelIndex& index, Orientation orientation)
  382. {
  383. auto rect = row_rect(index.row()).translated(0, -header_height());
  384. GScrollableWidget::scroll_into_view(rect, orientation);
  385. }
  386. GTableView::ColumnData& GTableView::column_data(int column) const
  387. {
  388. if (column >= m_column_data.size())
  389. m_column_data.resize(column + 1);
  390. return m_column_data.at(column);
  391. }
  392. bool GTableView::is_column_hidden(int column) const
  393. {
  394. return !column_data(column).visibility;
  395. }
  396. void GTableView::set_column_hidden(int column, bool hidden)
  397. {
  398. auto& column_data = this->column_data(column);
  399. if (column_data.visibility == !hidden)
  400. return;
  401. column_data.visibility = !hidden;
  402. update_content_size();
  403. update();
  404. }
  405. void GTableView::doubleclick_event(GMouseEvent& event)
  406. {
  407. if (!model())
  408. return;
  409. auto& model = *this->model();
  410. if (event.button() == GMouseButton::Left) {
  411. if (event.y() < header_height())
  412. return;
  413. if (model.selected_index().is_valid()) {
  414. if (is_editable())
  415. begin_editing(model.selected_index());
  416. else
  417. activate(model.selected_index());
  418. }
  419. }
  420. }
  421. GMenu& GTableView::ensure_header_context_menu()
  422. {
  423. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  424. // or if the column count/names change.
  425. if (!m_header_context_menu) {
  426. ASSERT(model());
  427. m_header_context_menu = make<GMenu>("");
  428. for (int column = 0; column < model()->column_count(); ++column) {
  429. auto& column_data = this->column_data(column);
  430. auto name = model()->column_name(column);
  431. column_data.visibility_action = GAction::create(name, [this, column](GAction& action) {
  432. action.set_checked(!action.is_checked());
  433. set_column_hidden(column, !action.is_checked());
  434. });
  435. column_data.visibility_action->set_checkable(true);
  436. column_data.visibility_action->set_checked(true);
  437. m_header_context_menu->add_action(*column_data.visibility_action);
  438. }
  439. }
  440. return *m_header_context_menu;
  441. }
  442. void GTableView::context_menu_event(GContextMenuEvent& event)
  443. {
  444. if (!model())
  445. return;
  446. if (event.position().y() < header_height()) {
  447. ensure_header_context_menu().popup(event.screen_position());
  448. return;
  449. }
  450. auto index = index_at_event_position(event.position());
  451. if (!index.is_valid())
  452. return;
  453. dbgprintf("context menu requested for index (%d,%d) '%s'\n", index.row(), index.column(), model()->data(index).to_string().characters());
  454. model()->set_selected_index(index);
  455. update();
  456. if (on_context_menu_request)
  457. on_context_menu_request(index, event);
  458. }
  459. void GTableView::leave_event(CEvent&)
  460. {
  461. window()->set_override_cursor(GStandardCursor::None);
  462. }
  463. const Font& GTableView::header_font()
  464. {
  465. return Font::default_bold_font();
  466. }
  467. void GTableView::set_cell_painting_delegate(int column, OwnPtr<GTableCellPaintingDelegate>&& delegate)
  468. {
  469. column_data(column).cell_painting_delegate = move(delegate);
  470. }