GTableView.cpp 18 KB

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