GAbstractColumnView.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. #include <AK/StringBuilder.h>
  2. #include <LibGUI/GAbstractColumnView.h>
  3. #include <LibGUI/GAction.h>
  4. #include <LibGUI/GMenu.h>
  5. #include <LibGUI/GPainter.h>
  6. #include <LibGUI/GScrollBar.h>
  7. static const int minimum_column_width = 2;
  8. GAbstractColumnView::GAbstractColumnView(GWidget* parent)
  9. : GAbstractView(parent)
  10. {
  11. set_frame_shape(FrameShape::Container);
  12. set_frame_shadow(FrameShadow::Sunken);
  13. set_frame_thickness(2);
  14. set_should_hide_unnecessary_scrollbars(true);
  15. }
  16. GAbstractColumnView::~GAbstractColumnView()
  17. {
  18. }
  19. void GAbstractColumnView::update_column_sizes()
  20. {
  21. if (!m_size_columns_to_fit_content)
  22. return;
  23. if (!model())
  24. return;
  25. auto& model = *this->model();
  26. int column_count = model.column_count();
  27. int row_count = model.row_count();
  28. for (int column = 0; column < column_count; ++column) {
  29. if (is_column_hidden(column))
  30. continue;
  31. int header_width = header_font().width(model.column_name(column));
  32. int column_width = header_width;
  33. for (int row = 0; row < row_count; ++row) {
  34. auto cell_data = model.data(model.index(row, column));
  35. int cell_width = 0;
  36. if (cell_data.is_bitmap()) {
  37. cell_width = cell_data.as_bitmap().width();
  38. } else {
  39. cell_width = font().width(cell_data.to_string());
  40. }
  41. column_width = max(column_width, cell_width);
  42. }
  43. auto& column_data = this->column_data(column);
  44. column_data.width = max(column_data.width, column_width);
  45. column_data.has_initialized_width = true;
  46. }
  47. }
  48. void GAbstractColumnView::update_content_size()
  49. {
  50. if (!model())
  51. return set_content_size({});
  52. int content_width = 0;
  53. int column_count = model()->column_count();
  54. for (int i = 0; i < column_count; ++i) {
  55. if (!is_column_hidden(i))
  56. content_width += column_width(i) + horizontal_padding() * 2;
  57. }
  58. int content_height = item_count() * item_height();
  59. set_content_size({ content_width, content_height });
  60. set_size_occupied_by_fixed_elements({ 0, header_height() });
  61. }
  62. Rect GAbstractColumnView::header_rect(int column_index) const
  63. {
  64. if (!model())
  65. return {};
  66. if (is_column_hidden(column_index))
  67. return {};
  68. int x_offset = 0;
  69. for (int i = 0; i < column_index; ++i) {
  70. if (is_column_hidden(i))
  71. continue;
  72. x_offset += column_width(i) + horizontal_padding() * 2;
  73. }
  74. return { x_offset, 0, column_width(column_index) + horizontal_padding() * 2, header_height() };
  75. }
  76. void GAbstractColumnView::set_hovered_header_index(int index)
  77. {
  78. if (m_hovered_column_header_index == index)
  79. return;
  80. m_hovered_column_header_index = index;
  81. update_headers();
  82. }
  83. void GAbstractColumnView::paint_headers(GPainter& painter)
  84. {
  85. if (!headers_visible())
  86. return;
  87. int exposed_width = max(content_size().width(), width());
  88. painter.fill_rect({ 0, 0, exposed_width, header_height() }, SystemColor::Window);
  89. painter.draw_line({ 0, 0 }, { exposed_width - 1, 0 }, SystemColor::ThreedHighlight);
  90. painter.draw_line({ 0, header_height() - 1 }, { exposed_width - 1, header_height() - 1 }, SystemColor::ThreedShadow1);
  91. int x_offset = 0;
  92. int column_count = model()->column_count();
  93. for (int column_index = 0; column_index < column_count; ++column_index) {
  94. if (is_column_hidden(column_index))
  95. continue;
  96. int column_width = this->column_width(column_index);
  97. bool is_key_column = model()->key_column() == column_index;
  98. Rect cell_rect(x_offset, 0, column_width + horizontal_padding() * 2, header_height());
  99. bool pressed = column_index == m_pressed_column_header_index && m_pressed_column_header_is_pressed;
  100. bool hovered = column_index == m_hovered_column_header_index && model()->column_metadata(column_index).sortable == GModel::ColumnMetadata::Sortable::True;
  101. StylePainter::paint_button(painter, cell_rect, ButtonStyle::Normal, pressed, hovered);
  102. String text;
  103. if (is_key_column) {
  104. StringBuilder builder;
  105. builder.append(model()->column_name(column_index));
  106. auto sort_order = model()->sort_order();
  107. if (sort_order == GSortOrder::Ascending)
  108. builder.append(" \xc3\xb6");
  109. else if (sort_order == GSortOrder::Descending)
  110. builder.append(" \xc3\xb7");
  111. text = builder.to_string();
  112. } else {
  113. text = model()->column_name(column_index);
  114. }
  115. auto text_rect = cell_rect.translated(horizontal_padding(), 0);
  116. if (pressed)
  117. text_rect.move_by(1, 1);
  118. painter.draw_text(text_rect, text, header_font(), TextAlignment::CenterLeft, SystemColor::ButtonText);
  119. x_offset += column_width + horizontal_padding() * 2;
  120. }
  121. }
  122. bool GAbstractColumnView::is_column_hidden(int column) const
  123. {
  124. return !column_data(column).visibility;
  125. }
  126. void GAbstractColumnView::set_column_hidden(int column, bool hidden)
  127. {
  128. auto& column_data = this->column_data(column);
  129. if (column_data.visibility == !hidden)
  130. return;
  131. column_data.visibility = !hidden;
  132. update_content_size();
  133. update();
  134. }
  135. GMenu& GAbstractColumnView::ensure_header_context_menu()
  136. {
  137. // FIXME: This menu needs to be rebuilt if the model is swapped out,
  138. // or if the column count/names change.
  139. if (!m_header_context_menu) {
  140. ASSERT(model());
  141. m_header_context_menu = GMenu::construct();
  142. for (int column = 0; column < model()->column_count(); ++column) {
  143. auto& column_data = this->column_data(column);
  144. auto name = model()->column_name(column);
  145. column_data.visibility_action = GAction::create(name, [this, column](GAction& action) {
  146. action.set_checked(!action.is_checked());
  147. set_column_hidden(column, !action.is_checked());
  148. });
  149. column_data.visibility_action->set_checkable(true);
  150. column_data.visibility_action->set_checked(true);
  151. m_header_context_menu->add_action(*column_data.visibility_action);
  152. }
  153. }
  154. return *m_header_context_menu;
  155. }
  156. const Font& GAbstractColumnView::header_font()
  157. {
  158. return Font::default_bold_font();
  159. }
  160. void GAbstractColumnView::set_cell_painting_delegate(int column, OwnPtr<GTableCellPaintingDelegate>&& delegate)
  161. {
  162. column_data(column).cell_painting_delegate = move(delegate);
  163. }
  164. void GAbstractColumnView::update_headers()
  165. {
  166. Rect rect { 0, 0, frame_inner_rect().width(), header_height() };
  167. rect.move_by(frame_thickness(), frame_thickness());
  168. update(rect);
  169. }
  170. GAbstractColumnView::ColumnData& GAbstractColumnView::column_data(int column) const
  171. {
  172. if (column >= m_column_data.size())
  173. m_column_data.resize(column + 1);
  174. return m_column_data.at(column);
  175. }
  176. Rect GAbstractColumnView::column_resize_grabbable_rect(int column) const
  177. {
  178. if (!model())
  179. return {};
  180. auto header_rect = this->header_rect(column);
  181. return { header_rect.right() - 1, header_rect.top(), 4, header_rect.height() };
  182. }
  183. int GAbstractColumnView::column_width(int column_index) const
  184. {
  185. if (!model())
  186. return 0;
  187. auto& column_data = this->column_data(column_index);
  188. if (!column_data.has_initialized_width) {
  189. ASSERT(!m_size_columns_to_fit_content);
  190. column_data.has_initialized_width = true;
  191. column_data.width = model()->column_metadata(column_index).preferred_width;
  192. }
  193. return column_data.width;
  194. }
  195. void GAbstractColumnView::mousemove_event(GMouseEvent& event)
  196. {
  197. if (!model())
  198. return;
  199. if (m_in_column_resize) {
  200. auto delta = event.position() - m_column_resize_origin;
  201. int new_width = m_column_resize_original_width + delta.x();
  202. if (new_width <= minimum_column_width)
  203. new_width = minimum_column_width;
  204. ASSERT(m_resizing_column >= 0 && m_resizing_column < model()->column_count());
  205. auto& column_data = this->column_data(m_resizing_column);
  206. if (column_data.width != new_width) {
  207. column_data.width = new_width;
  208. dbg() << "New column width: " << new_width;
  209. update_content_size();
  210. update();
  211. }
  212. return;
  213. }
  214. if (m_pressed_column_header_index != -1) {
  215. auto header_rect = this->header_rect(m_pressed_column_header_index);
  216. if (header_rect.contains(event.position())) {
  217. if (!m_pressed_column_header_is_pressed)
  218. update_headers();
  219. m_pressed_column_header_is_pressed = true;
  220. } else {
  221. if (m_pressed_column_header_is_pressed)
  222. update_headers();
  223. m_pressed_column_header_is_pressed = false;
  224. }
  225. return;
  226. }
  227. if (event.buttons() == 0) {
  228. int column_count = model()->column_count();
  229. bool found_hovered_header = false;
  230. for (int i = 0; i < column_count; ++i) {
  231. if (column_resize_grabbable_rect(i).contains(event.position())) {
  232. window()->set_override_cursor(GStandardCursor::ResizeHorizontal);
  233. set_hovered_header_index(-1);
  234. return;
  235. }
  236. if (header_rect(i).contains(event.position())) {
  237. set_hovered_header_index(i);
  238. found_hovered_header = true;
  239. }
  240. }
  241. if (!found_hovered_header)
  242. set_hovered_header_index(-1);
  243. }
  244. window()->set_override_cursor(GStandardCursor::None);
  245. }
  246. void GAbstractColumnView::mouseup_event(GMouseEvent& event)
  247. {
  248. auto adjusted_position = this->adjusted_position(event.position());
  249. if (event.button() == GMouseButton::Left) {
  250. if (m_in_column_resize) {
  251. if (!column_resize_grabbable_rect(m_resizing_column).contains(adjusted_position))
  252. window()->set_override_cursor(GStandardCursor::None);
  253. m_in_column_resize = false;
  254. }
  255. if (m_pressed_column_header_index != -1) {
  256. auto header_rect = this->header_rect(m_pressed_column_header_index);
  257. if (header_rect.contains(event.position())) {
  258. auto new_sort_order = GSortOrder::Ascending;
  259. if (model()->key_column() == m_pressed_column_header_index)
  260. new_sort_order = model()->sort_order() == GSortOrder::Ascending
  261. ? GSortOrder::Descending
  262. : GSortOrder::Ascending;
  263. model()->set_key_column_and_sort_order(m_pressed_column_header_index, new_sort_order);
  264. }
  265. m_pressed_column_header_index = -1;
  266. m_pressed_column_header_is_pressed = false;
  267. update_headers();
  268. }
  269. }
  270. }
  271. void GAbstractColumnView::mousedown_event(GMouseEvent& event)
  272. {
  273. if (!model())
  274. return;
  275. if (event.button() != GMouseButton::Left)
  276. return;
  277. if (event.y() < header_height()) {
  278. int column_count = model()->column_count();
  279. for (int i = 0; i < column_count; ++i) {
  280. if (column_resize_grabbable_rect(i).contains(event.position())) {
  281. m_resizing_column = i;
  282. m_in_column_resize = true;
  283. m_column_resize_original_width = column_width(i);
  284. m_column_resize_origin = event.position();
  285. return;
  286. }
  287. auto header_rect = this->header_rect(i);
  288. auto column_metadata = model()->column_metadata(i);
  289. if (header_rect.contains(event.position()) && column_metadata.sortable == GModel::ColumnMetadata::Sortable::True) {
  290. m_pressed_column_header_index = i;
  291. m_pressed_column_header_is_pressed = true;
  292. update_headers();
  293. return;
  294. }
  295. }
  296. return;
  297. }
  298. bool is_toggle;
  299. auto index = index_at_event_position(event.position(), is_toggle);
  300. if (!index.is_valid()) {
  301. selection().clear();
  302. return;
  303. }
  304. if (is_toggle && model()->row_count(index)) {
  305. toggle_index(index);
  306. return;
  307. }
  308. if (event.modifiers() & Mod_Ctrl)
  309. selection().toggle(index);
  310. else
  311. selection().set(index);
  312. }
  313. GModelIndex GAbstractColumnView::index_at_event_position(const Point& position, bool& is_toggle) const
  314. {
  315. is_toggle = false;
  316. if (!model())
  317. return {};
  318. auto adjusted_position = this->adjusted_position(position);
  319. for (int row = 0, row_count = model()->row_count(); row < row_count; ++row) {
  320. if (!row_rect(row).contains(adjusted_position))
  321. continue;
  322. for (int column = 0, column_count = model()->column_count(); column < column_count; ++column) {
  323. if (!content_rect(row, column).contains(adjusted_position))
  324. continue;
  325. return model()->index(row, column);
  326. }
  327. return model()->index(row, 0);
  328. }
  329. return {};
  330. }
  331. int GAbstractColumnView::item_count() const
  332. {
  333. if (!model())
  334. return 0;
  335. return model()->row_count();
  336. }
  337. void GAbstractColumnView::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 GAbstractColumnView::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. void GAbstractColumnView::doubleclick_event(GMouseEvent& event)
  408. {
  409. if (!model())
  410. return;
  411. if (event.button() == GMouseButton::Left) {
  412. if (event.y() < header_height())
  413. return;
  414. if (!selection().is_empty()) {
  415. if (is_editable()) {
  416. begin_editing(selection().first());
  417. } else {
  418. selection().for_each_index([this](auto& index) {
  419. activate(index);
  420. });
  421. }
  422. }
  423. }
  424. }
  425. void GAbstractColumnView::context_menu_event(GContextMenuEvent& event)
  426. {
  427. if (!model())
  428. return;
  429. if (event.position().y() < header_height()) {
  430. ensure_header_context_menu().popup(event.screen_position());
  431. return;
  432. }
  433. bool is_toggle;
  434. auto index = index_at_event_position(event.position(), is_toggle);
  435. if (index.is_valid()) {
  436. if (!selection().contains(index))
  437. selection().set(index);
  438. } else {
  439. selection().clear();
  440. }
  441. if (on_context_menu_request)
  442. on_context_menu_request(index, event);
  443. }
  444. void GAbstractColumnView::leave_event(CEvent&)
  445. {
  446. window()->set_override_cursor(GStandardCursor::None);
  447. set_hovered_header_index(-1);
  448. }
  449. Rect GAbstractColumnView::content_rect(int row, int column) const
  450. {
  451. auto row_rect = this->row_rect(row);
  452. int x = 0;
  453. for (int i = 0; i < column; ++i)
  454. x += column_width(i) + horizontal_padding() * 2;
  455. return { row_rect.x() + x, row_rect.y(), column_width(column) + horizontal_padding() * 2, item_height() };
  456. }
  457. Rect GAbstractColumnView::content_rect(const GModelIndex& index) const
  458. {
  459. return content_rect(index.row(), index.column());
  460. }
  461. Rect GAbstractColumnView::row_rect(int item_index) const
  462. {
  463. return { 0, header_height() + (item_index * item_height()), max(content_size().width(), width()), item_height() };
  464. }
  465. Point GAbstractColumnView::adjusted_position(const Point& position) const
  466. {
  467. return position.translated(horizontal_scrollbar().value() - frame_thickness(), vertical_scrollbar().value() - frame_thickness());
  468. }
  469. void GAbstractColumnView::did_update_model()
  470. {
  471. GAbstractView::did_update_model();
  472. update_column_sizes();
  473. update_content_size();
  474. update();
  475. }